1
0
Fork 0
mirror of synced 2024-06-02 19:04:49 +12:00

oauth2 providers in project grouped

This commit is contained in:
Damodar Lohani 2021-08-06 16:20:50 +05:45
parent 9b25d65c85
commit 475002eef8
7 changed files with 47 additions and 54 deletions

View file

@ -2,7 +2,9 @@
## Features
- Grouped auth related attributes in project collection. Introduced new attribute `auths` and removed all attributes related to auth methods and `usersAuthLimit` as well, all these are grouped under `auths` attribute
- Grouped auth related attributes in project collection. Introduced new attribute `auths` and removed all attributes related to auth methods and `usersAuthLimit` as well, all these are grouped under `auths` attribute
- Grouped oAuth related attributes in project collection. Introduced new attribute `providers` and removed all attributes related to OAuth2 providers. All OAuth2 attributes are grouped under `providers`
- Project model changed, `userAuth<AuthMethod>` => `auth<AuthMethod>` example `userAuthEmailPassword` => `authEmailPassword`, also `userOauth2<Provider>...` => `provider<Provider>...` example `userOauth2GithubAppid` => `providerGithubAppid`
# Version 0.9.3

View file

@ -166,6 +166,17 @@ $collections = [
'array' => false,
'filters' => ['json'],
],
[
'$id' => 'providers',
'type' => Database::VAR_STRING,
'format' => '',
'size' => 16384,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => ['json'],
],
[
'$id' => 'platforms',
'type' => Database::VAR_STRING,
@ -1394,37 +1405,4 @@ $collections = [
],
];
/*
* Add enabled OAuth2 providers to default data rules
*/
foreach ($providers as $index => $provider) {
if (!$provider['enabled']) {
continue;
}
$collections['projects']['attributes'][] = [
'$id' => 'usersOauth2' . \ucfirst($index) . 'Appid',
'type' => Database::VAR_STRING,
'format' => '',
'size' => 16384,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
];
$collections['projects']['attributes'][] = [
'$id' => 'usersOauth2' . \ucfirst($index) . 'Secret',
'type' => Database::VAR_STRING,
'format' => '',
'size' => 16384,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
];
}
return $collections;

View file

@ -256,8 +256,8 @@ App::get('/v1/account/sessions/oauth2/:provider')
$protocol = $request->getProtocol();
$callback = $protocol.'://'.$request->getHostname().'/v1/account/sessions/oauth2/callback/'.$provider.'/'.$project->getId();
$appId = $project->getAttribute('usersOauth2'.\ucfirst($provider).'Appid', '');
$appSecret = $project->getAttribute('usersOauth2'.\ucfirst($provider).'Secret', '{}');
$appId = $project->getAttribute('providers', [])[$provider.'Appid'] ?? '';
$appSecret = $project->getAttribute('providers', [])[$provider.'Secret'] ?? '{}';
if (!empty($appSecret) && isset($appSecret['version'])) {
$key = App::getEnv('_APP_OPENSSL_KEY_V'.$appSecret['version']);
@ -369,8 +369,8 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect')
$defaultState = ['success' => $project->getAttribute('url', ''), 'failure' => ''];
$validateURL = new URL();
$appId = $project->getAttribute('usersOauth2'.\ucfirst($provider).'Appid', '');
$appSecret = $project->getAttribute('usersOauth2'.\ucfirst($provider).'Secret', '{}');
$appId = $project->getAttribute('providers', [])[$provider.'Appid'] ?? '';
$appSecret = $project->getAttribute('providers', [])[$provider.'Secret'] ?? '{}';
if (!empty($appSecret) && isset($appSecret['version'])) {
$key = App::getEnv('_APP_OPENSSL_KEY_V'.$appSecret['version']);

View file

@ -505,10 +505,11 @@ App::patch('/v1/projects/:projectId/oauth2')
throw new Exception('Project not found', 404);
}
$project = $dbForConsole->updateDocument('projects', $project->getId(), $project
->setAttribute('usersOauth2' . \ucfirst($provider) . 'Appid', $appId)
->setAttribute('usersOauth2' . \ucfirst($provider) . 'Secret', $secret)
);
$providers = $project->getAttribute('providers', []);
$providers[$provider . 'Appid'] = $appId;
$providers[$provider . 'Secret'] = $secret;
$project = $dbForConsole->updateDocument('projects', $project->getId(), $project->setAttribute('providers', $providers));
$response->dynamic($project, Response::MODEL_PROJECT);
});

View file

