Class: Philiprehberger::JwtKit::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/jwt_kit/configuration.rb

Overview

Configuration singleton for JWT settings.

Examples:

Philiprehberger::JwtKit.configure do |c|
  c.secret = 'my-secret-key'
  c.algorithm = :hs256
  c.issuer = 'my-app'
  c.expiration = 3600
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

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

#algorithmSymbol

Returns signing algorithm (:hs256, :hs384, :hs512).

Returns:

  • (Symbol)

    signing algorithm (:hs256, :hs384, :hs512)



19
20
21
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 19

def algorithm
  @algorithm
end

#audienceString, ...

Returns expected audience for the ‘aud` claim.

Returns:

  • (String, Array<String>, nil)

    expected audience for the ‘aud` claim



25
26
27
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 25

def audience
  @audience
end

#expirationInteger

Returns default TTL in seconds for access tokens.

Returns:

  • (Integer)

    default TTL in seconds for access tokens



28
29
30
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 28

def expiration
  @expiration
end

#issuerString?

Returns optional issuer for the ‘iss` claim.

Returns:

  • (String, nil)

    optional issuer for the ‘iss` claim



22
23
24
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 22

def issuer
  @issuer
end

#refresh_expirationInteger

Returns default TTL in seconds for refresh tokens.

Returns:

  • (Integer)

    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

#secretString?

Returns HMAC secret key (required for HS* algorithms).

Returns:

  • (String, nil)

    HMAC secret key (required for HS* algorithms)



16
17
18
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 16

def secret
  @secret
end

#secretsArray<Hash>?

Returns array of { kid: String, secret: String } for key rotation.

Returns:

  • (Array<Hash>, nil)

    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_nameString

Returns the JWT algorithm header value.

Returns:

  • (String)

    algorithm name (e.g. ‘HS256’)



148
149
150
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 148

def algorithm_name
  @algorithm.to_s.upcase
end

#digest_algorithmString

Returns the OpenSSL digest algorithm name.

Returns:

  • (String)

    digest name (e.g. ‘SHA256’)

Raises:

  • (Error)

    if the algorithm is unsupported



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.

Parameters:

  • payload (Hash)


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.

Parameters:

  • token (String)
  • payload (Hash)


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.

Parameters:

  • new_token (String)


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.

Parameters:

  • jti (String, nil)


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.

Yield Parameters:

  • payload (Hash)

    the decoded payload

Returns:

  • (Proc)

    the stored callback



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.

Yield Parameters:

  • token (String)

    the encoded JWT token

  • payload (Hash)

    the merged payload that was encoded

Returns:

  • (Proc)

    the stored callback



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.

Yield Parameters:

  • new_token (String)

    the newly issued access token

Returns:

  • (Proc)

    the stored callback



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.

Yield Parameters:

  • jti (String, nil)

    the revoked token’s JTI

Returns:

  • (Proc)

    the stored callback



79
80
81
# File 'lib/philiprehberger/jwt_kit/configuration.rb', line 79

def on_revoke(&block)
  @on_revoke = block
end