Class: Virgil::Jwt::JwtGenerator
- Inherits:
-
Object
- Object
- Virgil::Jwt::JwtGenerator
- Defined in:
- lib/virgil/jwt/jwt_generator.rb
Instance Attribute Summary collapse
-
#access_token_signer ⇒ AccessTokenSigner
readonly
An instance of [AccessTokenSigner] that is used to generate token signature using #api_key.
-
#api_key ⇒ PrivateKey
readonly
Private Key which will be used for signing generated access tokens.
-
#api_public_key_id ⇒ String
readonly
Key id of #api_key Take it on https://dashboard.virgilsecurity.com/api-keys.
-
#app_id ⇒ String
readonly
Application id Take it on https://dashboard.virgilsecurity.com.
-
#life_time ⇒ Integer
readonly
Lifetime of generated tokens in minutes.
Instance Method Summary collapse
-
#generate_token(identity, data = nil) ⇒ Object
Generates new JWT using specified identity and additional data.
-
#initialize(app_id:, api_key:, api_public_key_id:, life_time:, access_token_signer:) ⇒ JwtGenerator
constructor
Initializes a new instance of the class.
Constructor Details
#initialize(app_id:, api_key:, api_public_key_id:, life_time:, access_token_signer:) ⇒ JwtGenerator
Initializes a new instance of the class
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_signer ⇒ AccessTokenSigner (readonly)
An instance of [AccessTokenSigner] that is used to generate token signature using #api_key
62 63 64 |
# File 'lib/virgil/jwt/jwt_generator.rb', line 62 def access_token_signer @access_token_signer end |
#api_key ⇒ PrivateKey (readonly)
Private Key which will be used for signing generated access tokens. Take it on https://dashboard.virgilsecurity.com/api-keys
43 44 45 |
# File 'lib/virgil/jwt/jwt_generator.rb', line 43 def api_key @api_key end |
#api_public_key_id ⇒ String (readonly)
Key id of #api_key Take it on https://dashboard.virgilsecurity.com/api-keys
48 49 50 |
# File 'lib/virgil/jwt/jwt_generator.rb', line 48 def api_public_key_id @api_public_key_id end |
#app_id ⇒ String (readonly)
Application id Take it on https://dashboard.virgilsecurity.com
53 54 55 |
# File 'lib/virgil/jwt/jwt_generator.rb', line 53 def app_id @app_id end |
#life_time ⇒ Integer (readonly)
Lifetime of generated tokens in minutes
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.
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 |