Module: Philiprehberger::JwtKit
- Defined in:
- lib/philiprehberger/jwt_kit.rb,
lib/philiprehberger/jwt_kit/decoder.rb,
lib/philiprehberger/jwt_kit/encoder.rb,
lib/philiprehberger/jwt_kit/version.rb,
lib/philiprehberger/jwt_kit/revocation.rb,
lib/philiprehberger/jwt_kit/token_pair.rb,
lib/philiprehberger/jwt_kit/configuration.rb
Defined Under Namespace
Modules: Decoder, Encoder, Revocation, TokenPair Classes: Configuration, DecodeError, Error, InvalidAudience, InvalidIssuer, InvalidSignature, InvalidToken, RevokedToken, TokenExpired, TokenNotYetValid
Constant Summary collapse
- VERSION =
'0.4.0'
Class Attribute Summary collapse
-
.revocation_store ⇒ #revoke, ...
Returns the revocation store.
Class Method Summary collapse
-
.configuration ⇒ Configuration
Returns the current configuration.
-
.configure {|config| ... } ⇒ void
Configures JwtKit using a block.
-
.decode(token) ⇒ Hash
Decodes a JWT token and validates its claims.
-
.encode(payload = {}) ⇒ String
Encodes a payload into a signed JWT token.
-
.expired?(token) ⇒ Boolean
Checks whether a token’s ‘exp` claim is in the past without verifying the signature.
-
.peek(token) ⇒ Hash
Decodes a JWT token WITHOUT verifying the signature.
-
.refresh(refresh_token) ⇒ String
Generates a new access token from a refresh token.
-
.reset_configuration! ⇒ Configuration
Resets the configuration to defaults.
-
.reset_revocation_store! ⇒ Revocation::MemoryStore
Resets the revocation store to the default MemoryStore.
-
.revoke(token) ⇒ void
Revokes a token.
-
.revoked?(token) ⇒ Boolean
Checks whether a token has been revoked.
-
.token_pair(payload = {}) ⇒ Array<String>
Generates an access/refresh token pair.
-
.validate(token) ⇒ Hash
Validates a token and returns a result hash instead of raising.
Class Attribute Details
.revocation_store ⇒ #revoke, ...
Returns the revocation store.
141 142 143 |
# File 'lib/philiprehberger/jwt_kit.rb', line 141 def revocation_store @revocation_store ||= Revocation::MemoryStore.new end |
Class Method Details
.configuration ⇒ Configuration
Returns the current configuration.
38 39 40 |
# File 'lib/philiprehberger/jwt_kit.rb', line 38 def configuration @configuration ||= Configuration.new end |
.configure {|config| ... } ⇒ void
This method returns an undefined value.
Configures JwtKit using a block.
31 32 33 |
# File 'lib/philiprehberger/jwt_kit.rb', line 31 def configure yield(configuration) end |
.decode(token) ⇒ Hash
Decodes a JWT token and validates its claims.
89 90 91 92 93 94 |
# File 'lib/philiprehberger/jwt_kit.rb', line 89 def decode(token) payload = Decoder.decode(token, configuration) raise RevokedToken, 'Token has been revoked' if revocation_store.revoked?(token) payload end |
.encode(payload = {}) ⇒ String
Encodes a payload into a signed JWT token.
80 81 82 |
# File 'lib/philiprehberger/jwt_kit.rb', line 80 def encode(payload = {}) Encoder.encode(payload, configuration) end |
.expired?(token) ⇒ Boolean
Checks whether a token’s ‘exp` claim is in the past without verifying the signature. Useful for proactive refresh decisions. Returns `true` for malformed tokens or when `exp` is missing.
66 67 68 69 70 71 72 73 74 |
# File 'lib/philiprehberger/jwt_kit.rb', line 66 def expired?(token) payload = peek(token)[:payload] exp = payload['exp'] return true unless exp.is_a?(Numeric) Time.now.to_i >= exp rescue DecodeError true end |
.peek(token) ⇒ Hash
Decodes a JWT token WITHOUT verifying the signature. Useful for inspecting the header and payload before choosing a key.
102 103 104 |
# File 'lib/philiprehberger/jwt_kit.rb', line 102 def peek(token) Decoder.peek(token) end |
.refresh(refresh_token) ⇒ String
Generates a new access token from a refresh token.
118 119 120 |
# File 'lib/philiprehberger/jwt_kit.rb', line 118 def refresh(refresh_token) TokenPair.refresh(refresh_token, configuration) end |
.reset_configuration! ⇒ Configuration
Resets the configuration to defaults.
45 46 47 |
# File 'lib/philiprehberger/jwt_kit.rb', line 45 def reset_configuration! @configuration = Configuration.new end |
.reset_revocation_store! ⇒ Revocation::MemoryStore
Resets the revocation store to the default MemoryStore.
154 155 156 |
# File 'lib/philiprehberger/jwt_kit.rb', line 154 def reset_revocation_store! @revocation_store = Revocation::MemoryStore.new end |
.revoke(token) ⇒ void
This method returns an undefined value.
Revokes a token.
126 127 128 |
# File 'lib/philiprehberger/jwt_kit.rb', line 126 def revoke(token) revocation_store.revoke(token) end |
.revoked?(token) ⇒ Boolean
Checks whether a token has been revoked.
134 135 136 |
# File 'lib/philiprehberger/jwt_kit.rb', line 134 def revoked?(token) revocation_store.revoked?(token) end |
.token_pair(payload = {}) ⇒ Array<String>
Generates an access/refresh token pair.
110 111 112 |
# File 'lib/philiprehberger/jwt_kit.rb', line 110 def token_pair(payload = {}) TokenPair.generate(payload, configuration) end |
.validate(token) ⇒ Hash
Validates a token and returns a result hash instead of raising.
53 54 55 56 57 58 |
# File 'lib/philiprehberger/jwt_kit.rb', line 53 def validate(token) payload = decode(token) { valid: true, payload: payload, error: nil } rescue DecodeError, RevokedToken => e { valid: false, payload: nil, error: e. } end |