Class: Usps::JwtAuth::Encode

Inherits:
Object
  • Object
show all
Defined in:
lib/usps/jwt_auth/encode.rb

Overview

Encode data as a JWT

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expiration_time: 15 * 60, audience: [], issuer: nil) ⇒ Encode

Returns a new instance of Encode.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/usps/jwt_auth/encode.rb', line 12

def initialize(expiration_time: 15 * 60, audience: [], issuer: nil)
  private_key
  @expiration_time = expiration_time
  @audience = audience
  @issuer = issuer
rescue StandardError
  @private_key = generate_private_key
  store_private_key
  retry
ensure
  store_public_key
end

Class Method Details

.encode(data, expiration_time: 15 * 60, audience: [], issuer: nil) ⇒ Object



8
9
10
# File 'lib/usps/jwt_auth/encode.rb', line 8

def self.encode(data, expiration_time: 15 * 60, audience: [], issuer: nil)
  new(expiration_time: expiration_time, audience: audience, issuer: issuer).encode(data)
end

Instance Method Details

#encode(data) ⇒ Object



25
26
27
# File 'lib/usps/jwt_auth/encode.rb', line 25

def encode(data)
  JWT.encode(payload(data), private_key, JwtAuth.config.algorithm)
end

#fingerprintObject



33
34
35
# File 'lib/usps/jwt_auth/encode.rb', line 33

def fingerprint
  OpenSSL::Digest::SHA256.new(public_key.to_der).to_s
end

#public_keyObject



29
30
31
# File 'lib/usps/jwt_auth/encode.rb', line 29

def public_key
  @public_key ||= private_key.public_key
end