class AzureADAuth::AzureAD

This is the main class responsible to evaluate the equations

Constants

VERSION

Public Class Methods

new(opts = {}) click to toggle source

tenant = “nvsistemas.onmicrosoft.com” app_id = “616f5cbb-b5e8-48b8-90c0-1193606880fc” redirect_url = “localhost:3000/mykaefer_oauth” state = “12345” client_secret = “nahpeukahsh8fi8ifier” client_secret = “cwdbdcej1a+4th5b3xsqj8gqe8shfo+sphkjd88vquo=” resource = “localhost:3000/mykaefer_oauth

# File lib/azure_ad.rb, line 15
def initialize(opts = {})
  @tenant = opts[:tenant]
  @app_id = opts[:app_id]
  @redirect_url = opts[:redirect_url]
  @state = opts[:state]
  @client_secret = opts[:client_secret]
  @resource = opts[:resource]
end

Public Instance Methods

authorization_url(login) click to toggle source
# File lib/azure_ad.rb, line 24
def authorization_url(login)
  "https://login.microsoftonline.com/#{@tenant}/oauth2/authorize?client_id=#{@app_id}&response_type=code&redirect_uri=#{@redirect_url}&response_mode=query&state=#{@state}&login_hint=#{login}"
end
request_access_token(opts = {}) click to toggle source

PARAMS

code = params[:code]
session_state = params[:session_state]
state = params[:state]

POST /{tenant}/oauth2/token HTTP/1.1 Host: login.microsoftonline.com Content-Type: application/x-www-form-urlencoded grant_type=authorization_code &client_id=2d4d11a2-f814-46a7-890a-274a72a7309e &code=AwABAAAAvPM1KaPlrEqdFSBzjqfTGBCmLdgfSTLEMP… &redirect_uri=https%3A%2F%2Flocalhost%2Fmyapp%2F &resource=https%3A%2F%2Fservice.contoso.com%2F &client_secret=p@ssw0rd

# File lib/azure_ad.rb, line 42
def request_access_token(opts = {})
  code = opts[:code]
  session_state = opts[:session_state]
  state = opts[:state]

  params = {
    grant_type: 'authorization_code', client_id: @app_id, code: code,
    redirect_uri: @redirect_url, client_secret: @client_secret,
    resource: @app_id
  }
  token_url = "https://login.microsoftonline.com/#{@tenant}/oauth2/token"

  response = Net::HTTP.post_form(URI.parse(token_url), params)

  body = JSON.parse(response.body)

  puts "#" * 90
  ap response.code
  ap body
  puts "#" * 90

  answer = { status: :failed, data: {} }

  if response.code == '200'
    access_token = body['access_token']
    token_type = body['token_type']
    expires_in = body['expires_in']
    ext_expires_in = body['ext_expires_in']
    not_before = body['not_before']
    resource = body['resource']
    refresh_token = body['refresh_token']
    id_token = body['id_token']
    jwt_token = JWT.decode(id_token, nil, false)
    scope = body['scope']
    puts "#" * 90
    ap jwt_token
    puts "#" * 90

    puts "Access Token Acquired"
    answer[:data] = jwt_token[0]

    if not answer[:data].include?('email') and answer[:data].include?('unique_name')
      answer[:data]['email'] = jwt_token[0]['unique_name']
    end

    if answer[:data]['email']
      answer[:data]['email'] = answer[:data]['email'].downcase
      answer[:status] = :success
    end
  else
    answer[:msg] = response.body
  end
  return answer
end