diff --git a/app/config/providers.php b/app/config/providers.php index f8c587b43a..b45ca52a74 100644 --- a/app/config/providers.php +++ b/app/config/providers.php @@ -56,4 +56,9 @@ return [ 'icon' => 'icon-dropbox', 'enabled' => true, ], + 'apple' => [ + 'developers' => 'https://www.dropbox.com/developers/documentation', + 'icon' => 'icon-apple', + 'enabled' => false, + ], ]; diff --git a/app/controllers/users.php b/app/controllers/users.php index 522bdfe8e5..a77436b0c0 100644 --- a/app/controllers/users.php +++ b/app/controllers/users.php @@ -381,6 +381,38 @@ $utopia->patch('/v1/users/:userId/status') } ); +$utopia->patch('/v1/users/:userId/prefs') + ->desc('Update Account Prefs') + ->label('scope', 'users.write') + ->label('sdk.namespace', 'users') + ->label('sdk.method', 'updateUserPrefs') + ->param('userId', '', function () { + return new UID(); + }, 'User unique ID.') + ->param('prefs', '', function () { + return new \Utopia\Validator\Mock(); + }, 'Prefs key-value JSON object string.') + ->label('sdk.description', 'Update user preferences by its unique ID. You can pass only the specific settings you wish to update.') + ->action( + function ($userId, $prefs) use ($response, $projectDB) { + $user = $projectDB->getDocument($userId); + + if (empty($user->getUid()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) { + throw new Exception('User not found', 404); + } + + $user = $projectDB->updateDocument(array_merge($user->getArrayCopy(), [ + 'prefs' => json_encode(array_merge(json_decode($user->getAttribute('prefs', '{}'), true), $prefs)), + ])); + if (false === $user) { + throw new Exception('Failed saving user to DB', 500); + } + + $response->json(array('result' => 'success')); + } + ); + + $utopia->delete('/v1/users/:userId/sessions/:session') ->desc('Delete User Session') ->label('scope', 'users.write') diff --git a/public/images/oauth/apple.png b/public/images/oauth/apple.png new file mode 100644 index 0000000000..ca56e16bc8 Binary files /dev/null and b/public/images/oauth/apple.png differ diff --git a/src/Auth/OAuth/Apple.php b/src/Auth/OAuth/Apple.php new file mode 100644 index 0000000000..2c9a99b8da --- /dev/null +++ b/src/Auth/OAuth/Apple.php @@ -0,0 +1,133 @@ +appID). + '&redirect_uri='.urlencode($this->callback). + '&state='.urlencode(json_encode($this->state)). + '&response_type=code'. + '&response_mode=form_post'. + '&scope=name+email'; + } + + /** + * @param string $code + * + * @return string + */ + public function getAccessToken(string $code): string + { + $headers[] = 'Content-Type: application/x-www-form-urlencoded'; + $accessToken = $this->request( + 'POST', + 'https://appleid.apple.com/auth/token', + $headers, + 'code='.urlencode($code). + '&client_id='.urlencode($this->appID). + '&client_secret='.urlencode($this->appSecret). + '&redirect_uri='.urlencode($this->callback). + '&grant_type=authorization_code' + ); + + var_dump($accessToken); + exit(); + + $accessToken = json_decode($accessToken, true); + + if (isset($accessToken['access_token'])) { + return $accessToken['access_token']; + } + + return ''; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserID(string $accessToken): string + { + $user = $this->getUser($accessToken); + + if (isset($user['account_id'])) { + return $user['account_id']; + } + + return ''; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserEmail(string $accessToken): string + { + $user = $this->getUser($accessToken); + + if (isset($user['email'])) { + return $user['email']; + } + + return ''; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserName(string $accessToken): string + { + $user = $this->getUser($accessToken); + + if (isset($user['name'])) { + return $user['name']['display_name']; + } + + return ''; + } + + /** + * @param string $accessToken + * + * @return array + */ + protected function getUser(string $accessToken): array + { + if (empty($this->user)) { + $headers[] = 'Authorization: Bearer '. urlencode($accessToken); + $user = $this->request('POST', 'https://api.dropboxapi.com/2/users/get_current_account', $headers); + $this->user = json_decode($user, true); + } + + return $this->user; + } +} diff --git a/src/Auth/OAuth/Gitlab.php b/src/Auth/OAuth/Gitlab.php index fbf21a4bd3..fbc6df60fa 100644 --- a/src/Auth/OAuth/Gitlab.php +++ b/src/Auth/OAuth/Gitlab.php @@ -4,6 +4,9 @@ namespace Auth\OAuth; use Auth\OAuth; +// Reference Material +// https://docs.gitlab.com/ee/api/oauth2.html + class Gitlab extends OAuth { /** diff --git a/src/Auth/OAuth/Google.php b/src/Auth/OAuth/Google.php index c07758d6b5..42f4357bd7 100644 --- a/src/Auth/OAuth/Google.php +++ b/src/Auth/OAuth/Google.php @@ -4,6 +4,10 @@ namespace Auth\OAuth; use Auth\OAuth; +// Reference Material +// https://developers.google.com/oauthplayground/ +// https://developers.google.com/identity/protocols/OAuth2 +// https://developers.google.com/identity/protocols/OAuth2WebServer class Google extends OAuth { /**