Skip to content

Commit bc1a535

Browse files
committed
refactoring
1 parent e5385c3 commit bc1a535

3 files changed

Lines changed: 60 additions & 20 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ $CONFIG = array (
7878
// This will only be effective if oidc_login_update_avatar is enabled.
7979
// * is_admin: If this value is truthy, the user is added to the admin group (optional)
8080
// * birthdate: Since attribute 'birthdate' is supported from NC version 30 onwards, this attribute
81-
// can be mapped too.
81+
// can be mapped too. Accepted format: YYYY-MM-DD
8282
//
8383
// The attributes in the OIDC response are flattened by adding the nested
8484
// array key as the prefix and an underscore. Thus,
@@ -112,7 +112,7 @@ $CONFIG = array (
112112
// https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
113113
//
114114
// note: on Keycloak, OIDC name claim = "${given_name} ${family_name}" or one of them if any is missing
115-
// note: for ID Austria, OIDC name claim = array('family_name', 'given_name')
115+
// note: for other IdPs providing given name and family name separately: OIDC name claim = array('family_name', 'given_name')
116116
//
117117
'oidc_login_attributes' => array (
118118
'id' => 'sub',
@@ -201,9 +201,9 @@ $CONFIG = array (
201201
// Enable use of WebDAV via OIDC bearer token.
202202
'oidc_login_webdav_enabled' => false,
203203

204-
// Enable removal of special characters in UID.
204+
// Enable removal of special characters in UID. Removal by converting to URL-safe Base64.
205205
// The default value is false.
206-
'oidc_login_allow_special_characters' => true,
206+
'oidc_login_remove_special_characters' => false,
207207

208208
// Enable authentication with user/password for DAV clients that do not
209209
// support token authentication (e.g. DAVx⁵)

lib/Service/AttributeMap.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,12 @@ public function __construct(IConfig $config)
8787
}
8888
}
8989

90-
/**
91-
* Function to remove unallowed characters.
92-
*
93-
* @param mixed $data
94-
*/
95-
public function base64url_encode($data): string
96-
{
97-
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
98-
}
99-
10090
/**
10191
* Get ID from profile.
10292
*/
10393
public function id(array $profile): ?string
10494
{
105-
if (true === $this->config->getSystemValue('oidc_login_allow_special_characters', false)) {
95+
if (true === $this->config->getSystemValue('oidc_login_remove_special_characters', false)) {
10696
return self::base64url_encode(self::get($this->_id, $profile));
10797
}
10898

@@ -231,6 +221,16 @@ public function managesAdmin(): bool
231221
return null !== $this->_isAdmin;
232222
}
233223

224+
/**
225+
* Function to remove unallowed characters.
226+
*
227+
* @param mixed $data
228+
*/
229+
private static function base64url_encode($data): string
230+
{
231+
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
232+
}
233+
234234
private static function get(string $attr, array $profile)
235235
{
236236
if (null !== $attr && \array_key_exists($attr, $profile)) {
@@ -249,4 +249,5 @@ private static function getFullDisplayName(array|string $attr, array $profile):
249249

250250
return implode(' ', $nameArr);
251251
}
252-
}
252+
253+
}

lib/Service/LoginService.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,19 @@ private function updateBasicProfile(IUser $user, array $profile): void
375375

376376
// Set Birthdate
377377
if (null !== ($birthdate = $this->attr->birthdate($profile))) {
378-
$account = $this->accountManager->getAccount($user);
379-
$account->setProperty(IAccountManager::PROPERTY_BIRTHDATE, $birthdate, IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED);
380-
$this->accountManager->updateAccount($account);
378+
$validateBirthdate = $this->validateBirthdate($birthdate);
379+
if (null !== $validateBirthdate) {
380+
$account = $this->accountManager->getAccount($user);
381+
$account->setProperty(
382+
IAccountManager::PROPERTY_BIRTHDATE,
383+
$validateBirthdate,
384+
IAccountManager::SCOPE_LOCAL,
385+
IAccountManager::NOT_VERIFIED
386+
);
387+
$this->accountManager->updateAccount($account);
388+
} else {
389+
$this->logger->debug("Skipping invalid birthdate for user: {$user->getUID()}");
390+
}
381391
}
382392

383393
// Set quota
@@ -544,4 +554,33 @@ private function flatten(array $array, string $prefix = ''): array
544554

545555
return $result;
546556
}
547-
}
557+
558+
/**
559+
* Validate and normalize birthdate according to OIDC spec.
560+
* Only accepts full dates in YYYY-MM-DD format.
561+
*/
562+
private function validateBirthdate(string $birthdate): ?string
563+
{
564+
$birthdate = trim($birthdate);
565+
566+
if (empty($birthdate)) {
567+
return null;
568+
}
569+
570+
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $birthdate)) {
571+
$this->logger->debug("Birthdate must be in YYYY-MM-DD format, got: {$birthdate}");
572+
573+
return null;
574+
}
575+
576+
$date = \DateTime::createFromFormat('Y-m-d', $birthdate);
577+
if (!$date || $date->format('Y-m-d') !== $birthdate) {
578+
$this->logger->debug("Invalid birthdate value: {$birthdate}");
579+
580+
return null;
581+
}
582+
583+
return $birthdate;
584+
}
585+
586+
}

0 commit comments

Comments
 (0)