Sha256: 3bfc15b9d5eedd07a2319613ec7e185ade27910908d83b37e0f31733e03e4904

Contents?: true

Size: 1.21 KB

Versions: 60

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module Twilio
  module JWT
    class BaseJWT
      # valid_until overrides ttl if specified
      def initialize(secret_key: nil, issuer: nil, subject: nil, nbf: nil, ttl: 3600, valid_until: nil)
        if secret_key.nil?
          raise ArgumentError, 'JWT does not have a signing key'
        end

        @secret_key = secret_key
        @issuer = issuer
        @subject = subject
        @algorithm = 'HS256'
        @nbf = nbf
        @ttl = ttl
        @valid_until = valid_until
      end

      def _generate_headers
        {}
      end

      def _generate_payload
        raise NotImplementedError
      end

      def headers
        headers = _generate_headers.clone
        headers['typ'] = 'JWT'
        headers['alg'] = @algorithm
        headers
      end

      def payload
        payload = _generate_payload.clone

        payload[:iss] = @issuer
        payload[:nbf] = @nbf || Time.now.to_i
        payload[:exp] = @valid_until.nil? ? Time.now.to_i + @ttl : @valid_until
        payload[:sub] = @subject unless @subject.nil?

        payload
      end

      def to_jwt
        ::JWT.encode payload, @secret_key, @algorithm, headers
      end
      alias to_s to_jwt
    end
  end
end

Version data entries

60 entries across 60 versions & 1 rubygems

Version Path
twilio-ruby-5.33.0 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.32.0 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.31.6 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.31.5 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.31.4 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.31.3 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.31.2 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.31.1 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.31.0 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.30.0 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.29.1 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.29.0 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.28.0 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.27.1 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.27.0 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.26.0 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.25.4 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.25.3 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.25.2 lib/twilio-ruby/jwt/jwt.rb
twilio-ruby-5.25.1 lib/twilio-ruby/jwt/jwt.rb