|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Appwrite\Auth\OAuth2; |
| 4 | + |
| 5 | +use Appwrite\Auth\OAuth2; |
| 6 | + |
| 7 | +class Zoom extends OAuth2 |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @var string |
| 11 | + */ |
| 12 | + private $endpoint = 'https://zoom.us'; |
| 13 | + |
| 14 | + /** |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + private $version = '2022-03-26'; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var array |
| 21 | + */ |
| 22 | + protected $user = []; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var array |
| 26 | + */ |
| 27 | + protected $tokens = []; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var array |
| 31 | + */ |
| 32 | + protected $scopes = []; |
| 33 | + |
| 34 | + /** |
| 35 | + * @return string |
| 36 | + */ |
| 37 | + public function getName():string |
| 38 | + { |
| 39 | + return 'zoom'; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @return string |
| 44 | + */ |
| 45 | + public function getLoginURL():string |
| 46 | + { |
| 47 | + return $this->endpoint . '/oauth/authorize?'. \http_build_query([ |
| 48 | + 'client_id' => $this->appID, |
| 49 | + 'redirect_uri' => $this->callback, |
| 50 | + 'response_type' => 'code', |
| 51 | + 'state' => \json_encode($this->state), |
| 52 | + ]); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param string $code |
| 57 | + * |
| 58 | + * @return array |
| 59 | + */ |
| 60 | + protected function getTokens(string $code): array |
| 61 | + { |
| 62 | + if(empty($this->tokens)) { |
| 63 | + $headers = ['Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret), 'Content-Type: application/x-www-form-urlencoded']; |
| 64 | + $this->tokens = \json_decode($this->request( |
| 65 | + 'POST', |
| 66 | + $this->endpoint . '/oauth/token', |
| 67 | + $headers, |
| 68 | + \http_build_query([ |
| 69 | + 'grant_type' => 'authorization_code', |
| 70 | + 'redirect_uri' => $this->callback, |
| 71 | + 'code' => $code |
| 72 | + ]) |
| 73 | + ), true); |
| 74 | + } |
| 75 | + |
| 76 | + return $this->tokens; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param string $refreshToken |
| 81 | + * |
| 82 | + * @return array |
| 83 | + */ |
| 84 | + public function refreshTokens(string $refreshToken):array |
| 85 | + { |
| 86 | + $headers = ['Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret), 'Content-Type: application/x-www-form-urlencoded']; |
| 87 | + $this->tokens = \json_decode($this->request( |
| 88 | + 'POST', |
| 89 | + $this->endpoint . '/oauth/token', |
| 90 | + $headers, |
| 91 | + \http_build_query([ |
| 92 | + 'grant_type' => 'refresh_token', |
| 93 | + 'refresh_token' => $refreshToken, |
| 94 | + ]) |
| 95 | + ), true); |
| 96 | + |
| 97 | + if(empty($this->tokens['refresh_token'])) { |
| 98 | + $this->tokens['refresh_token'] = $refreshToken; |
| 99 | + } |
| 100 | + |
| 101 | + return $this->tokens; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @param $accessToken |
| 106 | + * |
| 107 | + * @return string |
| 108 | + */ |
| 109 | + public function getUserID(string $accessToken):string |
| 110 | + { |
| 111 | + $response = $this->getUser($accessToken); |
| 112 | + return $response['id'] ?? ''; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @param $accessToken |
| 117 | + * |
| 118 | + * @return string |
| 119 | + */ |
| 120 | + public function getUserEmail(string $accessToken):string |
| 121 | + { |
| 122 | + $response = $this->getUser($accessToken); |
| 123 | + return $response['email'] ?? ''; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @param $accessToken |
| 128 | + * |
| 129 | + * @return string |
| 130 | + */ |
| 131 | + public function getUserName(string $accessToken):string |
| 132 | + { |
| 133 | + $response = $this->getUser($accessToken); |
| 134 | + return $response['first_name'] ?? '' . ' ' . $response['last_name'] ?? ''; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @param string $accessToken |
| 139 | + * |
| 140 | + * @return array |
| 141 | + */ |
| 142 | + protected function getUser(string $accessToken) |
| 143 | + { |
| 144 | + $headers = [ |
| 145 | + 'Authorization: Bearer '.\urlencode($accessToken) |
| 146 | + ]; |
| 147 | + |
| 148 | + if (empty($this->user)) { |
| 149 | + $this->user = \json_decode($this->request('GET', 'https://api.zoom.us/v2/users/me', $headers), true); |
| 150 | + } |
| 151 | + |
| 152 | + return $this->user; |
| 153 | + } |
| 154 | +} |
0 commit comments