Class: SelfSDK::JwtService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id, app_key) ⇒ JwtService

Jwt initializer

Parameters:

  • app_id (string)

    the app id.

  • app_key (string)

    the app api key provided by developer portal.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jwt_service.rb', line 16

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

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/jwt_service.rb', line 10

def id
  @id
end

#keyObject (readonly)

Returns the value of attribute key.



10
11
12
# File 'lib/jwt_service.rb', line 10

def key
  @key
end

#key_idObject (readonly)

Returns the value of attribute key_id.



10
11
12
# File 'lib/jwt_service.rb', line 10

def key_id
  @key_id
end

Instance Method Details

#auth_tokenObject

Generates the auth_token based on the app’s private key.



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jwt_service.rb', line 83

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


96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jwt_service.rb', line 96

def build_dynamic_link(body, env, callback)
  base_url = "https://#{env}.links.joinself.com"
  portal_url = "https://developer.#{env}.joinself.com"
  apn = "com.joinself.app.#{env}"

  if env.empty? || env == 'development'
    base_url = "https://links.joinself.com"
    portal_url = "https://developer.joinself.com"
    apn = "com.joinself.app"
  end
  apn = "com.joinself.app.dev" if env == 'development'

  "#{base_url}?link=#{portal_url}/callback/#{callback}%3Fqr=#{body}&apn=#{apn}"
end

#decode(input) ⇒ Object

Base64 decodes the input string

Parameters:

  • input (string)

    the string to be decoded.



58
59
60
# File 'lib/jwt_service.rb', line 58

def decode(input)
  Base64.urlsafe_decode64(input)
end

#encode(input) ⇒ Object

Encodes the input with base64

Parameters:

  • input (string)

    the string to be encoded.



51
52
53
# File 'lib/jwt_service.rb', line 51

def encode(input)
  Base64.urlsafe_encode64(input, padding: false)
end

#parse(input) ⇒ Object



44
45
46
# File 'lib/jwt_service.rb', line 44

def parse(input)
  JSON.parse(input, symbolize_names: true)
end

#prepare(input) ⇒ Object

Prepares a jwt object based on an input

Parameters:

  • input (string)

    input to be prepared



31
32
33
# File 'lib/jwt_service.rb', line 31

def prepare(input)
  signed(input).to_json
end

#sign(input) ⇒ Object

Signs the given input with the configured Ed25519 key.

Parameters:

  • input (string)

    the string to be signed.



65
66
67
68
69
# File 'lib/jwt_service.rb', line 65

def sign(input)
  signing_key = Ed25519::SigningKey.new(decode(@key))
  signature = signing_key.sign(input)
  encode(signature)
end

#signed(input) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/jwt_service.rb', line 35

def signed(input)
  payload = encode(input.to_json)
  {
    payload: payload,
    protected: header,
    signature: sign("#{header}.#{payload}")
  }
end

#verify(payload, key) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/jwt_service.rb', line 71

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