Sha256: 7aa92566eeeb8afbe71bd46026b8d2752f67fe1914fe040385cd080bebc6c03d

Contents?: true

Size: 691 Bytes

Versions: 3

Compression:

Stored size: 691 Bytes

Contents

module MaticJWT
  class Authenticator
    def initialize(header, scheme: SCHEME)
      @scheme = scheme
      @token = extract_token(header)
    end

    def client_name
      payload.first['client_name']
    end

    def authenticate_with_secret!(secret)
      JWT.decode(@token, secret, true, algorithm: ALGORITHM)
    end

    def payload
      JWT.decode(@token, nil, false)
    end

    private

    def extract_token(header)
      token = header&.slice(@scheme.length + 1..-1)
      validate_header_presence!(token)
      token
    end

    def validate_header_presence!(token)
      raise(JWT::DecodeError, 'Authorization token is incorrect') unless token&.present?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
matic-jwt-1.2.1 lib/matic-jwt/authenticator.rb
matic-jwt-1.2.0 lib/matic-jwt/authenticator.rb
matic-jwt-1.1.0 lib/matic-jwt/authenticator.rb