class Sorcery::Providers::Microsoft

This class adds support for OAuth with Microsoft Graph.

config.microsoft.key = <key>
config.microsoft.secret = <secret>
...

Attributes

auth_url[RW]
scope[RW]
token_url[RW]
user_info_url[RW]

Public Class Methods

new() click to toggle source
Calls superclass method Sorcery::Providers::Base::new
# File lib/sorcery/providers/microsoft.rb, line 14
def initialize
  super

  @site          = 'https://login.microsoftonline.com'
  @auth_url      = '/common/oauth2/v2.0/authorize'
  @token_url     = '/common/oauth2/v2.0/token'
  @user_info_url = 'https://graph.microsoft.com/v1.0/me'
  @scope         = 'openid email https://graph.microsoft.com/User.Read'
  @state         = SecureRandom.hex(16)
end

Public Instance Methods

authorize_url(options = {}) click to toggle source
# File lib/sorcery/providers/microsoft.rb, line 25
def authorize_url(options = {})
  oauth_params = {
    client_id: @key,
    response_type: 'code'
  }
  options.merge!(oauth_params)
  super(options)
end
get_user_hash(access_token) click to toggle source
# File lib/sorcery/providers/microsoft.rb, line 34
def get_user_hash(access_token)
  response = access_token.get(user_info_url)

  auth_hash(access_token).tap do |h|
    h[:user_info] = JSON.parse(response.body)
    h[:uid] = h[:user_info]['id']
  end
end
login_url(_params, _session) click to toggle source

calculates and returns the url to which the user should be redirected, to get authenticated at the external provider's site.

# File lib/sorcery/providers/microsoft.rb, line 45
def login_url(_params, _session)
  authorize_url(authorize_url: auth_url)
end
process_callback(params, _session) click to toggle source

tries to login the user from access token

# File lib/sorcery/providers/microsoft.rb, line 50
def process_callback(params, _session)
  args = {}.tap do |a|
    a[:code] = params[:code] if params[:code]
  end

  get_access_token(args, token_url: token_url, token_method: :post)
end