55use Psr \Http \Message \ResponseInterface ;
66use Psr \Http \Message \ServerRequestInterface ;
77
8- class AuthorizeController extends AbstractController
8+ class AuthorizeController extends ServerController
99{
1010 final public function __invoke (ServerRequestInterface $ request , array $ args ): ResponseInterface
1111 {
12- $ httpHost = $ request ->getServerParams ()['HTTP_HOST ' ];
13-
14- // // Create a request
15- // if (!$this->userManager->userExists($this->userId)) {
16- // $result = new JSONResponse('Authorization required');
17- // $result->setStatus(401);
18- // return $result;
19- // }
20-
21- $ parser = new \Lcobucci \JWT \Parser ();
22- $ token = $ parser ->parse ($ _GET ['request ' ]);
23- $ _SESSION ['token ' ] = $ token ;
12+ if (!isset ($ _SESSION ['userid ' ])) {
13+ $ response = $ this ->getResponse ();
14+ $ response = $ response ->withStatus (302 , "Approval required " );
2415
25- $ user = new \Pdsinterop \Solid \Auth \Entity \User ();
26- $ user ->setIdentifier ('https://server/profile/card#me ' );
27-
28- $ getVars = $ _GET ;
29- if (!isset ($ getVars ['grant_type ' ])) {
30- $ getVars ['grant_type ' ] = 'implicit ' ;
31- }
32- $ getVars ['response_type ' ] = 'token ' ;
33- $ getVars ['scope ' ] = "openid " ;
34-
35- if (!isset ($ getVars ['redirect_uri ' ])) {
36- $ getVars ['redirect_uri ' ] = 'https://solid.community/.well-known/solid/login ' ;
37- }
38- $ request = \Laminas \Diactoros \ServerRequestFactory::fromGlobals ($ _SERVER , $ getVars , $ _POST , $ _COOKIE , $ _FILES );
39- $ response = new \Laminas \Diactoros \Response ();
40- $ server = new \Pdsinterop \Solid \Auth \Server ($ this ->authServerFactory , $ this ->authServerConfig , $ response );
41-
42- // if (!$this->checkApproval()) {
43- // $result = new JSONResponse('Approval required');
44- // $result->setStatus(302);
45- // $result->addHeader("Location", $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("solid.server.sharing")));
46- // return $result;
47- // }
48-
49- // FIXME: check if the user has approved - if not, show approval screen;
50- $ approval = \Pdsinterop \Solid \Auth \Enum \Authorization::APPROVED ;
51- // $approval = false;
52- return $ server ->respondToAuthorizationRequest ($ request , $ user , $ approval );
53- }
54- }
16+ // FIXME: Generate a proper url for this;
17+ $ loginUrl = "https://localhost/login/?returnUrl= " . urlencode ($ _SERVER ['REQUEST_URI ' ]);
18+ $ response = $ response ->withHeader ("Location " , $ loginUrl );
19+ return $ response ;
20+ }
21+ $ parser = new \Lcobucci \JWT \Parser ();
22+
23+ try {
24+ $ token = $ parser ->parse ($ request ->getQueryParams ()['request ' ]);
25+ $ _SESSION ["nonce " ] = $ token ->getClaim ('nonce ' );
26+ } catch (\Exception $ e ) {
27+ $ _SESSION ["nonce " ] = $ request ->getQueryParams ()['nonce ' ];
28+ }
29+
30+ $ getVars = $ request ->getQueryParams ();
31+ if (!isset ($ getVars ['grant_type ' ])) {
32+ $ getVars ['grant_type ' ] = 'implicit ' ;
33+ }
34+ $ getVars ['response_type ' ] = $ this ->getResponseType ();
35+ $ getVars ['scope ' ] = "openid " ;
36+
37+ if (!isset ($ getVars ['redirect_uri ' ])) {
38+ try {
39+ $ getVars ['redirect_uri ' ] = $ token ->getClaim ("redirect_uri " );
40+ } catch (\Exception $ e ) {
41+ $ response = $ this ->getResponse ();
42+ $ response ->withStatus (400 , "Bad request, missing redirect uri " );
43+ return $ response ;
44+ }
45+ }
46+ $ clientId = $ getVars ['client_id ' ];
47+ $ approval = $ this ->checkApproval ($ clientId );
48+ if (!$ approval ) {
49+ $ response = $ this ->getResponse ();
50+ $ response = $ response ->withStatus (302 , "Approval required " );
51+
52+ // FIXME: Generate a proper url for this;
53+ $ approvalUrl = "https://localhost/sharing/ $ clientId/?returnUrl= " . urlencode ($ _SERVER ['REQUEST_URI ' ]);
54+ $ response = $ response ->withHeader ("Location " , $ approvalUrl );
55+ return $ response ;
56+ }
57+
58+ $ user = new \Pdsinterop \Solid \Auth \Entity \User ();
59+ $ user ->setIdentifier ($ this ->getProfilePage ());
60+
61+ $ request = $ request ->withQueryParams ($ getVars ); // replace the request getVars with the morphed version;
62+ $ response = new \Laminas \Diactoros \Response ();
63+ $ server = new \Pdsinterop \Solid \Auth \Server ($ this ->authServerFactory , $ this ->authServerConfig , $ response );
64+
65+ $ response = $ server ->respondToAuthorizationRequest ($ request , $ user , $ approval );
66+ $ response = $ this ->tokenGenerator ->addIdTokenToResponse ($ response , $ clientId , $ this ->getProfilePage (), $ _SESSION ['nonce ' ], $ this ->config ->getPrivateKey ());
67+ return $ response ;
68+ }
69+ }
0 commit comments