Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions lib/auth0/mixins/token_management.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
module Auth0
module Mixins
module TokenManagement

private

def initialize_token(options)
@token = options[:access_token] || options[:token]
# default expiry to an hour if a token was given but no expires_at
@token_expires_at = @token ? options[:token_expires_at] || Time.now.to_i + 3600 : nil

@audience = options[:api_identifier] || "https://#{@domain}/api/v2/"
get_token() if @token.nil?
end

# Get the Client's api token (or generate a new one if it has expired).
#
# @return [String] the api token
def get_token
# pp @token_expires_at
has_expired = @token && @token_expires_at ? @token_expires_at < (Time.now.to_i + 10) : false

if (@token.nil? || has_expired) && @client_id && (@client_secret || @client_assertion_signing_key)
response = api_token(audience: @audience)
@token = response.token
Expand All @@ -27,6 +19,17 @@ def get_token
@token
end
end

private

def initialize_token(options)
@token = options[:access_token] || options[:token]
# default expiry to an hour if a token was given but no expires_at
@token_expires_at = @token ? options[:token_expires_at] || Time.now.to_i + 3600 : nil

@audience = options[:api_identifier] || "https://#{@domain}/api/v2/"
get_token() if @token.nil?
end
end
end
end
end
44 changes: 22 additions & 22 deletions spec/lib/auth0/mixins/token_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
organization: nil
} }

let(:params) { {
let(:params) { {
domain: domain,
client_id: client_id,
client_secret: client_secret,
Expand Down Expand Up @@ -43,15 +43,15 @@
)))

expect(JSON.parse(arg[:payload], { symbolize_names: true })).to eq(payload)
StubResponse.new({
"access_token" => "test",
"expires_in" => 86400},
true,

StubResponse.new({
"access_token" => "test",
"expires_in" => 86400},
true,
200)
end

instance.send(:get_token)
instance.get_token

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

instance.send(:get_token)
instance.get_token

expect(instance.instance_variable_get('@token')).to eq('test-token')
expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86400)
Expand All @@ -84,15 +84,15 @@
)))

expect(JSON.parse(arg[:payload], { symbolize_names: true })).to eq(payload)
StubResponse.new({
"access_token" => "renewed_token",
"expires_in" => 86400},
true,

StubResponse.new({
"access_token" => "renewed_token",
"expires_in" => 86400},
true,
200)
end

instance.send(:get_token)
instance.get_token

expect(instance.instance_variable_get('@token')).to eq('renewed_token')
expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86400)
Expand All @@ -110,15 +110,15 @@
)))

expect(JSON.parse(arg[:payload], { symbolize_names: true })).to eq(payload)
StubResponse.new({
"access_token" => "renewed_token",
"expires_in" => 86400},
true,

StubResponse.new({
"access_token" => "renewed_token",
"expires_in" => 86400},
true,
200)
end

instance.send(:get_token)
instance.get_token

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

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

instance.send(:get_token)
instance.get_token
end
end
end
end