Skip to content

Commit

Permalink
Raise error if token auth fails
Browse files Browse the repository at this point in the history
  • Loading branch information
logan-stytch committed Aug 7, 2023
1 parent fe6fdfc commit 8041f14
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
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

0 comments on commit 8041f14

Please sign in to comment.