Class: MicrosoftKiotaAuthenticationOAuth::AuthorizationCodeContext

Inherits:
OAuthContext
  • Object
show all
Defined in:
lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb

Overview

Token request context class for the authorization code grant type.

Instance Attribute Summary collapse

Attributes inherited from OAuthContext

#scopes

Instance Method Summary collapse

Methods included from OAuthCustomFlow

get_oauth_provider, get_scopes, get_token

Constructor Details

#initialize(tenant_id, client_id, client_secret, redirect_uri, auth_code = nil) ⇒ AuthorizationCodeContext

This is the initializer for AuthorizationCodeContext, the token request context when using the authorization code grant flow. :params

tenant_id: a string containing the tenant id 
client_id: a string containing the client id 
client_secret: a string containing the client secret
redirect_uri: a string containing redirect_uri
auth_code: a string containting the auth code; default is nil, can be updated post-initialization

Raises:

  • (StandardError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb', line 21

def initialize(tenant_id, client_id, client_secret, redirect_uri, auth_code = nil)
  raise StandardError, 'redirect_uri cannot be nil/empty' if redirect_uri.nil? || redirect_uri.empty?

  @tenant_id = tenant_id
  @client_id = client_id
  @client_secret = client_secret
  @auth_code = auth_code
  @redirect_uri = redirect_uri
  @scopes = nil
  @oauth_provider = nil
  @grant_type = 'authorization code'

  if @tenant_id.nil? || @client_id.nil? || @client_secret.nil? || @tenant_id.empty? || @client_id.empty? || @client_secret.empty?
    raise StandardError, 'tenant_id, client_id, and client_secret cannot be empty'
  end
end

Instance Attribute Details

#additional_paramsObject

Returns the value of attribute additional_params.



9
10
11
# File 'lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb', line 9

def additional_params
  @additional_params
end

#auth_codeObject

Returns the value of attribute auth_code.



9
10
11
# File 'lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb', line 9

def auth_code
  @auth_code
end

#client_idObject

Returns the value of attribute client_id.



9
10
11
# File 'lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb', line 9

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



9
10
11
# File 'lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb', line 9

def client_secret
  @client_secret
end

#grant_typeObject

Returns the value of attribute grant_type.



9
10
11
# File 'lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb', line 9

def grant_type
  @grant_type
end

#oauth_providerObject

Returns the value of attribute oauth_provider.



9
10
11
# File 'lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb', line 9

def oauth_provider
  @oauth_provider
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



9
10
11
# File 'lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb', line 9

def redirect_uri
  @redirect_uri
end

#scopes=(value) ⇒ Object (writeonly)

Sets the attribute scopes

Parameters:

  • value

    the value to set the attribute scopes to.



11
12
13
# File 'lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb', line 11

def scopes=(value)
  @scopes = value
end

#tenant_idObject

Returns the value of attribute tenant_id.



9
10
11
# File 'lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb', line 9

def tenant_id
  @tenant_id
end

Instance Method Details

#generate_authorize_url(scopes, additional_params = {}) ⇒ Object

This function generates an authorize URL for obtaining the auth code. :params

scopes: an array of stings, where each string is a scope
additional_params: hash of symbols to string values, ie { response_mode: 'fragment', prompt: 'login' }
                   default is empty hash


50
51
52
53
54
55
56
57
58
59
# File 'lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb', line 50

def generate_authorize_url(scopes, additional_params = {})
  @additional_params = additional_params
  
  self.initialize_scopes(scopes)
  self.initialize_oauth_provider

  parameters = { scope: @scopes, redirect_uri: @redirect_uri, access_type: 'offline', prompt: 'consent'}
  parameters = parameters.merge(additional_params)
  @oauth_provider.auth_code.authorize_url(parameters)
end

#get_tokenObject



61
62
63
# File 'lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb', line 61

def get_token
  @oauth_provider.auth_code.get_token(@auth_code, redirect_uri: @redirect_uri)
end

#initialize_oauth_providerObject



65
66
67
68
69
70
# File 'lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb', line 65

def initialize_oauth_provider
  @oauth_provider = OAuth2::Client.new(@client_id, @client_secret,
                                       site: 'https://login.microsoftonline.com',
                                       authorize_url: "/#{@tenant_id}/oauth2/v2.0/authorize",
                                       token_url: "/#{@tenant_id}/oauth2/v2.0/token")
end

#initialize_scopes(scopes) ⇒ Object

Raises:

  • (StandardError)


72
73
74
75
76
77
78
79
80
# File 'lib/microsoft_kiota_authentication_oauth/contexts/authorization_code_context.rb', line 72

def initialize_scopes(scopes)
  scope_str = ''
  scopes.each { |scope| scope_str += scope + ' '}
  raise StandardError, 'scopes cannot be empty/nil.' if scope_str.empty?
  
  scope_str = 'offline_access ' + scope_str

  @scopes = scope_str
end