Аутентификация Azure AD с помощью Rails и разработка - PullRequest
1 голос
/ 21 мая 2019

У меня есть приложение rails с настроенной devise и базой данных mongodb. Я хочу настроить Microsoft Azure AD для аутентификации. Когда пользователь вводит URL-адрес моего проекта и пользователь не вошел в систему, он должен перенаправить на страницу входа Azure AD, а когда учетные данные верны, он должен перенаправить обратно в мое приложение. Я следовал за этим блогом, чтобы выполнить мое требование. Но это выбрасывает случайные ошибки. Может кто-нибудь подсказать, как это сделать?

1 Ответ

3 голосов
/ 23 мая 2019
class Integrations::Crm::MsDynamics

extend ActiveSupport::Concern

#to instantiate a new dynamics link directory_id/tenant_id,client_id/application_id,secret,username,password and resource link eg. https://maropost.crm3.dynamics.com
def initialize(tenant_id,client_id,client_secret,username,password,resource)
  @tenant_id=tenant_id
  @client_id=client_id
  @client_secret=client_secret
  @username=username
  @password=password
  @resource=resource
end

def get_token
  uri = URI.parse("https://login.microsoftonline.com/#{@tenant_id}/oauth2/token")
  request = Net::HTTP::Post.new(uri)
  request.content_type = "application/x-www-form-urlencoded"
  request["Cache-Control"] = "no-cache"
  request.set_form_data(
    "client_id" => "#{@client_id}",
    "resource" => "#{@resource}",
    "username" => "#{@username}",
    "password" => "#{@password}",
    "grant_type" => "password",
    "client_secret" => "#{@client_secret}",
  )
  req_options = {
    use_ssl: uri.scheme == "https",
  }
  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  return response
 end


 def get_access_token(code)
  uri = URI.parse("https://login.microsoftonline.com/#{@tenant_id}/oauth2/token")
  request = Net::HTTP::Post.new(uri)
  request.content_type = "application/x-www-form-urlencoded"
  request["Cache-Control"] = "no-cache"
  request.set_form_data(
    "client_id" => "#{@client_id}",
    "client_secret" => "#{@client_secret}",
    "code" => "#{code}",
    "grant_type" => "authorization_code",
    "redirect_uri" => "#{SSL_APP_SITE}/dynamic_crms_callbacks/dynamic_authorization_code",
    "resource" => "#{@resource}",
  )

  req_options = {
    use_ssl: uri.scheme == "https",
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
   return response
 end

 def ms_dynamics(response)
  obj = JSON.parse(response.body)
  client = MSDynamics.new({
      hostname: "#{@resource}",
      access_token: obj["access_token"],
      refresh_token: obj["refresh_token"],
      client_id: "#{@client_id}",
      client_secret: "#{@client_secret}"
  })
  return client
 end

end

Пожалуйста, используйте этот код, чтобы решить вашу проблему.

...