Class: Auth::ClientCredentials::JWTGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/lighthouse/auth/client_credentials/jwt_generator.rb

Constant Summary collapse

TTL =
300

Class Method Summary collapse

Class Method Details

.build_claims(client_id, aud) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/lighthouse/auth/client_credentials/jwt_generator.rb', line 12

def self.build_claims(client_id, aud)
  {
    iss: client_id,
    sub: client_id,
    aud:,
    iat:,
    exp: iat + TTL
  }
end

.build_rsa_instance(key_location) ⇒ Object



22
23
24
25
26
# File 'lib/lighthouse/auth/client_credentials/jwt_generator.rb', line 22

def self.build_rsa_instance(key_location)
  key = File.exist?(key_location) ? File.read(key_location) : key_location

  OpenSSL::PKey::RSA.new(key)
end

.generate_token(client_id, aud_claim_url, key_location) ⇒ Object



28
29
30
31
32
33
# File 'lib/lighthouse/auth/client_credentials/jwt_generator.rb', line 28

def self.generate_token(client_id, aud_claim_url, key_location)
  claims = build_claims(client_id, aud_claim_url)
  rsa_instance = build_rsa_instance(key_location)

  JWT.encode(claims, rsa_instance, 'RS256')
end

.iatObject



8
9
10
# File 'lib/lighthouse/auth/client_credentials/jwt_generator.rb', line 8

def self.iat
  Time.now.to_i
end