class Virgil::Jwt::JwtGenerator

Attributes

access_token_signer[R]

An instance of [AccessTokenSigner] that is used to generate token signature using api_key @return [AccessTokenSigner]

api_key[R]

Private Key which will be used for signing generated access tokens. Take it on {dashboard.virgilsecurity.com/api-keys} @return [PrivateKey]

api_public_key_id[R]

Key id of api_key Take it on {dashboard.virgilsecurity.com/api-keys} @return [String]

app_id[R]

Application id Take it on {dashboard.virgilsecurity.com} @return [String]

life_time[R]

Lifetime of generated tokens in minutes @return [Integer]

Public Class Methods

new(app_id:, api_key:, api_public_key_id:, life_time:, access_token_signer:) click to toggle source

Initializes a new instance of the class @param app_id [String] Application id

Take it on {https://dashboard.virgilsecurity.com}

@param api_key [PrivateKey] Private Key which will be used for signing

generated access tokens. Take it on {https://dashboard.virgilsecurity.com/api-keys}

@param api_public_key_id [String] Key id of api_key.

Take it on {https://dashboard.virgilsecurity.com/api-keys}

@param life_time [Integer] Lifetime of generated tokens in minutes @param access_token_signer [AccessTokenSigner] An instance of [AccessTokenSigner]

that is used to generate token signature using #api_key
# File lib/virgil/jwt/jwt_generator.rb, line 74
def initialize(app_id:, api_key:, api_public_key_id:, life_time:, access_token_signer:)
  @app_id = app_id
  @api_key = api_key
  @api_public_key_id = api_public_key_id
  @life_time = life_time
  @access_token_signer = access_token_signer
end

Public Instance Methods

generate_token(identity, data = nil) click to toggle source

Generates new JWT using specified identity and additional data. @param identity [String] identity to generate with. @param data [Hash] dictionary with additional data which will be kept in jwt body @return new instance of [Jwt]

# File lib/virgil/jwt/jwt_generator.rb, line 86
def generate_token(identity, data = nil)
  raise ArgumentError, 'Identity property is mandatory' if identity.nil?
  issued_at = Time.now.utc
  expires_at = Time.at(issued_at.to_i + @life_time * 60).utc
  jwt_body = JwtBodyContent.new(app_id: @app_id,
                                issued_at: issued_at,
                                identity: identity,
                                expires_at: expires_at,
                                data: data)

  jwt_header = JwtHeaderContent.new(algorithm: @access_token_signer.algorithm,
                                    key_id: @api_public_key_id)
  unsigned_jwt = Jwt.new(header_content: jwt_header,
                         body_content: jwt_body,
                         signature_data: nil)
  jwt_bytes = Bytes.from_string(unsigned_jwt.to_s)
  signature = @access_token_signer.generate_token_signature(jwt_bytes, @api_key)
  Jwt.new(header_content: jwt_header,
          body_content: jwt_body,
          signature_data: signature)
end