|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class OpenStreetmap2 |
| 4 | + * |
| 5 | + * @created 05.03.2024 |
| 6 | + * @author smiley <[email protected]> |
| 7 | + * @copyright 2024 smiley |
| 8 | + * @license MIT |
| 9 | + */ |
| 10 | + |
| 11 | +namespace chillerlan\OAuth\Providers; |
| 12 | + |
| 13 | +use chillerlan\HTTP\Utils\MessageUtil; |
| 14 | +use chillerlan\OAuth\Core\{CSRFToken, OAuth2Provider, ProviderException}; |
| 15 | +use Psr\Http\Message\ResponseInterface; |
| 16 | +use function sprintf, strip_tags; |
| 17 | + |
| 18 | +/** |
| 19 | + * @see https://wiki.openstreetmap.org/wiki/API |
| 20 | + * @see https://wiki.openstreetmap.org/wiki/OAuth |
| 21 | + * @see https://www.openstreetmap.org/.well-known/oauth-authorization-server |
| 22 | + * |
| 23 | + * @see https://github.com/chillerlan/php-oauth-providers/issues/2 |
| 24 | + */ |
| 25 | +class OpenStreetmap2 extends OAuth2Provider implements CSRFToken{ |
| 26 | + |
| 27 | + public const SCOPE_READ_PREFS = 'read_prefs'; |
| 28 | + public const SCOPE_WRITE_PREFS = 'write_prefs'; |
| 29 | + public const SCOPE_WRITE_DIARY = 'write_diary'; |
| 30 | + public const SCOPE_WRITE_API = 'write_api'; |
| 31 | + public const SCOPE_READ_GPX = 'read_gpx'; |
| 32 | + public const SCOPE_WRITE_GPX = 'write_gpx'; |
| 33 | + public const SCOPE_WRITE_NOTES = 'write_notes'; |
| 34 | +# public const SCOPE_READ_EMAIL = 'read_email'; |
| 35 | +# public const SCOPE_SKIP_AUTH = 'skip_authorization'; |
| 36 | + public const SCOPE_WRITE_REDACTIONS = 'write_redactions'; |
| 37 | + public const SCOPE_OPENID = 'openid'; |
| 38 | + |
| 39 | + protected array $defaultScopes = [ |
| 40 | + self::SCOPE_READ_GPX, |
| 41 | + self::SCOPE_READ_PREFS, |
| 42 | + ]; |
| 43 | + |
| 44 | + protected string $authURL = 'https://www.openstreetmap.org/oauth2/authorize'; |
| 45 | + protected string $accessTokenURL = 'https://www.openstreetmap.org/oauth2/token'; |
| 46 | +# protected string $revokeURL = 'https://www.openstreetmap.org/oauth2/revoke'; // not implemented yet? |
| 47 | + protected string $apiURL = 'https://api.openstreetmap.org'; |
| 48 | + protected string|null $apiDocs = 'https://wiki.openstreetmap.org/wiki/API'; |
| 49 | + protected string|null $applicationURL = 'https://www.openstreetmap.org/oauth2/applications'; |
| 50 | + |
| 51 | + /** |
| 52 | + * @inheritDoc |
| 53 | + */ |
| 54 | + public function me():ResponseInterface{ |
| 55 | + $response = $this->request('/api/0.6/user/details.json'); |
| 56 | + $status = $response->getStatusCode(); |
| 57 | + |
| 58 | + if($status === 200){ |
| 59 | + return $response; |
| 60 | + } |
| 61 | + |
| 62 | + $body = MessageUtil::getContents($response); |
| 63 | + |
| 64 | + if(!empty($body)){ |
| 65 | + throw new ProviderException(strip_tags($body)); |
| 66 | + } |
| 67 | + |
| 68 | + throw new ProviderException(sprintf('user info error error HTTP/%s', $status)); |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments