-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviantArt.php
111 lines (92 loc) · 3.33 KB
/
DeviantArt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* Class DeviantArt
*
* @created 26.10.2017
* @author Smiley <smiley@chillerlan.net>
* @copyright 2017 Smiley
* @license MIT
*
* @noinspection PhpUnused
*/
declare(strict_types=1);
namespace chillerlan\OAuth\Providers;
use chillerlan\HTTP\Utils\MessageUtil;
use chillerlan\OAuth\Core\{
AccessToken, AuthenticatedUser, ClientCredentials, ClientCredentialsTrait,
CSRFToken, OAuth2Provider, TokenInvalidate, TokenRefresh, UserInfo,
};
use chillerlan\OAuth\Storage\MemoryStorage;
use Throwable;
use function sprintf;
/**
* DeviantArt OAuth2
*
* @link https://www.deviantart.com/developers/
*/
class DeviantArt extends OAuth2Provider implements ClientCredentials, CSRFToken, TokenInvalidate, TokenRefresh, UserInfo{
use ClientCredentialsTrait;
public const IDENTIFIER = 'DEVIANTART';
public const SCOPE_BASIC = 'basic';
public const SCOPE_BROWSE = 'browse';
public const SCOPE_COLLECTION = 'collection';
public const SCOPE_COMMENT_POST = 'comment.post';
public const SCOPE_FEED = 'feed';
public const SCOPE_GALLERY = 'gallery';
public const SCOPE_MESSAGE = 'message';
public const SCOPE_NOTE = 'note';
public const SCOPE_STASH = 'stash';
public const SCOPE_USER = 'user';
public const SCOPE_USER_MANAGE = 'user.manage';
public const DEFAULT_SCOPES = [
self::SCOPE_BASIC,
self::SCOPE_BROWSE,
];
public const HEADERS_API = [
'dA-minor-version' => '20210526',
];
protected string $authorizationURL = 'https://www.deviantart.com/oauth2/authorize';
protected string $accessTokenURL = 'https://www.deviantart.com/oauth2/token';
protected string $revokeURL = 'https://www.deviantart.com/oauth2/revoke';
protected string $apiURL = 'https://www.deviantart.com/api/v1/oauth2';
protected string|null $userRevokeURL = 'https://www.deviantart.com/settings/applications';
protected string|null $apiDocs = 'https://www.deviantart.com/developers/';
protected string|null $applicationURL = 'https://www.deviantart.com/developers/apps';
/** @codeCoverageIgnore */
public function me():AuthenticatedUser{
$json = $this->getMeResponseData('/user/whoami');
$userdata = [
'data' => $json,
'avatar' => $json['usericon'],
'handle' => $json['username'],
'id' => $json['userid'],
'url' => sprintf('https://www.deviantart.com/%s', $json['username']),
];
return new AuthenticatedUser($userdata);
}
public function invalidateAccessToken(AccessToken|null $token = null, string|null $type = null):bool{
if($token !== null){
// to revoke a token different from the one of the currently authenticated user,
// we're going to clone the provider and feed the other token for the invalidate request
return (clone $this)
->setStorage(new MemoryStorage)
->storeAccessToken($token)
->invalidateAccessToken()
;
}
$request = $this->requestFactory->createRequest('POST', $this->revokeURL);
$response = $this->http->sendRequest($this->getRequestAuthorization($request));
try{
$json = MessageUtil::decodeJSON($response);
}
catch(Throwable){
return false;
}
if($response->getStatusCode() === 200 && !empty($json->success)){
// delete the token from storage
$this->storage->clearAccessToken($this->name);
return true;
}
return false; // @codeCoverageIgnore
}
}