Class: MicrosoftKiotaAuthenticationOAuth::OnBehalfOfContext

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

Overview

Token request context class for the on behlaf of 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, assertion, additional_params = {}) ⇒ OnBehalfOfContext

This is the initializer for OnBehalfOfContext, the token request context when using the client credential 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 
assertion: string containing assertion (access token used in the request)
additional_params: hash of symbols to string values, ie { response_mode: 'fragment', prompt: 'login' }
                   default is empty hash

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/on_behalf_of_context.rb', line 21

def initialize(tenant_id, client_id, client_secret, assertion, additional_params = {})
  raise StandardError, 'assertion cannot be empty' if assertion.nil? || assertion.empty?

  @tenant_id = tenant_id
  @client_id = client_id
  @client_secret = client_secret
  @assertion = assertion
  @additional_params = additional_params
  @scopes = nil
  @oauth_provider = nil
  @grant_type = 'urn:ietf:params:Oauth:grant-type:jwt-bearer'
    
  if @tenant_id.nil? || @client_id.nil? || @client_secret.nil? || @client_secret.empty? || @tenant_id.empty? || @client_id.empty?
    raise StandardError, 'tenant_id, client_secret, and client_id 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/on_behalf_of_context.rb', line 9

def additional_params
  @additional_params
end

#client_idObject

Returns the value of attribute client_id.



9
10
11
# File 'lib/microsoft_kiota_authentication_oauth/contexts/on_behalf_of_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/on_behalf_of_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/on_behalf_of_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/on_behalf_of_context.rb', line 9

def oauth_provider
  @oauth_provider
end

#scopes=(value) ⇒ Object (writeonly)

Sets the attribute scopes

Parameters:

  • value

    the value to set the attribute scopes to.



10
11
12
# File 'lib/microsoft_kiota_authentication_oauth/contexts/on_behalf_of_context.rb', line 10

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/on_behalf_of_context.rb', line 9

def tenant_id
  @tenant_id
end

Instance Method Details

#get_tokenObject



38
39
40
41
42
43
44
45
46
# File 'lib/microsoft_kiota_authentication_oauth/contexts/on_behalf_of_context.rb', line 38

def get_token
  params = {
    grant_type: @grant_type,
    assertion: @assertion, 
    scope: @scopes,
    requested_token_use: 'on_behalf_of'
  }
  @oauth_provider.on_behalf_of.get_token(params)
end

#initialize_oauth_providerObject



48
49
50
51
52
53
# File 'lib/microsoft_kiota_authentication_oauth/contexts/on_behalf_of_context.rb', line 48

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)


55
56
57
58
59
60
61
62
63
64
# File 'lib/microsoft_kiota_authentication_oauth/contexts/on_behalf_of_context.rb', line 55

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