class JwtGuard implements Guard

Traits

These methods are typically the same across all guards.

Constants

DEFAULT_TTL

private NO_EXPIRY

Properties

protected null|TProvider $provider

The user provider implementation.

from  GuardHelpers
static protected array $macros

The registered string macros.

from  Macroable
protected Dispatcher|null $events

The event dispatcher instance.

Methods

authenticate()

Determine if the current user is authenticated. If not, throw an exception.

bool
check()

Determine if the current user is authenticated.

bool
guest()

Determine if the current user is a guest.

int|string|null
id()

Get the ID for the currently authenticated user.

getProvider()

Get the user provider used by the guard.

void
setProvider(UserProvider $provider)

Set the user provider used by the guard.

string
contextKey(string $key)

Get this guard's coroutine context key for the given entry.

static void
macro(string $name, callable|object $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Check if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

__construct(string $name, UserProvider $provider, ManagerContract $jwtManager, ClaimFactory $claimFactory, Parser $parser, Container $app, bool $rehashOnLogin, int $timeboxDuration, int|null $ttl = self::DEFAULT_TTL, Timebox $timebox = new Timebox())

Create a new JWT authentication guard.

string|bool
attempt(array $credentials = [], bool $login = true)

Attempt to authenticate a user using the given credentials.

string|null
parseToken()

Parse the JWT token from the current request.

string
login(Authenticatable $user)

Log a user into the application and return the JWT token.

user()

Get the currently authenticated user.

bool
validate(array $credentials = [])

Validate a user's credentials.

bool
once(array $credentials = [])

Log a user into the application using their credentials without persisting.

onceUsingId(mixed $id)

Log the given user ID into the application.

string
fromUser(Authenticatable $user)

Create a new token for the given user without authenticating them.

string|null
tokenById(mixed $id)

Create a new token by user ID.

byId(mixed $id)

Alias for onceUsingId.

userOrFail()

Get the currently authenticated user or throw an exception.

int|string|null
getUserId()

Get the ID for the current user from the token without loading the user.

claims(array $claims)

Add custom claims to the next JWT token.

array
getPayload()

Get the payload from the current JWT token.

array
payload()

Alias for getPayload.

setToken(string $token)

Set the token.

string|null
getToken()

Get the current token.

int|null
getTTL()

Get the token TTL.

setTTL(int|null $ttl)

Set the token TTL for the next token-producing operation.

string|null
refresh(bool $forceForever = false, bool $resetClaims = false)

Refresh the current JWT token.

void
logout(bool $forceForever = false)

Log the user out, invalidating the current token when the blacklist is enabled.

invalidate(bool $forceForever = false)

Invalidate the current token.

getUser()

Return the currently cached user.

bool
hasUser()

Determine if the guard has a user instance.

setUser(Authenticatable $user)

Set the current user.

forgetUser()

Forget the current user.

array
getAuthContextKeys()

Get durable authentication Context keys.

void
attempting(callable $callback)

Register an authentication attempt event listener.

Dispatcher|null
getDispatcher()

Get the event dispatcher instance.

void
setDispatcher(Dispatcher $events)

Set the event dispatcher instance.

getLastAttempted()

Get the last user we attempted to authenticate.

void
rehashPasswordIfRequired(Authenticatable $user, array $credentials)

Rehash the user's password if enabled and required.

array
decodeToken(string $token)

Decode a JWT token, caching the result for the current request.

void
cacheUser(Authenticatable $user)

Cache the current user without firing guard events.

string
getExplicitUserContextKey()

Get the Context key for an explicitly assigned user.

array
pullCustomClaims()

Pull custom claims for the next token.

string
requireToken()

Require a token to be available.

mixed
getContextState(string $key, mixed $default = null)

Get Context state.

void
setContextState(string $key, mixed $value)

Set Context state.

void
forgetContextState(string $key)

Forget Context state.

string
getUserContextKey(string|null $token = null)

Get the Context key for caching the authenticated user.

string
getPayloadContextKey(string $token)

Get the Context key for caching a decoded payload.

void
dispatchIfListening(string $eventClass, Closure $event)

Dispatch the given event if listeners are registered.

void
fireAttemptEvent(array $credentials)

Fire the attempt event.

void
fireValidatedEvent(Authenticatable $user)

Fire the validated event.

void
fireFailedEvent(Authenticatable|null $user, array $credentials)

Fire the failed authentication attempt event.

void
fireLoginEvent(Authenticatable $user)

Fire the login event.

void
fireAuthenticatedEvent(Authenticatable $user)

Fire the authenticated event.

void
fireLogoutEvent(Authenticatable|null $user)

Fire the logout event.

static void
flushState()

Flush all static state.

Details

in GuardHelpers at line 55
Authenticatable authenticate()

Determine if the current user is authenticated. If not, throw an exception.

Return Value

Authenticatable

Exceptions

AuthenticationException

in GuardHelpers at line 63
bool check()

Determine if the current user is authenticated.

Return Value

bool

in GuardHelpers at line 71
bool guest()

Determine if the current user is a guest.

Return Value

bool

in GuardHelpers at line 79
int|string|null id()

Get the ID for the currently authenticated user.

Return Value

int|string|null

in GuardHelpers at line 89
UserProvider|null getProvider()

Get the user provider used by the guard.

Return Value

UserProvider|null

in GuardHelpers at line 102
void setProvider(UserProvider $provider)

Set the user provider used by the guard.

The provider is stored on the worker-lifetime guard and affects every subsequent request.

Parameters

UserProvider $provider

Return Value

void

in GuardHelpers at line 111
protected string contextKey(string $key)

Get this guard's coroutine context key for the given entry.

Parameters

string $key

Return Value

string

in Macroable at line 30
static void macro(string $name, callable|object $macro)

Register a custom macro.

Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.

Parameters

string $name
callable|object $macro

Return Value

void

in Macroable at line 44
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 61
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 72
static void flushMacros()

Flush the existing macros.

Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.

Return Value

void

in Macroable at line 83
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in Macroable at line 108
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

at line 61
__construct(string $name, UserProvider $provider, ManagerContract $jwtManager, ClaimFactory $claimFactory, Parser $parser, Container $app, bool $rehashOnLogin, int $timeboxDuration, int|null $ttl = self::DEFAULT_TTL, Timebox $timebox = new Timebox())

Create a new JWT authentication guard.

Parameters

string $name
UserProvider $provider
ManagerContract $jwtManager
ClaimFactory $claimFactory
Parser $parser
Container $app
bool $rehashOnLogin

indicates if passwords should be rehashed on login if needed

int $timeboxDuration

the number of microseconds that the timebox should wait for

int|null $ttl

token time-to-live in minutes, or null for no expiration

Timebox $timebox

at line 79
string|bool attempt(array $credentials = [], bool $login = true)

Attempt to authenticate a user using the given credentials.

Parameters

array $credentials
bool $login

Return Value

string|bool

at line 112
string|null parseToken()

Parse the JWT token from the current request.

Return Value

string|null

at line 124
string login(Authenticatable $user)

Log a user into the application and return the JWT token.

Parameters

Authenticatable $user

Return Value

string

at line 140
Authenticatable|null user()

Get the currently authenticated user.

Return Value

Authenticatable|null

at line 198
bool validate(array $credentials = [])

Validate a user's credentials.

Parameters

array $credentials

Return Value

bool

at line 206
bool once(array $credentials = [])

Log a user into the application using their credentials without persisting.

Parameters

array $credentials

Return Value

bool

at line 222
Authenticatable|false onceUsingId(mixed $id)

Log the given user ID into the application.

Parameters

mixed $id

Return Value

Authenticatable|false

at line 236
string fromUser(Authenticatable $user)

Create a new token for the given user without authenticating them.

Parameters

Authenticatable $user

Return Value

string

at line 255
string|null tokenById(mixed $id)

Create a new token by user ID.

Parameters

mixed $id

Return Value

string|null

at line 267
Authenticatable|false byId(mixed $id)

Alias for onceUsingId.

Parameters

mixed $id

Return Value

Authenticatable|false

at line 277
Authenticatable userOrFail()

Get the currently authenticated user or throw an exception.

Return Value

Authenticatable

Exceptions

UserNotDefinedException

at line 289
int|string|null getUserId()

Get the ID for the current user from the token without loading the user.

Return Value

int|string|null

at line 311
JwtGuard claims(array $claims)

Add custom claims to the next JWT token.

Parameters

array $claims

Return Value

JwtGuard

at line 324
array getPayload()

Get the payload from the current JWT token.

Return Value

array

at line 338
array payload()

Alias for getPayload.

Return Value

array

at line 346
JwtGuard setToken(string $token)

Set the token.

Parameters

string $token

Return Value

JwtGuard

at line 356
string|null getToken()

Get the current token.

Return Value

string|null

at line 371
int|null getTTL()

Get the token TTL.

Return Value

int|null

at line 385
JwtGuard setTTL(int|null $ttl)

Set the token TTL for the next token-producing operation.

Parameters

int|null $ttl

Return Value

JwtGuard

at line 395
string|null refresh(bool $forceForever = false, bool $resetClaims = false)

Refresh the current JWT token.

Parameters

bool $forceForever
bool $resetClaims

Return Value

string|null

at line 425
void logout(bool $forceForever = false)

Log the user out, invalidating the current token when the blacklist is enabled.

Parameters

bool $forceForever

Return Value

void

at line 448
JwtGuard invalidate(bool $forceForever = false)

Invalidate the current token.

Parameters

bool $forceForever

Return Value

JwtGuard

at line 462
Authenticatable|null getUser()

Return the currently cached user.

Return Value

Authenticatable|null

at line 481
bool hasUser()

Determine if the guard has a user instance.

Return Value

bool

at line 489
Guard setUser(Authenticatable $user)

Set the current user.

Parameters

Authenticatable $user

Return Value

Guard

at line 500
JwtGuard forgetUser()

Forget the current user.

Return Value

JwtGuard

at line 516
array getAuthContextKeys()

Get durable authentication Context keys.

Per-request token resolution caches must not cross a request boundary.

Return Value

array

at line 527
void attempting(callable $callback)

Register an authentication attempt event listener.

When the dispatcher is the application's worker-shared dispatcher, listener registrations persist and affect every subsequent request.

Parameters

callable $callback

Return Value

void

at line 535
Dispatcher|null getDispatcher()

Get the event dispatcher instance.

Return Value

Dispatcher|null

at line 546
void setDispatcher(Dispatcher $events)

Set the event dispatcher instance.

The dispatcher is stored on the worker-lifetime guard and affects every subsequent request.

Parameters

Dispatcher $events

Return Value

void

at line 555
Authenticatable|null getLastAttempted()

Get the last user we attempted to authenticate.

Return Value

Authenticatable|null

at line 563
protected void rehashPasswordIfRequired(Authenticatable $user, array $credentials)

Rehash the user's password if enabled and required.

Parameters

Authenticatable $user
array $credentials

Return Value

void

at line 573
protected array decodeToken(string $token)

Decode a JWT token, caching the result for the current request.

Parameters

string $token

Return Value

array

at line 584
protected void cacheUser(Authenticatable $user)

Cache the current user without firing guard events.

Parameters

Authenticatable $user

Return Value

void

at line 592
protected string getExplicitUserContextKey()

Get the Context key for an explicitly assigned user.

Return Value

string

at line 600
protected array pullCustomClaims()

Pull custom claims for the next token.

Return Value

array

at line 614
protected string requireToken()

Require a token to be available.

Return Value

string

Exceptions

JwtException

at line 626
protected mixed getContextState(string $key, mixed $default = null)

Get Context state.

Parameters

string $key
mixed $default

Return Value

mixed

at line 634
protected void setContextState(string $key, mixed $value)

Set Context state.

Parameters

string $key
mixed $value

Return Value

void

at line 642
protected void forgetContextState(string $key)

Forget Context state.

Parameters

string $key

Return Value

void

at line 650
protected string getUserContextKey(string|null $token = null)

Get the Context key for caching the authenticated user.

Parameters

string|null $token

Return Value

string

at line 664
protected string getPayloadContextKey(string $token)

Get the Context key for caching a decoded payload.

Parameters

string $token

Return Value

string

at line 672
protected void dispatchIfListening(string $eventClass, Closure $event)

Dispatch the given event if listeners are registered.

Parameters

string $eventClass
Closure $event

Return Value

void

at line 682
protected void fireAttemptEvent(array $credentials)

Fire the attempt event.

Parameters

array $credentials

Return Value

void

at line 690
protected void fireValidatedEvent(Authenticatable $user)

Fire the validated event.

Parameters

Authenticatable $user

Return Value

void

at line 698
protected void fireFailedEvent(Authenticatable|null $user, array $credentials)

Fire the failed authentication attempt event.

Parameters

Authenticatable|null $user
array $credentials

Return Value

void

at line 706
protected void fireLoginEvent(Authenticatable $user)

Fire the login event.

Parameters

Authenticatable $user

Return Value

void

at line 714
protected void fireAuthenticatedEvent(Authenticatable $user)

Fire the authenticated event.

Parameters

Authenticatable $user

Return Value

void

at line 722
protected void fireLogoutEvent(Authenticatable|null $user)

Fire the logout event.

Parameters

Authenticatable|null $user

Return Value

void

at line 730
static void flushState()

Flush all static state.

Return Value

void