Skip to content

Commit f5b90e2

Browse files
committed
feat(oauth2): simple userinfo endpoint
Signed-off-by: d.kudrinskiy <hardviper@icloud.com>
1 parent f1fe3be commit f5b90e2

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

apps/oauth2/appinfo/routes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,10 @@
4343
'url' => '/api/v1/token',
4444
'verb' => 'POST'
4545
],
46+
[
47+
'name' => 'OauthApi#getUserInfo',
48+
'url' => '/api/v1/userinfo',
49+
'verb' => 'GET'
50+
],
4651
],
4752
];

apps/oauth2/lib/Controller/OauthApiController.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use OCP\Security\ICrypto;
4646
use OCP\Security\ISecureRandom;
4747
use Psr\Log\LoggerInterface;
48+
use OCP\IUserSession;
4849

4950
class OauthApiController extends Controller {
5051
// the authorization code expires after 10 minutes
@@ -62,6 +63,7 @@ public function __construct(
6263
private LoggerInterface $logger,
6364
private IThrottler $throttler,
6465
private ITimeFactory $timeFactory,
66+
private IUserSession $userSession,
6567
) {
6668
parent::__construct($appName, $request);
6769
}
@@ -226,4 +228,21 @@ public function getToken(
226228
]
227229
);
228230
}
231+
232+
/**
233+
* @PublicPage
234+
* @NoCSRFRequired
235+
*
236+
* @return JSONResponse
237+
*/
238+
public function getUserInfo() {
239+
$user = $this->userSession->getUser();
240+
$displayname = explode(' ', $user->getDisplayName());
241+
return new JSONResponse([
242+
'sub' => $user->getUID(),
243+
'given_name' => $displayname[0],
244+
'family_name' => $displayname[1] ? $displayname[1] : $displayname[0],
245+
'email' => $user->getEMailAddress()
246+
]);
247+
}
229248
}

0 commit comments

Comments
 (0)