Sha256: 47ccd56f96c32700b64c46689f42e82134ce2a5cbe50ab3b9f5dfe15e543cd9c

Contents?: true

Size: 801 Bytes

Versions: 3

Compression:

Stored size: 801 Bytes

Contents

# frozen_string_literal: true

require 'securerandom'

module Warden
  module JWTAuth
    # Encodes a payload into a JWT token, adding some configurable
    # claims
    class TokenEncoder
      include JWTAuth::Import['secret', 'algorithm', 'expiration_time']

      # Encodes a payload into a JWT
      #
      # @param payload [Hash] what has to be encoded
      # @return [String] JWT
      def call(payload)
        payload_to_encode = merge_with_default_claims(payload)
        JWT.encode(payload_to_encode, secret, algorithm)
      end

      private

      def merge_with_default_claims(payload)
        now = Time.now.to_i
        payload['iat'] ||= now
        payload['exp'] ||= now + expiration_time
        payload['jti'] ||= SecureRandom.uuid
        payload
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
warden-jwt_auth-0.8.0 lib/warden/jwt_auth/token_encoder.rb
warden-jwt_auth-0.7.0 lib/warden/jwt_auth/token_encoder.rb
warden-jwt_auth-0.6.0 lib/warden/jwt_auth/token_encoder.rb