Skip to content

Commit de0f7af

Browse files
committed
Introduce data legacy mode
Uses Galette data without reworking it per default. Add a word in README file on upgrading to this BC breaking release
1 parent 1b3b1c0 commit de0f7af

6 files changed

Lines changed: 70 additions & 27 deletions

File tree

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ cd plugin-oauth2
1010
composer install
1111
```
1212

13+
# Updating to version 3.0.0
14+
15+
Before updating to version 3.0.0, please take care of the following:
16+
- the existing `options` entry in configuration file has been renamed to `authorize`. Please update your configuration file accordingly.
17+
- the `scopes` entry in configuration file has been added; some data you were previously using may be missing.
18+
- previous versions were using non Galette data (like `username`). If you were using this data and still want to rely on them; add a `legacy_data: true` in you applications entries.
19+
1320
# Configuration
1421

1522
## Prepare public/private keys
@@ -27,7 +34,7 @@ copy-paste the hexadecimal string result in plugin-oauth2/config/encryption-key.
2734

2835
## Configure a ClientEntity
2936

30-
Rename `config/config.yml.dist` to `config/config.yml` and edit according to your third party applicaiton settings:
37+
Rename `config/config.yml.dist` to `config/config.yml` and edit according to your third party application settings:
3138

3239
```
3340
global:
@@ -52,7 +59,6 @@ The corresponding NextCloud configuration:
5259

5360
![Nextcloud configuration example](examples/nextcloud.png)
5461

55-
5662
### Available authorizations:
5763

5864
* `uptodate`: only active and up-to-date members can login

lib/GaletteOAuth2/Authorization/UserHelper.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,15 @@ public static function logout(Container $container): void
106106
* @param int $id User ID
107107
* @param string $acl Requested authorization
108108
* @param array|string $scopes Scopes
109+
* @param bool $legacy Legacy mode for data
110+
*
109111
* @return array
110112
* @throws UserAuthorizationException
111113
* @throws \DI\DependencyException
112114
* @throws \DI\NotFoundException
113115
* @throws \Throwable
114116
*/
115-
public static function getUserData(Container $container, int $id, string $acl, array|string $scopes): array
117+
public static function getUserData(Container $container, int $id, string $acl, array|string $scopes, bool $legacy = false): array
116118
{
117119
/** @var Db $zdb */
118120
$zdb = $container->get('zdb');
@@ -160,25 +162,29 @@ public static function getUserData(Container $container, int $id, string $acl, a
160162
);
161163
}
162164

