1
0
Fork 0
mirror of synced 2024-06-28 19:20:25 +12:00

Fixed OAuth2 tests

This commit is contained in:
Eldad Fux 2021-07-18 00:21:33 +03:00
parent 318c4843ca
commit 6fe19370f3
2 changed files with 29 additions and 16 deletions

View file

@ -442,27 +442,23 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect')
}
}
$user = (empty($user->getId())) ? $dbForInternal->getCollectionFirst([ // Get user by provider id
'limit' => 1,
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_USERS,
'sessions.provider='.$provider,
'sessions.providerUid='.$oauth2ID
],
]) : $user;
$user = ($user->isEmpty()) ? $dbForInternal->findFirst('sessions', [ // Get user by provider id
new Query('provider', QUERY::TYPE_EQUAL, [$provider]),
new Query('providerUid', QUERY::TYPE_EQUAL, [$oauth2ID]),
], 1) : $user;
if (empty($user)) { // No user logged in or with OAuth2 provider ID, create new one or connect with account with same email
if ($user === false || $user->isEmpty()) { // No user logged in or with OAuth2 provider ID, create new one or connect with account with same email
$name = $oauth2->getUserName($accessToken);
$email = $oauth2->getUserEmail($accessToken);
$user = $dbForInternal->findFirst('users', [new Query('email', Query::TYPE_EQUAL, [$email])], 1); // Get user by email address
if (!$user || empty($user->getId())) { // Last option -> create the user, generate random password
if ($user === false || $user->isEmpty()) { // Last option -> create the user, generate random password
$limit = $project->getAttribute('usersAuthLimit', 0);
if ($limit !== 0) {
$sum = $dbForInternal->count('users', [], APP_LIMIT_COUNT);
if($sum >= $limit) {
throw new Exception('Project registration is restricted. Contact your administrator for more information.', 501);
}
@ -529,11 +525,6 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect')
;
}
$session = $dbForInternal->createDocument('sessions', $session
->setAttribute('$read', ['user:'.$user->getId()])
->setAttribute('$write', ['user:'.$user->getId()])
);
$user
->setAttribute('status', Auth::USER_STATUS_ACTIVATED)
->setAttribute('sessions', $session, Document::SET_TYPE_APPEND)
@ -541,6 +532,11 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect')
Authorization::setRole('user:'.$user->getId());
$session = $dbForInternal->createDocument('sessions', $session
->setAttribute('$read', ['user:'.$user->getId()])
->setAttribute('$write', ['user:'.$user->getId()])
);
$user = $dbForInternal->updateDocument('users', $user->getId(), $user);
$audits

View file

@ -14,6 +14,9 @@ class AccountCustomClientTest extends Scope
use ProjectCustom;
use SideClient;
/**
* @depends testCreateAccountSession
*/
public function testCreateOAuth2AccountSession():array
{
$provider = 'mock';
@ -384,6 +387,17 @@ class AccountCustomClientTest extends Scope
/**
* Test for SUCCESS
*/
$response = $this->client->call(Client::METHOD_GET, '/account', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'cookie' => 'a_session_'.$this->getProject()['$id'].'=' . $session,
]));
$this->assertEquals($response['headers']['status-code'], 200);
$userId = $response['body']['$id'] ?? '';
$response = $this->client->call(Client::METHOD_PATCH, '/projects/'.$this->getProject()['$id'].'/oauth2', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
@ -406,6 +420,8 @@ class AccountCustomClientTest extends Scope
'success' => 'http://localhost/v1/mock/tests/general/oauth2/success',
'failure' => 'http://localhost/v1/mock/tests/general/oauth2/failure',
]);
$session = $this->client->parseCookie((string)$response['headers']['set-cookie'])['a_session_'.$this->getProject()['$id']];
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('success', $response['body']['result']);
@ -418,6 +434,7 @@ class AccountCustomClientTest extends Scope
]));
$this->assertEquals($response['headers']['status-code'], 200);
$this->assertEquals($response['body']['$id'], $userId);
$this->assertEquals($response['body']['name'], 'User Name');
$this->assertEquals($response['body']['email'], 'user@localhost.test');