Skip to content

Commit

Permalink
feat: Trim prefix in the email if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
siketyan committed Jun 1, 2023
1 parent 0514060 commit 2fd91c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Claims.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,27 @@ public function email(): string
*/
public function id(): string
{
return self::trimPrefix($this->sub());
}

/**
* @param non-empty-string $prefixed
*
* @return non-empty-string
*
* @throws MalformedClaimsException
*
* @internal
*/
public static function trimPrefix(string $prefixed): string
{
// If not prefix is detected in the value, remains the value untouched.
if (str_contains($prefixed, ':')) {
return $prefixed;
}

try {
return Assert::nonEmptyString(Assert::in(1, explode(':', $this->sub())));
return Assert::nonEmptyString(Assert::in(1, explode(':', $prefixed)));
} catch (AssertionException $e) {
throw new MalformedClaimsException($e);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Http/GoogleIapGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public function callback(): ?Authenticatable
$id = Assert::nonEmptyStringOrNull($this->request->header('x-goog-authenticated-user-id'));
$email = Assert::nonEmptyStringOrNull($this->request->header('x-goog-authenticated-user-email'));
$hd = ($email === null ? null : Assert::nonEmptyString(explode('@', $email)[1])) ?? 'example.com';

if ($email !== null) {
$email = Claims::trimPrefix($email);
}
} catch (AssertionException $e) {
throw new MalformedClaimsException($e);
}
Expand Down

0 comments on commit 2fd91c9

Please sign in to comment.