Skip to content

Commit 6b00fdb

Browse files
authored
Make Auth0::Client#get_token public (#725)
2 parents 06d48e8 + dfa7f3c commit 6b00fdb

2 files changed

Lines changed: 39 additions & 36 deletions

File tree

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
module Auth0
22
module Mixins
33
module TokenManagement
4-
5-
private
6-
7-
def initialize_token(options)
8-
@token = options[:access_token] || options[:token]
9-
# default expiry to an hour if a token was given but no expires_at
10-
@token_expires_at = @token ? options[:token_expires_at] || Time.now.to_i + 3600 : nil
11-
12-
@audience = options[:api_identifier] || "https://#{@domain}/api/v2/"
13-
get_token() if @token.nil?
14-
end
154

5+
# Get the Client's api token (or generate a new one if it has expired).
6+
#
7+
# @note This method may perform a network request to refresh an expired token. It is not thread-safe.
8+
# @return [String] the api token
169
def get_token
17-
# pp @token_expires_at
1810
has_expired = @token && @token_expires_at ? @token_expires_at < (Time.now.to_i + 10) : false
19-
11+
2012
if (@token.nil? || has_expired) && @client_id && (@client_secret || @client_assertion_signing_key)
2113
response = api_token(audience: @audience)
2214
@token = response.token
@@ -27,6 +19,17 @@ def get_token
2719
@token
2820
end
2921
end
22+
23+
private
24+
25+
def initialize_token(options)
26+
@token = options[:access_token] || options[:token]
27+
# default expiry to an hour if a token was given but no expires_at
28+
@token_expires_at = @token ? options[:token_expires_at] || Time.now.to_i + 3600 : nil
29+
30+
@audience = options[:api_identifier] || "https://#{@domain}/api/v2/"
31+
get_token() if @token.nil?
32+
end
3033
end
3134
end
32-
end
35+
end

spec/lib/auth0/mixins/token_management_spec.rb

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
organization: nil
1616
} }
1717

18-
let(:params) { {
18+
let(:params) { {
1919
domain: domain,
2020
client_id: client_id,
2121
client_secret: client_secret,
@@ -43,15 +43,15 @@
4343
)))
4444

4545
expect(JSON.parse(arg[:payload], { symbolize_names: true })).to eq(payload)
46-
47-
StubResponse.new({
48-
"access_token" => "test",
49-
"expires_in" => 86400},
50-
true,
46+
47+
StubResponse.new({
48+
"access_token" => "test",
49+
"expires_in" => 86400},
50+
true,
5151
200)
5252
end
5353

54-
instance.send(:get_token)
54+
instance.get_token
5555

5656
expect(instance.instance_variable_get('@token')).to eq('test')
5757
expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86400)
@@ -66,7 +66,7 @@
6666
url: 'https://samples.auth0.com/oauth/token',
6767
))
6868

69-
instance.send(:get_token)
69+
instance.get_token
7070

7171
expect(instance.instance_variable_get('@token')).to eq('test-token')
7272
expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86400)
@@ -84,15 +84,15 @@
8484
)))
8585

8686
expect(JSON.parse(arg[:payload], { symbolize_names: true })).to eq(payload)
87-
88-
StubResponse.new({
89-
"access_token" => "renewed_token",
90-
"expires_in" => 86400},
91-
true,
87+
88+
StubResponse.new({
89+
"access_token" => "renewed_token",
90+
"expires_in" => 86400},
91+
true,
9292
200)
9393
end
9494

95-
instance.send(:get_token)
95+
instance.get_token
9696

9797
expect(instance.instance_variable_get('@token')).to eq('renewed_token')
9898
expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86400)
@@ -110,15 +110,15 @@
110110
)))
111111

112112
expect(JSON.parse(arg[:payload], { symbolize_names: true })).to eq(payload)
113-
114-
StubResponse.new({
115-
"access_token" => "renewed_token",
116-
"expires_in" => 86400},
117-
true,
113+
114+
StubResponse.new({
115+
"access_token" => "renewed_token",
116+
"expires_in" => 86400},
117+
true,
118118
200)
119119
end
120120

121-
instance.send(:get_token)
121+
instance.get_token
122122

123123
expect(instance.instance_variable_get('@token')).to eq('renewed_token')
124124
expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86400)
@@ -130,7 +130,7 @@
130130

131131
expect(RestClient::Request).not_to receive(:execute)
132132

133-
instance.send(:get_token)
133+
expect(instance.get_token).to eq('test-token')
134134
end
135135
end
136-
end
136+
end

0 commit comments

Comments
 (0)