microsoft_kiota_authentication_oauth 0.25.1 → 0.25.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b00ad195eb3933fe1efd796a5346257abf818e80a5063bda1a63c5a3ef9e7c5c
4
- data.tar.gz: 8fc9c22ce06ceed7f06458b1faf10f4d304be855ae3b13888d9680a7ff4e0be3
3
+ metadata.gz: 16a7322e45b5fe81041d3ee2615f4101f3eec9fb795bb20edc2477c2236e0069
4
+ data.tar.gz: c9bb37a3fe121fb366f6a3cb6b00201ae612981bab514d1da821937e2a77a387
5
5
  SHA512:
6
- metadata.gz: 911583ddfadad02fd709e60983e28533058de14952e6b36b6dc9301c6abd844d4dd723bf36ce92687b1e5150bad20593344519530b2b10853490c7ffc32fcb98
7
- data.tar.gz: 2e0c098e7780bc71c9ae2472ae4781926b86335746039ce1937b08ffc54f5cfdd8174ddea6b9f79886b30fa828c79fa04a3dba2f70767895aec71ee5d4acdedf
6
+ metadata.gz: 7fa056cfa525c11c57f9b512678bb45135b3b83dfeaaafeb349a1037ff1738433f5856628007768c4d2413b0bef5caac66ae084b9fc65835195b38ad233a74bc
7
+ data.tar.gz: 50afae4667edee83700ffed74d9b68012999a90227044c062fc2de50fff3f9fbecc06878f9ad4e92a29b939478565606ee691177f0c9c8f52d846e9c7182813e
@@ -12,6 +12,8 @@ require_relative 'contexts/oauth_custom_flow'
12
12
  module MicrosoftKiotaAuthenticationOAuth
13
13
  # Access Token Provider class implementation
14
14
  class OAuthAccessTokenProvider
15
+ LOCALHOST_STRINGS = MicrosoftKiotaAbstractions::AllowedHostsValidator::LOCALHOST_STRINGS
16
+
15
17
  # This is the initializer for OAuthAccessTokenProvider.
16
18
  # :params
17
19
  # token_request_context: a instance of one of our token request context or a custom implementation
@@ -27,7 +29,7 @@ module MicrosoftKiotaAuthenticationOAuth
27
29
  'Parameter token_request_context must be an instance of one of our grant flow context classes.'
28
30
  end
29
31
 
30
- @cached_token = nil
32
+ @cached_tokens = {}
31
33
 
32
34
  @host_validator = if allowed_hosts.nil? || allowed_hosts.empty?
33
35
  MicrosoftKiotaAbstractions::AllowedHostsValidator.new([])
@@ -38,7 +40,7 @@ module MicrosoftKiotaAuthenticationOAuth
38
40
  @scopes = if scopes.nil?
39
41
  []
40
42
  else
41
- scopes
43
+ scopes.dup
42
44
  end
43
45
  end
44
46
 
@@ -48,40 +50,98 @@ module MicrosoftKiotaAuthenticationOAuth
48
50
  # additional_params: hash of symbols to string values, ie { response_mode: 'fragment', prompt: 'login' }
49
51
  # default is empty hash
50
52
  def get_authorization_token(uri, _additional_properties = {})
51
- return Fiber.new { nil } if !uri || !@host_validator.url_host_valid?(uri)
53
+ return Fiber.new { nil } if !uri || uri.to_s.empty?
52
54
 
53
- parsed_url = URI(uri)
55
+ parsed_url = parse_uri(uri)
56
+ return Fiber.new { nil } if parsed_url.nil? || !valid_host?(parsed_url)
54
57
 
55
- raise StandardError, 'Only https is supported' if parsed_url.scheme != 'https'
58
+ validate_scheme!(parsed_url)
56
59
 
57
- @scopes << "#{parsed_url.scheme}://#{parsed_url.host}/.default" if @scopes.empty?
58
- @token_request_context.initialize_scopes(@scopes)
60
+ effective_scopes = determine_scopes(parsed_url)
61
+ cache_key = effective_scopes.freeze
59
62
  Fiber.new do
