diff --git a/apps/oauth2/appinfo/routes.php b/apps/oauth2/appinfo/routes.php index 55b3c5bc7ff1e..96817bb6a3879 100644 --- a/apps/oauth2/appinfo/routes.php +++ b/apps/oauth2/appinfo/routes.php @@ -44,5 +44,15 @@ 'url' => '/api/v1/token', 'verb' => 'POST' ], + [ + 'name' => 'OauthApi#discovery', + 'url' => '/.well-known/openid-configuration', + 'verb' => 'GET', + ], + [ + 'name' => 'OauthApi#getUserInfo', + 'url' => '/api/v1/userinfo', + 'verb' => 'GET' + ], ], ]; diff --git a/apps/oauth2/lib/Controller/OauthApiController.php b/apps/oauth2/lib/Controller/OauthApiController.php index 6e12132ed0feb..888c0a0db8bd4 100644 --- a/apps/oauth2/lib/Controller/OauthApiController.php +++ b/apps/oauth2/lib/Controller/OauthApiController.php @@ -42,6 +42,9 @@ use OCP\IRequest; use OCP\Security\ICrypto; use OCP\Security\ISecureRandom; +use OCP\Util; +use OCP\IURLGenerator; +use OCP\IUserSession; class OauthApiController extends Controller { /** @var AccessTokenMapper */ @@ -58,6 +61,10 @@ class OauthApiController extends Controller { private $time; /** @var Throttler */ private $throttler; + /** @var IUserSession */ + private $userSession; + /** @var IUrlGenerator */ + private $urlGenerator; public function __construct(string $appName, IRequest $request, @@ -67,7 +74,9 @@ public function __construct(string $appName, TokenProvider $tokenProvider, ISecureRandom $secureRandom, ITimeFactory $time, - Throttler $throttler) { + Throttler $throttler, + IUserSession $userSession, + IURLGenerator $urlGenerator) { parent::__construct($appName, $request); $this->crypto = $crypto; $this->accessTokenMapper = $accessTokenMapper; @@ -76,6 +85,8 @@ public function __construct(string $appName, $this->secureRandom = $secureRandom; $this->time = $time; $this->throttler = $throttler; + $this->userSession = $userSession; + $this->urlGenerator = $urlGenerator; } /** @@ -177,4 +188,35 @@ public function getToken($grant_type, $code, $refresh_token, $client_id, $client ] ); } + + /** + * @PublicPage + * @NoCSRFRequired + * + * @return JSONResponse + */ + public function discovery() { + $util = new Util(); + return new JSONResponse([ + 'issuer' => $this->urlGenerator->linkToRouteAbsolute(''), + 'authorization_endpoint' => $this->urlGenerator->linkToRouteAbsolute('oauth2.LoginRedirector.authorize'), + 'token_endpoint' => $this->urlGenerator->linkToRouteAbsolute('oauth2.OauthApi.getToken'), + 'userinfo_endpoint' => $this->urlGenerator->linkToRouteAbsolute('oauth2.OauthApi.getUserInfo') + ]); + } + + /** + * @PublicPage + * @NoCSRFRequired + * + * @return JSONResponse + */ + public function getUserInfo() { + $user = $this->userSession->getUser(); + return new JSONResponse([ + 'sub' => $user->getUID(), + 'name' => $user->getDisplayName(), + 'email' => $user->getEMailAddress() + ]); + } }