项目作者: tulipretail

项目描述 :
Hydra Provider for the PHP League OAuth 2.0 Client
高级语言: PHP
项目地址: git://github.com/tulipretail/oauth2-hydra.git
创建时间: 2018-01-25T21:19:52Z
项目社区:https://github.com/tulipretail/oauth2-hydra

开源协议:

下载


Hydra PHP Oauth2 Client

This package provides Hydra OAuth 2.0 support for the PHP League’s OAuth 2.0 Client.

Installation

To install, use composer:

  1. composer require tulip/oauth2-hydra

Usage

Usage is the same as The League’s OAuth client, using \Hydra\OAuth2\Provider\OAuth2 as the provider.

With the Hydra SDK

You can use this library to acquire an access token for use with the Hydra SDK.

Here we get one with the ‘hydra.clients’ scope:

  1. $provider = new \Hydra\OAuth2\Provider\OAuth2([
  2. 'clientId' => 'admin',
  3. 'clientSecret' => 'demo-password',
  4. 'domain' => 'https://your-hydra-domain',
  5. ]);
  6. try {
  7. // Get an access token using the client credentials grant.
  8. // Note that you must separate multiple scopes with a plus (+)
  9. $accessToken = $provider->getAccessToken(
  10. 'client_credentials', ['scope' => 'hydra.clients']
  11. );
  12. } catch (\Hydra\Oauth2\Provider\Exception\ConnectionException $e) {
  13. die("Connection to Hydra failed: ".$e->getMessage());
  14. } catch (\Hydra\Oauth2\Provider\Exception\IdentityProviderException $e) {
  15. die("Failed to get an access token: ".$e->getMessage());
  16. }
  17. // You may now pass $accessToken to the hydra SDK to manage clients

As an OIDC Client

You can also use this library if you are a Relying Party.

Here we send users to Hydra to authenticate so that we can complete the authorization code flow:

  1. $provider = new \Hydra\OAuth2\Provider\OAuth2([
  2. 'clientId' => 'admin',
  3. 'clientSecret' => 'demo-password',
  4. 'domain' => 'https://your-hydra-domain',
  5. // Be sure this is a redirect URI you registered with Hydra for your client!
  6. 'redirectUri' => 'http://your-domain.com/bobsflowers',
  7. ]);
  8. if (!isset($_GET['code'])) {
  9. // If we don't have an authorization code then get one
  10. $authUrl = $provider->getAuthorizationUrl(['scope' => ['openid']]);
  11. $_SESSION['oauth2state'] = $provider->getState();
  12. header('Location: '.$authUrl);
  13. die();
  14. // Check given state against previously stored one to mitigate CSRF attack
  15. } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
  16. unset($_SESSION['oauth2state']);
  17. die('Invalid state');
  18. } else {
  19. // Try to get an access token (using the authorization code grant)
  20. $token = $provider->getAccessToken('authorization_code', [
  21. 'code' => $_GET['code']
  22. ]);
  23. // Optional: Now you have a token you can look up a users profile data
  24. try {
  25. // We got an access token, let's now get the user's details
  26. $user = $provider->getResourceOwner($token);
  27. // $user contains public claims from the id token
  28. printf('User info: ', json_encode($user));
  29. } catch (\Hydra\Oauth2\Provider\Exception\IdentityProviderException $e) {
  30. die('Unable to fetch user details: '.$e->getMessage());
  31. }
  32. // Use this to interact with an API on the users behalf
  33. echo $token->getToken();
  34. }