lib/warden/jwt_auth/hooks.rb in warden-jwt_auth-0.2.1 vs lib/warden/jwt_auth/hooks.rb in warden-jwt_auth-0.3.0
- old
+ new
@@ -2,11 +2,11 @@
module Warden
module JWTAuth
# Warden hooks
class Hooks
- include JWTAuth::Import['mappings', 'dispatch_requests']
+ include JWTAuth::Import['mappings', 'dispatch_requests', 'aud_header']
# `env` key where JWT is added
PREPARED_TOKEN_ENV_KEY = 'warden-jwt_auth.token'
# Adds a token for the signed in user to the request `env` if current path
@@ -22,17 +22,24 @@
def prepare_token(user, auth, opts)
env = auth.env
scope = opts[:scope]
return unless token_should_be_added?(scope, env)
- token = UserEncoder.new.call(user, scope)
- env[PREPARED_TOKEN_ENV_KEY] = token
+ add_token_to_env(user, scope, env)
end
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
+
+ # :reek:ManualDispatch
+ # :reek:FeatureEnvy
+ def add_token_to_env(user, scope, env)
+ token, payload = UserEncoder.new.call(user, scope, env[aud_header])
+ user.on_jwt_dispatch(token, payload) if user.respond_to?(:on_jwt_dispatch)
+ env[PREPARED_TOKEN_ENV_KEY] = token
end
def jwt_scope?(scope)
jwt_scopes = mappings.keys
jwt_scopes.include?(scope)