phpleague oauth client

Solutions on MaxInterview for phpleague oauth client by the best coders in the world

showing results for - "phpleague oauth client"
Louise
23 Apr 2018
1$provider = new \League\OAuth2\Client\Provider\GenericProvider([
2    'clientId'                => 'XXXXXX',    // The client ID assigned to you by the provider
3    'clientSecret'            => 'XXXXXX',    // The client password assigned to you by the provider
4    'redirectUri'             => 'https://my.example.com/your-redirect-url/',
5    'urlAuthorize'            => 'https://service.example.com/authorize',
6    'urlAccessToken'          => 'https://service.example.com/token',
7    'urlResourceOwnerDetails' => 'https://service.example.com/resource'
8]);
9
10// If we don't have an authorization code then get one
11if (!isset($_GET['code'])) {
12
13    // Fetch the authorization URL from the provider; this returns the
14    // urlAuthorize option and generates and applies any necessary parameters
15    // (e.g. state).
16    $authorizationUrl = $provider->getAuthorizationUrl();
17
18    // Get the state generated for you and store it to the session.
19    $_SESSION['oauth2state'] = $provider->getState();
20
21    // Redirect the user to the authorization URL.
22    header('Location: ' . $authorizationUrl);
23    exit;
24
25// Check given state against previously stored one to mitigate CSRF attack
26} elseif (empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])) {
27
28    if (isset($_SESSION['oauth2state'])) {
29        unset($_SESSION['oauth2state']);
30    }
31
32    exit('Invalid state');
33
34} else {
35
36    try {
37
38        // Try to get an access token using the authorization code grant.
39        $accessToken = $provider->getAccessToken('authorization_code', [
40            'code' => $_GET['code']
41        ]);
42
43        // We have an access token, which we may use in authenticated
44        // requests against the service provider's API.
45        echo 'Access Token: ' . $accessToken->getToken() . "<br>";
46        echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "<br>";
47        echo 'Expired in: ' . $accessToken->getExpires() . "<br>";
48        echo 'Already expired? ' . ($accessToken->hasExpired() ? 'expired' : 'not expired') . "<br>";
49
50        // Using the access token, we may look up details about the
51        // resource owner.
52        $resourceOwner = $provider->getResourceOwner($accessToken);
53
54        var_export($resourceOwner->toArray());
55
56        // The provider provides a way to get an authenticated API request for
57        // the service, using the access token; it returns an object conforming
58        // to Psr\Http\Message\RequestInterface.
59        $request = $provider->getAuthenticatedRequest(
60            'GET',
61            'https://service.example.com/resource',
62            $accessToken
63        );
64
65    } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
66
67        // Failed to get the access token or user details.
68        exit($e->getMessage());
69
70    }
71
72}
73