Module: Alula::JwtHelper
- Defined in:
- lib/alula/helpers/jwt_helper.rb
Overview
Helper class to build JWT tokens Used for internal video API calls or video API calls with a customer ID
Constant Summary collapse
- ONE_HOUR =
60 * 60
Class Method Summary collapse
- .add_timestamp_to_payload(payload) ⇒ Object
- .build_jwt_header ⇒ Object
- .build_jwt_token(jwt_payload) ⇒ Object
- .convert_to_base64(source) ⇒ Object
- .sign_token(token, secret) ⇒ Object
Class Method Details
.add_timestamp_to_payload(payload) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/alula/helpers/jwt_helper.rb', line 34 def (payload) = Time.now.to_i payload.merge( { 'iat' => , 'exp' => + ONE_HOUR # expiry time is 60 minutes from time of creation } ) end |
.build_jwt_header ⇒ Object
27 28 29 30 31 32 |
# File 'lib/alula/helpers/jwt_helper.rb', line 27 def build_jwt_header { 'typ' => 'JWT', 'alg' => 'HS256' } end |
.build_jwt_token(jwt_payload) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/alula/helpers/jwt_helper.rb', line 11 def build_jwt_token(jwt_payload) jwt_secret = Alula::Client.config.video_api_key header = build_jwt_header token_data = (jwt_payload) encoded_header = convert_to_base64(JSON.generate(header)) encoded_data = convert_to_base64(JSON.generate(token_data)) token = "#{encoded_header}.#{encoded_data}" signature = sign_token(token, jwt_secret) "#{token}.#{signature}" end |
.convert_to_base64(source) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/alula/helpers/jwt_helper.rb', line 49 def convert_to_base64(source) # Encode in classical base64, encoded_source = Base64.strict_encode64(source) # Remove padding equal characters, encoded_source = encoded_source.gsub('=', '') # Replace characters according to base64url specifications, encoded_source.tr('+/', '-_') end |
.sign_token(token, secret) ⇒ Object
44 45 46 47 |
# File 'lib/alula/helpers/jwt_helper.rb', line 44 def sign_token(token, secret) signature = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), secret, token) convert_to_base64(signature) end |