Class: MicrosoftKiotaAuthenticationOAuth::OAuthAccessTokenProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/microsoft_kiota_authentication_oauth/oauth_access_token_provider.rb

Overview

Access Token Provider class implementation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token_request_context, allowed_hosts = [], scopes = []) ⇒ OAuthAccessTokenProvider

This is the initializer for OAuthAccessTokenProvider. :params

token_request_context: a instance of one of our token request context or a custom implementation
allowed_hosts: an array of strings, where each string is an allowed host, default is empty
scopes: an array of strings, where each string is a scope, default is empty array

Raises:

  • (StandardError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/microsoft_kiota_authentication_oauth/oauth_access_token_provider.rb', line 20

def initialize(token_request_context, allowed_hosts = [], scopes = [])
  raise StandardError, 'Parameter token_request_context cannot be nil.' if token_request_context.nil?

  @token_request_context = token_request_context

  unless @token_request_context.is_a?(MicrosoftKiotaAuthenticationOAuth::OAuthContext)
    raise StandardError, 'Parameter token_request_context must be an instance of one of our grant flow context classes.'
  end

  @cached_token = nil

  @host_validator = if allowed_hosts.nil? || allowed_hosts.size.zero?
                      MicrosoftKiotaAbstractions::AllowedHostsValidator.new([])
                    else
                      MicrosoftKiotaAbstractions::AllowedHostsValidator.new(allowed_hosts)
                    end
  @token_request_context.initialize_oauth_provider
  if scopes.nil?
    @scopes = []
  else
    @scopes = scopes
  end
end

Instance Attribute Details

#host_validatorObject

Returns the value of attribute host_validator.



84
85
86
# File 'lib/microsoft_kiota_authentication_oauth/oauth_access_token_provider.rb', line 84

def host_validator
  @host_validator
end

#scopesObject

Returns the value of attribute scopes.



84
85
86
# File 'lib/microsoft_kiota_authentication_oauth/oauth_access_token_provider.rb', line 84

def scopes
  @scopes
end

Instance Method Details

#get_authorization_token(uri, additional_properties = {}) ⇒ Object

This function obtains the authorization token. :params

uri: a string containing the uri 
additional_params: hash of symbols to string values, ie { response_mode: 'fragment', prompt: 'login' }
                   default is empty hash

Raises:

  • (StandardError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/microsoft_kiota_authentication_oauth/oauth_access_token_provider.rb', line 49

def get_authorization_token(uri, additional_properties = {})
  nil if !uri || !@host_validator.url_host_valid?(uri)

  parsed_url = URI(uri)

  raise StandardError, 'Only https is supported' if parsed_url.scheme != 'https'

  if @scopes.empty?
    @scopes << "#{parsed_url.scheme}://#{parsed_url.host}/.default"
  end
  @token_request_context.initialize_scopes(@scopes)
  Fiber.new do
    if @cached_token
      token = OAuth2::AccessToken.from_hash(@token_request_context.oauth_provider, @cached_token) 
      token.token unless token.nil? || token.expired?

      if token.expired?
        token = token.refresh!
        @cached_token = token.to_hash
        token.token
      end
    end

    token = nil
    token = @token_request_context.get_token

    if !token.nil?
      @cached_token = token.to_hash
      token.token
    else
      nil
    end
  end
end