julesr2 / jwt-verifier
Okta JWT 的验证库
Requires
- php: ^7.2
- bretterer/iso_duration_converter: ^0.1.0
- nesbot/carbon: ^2.0
- php-http/client-common: ^2.3
- php-http/curl-client: ^2.1
- php-http/discovery: ^1.9
- php-http/httplug: ^2.2
- php-http/message: ^1.8
- psr/http-message: ^1.0
Requires (Dev)
- firebase/php-jwt: ^5.2
- guzzlehttp/psr7: dev-master#ad1de77a65b751d598ced37747bf4c17d457fbc9
- php-http/mock-client: ^1.4
- phpunit/phpunit: ^7.0
- squizlabs/php_codesniffer: ^3.5
- symfony/var-dumper: ^5.1
This package is not auto-updated.
Last update: 2024-09-30 11:31:40 UTC
README
PHP Okta JWT 验证器
在用户成功认证并获取授权后(例如,通过 从用户那里获取授权 或使用 Okta API),您将获得一个签名 JWT(id_token 和/或 access_token)。这些访问令牌的常见用途是在 Bearer 认证头中使用,以便您的应用程序知道发起请求的用户是谁。为了确保您知道这种用法有效,您需要知道如何将令牌与 Okta 进行验证。本指南为您提供了一个使用 Okta 的 JWT 验证库进行 PHP 验证的示例。
发布状态
此库使用语义版本控制并遵循 Okta 的 库版本策略。
最新版本始终可以在 [发布页面][github-releases] 上找到。
安装
Okta JWT 验证器可以通过 composer 安装。
composer require okta/jwt-verifier
此库需要 JWT 库。我们目前支持 firebase/php-jwt。您必须安装此库或创建自己的适配器。
composer require firebase/php-jwt
要创建自己的适配器,只需在您的类中实现 Okta/JwtVerifier/Adaptors/Adaptor。
您还需要安装一个 PSR-7 兼容的库。我们建议您在项目中使用 guzzlehttp/psr7。
composer require guzzlehttp/psr7
设置库
为了验证 JWT,您需要以下几样东西
- 您的发行者 URL
- 您想要验证的 JWT 字符串
- 在您的脚本中访问您的供应商自动加载文件。
require_once("/vendor/autoload.php"); // This should be replaced with your path to your vendor/autoload.php file $jwtVerifier = (new \Okta\JwtVerifier\JwtVerifierBuilder()) ->setDiscovery(new \Okta\JwtVerifier\Discovery\Oauth) // This is not needed if using oauth. The other option is `new \Okta\JwtVerifier\Discovery\OIDC` ->setAdaptor(new \Okta\JwtVerifier\Adaptors\FirebasePhpJwt) ->setAudience('api://default') ->setClientId('{clientId}') ->setIssuer('https://{yourOktaDomain}.com/oauth2/default') ->build();
验证访问令牌
在您从上述部分获得 $jwtVerifier 和从成功登录或授权头中的 Bearer token 获取 access_token 之后,您需要确保它仍然有效。您只需调用 verifyAccessToken 方法(其中 $jwtString 是您的访问令牌的字符串格式)。
$jwt = $jwtVerifier->verifyAccessToken($jwtString);
这将验证您的 JWT 的以下内容
- 令牌过期时间
- 发行时间
- 令牌发行者与上述辅助函数中传入的预期值匹配
- 令牌受众与上述辅助函数中传入的预期值匹配
验证方法的结果是一个 Jwt 对象,该对象有几个辅助方法可供您使用
dump($jwt); //Returns instance of \Okta\JwtVerifier\JWT dump($jwt->toJson()); // Returns Claims as JSON Object dump($jwt->getClaims()); // Returns Claims as they come from the JWT Package used dump($jwt->getIssuedAt()); // returns Carbon instance of issued at time dump($jwt->getIssuedAt(false)); // returns timestamp of issued at time dump($jwt->getExpirationTime()); //returns Carbon instance of Expiration Time dump($jwt->getExpirationTime(false)); //returns timestamp of Expiration Time
验证 Id 令牌
$jwt = $jwtVerifier->verifyIdToken($jwtString);
这将验证您的 JWT 的以下内容
- 令牌过期时间
- 发行时间
- 令牌发行者与上述辅助函数中传入的预期值匹配
- 令牌受众与上述辅助函数中传入的预期值匹配
验证方法的结果是一个 Jwt 对象,该对象有几个辅助方法可供您使用
dump($jwt); //Returns instance of \Okta\JwtVerifier\JWT dump($jwt->toJson()); // Returns Claims as JSON Object dump($jwt->getClaims()); // Returns Claims as they come from the JWT Package used dump($jwt->getIssuedAt()); // returns Carbon instance of issued at time dump($jwt->getIssuedAt(false)); // returns timestamp of issued at time dump($jwt->getExpirationTime()); //returns Carbon instance of Expiration Time dump($jwt->getExpirationTime(false)); //returns timestamp of Expiration Time ## Need help? If you run into problems using the SDK, you can * Ask questions on the [Okta Developer Forums][devforum] * Post [issues][github-issues] here on GitHub * [Working With OAuth 2.0 Tokens](https://developer.okta.com/authentication-guide/tokens/) ## Conclusion The above are the basic steps for verifying an access token locally. The steps are not tied directly to a framework so you could plug in the `okta/okta-jwt` into the framework of your choice. [devforum]: https://devforum.okta.com/ [lang-landing]: https://developer.okta.com/code/php/ [github-issues]: /okta/okta-jwt-verifier-php/issues [github-releases]: /okta/okta-jwt-verifier-php/releases