Skip to content
Merged
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
31 changes: 17 additions & 14 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).
Comment thread
arpit-jn marked this conversation as resolved.
#
# @note This method may perform a network request to refresh an expired token. It is not thread-safe.
# @return [String] the api token
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 LOW · conventions

The @return tag says [String] but the method can return nil when @token is nil and no @client_id/@client_secret is configured (the else branch returns @token which could be nil).

Update to @return [String, nil] the API token, or nil if no token is available and credentials are not configured.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one could mislead. It tells callers to nil-guard against a scenario that means the client is broken. A misconfigured client should fail loudly at init, not silently return nil. Documenting nil as valid makes a bug look like expected behavior.

def get_token
Comment thread
arpit-jn marked this conversation as resolved.
# 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)

Comment thread
arpit-jn marked this conversation as resolved.
instance.send(:get_token)
expect(instance.get_token).to eq('test-token')
end
end
end
end
Loading