getIdentifier(); $userId = $accessTokenEntity->getUserIdentifier(); $clientId = $accessTokenEntity->getClient()->getIdentifier(); $workspaceId = null; if ($this->clientRequiresWorkspace($clientId)) { $workspaceId = $this->resolveWorkspaceId($userId); if ($workspaceId === null) { throw OAuthServerException::invalidGrant( 'Unable to bind this connection to a workspace. Reconnect from a workspace you belong to.', ); } } Passport::token()->forceFill([ 'id' => $id, 'user_id' => $userId, 'client_id' => $clientId, 'workspace_id' => $workspaceId, 'scopes' => $accessTokenEntity->getScopes(), 'revoked' => false, 'expires_at' => $accessTokenEntity->getExpiryDateTime(), ])->save(); $this->events->dispatch(new AccessTokenCreated($id, $userId, $clientId)); } private function clientRequiresWorkspace(string $clientId): bool { $client = Passport::client()->newQuery()->find($clientId); return $client !== null && ! $client->hasGrantType('personal_access'); } private function resolveWorkspaceId(?string $userId): ?string { $user = $userId ? User::query()->find($userId) : null; return match (request('grant_type')) { 'refresh_token' => $this->ownedWorkspace( $user, AccessToken::query() ->find($this->payloadId('refresh_token', 'access_token_id')) ?->workspace_id, ), 'authorization_code' => $this->ownedWorkspace( $user, AuthCode::query()->find($this->payloadId('code', 'auth_code_id'))?->workspace_id, ), default => null, }; } private function ownedWorkspace(?User $user, mixed $workspaceId): ?string { if ($user === null || blank($workspaceId)) { return null; } $workspace = Workspace::query()->find($workspaceId); return $workspace && $user->belongsToWorkspace($workspace) ? $workspace->id : null; } private function payloadId(string $input, string $key): mixed { $encrypted = request($input); if (! is_string($encrypted) || $encrypted === '') { return null; } return data_get($this->decryptor->decrypt($encrypted), $key) ?: null; } }