Module: JwtAuthenticable::Auth
- Includes:
- Exceptions, Responses
- Defined in:
- lib/jwt_authenticable/auth.rb
Overview
Module that adds jwt authentication methods to the client
Constant Summary collapse
- ALGORITHM =
JwtAuthenticable.config.algorithm
Instance Method Summary collapse
-
#authenticate_user! ⇒ Object
Authenticates a user.
-
#authorization_token! ⇒ String
Extracts the authorization token from the Authorization header.
-
#validate_jwt_token!(token:) ⇒ Hash
Validate that the JWT token signature and the following claims are valid: - exp - scope.
Methods included from Responses
#accepted, #access_denied, #created, #destroyed, #no_content, #not_found, #payload_too_large, #render_page, #render_resource, #render_success_resource, #unauthorized, #unprocessable_entity, #validation_error
Instance Method Details
#authenticate_user! ⇒ Object
Authenticates a user.
16 17 18 19 20 |
# File 'lib/jwt_authenticable/auth.rb', line 16 def authenticate_user! validate_jwt_token! token: rescue MissingAuth, MissingAuthScope, InvalidAuthScheme, JWT::VerificationError, JWT::ExpiredSignature => e (e.) end |
#authorization_token! ⇒ String
Note:
For now we only support Bearer schema with JWT
Extracts the authorization token from the Authorization header
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jwt_authenticable/auth.rb', line 41 def raise InvalidIncluder unless defined? request auth_token = request.headers['Authorization'] auth_token ||= request.['bearer_token'] raise MissingAuth if auth_token.nil? || auth_token == '' raise InvalidAuthScheme if auth_token[0..6] != 'Bearer ' auth_token[7..] end |
#validate_jwt_token!(token:) ⇒ Hash
Validate that the JWT token signature and the following claims are valid:
- exp
- scope
31 32 33 34 |
# File 'lib/jwt_authenticable/auth.rb', line 31 def validate_jwt_token!(token:) # NOTE: it is still safe if JWT_SECRET_KEY is not set. The method will trigger a JWT exception JWT.decode(token, JwtAuthenticable.config.jwt_secret_key, true, { algorithm: ALGORITHM }).first end |