Class: MicrosoftKiotaAuthenticationOAuth::ClientCredentialContext

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

Overview

Token request context class for the client credential 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, additional_params = {}) ⇒ ClientCredentialContext

This is the initializer for ClientCredentialContext, 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
additional_params: hash of symbols to string values, ie { response_mode: 'fragment', prompt: 'login' }
                   default is empty hash


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

def initialize(tenant_id, client_id, client_secret, additional_params = {})
  @tenant_id = tenant_id
  @client_id = client_id
  @client_secret = client_secret
  @additional_params = additional_params
  @scopes = nil
  @oauth_provider = nil
  @grant_type = 'client credential'


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

def tenant_id
  @tenant_id
end

Instance Method Details

#get_tokenObject



35
36
37
# File 'lib/microsoft_kiota_authentication_oauth/contexts/client_credential_context.rb', line 35

def get_token
  @oauth_provider.client_credentials.get_token({ scope: @scopes })
end

#initialize_oauth_providerObject



39
40
41
42
43
44
# File 'lib/microsoft_kiota_authentication_oauth/contexts/client_credential_context.rb', line 39

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

Function to initialize the scope for the client credential context object. Only a single scope is supported for this flow and it’s expected to be schme://service/.default



48
49
50
# File 'lib/microsoft_kiota_authentication_oauth/contexts/client_credential_context.rb', line 48

def initialize_scopes(scopes = []) 
  @scopes = scopes[0] unless scopes.empty?
end