Sha256: 561a6e70afed4f1a90a0183ed9771e8ed65a5b15b9775a625c7dedcc8c656072

Contents?: true

Size: 1.51 KB

Versions: 6

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Warden
  module Auth0
    # Helpers to parse token from a request and to a response
    module HeaderParser
      # Method for `Authorization` header. Token is present in request/response
      # headers as `Bearer %token%`
      METHOD = 'Bearer'

      # Parses the token from a rack request
      #
      # @param env [Hash] rack env hash
      # @return [String] JWT token
      # @return [nil] if token is not present
      def self.from_env(env)
        auth = EnvHelper.authorization_header(env)
        return nil unless auth

        method, token = auth.split
        method == METHOD ? token : nil
      end

      # Returns a copy of `env` with token added to the header configured through
      # `token_header` option. Be aware than `env` is not modified in place.
      #
      # @param env [Hash] rack env hash
      # @param token [String] JWT token
      # @return [Hash] modified rack env
      def self.to_env(env, token)
        EnvHelper.set_authorization_header(env, "#{METHOD} #{token}")
      end

      # Returns a copy of headers with token added in the `Authorization` key.
      # Be aware that headers is not modified in place
      #
      # @param headers [Hash] rack hash response headers
      # @param token [String] JWT token
      # @return [Hash] response headers with the token added
      def self.to_headers(headers, token)
        headers = headers.dup
        headers[Auth0.config.token_header] = "#{METHOD} #{token}"
        headers
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
warden-auth0-0.4.2 lib/warden/auth0/header_parser.rb
warden-auth0-0.4.1 lib/warden/auth0/header_parser.rb
warden-auth0-0.4.0 lib/warden/auth0/header_parser.rb
warden-auth0-0.3.0 lib/warden/auth0/header_parser.rb
warden-auth0-0.2.0 lib/warden/auth0/header_parser.rb
warden-auth0-0.1.0 lib/warden/auth0/header_parser.rb