Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 4647553

Browse files
committed
✨ OpenStreetmap2 (OSM OAuth2)
1 parent b9a4c4f commit 4647553

File tree

7 files changed

+154
-17
lines changed

7 files changed

+154
-17
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ A list of already implemented Providers.
7878
| [NPROne](https://dev.npr.org/api/) | [link](https://dev.npr.org/console) | | 2 | |
7979
| [OpenCaching](https://www.opencaching.de/okapi/) | [link](https://www.opencaching.de/okapi/signup.html) | [link](https://www.opencaching.de/okapi/apps/) | 1 | |
8080
| [OpenStreetmap](https://wiki.openstreetmap.org/wiki/API) | [link](https://www.openstreetmap.org/user/{USERNAME}/oauth_clients) | | 1 | |
81+
| [OpenStreetmap2](https://wiki.openstreetmap.org/wiki/API) | [link](https://www.openstreetmap.org/oauth2/applications) | | 2 | |
8182
| [Patreon](https://docs.patreon.com/) | [link](https://www.patreon.com/portal/registration/register-clients) | | 2 | |
8283
| [PayPal](https://developer.paypal.com/docs/connect-with-paypal/reference/) | [link](https://developer.paypal.com/developer/applications/) | | 2 ||
8384
| [PayPalSandbox](https://developer.paypal.com/docs/connect-with-paypal/reference/) | [link](https://developer.paypal.com/developer/applications/) | | 2 ||

config/.env_example

+6
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ OPENSTREETMAP_SECRET=
147147
OPENSTREETMAP_CALLBACK_URL=
148148
#OPENSTREETMAP_TESTUSER=
149149

150+
# https://www.openstreetmap.org/oauth2/applications
151+
OPENSTREETMAP2_KEY=
152+
OPENSTREETMAP2_SECRET=
153+
OPENSTREETMAP2_CALLBACK_URL=
154+
#OPENSTREETMAP2_TESTUSER=
155+
150156
# https://www.patreon.com/portal/registration/register-clients
151157
PATREON_KEY=
152158
PATREON_SECRET=

examples/get-token/OpenStreetmap2.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* @link https://wiki.openstreetmap.org/wiki/OAuth
4+
*
5+
* @created 05.03.2024
6+
* @author Smiley <[email protected]>
7+
* @copyright 2024 Smiley
8+
* @license MIT
9+
*/
10+
11+
use chillerlan\OAuth\Providers\OpenStreetmap2;
12+
13+
$ENVVAR ??= 'OPENSTREETMAP2';
14+
15+
require_once __DIR__.'/../provider-example-common.php';
16+
17+
/** @var \OAuthProviderFactory $factory */
18+
$provider = $factory->getProvider(OpenStreetmap2::class, $ENVVAR);
19+
20+
require_once __DIR__.'/_flow-oauth2.php';
21+
22+
exit;

src/OpenStreetmap.php

+2-17
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,9 @@
1818
/**
1919
* @see https://wiki.openstreetmap.org/wiki/API
2020
* @see https://wiki.openstreetmap.org/wiki/OAuth
21-
*
22-
* @see https://github.com/chillerlan/php-oauth-providers/issues/2
2321
*/
2422
class OpenStreetmap extends OAuth1Provider{
2523

26-
public const SCOPE_READ_PREFS = 'read_prefs';
27-
public const SCOPE_WRITE_PREFS = 'write_prefs';
28-
public const SCOPE_WRITE_DIARY = 'write_diary';
29-
public const SCOPE_WRITE_API = 'write_api';
30-
public const SCOPE_READ_GPX = 'read_gpx';
31-
public const SCOPE_WRITE_GPX = 'write_gpx';
32-
public const SCOPE_WRITE_NOTES = 'write_notes';
33-
34-
protected array $defaultScopes = [
35-
self::SCOPE_READ_PREFS,
36-
self::SCOPE_READ_GPX,
37-
];
38-
3924
protected string $requestTokenURL = 'https://www.openstreetmap.org/oauth/request_token';
4025
protected string $authURL = 'https://www.openstreetmap.org/oauth/authorize';
4126
protected string $accessTokenURL = 'https://www.openstreetmap.org/oauth/access_token';
@@ -46,8 +31,8 @@ class OpenStreetmap extends OAuth1Provider{
4631
/**
4732
* @inheritDoc
4833
*/
49-
public function me(bool|null $json = null):ResponseInterface{
50-
$response = $this->request('/api/0.6/user/details'.(($json ?? true) ? '.json' : ''));
34+
public function me():ResponseInterface{
35+
$response = $this->request('/api/0.6/user/details.json');
5136
$status = $response->getStatusCode();
5237

5338
if($status === 200){

src/OpenStreetmap2.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

tests/Live/OpenStreetmap2APITest.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Class OpenStreetmapAPITest
4+
*
5+
* @created 05.03.2024
6+
* @author smiley <[email protected]>
7+
* @copyright 2024 smiley
8+
* @license MIT
9+
*/
10+
11+
namespace chillerlan\OAuthTest\Providers\Live;
12+
13+
use chillerlan\HTTP\Utils\MessageUtil;
14+
use chillerlan\OAuth\Providers\OpenStreetmap2;
15+
use chillerlan\OAuthTest\Providers\OAuth2APITestAbstract;
16+
17+
/**
18+
* @property \chillerlan\OAuth\Providers\OpenStreetmap2 $provider
19+
*/
20+
class OpenStreetmap2APITest extends OAuth2APITestAbstract{
21+
22+
protected string $FQN = OpenStreetmap2::class;
23+
protected string $ENV = 'OPENSTREETMAP2';
24+
25+
public function testMe():void{
26+
$this::assertSame($this->testuser, MessageUtil::decodeJSON($this->provider->me())->user->display_name);
27+
}
28+
29+
}

tests/Unit/OpenStreetmap2Test.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Class OpenStreetmapTest
4+
*
5+
* @created 05.03.2024
6+
* @author smiley <[email protected]>
7+
* @copyright 2024 smiley
8+
* @license MIT
9+
*/
10+
11+
namespace chillerlan\OAuthTest\Providers\Unit;
12+
13+
use chillerlan\OAuth\Providers\OpenStreetmap2;
14+
use chillerlan\OAuthTest\Providers\OAuth2ProviderTestAbstract;
15+
16+
/**
17+
* @property \chillerlan\OAuth\Providers\OpenStreetmap $provider
18+
*/
19+
class OpenStreetmap2Test extends OAuth2ProviderTestAbstract{
20+
21+
protected string $FQN = OpenStreetmap2::class;
22+
23+
}

0 commit comments

Comments
 (0)