Class: SelfSDK::JwtService
- Inherits:
-
Object
- Object
- SelfSDK::JwtService
- Defined in:
- lib/jwt_service.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#key_id ⇒ Object
readonly
Returns the value of attribute key_id.
Instance Method Summary collapse
-
#auth_token ⇒ Object
Generates the auth_token based on the app’s private key.
-
#decode(input) ⇒ Object
Base64 decodes the input string.
-
#encode(input) ⇒ Object
Encodes the input with base64.
-
#initialize(app_id, app_key) ⇒ JwtService
constructor
Jwt initializer.
- #parse(input) ⇒ Object
-
#prepare(input) ⇒ Object
Prepares a jwt object based on an input.
-
#sign(input) ⇒ Object
Signs the given input with the configured Ed25519 key.
- #signed(input) ⇒ Object
- #verify(payload, key) ⇒ Object
Constructor Details
permalink #initialize(app_id, app_key) ⇒ JwtService
Jwt initializer
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/jwt_service.rb', line 14 def initialize(app_id, app_key) @id = app_id parts = app_key.split(':') if parts.length > 1 @key_id = parts[0] @key = parts[1] else @key_id = "1" @key = app_key end end |
Instance Attribute Details
permalink #id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/jwt_service.rb', line 8 def id @id end |
permalink #key ⇒ Object (readonly)
Returns the value of attribute key.
8 9 10 |
# File 'lib/jwt_service.rb', line 8 def key @key end |
permalink #key_id ⇒ Object (readonly)
Returns the value of attribute key_id.
8 9 10 |
# File 'lib/jwt_service.rb', line 8 def key_id @key_id end |
Instance Method Details
permalink #auth_token ⇒ Object
Generates the auth_token based on the app’s private key.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/jwt_service.rb', line 81 def auth_token payload = header + "." + encode({ jti: SecureRandom.uuid, cid: SecureRandom.uuid, typ: 'auth.token', iat: (SelfSDK::Time.now - 5).to_i, exp: (SelfSDK::Time.now + 60).to_i, iss: @id, sub: @id}.to_json) signature = sign(payload) "#{payload}.#{signature}" end |
permalink #decode(input) ⇒ Object
Base64 decodes the input string
56 57 58 |
# File 'lib/jwt_service.rb', line 56 def decode(input) Base64.urlsafe_decode64(input) end |
permalink #encode(input) ⇒ Object
Encodes the input with base64
49 50 51 |
# File 'lib/jwt_service.rb', line 49 def encode(input) Base64.urlsafe_encode64(input, padding: false) end |
permalink #parse(input) ⇒ Object
[View source]
42 43 44 |
# File 'lib/jwt_service.rb', line 42 def parse(input) JSON.parse(input, symbolize_names: true) end |
permalink #prepare(input) ⇒ Object
Prepares a jwt object based on an input
29 30 31 |
# File 'lib/jwt_service.rb', line 29 def prepare(input) signed(input).to_json end |
permalink #sign(input) ⇒ Object
Signs the given input with the configured Ed25519 key.
63 64 65 66 67 |
# File 'lib/jwt_service.rb', line 63 def sign(input) signing_key = Ed25519::SigningKey.new(decode(@key)) signature = signing_key.sign(input) encode(signature) end |
permalink #signed(input) ⇒ Object
[View source]
33 34 35 36 37 38 39 40 |
# File 'lib/jwt_service.rb', line 33 def signed(input) payload = encode(input.to_json) { payload: payload, protected: header, signature: sign("#{header}.#{payload}") } end |
permalink #verify(payload, key) ⇒ Object
[View source]
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/jwt_service.rb', line 69 def verify(payload, key) verify_key = Ed25519::VerifyKey.new(decode(key)) if verify_key.verify(decode(payload[:signature]), "#{payload[:protected]}.#{payload[:payload]}") return true end false rescue StandardError => e SelfSDK.logger.info e false end |