Class: HasJwtToken::JwtConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/has_jwt_token/jwt_configuration.rb

Constant Summary collapse

DEFAULT_AUTH_ATTRIBUTE =
:id
CLAIMS =
{
  expiration_time: :exp,
  not_before_time: :nbf,
  issuer: :iss,
  audience: :aud,
  jwt_id: :jti,
  issued_at: :iat,
  subject: :sub
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJwtConfiguration

Returns a new instance of JwtConfiguration.



19
20
21
22
23
# File 'lib/has_jwt_token/jwt_configuration.rb', line 19

def initialize
  @payload = {}
  @header = {}
  @defined_claims = []
end

Instance Attribute Details

#defined_claimsObject (readonly)

Returns the value of attribute defined_claims.



17
18
19
# File 'lib/has_jwt_token/jwt_configuration.rb', line 17

def defined_claims
  @defined_claims
end

#modelObject

Returns the value of attribute model.



16
17
18
# File 'lib/has_jwt_token/jwt_configuration.rb', line 16

def model
  @model
end

Instance Method Details

#algorithm(value = nil) ⇒ Object



25
26
27
28
29
# File 'lib/has_jwt_token/jwt_configuration.rb', line 25

def algorithm(value = nil)
  return @algorithm unless value

  @algorithm = value
end

#authenticate_byObject



83
84
85
# File 'lib/has_jwt_token/jwt_configuration.rb', line 83

def authenticate_by
  @authenticate_by || DEFAULT_AUTH_ATTRIBUTE
end

#claims_payloadObject



66
67
68
69
70
71
# File 'lib/has_jwt_token/jwt_configuration.rb', line 66

def claims_payload
  defined_claims.each_with_object({}) do |claim_name, memo|
    claim_key = CLAIMS[claim_name]
    memo[claim_key] = public_send(claim_name)
  end
end

#header(name = nil, value = nil) ⇒ Object



73
74
75
# File 'lib/has_jwt_token/jwt_configuration.rb', line 73

def header(name = nil, value = nil)
  @header[name] = value if name
end

#header_fieldsObject



77
78
79
80
81
# File 'lib/has_jwt_token/jwt_configuration.rb', line 77

def header_fields
  @header.transform_values do |val|
    val.is_a?(Proc) ? val.call : val
  end
end

#model_payloadObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/has_jwt_token/jwt_configuration.rb', line 49

def model_payload
  @payload.transform_values do |val|
    next val if !val.is_a?(Proc) || !model

    begin
      val.call(model)
    rescue ArgumentError
      val.call
    end
  end
end

#payload(name = nil, value: nil, auth_by: false) ⇒ Object



61
62
63
64
# File 'lib/has_jwt_token/jwt_configuration.rb', line 61

def payload(name = nil, value: nil, auth_by: false)
  @authenticate_by = name if auth_by
  @payload[name] = value || ->(model) { model.respond_to?(name) && model.public_send(name) } if name
end

#secret(value = nil) ⇒ Object



31
32
33
34
35
# File 'lib/has_jwt_token/jwt_configuration.rb', line 31

def secret(value = nil)
  return @secret unless value

  @secret = value
end