Class: Sorcery::Providers::Microsoft

Inherits:
Base
  • Object
show all
Includes:
Sorcery::Protocols::Oauth2
Defined in:
lib/sorcery/providers/microsoft.rb

Overview

This class adds support for OAuth with Microsoft Graph.

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

Instance Attribute Summary collapse

Attributes inherited from Base

#access_token, #callback_url, #key, #original_callback_url, #secret, #site, #state, #user_info_mapping

Instance Method Summary collapse

Methods included from Sorcery::Protocols::Oauth2

#build_client, #get_access_token, #oauth_version

Methods inherited from Base

#auth_hash, descendants, #has_callback?, name

Constructor Details

#initializeMicrosoft

Returns a new instance of Microsoft.



14
15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#auth_urlObject

Returns the value of attribute auth_url.



12
13
14
# File 'lib/sorcery/providers/microsoft.rb', line 12

def auth_url
  @auth_url
end

#scopeObject

Returns the value of attribute scope.



12
13
14
# File 'lib/sorcery/providers/microsoft.rb', line 12

def scope
  @scope
end

#token_urlObject

Returns the value of attribute token_url.



12
13
14
# File 'lib/sorcery/providers/microsoft.rb', line 12

def token_url
  @token_url
end

#user_info_urlObject

Returns the value of attribute user_info_url.



12
13
14
# File 'lib/sorcery/providers/microsoft.rb', line 12

def 
  @user_info_url
end

Instance Method Details

#authorize_url(options = {}) ⇒ Object



25
26
27
28
29
30
31
32
# 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) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/sorcery/providers/microsoft.rb', line 34

def get_user_hash(access_token)
  response = access_token.get()

  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) ⇒ Object

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



45
46
47
# File 'lib/sorcery/providers/microsoft.rb', line 45

def (_params, _session)
  authorize_url(authorize_url: auth_url)
end

#process_callback(params, _session) ⇒ Object

tries to login the user from access token



50
51
52
53
54
55
56
# 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