Class: Virgil::Jwt::JwtGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/virgil/jwt/jwt_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id:, api_key:, api_public_key_id:, life_time:, access_token_signer:) ⇒ JwtGenerator

Initializes a new instance of the class

Parameters:



74
75
76
77
78
79
80
# 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

Instance Attribute Details

#access_token_signerAccessTokenSigner (readonly)

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

Returns:

  • (AccessTokenSigner)


62
63
64
# File 'lib/virgil/jwt/jwt_generator.rb', line 62

def access_token_signer
  @access_token_signer
end

#api_keyPrivateKey (readonly)

Private Key which will be used for signing generated access tokens. Take it on https://dashboard.virgilsecurity.com/api-keys

Returns:

  • (PrivateKey)


43
44
45
# File 'lib/virgil/jwt/jwt_generator.rb', line 43

def api_key
  @api_key
end

#api_public_key_idString (readonly)

Key id of #api_key Take it on https://dashboard.virgilsecurity.com/api-keys

Returns:

  • (String)


48
49
50
# File 'lib/virgil/jwt/jwt_generator.rb', line 48

def api_public_key_id
  @api_public_key_id
end

#app_idString (readonly)

Application id Take it on https://dashboard.virgilsecurity.com

Returns:

  • (String)


53
54
55
# File 'lib/virgil/jwt/jwt_generator.rb', line 53

def app_id
  @app_id
end

#life_timeInteger (readonly)

Lifetime of generated tokens in minutes

Returns:

  • (Integer)


57
58
59
# File 'lib/virgil/jwt/jwt_generator.rb', line 57

def life_time
  @life_time
end

Instance Method Details

#generate_token(identity, data = nil) ⇒ Object

Generates new JWT using specified identity and additional data.

Parameters:

  • identity (String)

    identity to generate with.

  • data (Hash) (defaults to: nil)

    dictionary with additional data which will be kept in jwt body

Returns:

  • new instance of [Jwt]

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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