Class: CirroIO::Client::JwtAuthentication

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/cirro_io/client/jwt_authentication.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cirro_io/client/jwt_authentication.rb', line 7

def call(env)
  private_pem = File.read(CirroIO::Client.configuration.private_key_path)
  private_key = OpenSSL::PKey::RSA.new(private_pem)

  payload = {
    # JWT expiration time (10 minute maximum)
    exp: Time.now.to_i + (10 * 60),
    # App client id
    iss: CirroIO::Client.configuration.app_id,
  }

  token = JWT.encode(payload, private_key, 'RS256')

  env[:request_headers]['Authorization'] = "Bearer #{token}"
  @app.call(env)
end