Module: DashApi::JsonWebToken

Defined in:
app/services/dash_api/json_web_token.rb

Constant Summary collapse

JWT_HASH_ALGORITHM =
'HS256'
GOOGLE_CERTS_URL =
'https://www.googleapis.com/robot/v1/metadata/x509/[email protected]'

Class Method Summary collapse

Class Method Details

.decode(jwt_token) ⇒ Object



16
17
18
19
20
21
# File 'app/services/dash_api/json_web_token.rb', line 16

def self.decode(jwt_token)
  jwt = JWT.decode(jwt_token, DashApi.jwt_secret, true, {
    algorithm: DashApi.jwt_algorithm || JWT_HASH_ALGORITHM
  })
  HashWithIndifferentAccess.new(jwt[0])
end

.decode_firebase(jwt_token) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'app/services/dash_api/json_web_token.rb', line 23

def self.decode_firebase(jwt_token)
  firebase_web_key = DashApi.firebase_web_key
  jwt = JWT.decode(jwt_token, firebase_web_key, true, { algorithm: 'RS256' })  do |header|
    url = URI(GOOGLE_CERTS_URL)
    json = JSON.parse(Net::HTTP.get(url))
    public_key = OpenSSL::X509::Certificate.new(json[header['kid']]).public_key
  end
  jwt = jwt[0].merge({ role: "user", provider: "firebase" })
  HashWithIndifferentAccess.new(jwt)
end

.decode_unverified(jwt_token) ⇒ Object



34
35
36
# File 'app/services/dash_api/json_web_token.rb', line 34

def self.decode_unverified(jwt_token)
  HashWithIndifferentAccess.new(JWT.decode(jwt_token, nil, false)[0])
end

.encode(payload:, expiration:) ⇒ Object



11
12
13
14
# File 'app/services/dash_api/json_web_token.rb', line 11

def self.encode(payload:, expiration:)
  payload[:exp] = expiration || 15.minutes.from_now.to_i
  JWT.encode(payload, DashApi.jwt_secret, DashApi.jwt_hash_algorithm || JWT_HASH_ALGORITHM)
end