Skip to content

Commit

Permalink
feat(oauth2): simple userinfo endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: d.kudrinskiy <hardviper@icloud.com>
  • Loading branch information
hardviper committed Feb 20, 2024
1 parent f1fe3be commit f5b90e2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apps/oauth2/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@
'url' => '/api/v1/token',
'verb' => 'POST'
],
[
'name' => 'OauthApi#getUserInfo',
'url' => '/api/v1/userinfo',
'verb' => 'GET'
],
],
];
19 changes: 19 additions & 0 deletions apps/oauth2/lib/Controller/OauthApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;
use OCP\IUserSession;

class OauthApiController extends Controller {
// the authorization code expires after 10 minutes
Expand All @@ -62,6 +63,7 @@ public function __construct(
private LoggerInterface $logger,
private IThrottler $throttler,
private ITimeFactory $timeFactory,
private IUserSession $userSession,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -226,4 +228,21 @@ public function getToken(
]
);
}

/**
* @PublicPage
* @NoCSRFRequired
*
* @return JSONResponse
*/
public function getUserInfo() {
$user = $this->userSession->getUser();
$displayname = explode(' ', $user->getDisplayName());
return new JSONResponse([
'sub' => $user->getUID(),
'given_name' => $displayname[0],
'family_name' => $displayname[1] ? $displayname[1] : $displayname[0],
'email' => $user->getEMailAddress()
]);
}
}

0 comments on commit f5b90e2

Please sign in to comment.