Class: Apill::Tokens::JsonWebToken

Inherits:
Object
  • Object
show all
Defined in:
lib/apill/tokens/json_web_token.rb

Constant Summary collapse

TRANSFORMATION_EXCEPTIONS =
[
  JSON::JWT::Exception,
  JSON::JWT::InvalidFormat,
  JSON::JWT::VerificationFailed,
  JSON::JWT::UnexpectedAlgorithm,
  JWT::DecodeError,
  JWT::VerificationError,
  JWT::ExpiredSignature,
  JWT::IncorrectAlgorithm,
  JWT::ImmatureSignature,
  JWT::InvalidIssuerError,
  JWT::InvalidIatError,
  JWT::InvalidAudError,
  JWT::InvalidSubError,
  JWT::InvalidJtiError,
  OpenSSL::PKey::RSAError,
  OpenSSL::Cipher::CipherError,
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, private_key: Apill.configuration.token_private_key) ⇒ JsonWebToken

Returns a new instance of JsonWebToken.



31
32
33
34
35
36
# File 'lib/apill/tokens/json_web_token.rb', line 31

def initialize(data:,
               private_key: Apill.configuration.token_private_key)

  self.data        = data
  self.private_key = private_key
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



28
29
30
# File 'lib/apill/tokens/json_web_token.rb', line 28

def data
  @data
end

#private_keyObject

Returns the value of attribute private_key.



28
29
30
# File 'lib/apill/tokens/json_web_token.rb', line 28

def private_key
  @private_key
end

Class Method Details

.from_jwe(encrypted_token, private_key: Apill.configuration.token_private_key) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/apill/tokens/json_web_token.rb', line 74

def self.from_jwe(encrypted_token,
                  private_key: Apill.configuration.token_private_key)

  return JsonWebTokens::Null.instance if encrypted_token.to_s == ''

  decrypted_token = JSON::JWT.
                      decode(encrypted_token, private_key).
                      plain_text

  from_jws(decrypted_token, private_key: private_key)
rescue *TRANSFORMATION_EXCEPTIONS
  JsonWebTokens::Invalid.instance
end

.from_jws(signed_token, private_key: Apill.configuration.token_private_key) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/apill/tokens/json_web_token.rb', line 88

def self.from_jws(signed_token,
                  private_key: Apill.configuration.token_private_key)

  return JsonWebTokens::Null.instance if signed_token.to_s == ''

  data = JWT.decode(
                     signed_token,
                     private_key,
                     true,
                     algorithm:         'RS256',
                     verify_expiration: true,
                     verify_not_before: true,
                     verify_iat:        true,
                     leeway:            5,
  )

  new(data:        data,
      private_key: private_key)
rescue *TRANSFORMATION_EXCEPTIONS
  JsonWebTokens::Invalid.instance
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/apill/tokens/json_web_token.rb', line 42

def blank?
  false
end

#to_hObject



46
47
48
# File 'lib/apill/tokens/json_web_token.rb', line 46

def to_h
  data
end

#to_jweObject



66
67
68
# File 'lib/apill/tokens/json_web_token.rb', line 66

def to_jwe
  @jwe ||= to_jws.encrypt(private_key, 'RSA-OAEP', 'A256GCM')
end

#to_jwe_sObject



70
71
72
# File 'lib/apill/tokens/json_web_token.rb', line 70

def to_jwe_s
  @jwe_s ||= to_jwe.to_s
end

#to_jwsObject



58
59
60
# File 'lib/apill/tokens/json_web_token.rb', line 58

def to_jws
  @jws ||= to_jwt.sign(private_key,    'RS256')
end

#to_jws_sObject



62
63
64
# File 'lib/apill/tokens/json_web_token.rb', line 62

def to_jws_s
  @jws_s ||= to_jws.to_s
end

#to_jwtObject



50
51
52
# File 'lib/apill/tokens/json_web_token.rb', line 50

def to_jwt
  @jwt ||= JSON::JWT.new(data)
end

#to_jwt_sObject



54
55
56
# File 'lib/apill/tokens/json_web_token.rb', line 54

def to_jwt_s
  @jwt_s ||= to_jwt.to_s
end

#valid?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/apill/tokens/json_web_token.rb', line 38

def valid?
  true
end