friendship ended with social-app. php is my new best friend
1<?php
2/**
3 * Class NPROne
4 *
5 * @created 28.07.2019
6 * @author smiley <smiley@chillerlan.net>
7 * @copyright 2019 smiley
8 * @license MIT
9 *
10 * @noinspection PhpUnused
11 */
12declare(strict_types=1);
13
14namespace chillerlan\OAuth\Providers;
15
16use chillerlan\OAuth\Core\{
17 AuthenticatedUser, CSRFToken, OAuth2Provider, TokenInvalidate, TokenInvalidateTrait, TokenRefresh, UserInfo,
18};
19use function in_array, sprintf, strtolower;
20
21/**
22 * NPR API services (OAuth2)
23 *
24 * @link https://dev.npr.org
25 * @link https://github.com/npr/npr-one-backend-proxy-php
26 */
27class NPROne extends OAuth2Provider implements CSRFToken, TokenRefresh, TokenInvalidate, UserInfo{
28 use TokenInvalidateTrait;
29
30 public const IDENTIFIER = 'NPRONE';
31
32 public const SCOPE_IDENTITY_READONLY = 'identity.readonly';
33 public const SCOPE_IDENTITY_WRITE = 'identity.write';
34 public const SCOPE_LISTENING_READONLY = 'listening.readonly';
35 public const SCOPE_LISTENING_WRITE = 'listening.write';
36 public const SCOPE_LOCALACTIVATION = 'localactivation';
37
38 public const DEFAULT_SCOPES = [
39 self::SCOPE_IDENTITY_READONLY,
40 self::SCOPE_LISTENING_READONLY,
41 ];
42
43 protected string $apiURL = 'https://listening.api.npr.org';
44 protected string $authorizationURL = 'https://authorization.api.npr.org/v2/authorize';
45 protected string $accessTokenURL = 'https://authorization.api.npr.org/v2/token';
46 protected string $revokeURL = 'https://authorization.api.npr.org/v2/token/revoke';
47 protected string|null $apiDocs = 'https://dev.npr.org/api/';
48 protected string|null $applicationURL = 'https://dev.npr.org/console';
49
50 /**
51 * Sets the API to work with ("listening" is set as default)
52 *
53 * @throws \chillerlan\OAuth\Providers\ProviderException
54 */
55 public function setAPI(string $api):static{
56 $api = strtolower($api);
57
58 if(!in_array($api, ['identity', 'listening', 'station'], true)){
59 throw new ProviderException(sprintf('invalid API: "%s"', $api));
60 }
61
62 $this->apiURL = sprintf('https://%s.api.npr.org', $api);
63
64 return $this;
65 }
66
67 /** @codeCoverageIgnore */
68 public function me():AuthenticatedUser{
69 $json = $this->getMeResponseData('https://identity.api.npr.org/v2/user');
70
71 $userdata = [
72 'data' => $json,
73 'email' => $json['attributes']['email'],
74 ];
75
76 return new AuthenticatedUser($userdata);
77 }
78
79}