class Warden::JWTAuth::Hooks

Warden hooks

Constants

PREPARED_TOKEN_ENV_KEY

`env` key where JWT is added

Public Class Methods

after_set_user(user, auth, opts) click to toggle source

Adds a token for the signed in user to the request `env` if current path and verb match with configuration. This will be picked up later on by a rack middleware which will add it to the response headers.

@see github.com/hassox/warden/wiki/Callbacks

# File lib/warden/jwt_auth/hooks.rb, line 17
def self.after_set_user(user, auth, opts)
  new.send(:prepare_token, user, auth, opts)
end

Private Instance Methods

add_token_to_env(user, scope, env) click to toggle source

:reek: ManualDispatch :reek: UtilityFunction

# File lib/warden/jwt_auth/hooks.rb, line 39
def add_token_to_env(user, scope, env)
  aud = EnvHelper.aud_header(env)
  token, payload = UserEncoder.new.call(user, scope, aud)
  user.on_jwt_dispatch(token, payload) if user.respond_to?(:on_jwt_dispatch)
  env[PREPARED_TOKEN_ENV_KEY] = token
end
jwt_scope?(scope) click to toggle source
# File lib/warden/jwt_auth/hooks.rb, line 46
def jwt_scope?(scope)
  jwt_scopes = mappings.keys
  jwt_scopes.include?(scope)
end
prepare_token(user, auth, opts) click to toggle source
# File lib/warden/jwt_auth/hooks.rb, line 23
def prepare_token(user, auth, opts)
  env = auth.env
  scope = opts[:scope]
  return unless token_should_be_added?(scope, env)

  add_token_to_env(user, scope, env)
end
request_matches?(path_info, method) click to toggle source

:reek: ControlParameter

# File lib/warden/jwt_auth/hooks.rb, line 52
def request_matches?(path_info, method)
  dispatch_requests.each do |tuple|
    dispatch_method, dispatch_path = tuple
    return true if path_info.match(dispatch_path) &&
                   method == dispatch_method
  end
  false
end
token_should_be_added?(scope, env) click to toggle source
# File lib/warden/jwt_auth/hooks.rb, line 31
def token_should_be_added?(scope, env)
  path_info = EnvHelper.path_info(env)
  method = EnvHelper.request_method(env)
  jwt_scope?(scope) && request_matches?(path_info, method)
end