Class: Philiprehberger::JwtKit::Configuration
- Inherits:
-
Object
- Object
- Philiprehberger::JwtKit::Configuration
- Defined in:
- lib/philiprehberger/jwt_kit/configuration.rb
Overview
Configuration singleton for JWT settings.
Instance Attribute Summary collapse
-
#algorithm ⇒ Symbol
Signing algorithm (:hs256, :hs384, :hs512).
-
#audience ⇒ String, ...
Expected audience for the ‘aud` claim.
-
#expiration ⇒ Integer
Default TTL in seconds for access tokens.
-
#issuer ⇒ String?
Optional issuer for the ‘iss` claim.
-
#refresh_expiration ⇒ Integer
Default TTL in seconds for refresh tokens.
-
#secret ⇒ String?
HMAC secret key (required for HS* algorithms).
-
#secrets ⇒ Array<Hash>?
Array of { kid: String, secret: String } for key rotation.
Instance Method Summary collapse
-
#algorithm_name ⇒ String
Returns the JWT algorithm header value.
-
#digest_algorithm ⇒ String
Returns the OpenSSL digest algorithm name.
-
#fire_on_decode(payload) ⇒ void
Invokes the on_decode callback if registered.
-
#fire_on_encode(token, payload) ⇒ void
Invokes the on_encode callback if registered.
-
#fire_on_refresh(new_token) ⇒ void
Invokes the on_refresh callback if registered.
-
#fire_on_revoke(jti) ⇒ void
Invokes the on_revoke callback if registered.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#on_decode {|payload| ... } ⇒ Proc
Registers a callback fired after a successful decode.
-
#on_encode {|token, payload| ... } ⇒ Proc
Registers a callback fired after a successful encode.
-
#on_refresh {|new_token| ... } ⇒ Proc
Registers a callback fired after a successful refresh.
-
#on_revoke {|jti| ... } ⇒ Proc
Registers a callback fired after a successful revoke.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 36 def initialize @secret = nil @algorithm = :hs256 @issuer = nil @audience = nil @expiration = 3600 @refresh_expiration = 86_400 * 7 @secrets = nil @on_encode = nil @on_decode = nil @on_refresh = nil @on_revoke = nil end |
Instance Attribute Details
#algorithm ⇒ Symbol
Returns signing algorithm (:hs256, :hs384, :hs512).
19 20 21 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 19 def algorithm @algorithm end |
#audience ⇒ String, ...
Returns expected audience for the ‘aud` claim.
25 26 27 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 25 def audience @audience end |
#expiration ⇒ Integer
Returns default TTL in seconds for access tokens.
28 29 30 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 28 def expiration @expiration end |
#issuer ⇒ String?
Returns optional issuer for the ‘iss` claim.
22 23 24 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 22 def issuer @issuer end |
#refresh_expiration ⇒ Integer
Returns default TTL in seconds for refresh tokens.
31 32 33 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 31 def refresh_expiration @refresh_expiration end |
#secret ⇒ String?
Returns HMAC secret key (required for HS* algorithms).
16 17 18 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 16 def secret @secret end |
#secrets ⇒ Array<Hash>?
Returns array of { kid: String, secret: String } for key rotation.
34 35 36 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 34 def secrets @secrets end |
Instance Method Details
#algorithm_name ⇒ String
Returns the JWT algorithm header value.
148 149 150 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 148 def algorithm_name @algorithm.to_s.upcase end |
#digest_algorithm ⇒ String
Returns the OpenSSL digest algorithm name.
136 137 138 139 140 141 142 143 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 136 def digest_algorithm case @algorithm when :hs256 then 'SHA256' when :hs384 then 'SHA384' when :hs512 then 'SHA512' else raise Error, "Unsupported algorithm: #{@algorithm}" end end |
#fire_on_decode(payload) ⇒ void
This method returns an undefined value.
Invokes the on_decode callback if registered. Errors raised by the callback are swallowed.
100 101 102 103 104 105 106 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 100 def fire_on_decode(payload) return unless @on_decode @on_decode.call(payload) rescue StandardError nil end |
#fire_on_encode(token, payload) ⇒ void
This method returns an undefined value.
Invokes the on_encode callback if registered. Errors raised by the callback are swallowed.
88 89 90 91 92 93 94 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 88 def fire_on_encode(token, payload) return unless @on_encode @on_encode.call(token, payload) rescue StandardError nil end |
#fire_on_refresh(new_token) ⇒ void
This method returns an undefined value.
Invokes the on_refresh callback if registered. Errors raised by the callback are swallowed.
112 113 114 115 116 117 118 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 112 def fire_on_refresh(new_token) return unless @on_refresh @on_refresh.call(new_token) rescue StandardError nil end |
#fire_on_revoke(jti) ⇒ void
This method returns an undefined value.
Invokes the on_revoke callback if registered. Errors raised by the callback are swallowed.
124 125 126 127 128 129 130 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 124 def fire_on_revoke(jti) return unless @on_revoke @on_revoke.call(jti) rescue StandardError nil end |
#on_decode {|payload| ... } ⇒ Proc
Registers a callback fired after a successful decode.
63 64 65 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 63 def on_decode(&block) @on_decode = block end |
#on_encode {|token, payload| ... } ⇒ Proc
Registers a callback fired after a successful encode.
55 56 57 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 55 def on_encode(&block) @on_encode = block end |
#on_refresh {|new_token| ... } ⇒ Proc
Registers a callback fired after a successful refresh.
71 72 73 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 71 def on_refresh(&block) @on_refresh = block end |
#on_revoke {|jti| ... } ⇒ Proc
Registers a callback fired after a successful revoke.
79 80 81 |
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 79 def on_revoke(&block) @on_revoke = block end |