11<?php
22
3- require __DIR__ . "/../../config/bootstrap.php " ;
3+ require_once __DIR__ . "/../../config/bootstrap.php " ;
44
5+ use League \OAuth2 \Client \Token \AccessToken ;
56use OpenVRE \LoggerFactory ;
67use OpenVRE \Oauth2Provider ;
78
@@ -18,15 +19,43 @@ function getLoginLogger()
1819}
1920
2021
21- // Setting auth server
22- $ provider = new Oauth2Provider (['redirectUri ' => $ GLOBALS ['URL ' ] . "applib/loginToken.php " ]);
22+ if (isset ($ _SERVER ['OIDC_access_token ' ])) {
23+ getLoginLogger ()->info ("Get OIDC claims. " );
24+ $ userInfo = [];
25+ foreach ($ _SERVER as $ key => $ value ) {
26+ if (strpos ($ key , 'OIDC_CLAIM_ ' ) === 0 ) {
27+ $ claim = substr ($ key , strlen ('OIDC_CLAIM_ ' ));
28+ $ userInfo [$ claim ] = $ value ;
29+ }
30+ }
2331
24- // Get auth code. Redirect user to the authorization URL
25- if (!isset ($ _GET ['code ' ])) {
32+ $ userToken = [];
33+ $ userToken ['access_token ' ] = $ _SERVER ['OIDC_access_token ' ];
34+ $ userToken ['expires ' ] = $ _SERVER ['OIDC_access_token_expires ' ];
35+ $ accessToken = new AccessToken ($ userToken );
2636
37+ $ user = getUserById (sanitizeString ($ _SERVER ['OIDC_CLAIM_email ' ]));
38+ if (is_null ($ user )) {
39+ try {
40+ $ user = createUserFromToken ($ _SERVER ['OIDC_CLAIM_email ' ], $ accessToken , $ userInfo , false );
41+ getLoginLogger ()->info ("Created new user from user access token. " );
42+ } catch (\Exception $ e ) {
43+ exit ('Login error: failed to create local VRE user: ' . $ e ->getMessage ());
44+ }
45+ }
46+
47+ $ user = loadUserWithToken ($ user , $ userInfo , $ accessToken );
48+ getLoginLogger ()->info ("Loaded existing user from access token. " );
49+
50+ if ($ user ) {
51+ redirect ("../home/redirect.php " );
52+ } else {
53+ redirect ($ GLOBALS ['URL ' ]);
54+ }
55+ } elseif (!isset ($ _GET ['code ' ])) {
56+ $ provider = new Oauth2Provider (['redirectUri ' => $ GLOBALS ['URL ' ] . "applib/loginToken.php " ]);
2757 // Fetch the authorization URL from the provider; returns urlAuthorize and generates state
2858 $ authorizationUrl = $ provider ->getAuthorizationUrl ();
29- getLoginLogger ()->info ("Redirect user to the authorization URL: " . $ authorizationUrl );
3059
3160 header ('Location: ' . $ authorizationUrl );
3261 exit ;
@@ -37,10 +66,9 @@ function getLoginLogger()
3766 if (isset ($ _SESSION ['oauth2state ' ])) {
3867 unset($ _SESSION ['oauth2state ' ]);
3968 }
40-
4169 exit ('Login error: invalid state. Start login process again, please. ' );
4270} else {
43-
71+ $ provider = new Oauth2Provider ([ ' redirectUri ' => $ GLOBALS [ ' URL ' ] . " applib/loginToken.php " ]);
4472 // Get an access token using the authorization code grant.
4573 try {
4674 $ accessToken = $ provider ->getAccessToken ('authorization_code ' , ['code ' => $ _GET ['code ' ]]);
@@ -65,35 +93,6 @@ function getLoginLogger()
6593 redirect ("../home/redirect.php " );
6694 }
6795
68- function base64UrlDecode ($ input )
69- {
70- $ remainder = strlen ($ input ) % 4 ;
71- if ($ remainder ) {
72- $ padlen = 4 - $ remainder ;
73- $ input .= str_repeat ('= ' , $ padlen );
74- }
75- return base64_decode (strtr ($ input , '-_ ' , '+/ ' ));
76- }
77-
78- $ _SESSION ['allowedDatasetIds ' ] = [];
79- if (isset ($ userInfo ['ga4gh_passport_v1 ' ])) {
80- $ gh4ghPassport = $ userInfo ['ga4gh_passport_v1 ' ];
81-
82- foreach ($ gh4ghPassport as $ gh4ghVisaJwt ) {
83- $ gh4ghVisaTokenParts = explode (". " , $ gh4ghVisaJwt );
84- $ gh4ghTokenHeader = base64UrlDecode ($ gh4ghVisaTokenParts [0 ]);
85- $ gh4ghTokenPayload = base64UrlDecode ($ gh4ghVisaTokenParts [1 ]);
86- $ gh4ghJwtHeader = json_decode ($ gh4ghTokenHeader );
87- $ gh4ghJwtPayload = json_decode ($ gh4ghTokenPayload );
88-
89- if ($ gh4ghJwtPayload ->ga4gh_visa_v1 ->type == "ControlledAccessGrants " ) {
90- array_push ($ _SESSION ['allowedDatasetIds ' ], $ gh4ghJwtPayload ->ga4gh_visa_v1 ->value );
91- }
92- }
93-
94- getLoginLogger ()->info ("GA4GH passport obtained from user access token and included into user session info. " );
95- }
96-
9796 // Check if user exists.
9897 $ user = getUserById (sanitizeString ($ userInfo ['email ' ]));
9998
0 commit comments