Module: Warden::JWTAuth::HeaderParser

Defined in:
lib/warden/jwt_auth/header_parser.rb

Overview

Helpers to parse token from a request and to a response

Constant Summary collapse

METHOD =

Method for ‘Authorization` header. Token is present in request/response headers as `Bearer %token%`

'Bearer'

Class Method Summary collapse

Class Method Details

.from_env(env) ⇒ String?

Parses the token from a rack request

Parameters:

  • env (Hash)

    rack env hash

Returns:

  • (String)

    JWT token

  • (nil)

    if token is not present



16
17
18
19
20
21
22
# File 'lib/warden/jwt_auth/header_parser.rb', line 16

def self.from_env(env)
  auth = EnvHelper.authorization_header(env)
  return nil unless auth

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

.to_env(env, token) ⇒ Hash

Returns a copy of ‘env` with token added to the `HTTP_AUTHORIZATION` header. Be aware than `env` is not modified in place.

Parameters:

  • env (Hash)

    rack env hash

  • token (String)

    JWT token

Returns:

  • (Hash)

    modified rack env



30
31
32
# File 'lib/warden/jwt_auth/header_parser.rb', line 30

def self.to_env(env, token)
  EnvHelper.set_authorization_header(env, "#{METHOD} #{token}")
end

.to_headers(headers, token) ⇒ Hash

Returns a copy of headers with token added in the ‘Authorization` key. Be aware that headers is not modified in place

Parameters:

  • headers (Hash)

    rack hash response headers

  • token (String)

    JWT token

Returns:

  • (Hash)

    response headers with the token added



40
41
42
43
44
# File 'lib/warden/jwt_auth/header_parser.rb', line 40

def self.to_headers(headers, token)
  headers = headers.dup
  headers['Authorization'] = "#{METHOD} #{token}"
  headers
end