【问题标题】:Laravel passport get token from databaseLaravel 护照从数据库中获取令牌
【发布时间】:2023-04-03 02:13:01
【问题描述】:

我正在使用 Laravel 5.5 版并使用 Passport 进行身份验证。 我使用以下方法创建了令牌:

$token = $user->createToken('string')->accessToken;

它生成 1075 个字符的令牌'oauth_access_tokens' 表中的条目具有 80 个字符的 ID

如何使用 using 80 字符令牌从数据库中获取 1075 字符令牌?

【问题讨论】:

  • 我认为您可以使用 $user->tokens()->get() 访问用户令牌,但我不确定是否是您正在寻找的
  • @sochas $user->tokens$user->tokens()->get() 相同,您将从 oauth_access_tokens 表中获取记录,其中 id 为 80 个字符,但 @user10790140 想要获取真正的访问令牌

标签: laravel laravel-passport


【解决方案1】:

我还没有找到从护照中获取 access_token 的方法,但我找到了自己生成令牌的解决方案。这是 github 上的问题的一部分。您可以在链接中查看解决方案,希望它对我有用。

Passport link to issue

您也可以在护照库link中查看access_token的生成

【讨论】:

    【解决方案2】:

    (这是在 Laravel 8 上测试的)。

    用于生成 JWT 令牌的代码可以在 League\OAuth2\Server\Entities\Traits\AccessTokenTrait 中找到。

    看起来像这样:

    /**
         * Generate a JWT from the access token
         *
         * @return Token
         */
        private function convertToJWT()
        {
            $this->initJwtConfiguration();
    
            return $this->jwtConfiguration->builder()
                ->permittedFor($this->getClient()->getIdentifier())
                ->identifiedBy($this->getIdentifier())
                ->issuedAt(new DateTimeImmutable())
                ->canOnlyBeUsedAfter(new DateTimeImmutable())
                ->expiresAt($this->getExpiryDateTime())
                ->relatedTo((string) $this->getUserIdentifier())
                ->withClaim('scopes', $this->getScopes())
                ->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey());
        }
    

    因此,要使用oauth_access_tokens 表中的数据重新创建您的 JWT 令牌,请使用以下代码:

    $oauthClientId = 1; // CHANGE! The ID of oauth client found in "oauth_clients" table
    $tokenId = "..." // CHANGE! The 80 character ID found in "oauth_access_tokens"
    
    $clientRepository = app(\Laravel\Passport\Bridge\ClientRepository::class);
    $clientEntity = $clientRepository->getClientEntity($personalAccessClientId); 
    $token = Token::query()->where('id', $tokenId)->firstOrFail();
    $issuedAt = Carbon::createFromFormat('Y-m-d H:i:s', $token->created_at)
                ->toDateTimeImmutable();
    $expiresAt = Carbon::createFromFormat('Y-m-d H:i:s', $token->expires_at)
                ->toDateTimeImmutable();
    $scopes = collect($token->scopes)->map(function ($id) {
                return new \Laravel\Passport\Scope($id);
            })->all();
    
    $jwtConfiguration = \Lcobucci\JWT\Configuration::forAsymmetricSigner(
                new Sha256(),
                LocalFileReference::file(Passport::keyPath('oauth-private.key'), ''),
                InMemory::plainText('')
            );
    
    $jwtToken = $jwtConfiguration->builder()
                ->permittedFor($clientEntity->getIdentifier())
                ->identifiedBy($tokenId)
                ->issuedAt($issuedAt)
                ->canOnlyBeUsedAfter($issuedAt)
                ->expiresAt($expiresAt)
                ->relatedTo((string)$token->user_id)
                ->withClaim('scopes', $scopes)
                ->getToken($jwtConfiguration->signer(), $jwtConfiguration->signingKey());
    
    $token = (string)$jwtToken;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-18
      • 2019-05-15
      • 2020-01-14
      • 2018-08-04
      • 2019-09-13
      • 2019-11-17
      • 2019-12-22
      • 2018-12-20
      相关资源
      最近更新 更多