@ -439,15 +439,15 @@ $auth = $this->getParam('auth', []);
<?php if(!$form): ?>
<label for="oauth2<?php echo $this->escape(ucfirst($provider)); ?>Appid">App ID</label>
<input name="appId" id="oauth2<?php echo $this->escape(ucfirst($provider)); ?>Appid" type="text" autocomplete="off" data-ls-bind="{{console-project.usersOauth2<?php echo $this->escape(ucfirst($provider)); ?>Appid}}">
<input name="appId" id="oauth2<?php echo $this->escape(ucfirst($provider)); ?>Appid" type="text" autocomplete="off" data-ls-bind="{{console-project.provider<?php echo $this->escape(ucfirst($provider)); ?>Appid}}">
<label for="oauth2<?php echo $this->escape(ucfirst($provider)); ?>Secret">App Secret</label>
<input name="secret" data-forms-show-secret id="oauth2<?php echo $this->escape(ucfirst($provider)); ?>Secret" type="password" autocomplete="off" data-ls-bind="{{console-project.usersOauth2<?php echo $this->escape(ucfirst($provider)); ?>Secret}}">
<input name="secret" data-forms-show-secret id="oauth2<?php echo $this->escape(ucfirst($provider)); ?>Secret" type="password" autocomplete="off" data-ls-bind="{{console-project.provider<?php echo $this->escape(ucfirst($provider)); ?>Secret}}">
<?php else: ?>
<label for="oauth2<?php echo $this->escape(ucfirst($provider)); ?>Appid">Bundle ID <span class="tooltip" data-tooltip="Attribute internal display name"><i class="icon-info-circled"></i></span></label>
<input name="appId" id="oauth2<?php echo $this->escape(ucfirst($provider)); ?>Appid" type="text" autocomplete="off" data-ls-bind="{{console-project.usersOauth2<?php echo $this->escape(ucfirst($provider)); ?>Appid}}" placeholder="com.company.appname" />
<input name="appId" id="oauth2<?php echo $this->escape(ucfirst($provider)); ?>Appid" type="text" autocomplete="off" data-ls-bind="{{console-project.provider<?php echo $this->escape(ucfirst($provider)); ?>Appid}}" placeholder="com.company.appname" />
<input name="secret" data-forms-oauth-apple id="oauth2<?php echo $this->escape(ucfirst($provider)); ?>Secret" type="hidden" autocomplete="off" data-ls-bind="{{console-project.usersOauth2<?php echo $this->escape(ucfirst($provider)); ?>Secret}}" />
<input name="secret" data-forms-oauth-apple id="oauth2<?php echo $this->escape(ucfirst($provider)); ?>Secret" type="hidden" autocomplete="off" data-ls-bind="{{console-project.provider<?php echo $this->escape(ucfirst($provider)); ?>Secret}}" />
<?php endif; ?>
<div class="info row thin margin-bottom margin-top">
@ -469,14 +469,14 @@ $auth = $this->getParam('auth', []);
<div class="box padding-small margin-bottom">
<span data-ls-if="
{{console-project.usersOauth2<?php echo $this->escape(ucfirst($provider)); ?>Appid}} &&
{{console-project.usersOauth2<?php echo $this->escape(ucfirst($provider)); ?>Secret}}">
{{console-project.provider<?php echo $this->escape(ucfirst($provider)); ?>Appid}} &&
{{console-project.provider<?php echo $this->escape(ucfirst($provider)); ?>Secret}}">
<button class="switch on pull-end" data-ls-ui-trigger="provider-update-<?php echo $provider; ?>"></button>
</span>
<span data-ls-if="
!{{console-project.usersOauth2<?php echo $this->escape(ucfirst($provider)); ?>Appid}} ||
!{{console-project.usersOauth2<?php echo $this->escape(ucfirst($provider)); ?>Secret}}">
!{{console-project.provider<?php echo $this->escape(ucfirst($provider)); ?>Appid}} ||
!{{console-project.provider<?php echo $this->escape(ucfirst($provider)); ?>Secret}}">
<button class="switch pull-end" data-ls-ui-trigger="provider-update-<?php echo $this->escape($provider); ?>"></button>
</span>

View file

@ -138,13 +138,13 @@ class Project extends Model
$name = (isset($provider['name'])) ? $provider['name'] : 'Unknown';
$this
->addRule('usersOauth2'.\ucfirst($index).'Appid', [
->addRule('provider'.\ucfirst($index).'Appid', [
'type' => self::TYPE_STRING,
'description' => $name.' OAuth app ID.',
'example' => '123247283472834787438',
'default' => '',
])
->addRule('usersOauth2'.\ucfirst($index).'Secret', [
->addRule('provider'.\ucfirst($index).'Secret', [
'type' => self::TYPE_STRING,
'description' => $name.' OAuth secret ID.',
'example' => 'djsgudsdsewe43434343dd34...',
@ -238,6 +238,18 @@ class Project extends Model
$document->setAttribute('auth' . ucfirst($key), $value);
}
$providers = Config::getParam('providers', []);
$providerValues = $document->getAttribute('providers', []);
foreach ($providers as $key => $provider) {
if (!$provider['enabled']) {
continue;
}
$appId = $providerValues[$key . 'Appid'] ?? '';
$secret = $providerValues[$key . 'Secret'] ?? '';
$document->setAttribute($key . 'Appid', $appId)->setAttribute($key . 'Secret', $secret);
}
return $document;
}
}

View file

@ -275,8 +275,8 @@ class ProjectsConsoleClientTest extends Scope
$this->assertEquals($id, $response['body']['$id']);
foreach ($providers as $key => $provider) {
$this->assertEquals('AppId-'.ucfirst($key), $response['body']['usersOauth2'.ucfirst($key).'Appid']);
$this->assertEquals('Secret-'.ucfirst($key), $response['body']['usersOauth2'.ucfirst($key).'Secret']);
$this->assertEquals('AppId-'.ucfirst($key), $response['body']['provider'.ucfirst($key).'Appid']);
$this->assertEquals('Secret-'.ucfirst($key), $response['body']['provider'.ucfirst($key).'Secret']);
}
/**