60
- if @cached_token
61
- token = OAuth2::AccessToken.from_hash(@token_request_context.oauth_provider, @cached_token)
62
- token.token unless token.nil? || token.expired?
63
-
64
- if token.expired?
65
- token = token.refresh!
66
- @cached_token = token.to_hash
67
- token.token
68
- end
69
- end
63
+ @token_request_context.initialize_scopes(effective_scopes)
64
+ fetch_token(cache_key)
65
+ end
66
+ end
70
67
 
71
- nil
72
- token = @token_request_context.get_token
68
+ attr_reader :scopes, :host_validator
69
+
70
+ protected
71
+
72
+ attr_writer :host_validator, :token_credential, :scopes, :cached_tokens
73
73
 
74
+ private
75
+
76
+ def parse_uri(uri)
77
+ parsed = URI(uri)
78
+ return nil if parsed.nil? || parsed.host.nil? || parsed.host.empty?
79
+
80
+ parsed
81
+ rescue URI::InvalidURIError, ArgumentError
82
+ nil
83
+ end
84
+
85
+ def valid_host?(parsed_url)
86
+ https_version = parsed_url.dup
87
+ https_version.scheme = 'https'
88
+ @host_validator.url_host_valid?(https_version.to_s)
89
+ end
90
+
91
+ def validate_scheme!(parsed_url)
92
+ scheme = parsed_url.scheme&.downcase
93
+ return if scheme == 'https' || (scheme == 'http' && localhost?(parsed_url))
94
+
95
+ raise StandardError, 'Only https is supported'
96
+ end
97
+
98
+ def determine_scopes(parsed_url)
99
+ @scopes.empty? ? ["#{parsed_url.scheme}://#{parsed_url.host}/.default"] : @scopes.dup
100
+ end
101
+
102
+ def fetch_token(cache_key)
103
+ token_value = nil
104
+ cached_token = @cached_tokens[cache_key]
105
+ token_value = resolve_cached_token(cached_token, cache_key) unless cached_token.nil?
106
+
107
+ if token_value.nil?
108
+ token = @token_request_context.get_token
74
109
  unless token.nil?
75
- @cached_token = token.to_hash
76
- token.token
110
+ @cached_tokens[cache_key] = token.to_hash
111
+ token_value = token.token
77
112
  end
78
113
  end
114
+
115
+ token_value
79
116
  end
80
117
 
81
- attr_reader :scopes, :host_validator
118
+ def resolve_cached_token(cached_token, cache_key)
119
+ token = OAuth2::AccessToken.from_hash(@token_request_context.oauth_provider, cached_token)
120
+ return nil unless token
82
121
 
83
- protected
122
+ if token.expired?
123
+ return nil if token.refresh_token.nil? || token.refresh_token.empty?
84
124
 
85
- attr_writer :host_validator, :token_credential, :scopes, :cached_token
125
+ token = token.refresh!
126
+ @cached_tokens[cache_key] = token.to_hash
127
+ end
128
+ token.token
129
+ end
130
+
131
+ def localhost?(parsed_url)
132
+ if @host_validator.respond_to?(:localhost?)
133
+ @host_validator.localhost?(parsed_url)
134
+ else
135
+ host = parsed_url.host
136
+ return false if host.nil?
137
+
138
+ return true if LOCALHOST_STRINGS.include?(host.downcase)
139
+
140
+ hostname = parsed_url.hostname
141
+ return false if hostname.nil?
142
+
143
+ LOCALHOST_STRINGS.include?(hostname.downcase)
144
+ end
145
+ end
86
146
  end
87
147
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MicrosoftKiotaAuthenticationOAuth
4
- VERSION = '0.25.1' # x-release-please-version
4
+ VERSION = '0.25.3' # x-release-please-version
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: microsoft_kiota_authentication_oauth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.25.1
4
+ version: 0.25.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Microsoft Corporation
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 0.25.1
18
+ version: 0.25.3
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 0.25.1
25
+ version: 0.25.3
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: oauth2
28
28
  requirement: !ruby/object:Gem::Requirement