| 1 | <?php |
| 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | |
| 6 | // Simple server based on https://bshaffer.github.io/oauth2-server-php-docs/cookbook |
| 7 | |
| 8 | // Include the common auth system files (including the OAuth2 Server object). |
| 9 | require_once(__DIR__.'/authsystem.inc.php'); |
| 10 | |
| 11 | $request = OAuth2\Request::createFromGlobals(); |
| 12 | $response = new OAuth2\Response(); |
| 13 | |
| 14 | // validate the authorize request |
| 15 | if (!$server->validateAuthorizeRequest($request, $response)) { |
| 16 | $response->send(); |
| 17 | die; |
| 18 | } |
| 19 | // display an authorization form |
| 20 | if (empty($_POST)) { |
| 21 | exit(' |
| 22 | <form method="post"> |
| 23 | <label>Do You Authorize TestClient?</label><br /> |
| 24 | <input type="submit" name="authorized" value="yes"> |
| 25 | <input type="submit" name="authorized" value="no"> |
| 26 | </form>'); |
| 27 | } |
| 28 | |
| 29 | // print the authorization code if the user has authorized your client |
| 30 | $is_authorized = ($_POST['authorized'] === 'yes'); |
| 31 | $server->handleAuthorizeRequest($request, $response, $is_authorized); |
| 32 | if ($is_authorized) { |
| 33 | // this is only here so that you get to see your code in the cURL request. Otherwise, we'd redirect back to the client |
| 34 | $code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40); |
| 35 | exit("SUCCESS! Authorization Code: $code"); |
| 36 | } |
| 37 | $response->send(); |
| 38 | |
| 39 | ?> |