friendship ended with social-app. php is my new best friend
1<?php
2/**
3 * Class Vimeo
4 *
5 * @created 09.04.2018
6 * @author Smiley <smiley@chillerlan.net>
7 * @copyright 2018 Smiley
8 * @license MIT
9 *
10 * @noinspection PhpUnused
11 */
12declare(strict_types=1);
13
14namespace chillerlan\OAuth\Providers;
15
16use chillerlan\OAuth\Core\{
17 AccessToken, AuthenticatedUser, ClientCredentials, ClientCredentialsTrait,
18 CSRFToken, OAuth2Provider, TokenInvalidate, UserInfo,
19};
20use chillerlan\OAuth\Storage\MemoryStorage;
21use function str_replace;
22
23/**
24 * Vimeo OAuth2
25 *
26 * @link https://developer.vimeo.com/
27 * @link https://developer.vimeo.com/api/authentication
28 */
29class Vimeo extends OAuth2Provider implements ClientCredentials, CSRFToken, TokenInvalidate, UserInfo{
30 use ClientCredentialsTrait;
31
32 public const IDENTIFIER = 'VIMEO';
33
34 /**
35 * @link https://developer.vimeo.com/api/authentication#understanding-the-auth-process
36 */
37 public const SCOPE_PUBLIC = 'public';
38 public const SCOPE_PRIVATE = 'private';
39 public const SCOPE_PURCHASED = 'purchased';
40 public const SCOPE_CREATE = 'create';
41 public const SCOPE_EDIT = 'edit';
42 public const SCOPE_DELETE = 'delete';
43 public const SCOPE_INTERACT = 'interact';
44 public const SCOPE_STATS = 'stats';
45 public const SCOPE_UPLOAD = 'upload';
46 public const SCOPE_PROMO_CODES = 'promo_codes';
47 public const SCOPE_VIDEO_FILES = 'video_files';
48
49 public const DEFAULT_SCOPES = [
50 self::SCOPE_PUBLIC,
51 self::SCOPE_PRIVATE,
52 self::SCOPE_INTERACT,
53 self::SCOPE_STATS,
54 ];
55
56 // @link https://developer.vimeo.com/api/changelog
57 protected const API_VERSION = '3.4';
58
59 public const HEADERS_AUTH = [
60 'Accept' => 'application/vnd.vimeo.*+json;version='.self::API_VERSION,
61 ];
62
63 public const HEADERS_API = [
64 'Accept' => 'application/vnd.vimeo.*+json;version='.self::API_VERSION,
65 ];
66
67 protected string $authorizationURL = 'https://api.vimeo.com/oauth/authorize';
68 protected string $accessTokenURL = 'https://api.vimeo.com/oauth/access_token';
69 protected string $revokeURL = 'https://api.vimeo.com/tokens';
70 protected string $apiURL = 'https://api.vimeo.com';
71 protected string|null $userRevokeURL = 'https://vimeo.com/settings/apps';
72 protected string|null $clientCredentialsTokenURL = 'https://api.vimeo.com/oauth/authorize/client';
73 protected string|null $apiDocs = 'https://developer.vimeo.com';
74 protected string|null $applicationURL = 'https://developer.vimeo.com/apps';
75
76 /** @codeCoverageIgnore */
77 public function me():AuthenticatedUser{
78 $json = $this->getMeResponseData('/me');
79
80 $userdata = [
81 'data' => $json,
82 'avatar' => $json['pictures']['base_link'],
83 'handle' => str_replace('https://vimeo.com/', '', $json['link']),
84 'displayName' => $json['name'],
85 'id' => str_replace('/users/', '', $json['uri']),
86 'url' => $json['link'],
87 ];
88
89 return new AuthenticatedUser($userdata);
90 }
91
92 public function invalidateAccessToken(AccessToken|null $token = null, string|null $type = null):bool{
93
94 if($token !== null){
95 // to revoke a token different from the one of the currently authenticated user,
96 // we're going to clone the provider and feed the other token for the invalidate request
97 return (clone $this)
98 ->setStorage(new MemoryStorage)
99 ->storeAccessToken($token)
100 ->invalidateAccessToken()
101 ;
102 }
103
104 $request = $this->requestFactory->createRequest('DELETE', $this->revokeURL);
105 $response = $this->http->sendRequest($this->getRequestAuthorization($request));
106
107 if($response->getStatusCode() === 204){
108 // delete the token from storage
109 $this->storage->clearAccessToken($this->name);
110
111 return true;
112 }
113
114 return false;
115 }
116
117}