33import com .nimbusds .oauth2 .sdk .*;
44import com .nimbusds .oauth2 .sdk .http .HTTPRequest ;
55import com .nimbusds .oauth2 .sdk .http .HTTPResponse ;
6+ import com .nimbusds .oauth2 .sdk .id .Audience ;
67import com .nimbusds .oauth2 .sdk .id .ClientID ;
78import com .nimbusds .oauth2 .sdk .id .State ;
89import com .nimbusds .oauth2 .sdk .pkce .CodeChallengeMethod ;
2728import java .util .concurrent .TimeUnit ;
2829
2930public class InteractiveLogin {
31+ public final static String authClientID = "cjoWhd7W8A8znf7Z7vizyvKJCiqTgRtf" ;
32+ public final static String authDomain = "https://dev-dzhx7i2db3x3kkvq.us.auth0.com" ;
33+
34+
3035
3136 private InteractiveLogin () {
3237 }
3338
39+ public static AuthApiClient login () throws URISyntaxException , IOException , ParseException {
40+ return login (new URI (authDomain ), new URI ("https://vcell.cam.uchc.edu" ), false );
41+ }
42+
3443 /**
3544 * 1. Goes to the authorization server, gather metadata about it's OIDC configuration
3645 * (ex. Scopes supported, signing methods supported, auth and token endpoint, response types...)
@@ -50,7 +59,6 @@ private InteractiveLogin() {
5059 * <br>
5160 * P.S: This HTTP client created has an automated refresh capability for the access token, allowing users to stay logged in
5261 * for an extended period of time.
53- * @param clientID
5462 * @param authServerUri
5563 * @param apiBaseUri
5664 * @param ignoreSSLCertProblems
@@ -60,7 +68,7 @@ private InteractiveLogin() {
6068 * @throws ParseException
6169 */
6270
63- public static AuthApiClient login (String clientID , URI authServerUri , URI apiBaseUri , boolean ignoreSSLCertProblems ) throws URISyntaxException , IOException , ParseException {
71+ public static AuthApiClient login (URI authServerUri , URI apiBaseUri , boolean ignoreSSLCertProblems ) throws URISyntaxException , IOException , ParseException {
6472 URI successRedirectURI = new URI (apiBaseUri +( apiBaseUri .getHost ().equals ("localhost" )? "" : "/login_success" ));
6573
6674 // Retrieve OpenID Provider Metadata
@@ -93,9 +101,10 @@ public static AuthApiClient login(String clientID, URI authServerUri, URI apiBas
93101 String callback_endpoint_path = "/oidc_test_callback" ;
94102
95103 URI redirectURI = new URI ("http://" + "localhost" + ":" + localHttpServerPort + callback_endpoint_path );
96- Scope scope = new Scope ("openid" , "email" , "profile" ); //, "email"); //, "profile", "offline_access");
104+ Scope scope = new Scope ("openid" , "email" , "profile" , "offline_access" ); //, "email"); //, "profile", "offline_access");
105+
97106 CodeVerifier codeVerifier = new CodeVerifier ();
98- URI authRequestURI = getAuthRequestURI (oidcProviderMetadata , redirectURI , new ClientID (clientID ), scope , state , codeVerifier );
107+ URI authRequestURI = getAuthRequestURI (oidcProviderMetadata , redirectURI , new ClientID (authClientID ), scope , state , codeVerifier , apiBaseUri );
99108
100109 final AuthorizationResponse authorizationResponse ;
101110 if (Desktop .isDesktopSupported () && Desktop .getDesktop ().isSupported (Desktop .Action .BROWSE )) {
@@ -120,13 +129,9 @@ public static AuthApiClient login(String clientID, URI authServerUri, URI apiBas
120129 authorizationResponse = getAuthorizationResponseManual (authRequestURI );
121130 }
122131
123- OIDCTokens oidcTokens = exchangeCodeForTokens (authorizationResponse , oidcProviderMetadata .getTokenEndpointURI (), new ClientID (clientID ), scope , redirectURI , codeVerifier );
124- String accessToken = oidcTokens .getAccessToken ().getValue ();
125- String idToken = oidcTokens .getIDTokenString ();
132+ OIDCTokens oidcTokens = exchangeCodeForTokens (authorizationResponse , oidcProviderMetadata .getTokenEndpointURI (), new ClientID (authClientID ), scope , redirectURI , codeVerifier );
126133
127- AuthApiClient authApiClient = new AuthApiClient (apiBaseUri , oidcProviderMetadata .getTokenEndpointURI (), oidcTokens .getAccessToken (), oidcTokens .getRefreshToken (), ignoreSSLCertProblems );
128- authApiClient .setRequestInterceptor (request -> request .header ("Authorization" , "Bearer " + idToken ));
129- return authApiClient ;
134+ return new AuthApiClient (apiBaseUri , oidcProviderMetadata .getTokenEndpointURI (), oidcTokens .getAccessToken (), oidcTokens .getRefreshToken (), ignoreSSLCertProblems );
130135 }
131136
132137 static int findAvailablePort (List <Integer > potentialPorts ) {
@@ -275,13 +280,16 @@ private static void pingHttpServer(URL url, String expectedPingResponse) throws
275280 * @throws IOException
276281 * @throws ParseException
277282 */
278- private static URI getAuthRequestURI (OIDCProviderMetadata oidcProviderMetadata , URI redirectURI , ClientID clientID , Scope scope , State state , CodeVerifier codeVerifier ) throws URISyntaxException , IOException , ParseException {
283+ private static URI getAuthRequestURI (OIDCProviderMetadata oidcProviderMetadata , URI redirectURI , ClientID clientID ,
284+ Scope scope , State state , CodeVerifier codeVerifier , URI audience ) throws URISyntaxException , IOException , ParseException {
279285 // Create the authorization request
280286 URI authorizationEndpoint = oidcProviderMetadata .getAuthorizationEndpointURI ();
281287
288+ String audiencePort = audience .getHost ().equals ("localhost" ) ? ":" + audience .getPort () : "" ;
282289 var authorizationRequest = new AuthorizationRequest .Builder (new ResponseType ("code" ), clientID )
283290 .endpointURI (authorizationEndpoint )
284291 .redirectionURI (redirectURI )
292+ .customParameter ("audience" , audience .getScheme () + "://" + audience .getHost () + audiencePort )
285293 .state (state )
286294 .scope (scope ) // Add any other required scopes
287295 .codeChallenge (codeVerifier , CodeChallengeMethod .S256 )
0 commit comments