163-
//FIXME: I really doubt reworking names is a good idea outside a specific usage
164-
$nameExplode = preg_split('/[\\s,-]+/', $member->name);
165-
if (count($nameExplode) > 0) {
166-
$nameFPart = $nameExplode[0];
167-
//too short?
168-
if (mb_strlen($nameFPart) < 4 && count($nameExplode) > 1) {
169-
$nameFPart .= $nameExplode[1];
165+
$login = $member->login;
166+
167+
if ($legacy === true) {
168+
//FIXME: I really doubt reworking names is a good idea outside a specific usage
169+
$nameExplode = preg_split('/[\\s,-]+/', $member->name);
170+
if (count($nameExplode) > 0) {
171+
$nameFPart = $nameExplode[0];
172+
//too short?
173+
if (mb_strlen($nameFPart) < 4 && count($nameExplode) > 1) {
174+
$nameFPart .= $nameExplode[1];
175+
}
176+
} else {
177+
$nameFPart = $member->name;
170178
}
171-
} else {
172-
$nameFPart = $member->name;
173-
}
174179

175-
//Normalized format s.name (example mail usage : s.name@xxxx.xx )
176-
//FIXME: why don't use email directly?
177-
$norm_login = sprintf(
178-
'%s.%s',
179-
mb_substr(self::stripAccents($member->surname), 0, 1),
180-
self::stripAccents($nameFPart)
181-
);
180+
//Normalized format s.name (example mail usage : s.name@xxxx.xx )
181+
//FIXME: why don't use email directly?
182+
$login = sprintf(
183+
'%s.%s',
184+
mb_substr(self::stripAccents($member->surname), 0, 1),
185+
self::stripAccents($nameFPart)
186+
);
187+
}
182188

183189
//FIXME: be compliant with OpenID-Connect (see https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims)
184190
$oauth_data = [
@@ -187,8 +193,8 @@ public static function getUserData(Container $container, int $id, string $acl, a
187193
'identifier' => $member->id, //nextcloud
188194
'name' => $member->sfullname, //OpenID-Connect
189195
'displayName' => $member->sname,
190-
'username' => $norm_login, //FIXME: $member->login,
191-
'userName' => $norm_login, //FIXME: $member->login,
196+
'username' => $login,
197+
'userName' => $login,
192198
'email' => $member->email,
193199
'mail' => $member->email,
194200
'locale' => $member->language, //OpenID-Connect

lib/GaletteOAuth2/Controllers/ApiController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public function user(Request $request, Response $response): Response
8585
$this->config,
8686
$client_id,
8787
$rep->getAttribute('oauth_scopes')
88-
)
88+
),
89+
(bool)$this->config->get($client_id . '.legacy_data', false)
8990
);
9091
} catch (UserAuthorizationException $e) {
9192
UserHelper::logout($this->container);

lib/GaletteOAuth2/Controllers/LoginController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ public function login(Request $request, Response $response): Response
130130
$client_id,
131131
$this->session->request_args['scope'] ?? [],
132132
true
133-
)
133+
),
134+
(bool)$this->config->get($client_id . '.legacy_data', false)
134135
);
135136
} catch (UserAuthorizationException $e) {
136137
UserHelper::logout($this->container);

tests/GaletteOAuth2/Authorization/tests/units/UserHelper.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,13 @@ public function testGetUserData(): void
9191
$store = $this->adh->store();
9292
$this->assertTrue($store);
9393

94-
//test for default scope
94+
//test for default scope - legacy data mode
9595
$user_data = \GaletteOAuth2\Authorization\UserHelper::getUserData(
9696
$container,
9797
$adh1->id,
9898
'',
99-
['member']
99+
['member'],
100+
true
100101
);
101102

102103
$expected_base = [
@@ -119,6 +120,34 @@ public function testGetUserData(): void
119120
$user_data
120121
);
121122

123+
//test for default scope
124+
$user_data = \GaletteOAuth2\Authorization\UserHelper::getUserData(
125+
$container,
126+
$adh1->id,
127+
'',
128+
['member']
129+
);
130+
131+
$expected_base = [
132+
'id' => $adh1->id,
133+
'sub' => $adh1->id,
134+
'identifier' => $adh1->id,
135+
'name' => $adh1->sfullname,
136+
'displayName' => $adh1->sname,
137+
'username' => $adh1->login,
138+
'userName' => $adh1->login,
139+
'email' => $adh1->email,
140+
'mail' => $adh1->email,
141+
'locale' => $adh1->language,
142+
'language' => $adh1->language,
143+
'status' => $adh1->status,
144+
];
145+
146+
$this->assertSame(
147+
$expected_base,
148+
$user_data
149+
);
150+
122151
//test personal scope
123152
$user_data = \GaletteOAuth2\Authorization\UserHelper::getUserData(
124153
$container,

tests/GaletteOAuth2/GaletteOAuth2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function testFlow(): void
151151

152152
//check values
153153
$this->assertSame($adh1->id, $resourceOwner->getId());
154-
$this->assertSame('r.durand', $resourceOwner->getUsername()); //not a Galette data
154+
$this->assertSame($data['login_adh'], $resourceOwner->getUsername());
155155
$this->assertSame($data['email_adh'], $resourceOwner->getEmail());
156156
//due date scope is requested from configuration file
157157
$this->assertArrayHasKey('due_date', $resourceOwner_array);

0 commit comments

Comments
 (0)