Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error if token auth fails #89

Merged
merged 1 commit into from
Aug 7, 2023
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
13 changes: 13 additions & 0 deletions lib/stytch/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,17 @@ def initialize(msg = 'JWT algorithm is incorrect')
super
end
end

class JWTExpiredError < StandardError
def initialize(msg = 'JWT has expired')
super
end
end

class TokenMissingScopeError < StandardError
def initialize(scope)
msg = "Missing required scope #{scope}"
super(msg)
end
end
end
15 changes: 6 additions & 9 deletions lib/stytch/m2m.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,24 @@ def token(client_id:, client_secret:, scopes: nil)
# A map of custom claims present in the token.
# The type of this field is +object+.
def authenticate_token(access_token:, required_scopes: nil, max_token_age: nil)
begin
decoded_jwt = authenticate_token_local(access_token)
rescue StandardError
# Could not authenticate locally
return nil
end
# Intentionally allow this to re-raise if authentication fails
decoded_jwt = authenticate_token_local(access_token)

iat_time = Time.at(decoded_jwt['iat']).to_datetime

# Token too old
unless max_token_age.nil?
return nil if iat_time + max_token_age < Time.now
if iat_time + max_token_age < Time.now
raise JWTExpiredError
end
end

resp = marshal_jwt_into_response(decoded_jwt)

unless required_scopes.nil?
for scope in required_scopes
unless resp['scopes'].include?(scope)
# Token missing a required scope
return nil
raise TokenMissingScopeError.new(scope)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/stytch/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Stytch
VERSION = '6.2.0'
VERSION = '6.2.1'
end