public static Token request(final String tokenUrl, final TokenRequest tokenRequest) throws Exception { if (tokenRequest.getGrantType() != GrantType.CLIENT_CREDENTIALS) { return null; } TokenClient tokenClient = new TokenClient(tokenUrl); tokenClient.setRequest(tokenRequest); TokenResponse response = tokenClient.exec(); if (response.getStatus() == 200) { final String patToken = response.getAccessToken(); final Integer expiresIn = response.getExpiresIn(); if (Util.allNotBlank(patToken)) { return new Token(null, null, patToken, response.getScope(), expiresIn); } } return null; }
/** * <p> * Executes the call to the REST Service requesting the authorization and * processes the response. * </p> * <p> * The client can request an access token using only its client credentials * when the client is requesting access to the protected resources under its * control, or those of another resource owner which has been previously * arranged with the authorization server. The client credentials grant type * must only be used by confidential clients. * </p> * * @param scope The scope of the access request. This parameter is optional. * @param clientId The client identifier. * @param clientSecret The client secret. * @return The token response. */ public TokenResponse execClientCredentialsGrant( String scope, String clientId, String clientSecret) { setRequest(new TokenRequest(GrantType.CLIENT_CREDENTIALS)); getRequest().setScope(scope); getRequest().setAuthUsername(clientId); getRequest().setAuthPassword(clientSecret); return exec(); }
/** * <p> * Executes the call to the REST Service requesting the authorization and * processes the response. * </p> * <p> * The client uses an extension grant type by specifying the grant type * using an absolute URI (defined by the authorization server) as the value * of the grant_type parameter of the token endpoint, and by adding any * additional parameters necessary. * </p> * * @param grantTypeUri Absolute URI. * @param assertion Assertion grant type. * @param clientId The client identifier. * @param clientSecret The client secret. * @return The token response. */ public TokenResponse execExtensionGrant(String grantTypeUri, String assertion, String clientId, String clientSecret) { GrantType grantType = GrantType.fromString(grantTypeUri); setRequest(new TokenRequest(grantType)); getRequest().setAssertion(assertion); getRequest().setAuthUsername(clientId); getRequest().setAuthPassword(clientSecret); return exec(); }
@Parameters({"userId", "userSecret"}) @Test(dependsOnMethods = "requestClientRegistrationWithCustomAttributes") public void requestAccessTokenCustomClientAuth2(final String userId, final String userSecret) throws Exception { showTitle("requestAccessTokenCustomClientAuth2"); String username = userId; String password = userSecret; TokenRequest tokenRequest = new TokenRequest(GrantType.RESOURCE_OWNER_PASSWORD_CREDENTIALS); tokenRequest.setUsername(username); tokenRequest.setPassword(password); tokenRequest.setAuthenticationMethod(AuthenticationMethod.CLIENT_SECRET_POST); tokenRequest.addCustomParameter("myCustomAttr1", customAttrValue1); TokenClient tokenClient = new TokenClient(tokenEndpoint); tokenClient.setRequest(tokenRequest); TokenResponse tokenResponse = tokenClient.exec(); showClient(tokenClient); assertEquals(tokenResponse.getStatus(), 200, "Unexpected response code: " + tokenResponse.getStatus()); assertNotNull(tokenResponse.getEntity(), "The entity is null"); assertNotNull(tokenResponse.getAccessToken(), "The access token is null"); assertNotNull(tokenResponse.getTokenType(), "The token type is null"); assertNotNull(tokenResponse.getRefreshToken(), "The refresh token is null"); } }
/** * <p> * Executes the call to the REST Service requesting the authorization and * processes the response. * </p> * <p> * If the authorization server issued a refresh token to the client, the * client can make a request to the token endpoint for a new access token. * </p> * * @param scope The scope of the access request. This value is optional. * @param refreshToken The refresh token issued to the client. This value is * required. * @param clientId The client identifier. * @param clientSecret The client secret. * @return The token response. */ public TokenResponse execRefreshToken(String scope, String refreshToken, String clientId, String clientSecret) { setRequest(new TokenRequest(GrantType.REFRESH_TOKEN)); getRequest().setScope(scope); getRequest().setRefreshToken(refreshToken); getRequest().setAuthUsername(clientId); getRequest().setAuthPassword(clientSecret); return exec(); }
public void exec() { try { TokenRequest request = new TokenRequest(grantType); request.setAuthUsername(clientId); request.setAuthPassword(clientSecret); request.setCode(code); request.setRedirectUri(redirectUri); request.setUsername(username); request.setPassword(password); request.setScope(scope); request.setAssertion(assertion); request.setRefreshToken(refreshToken); request.setAuthenticationMethod(authenticationMethod); if (authenticationMethod.equals(AuthenticationMethod.CLIENT_SECRET_JWT)) { request.setAudience(tokenEndpoint); } TokenClient client = new TokenClient(tokenEndpoint); client.setRequest(request); TokenResponse response = client.exec(); if (response.getStatus() == 200) { userInfoAction.setAccessToken(response.getAccessToken()); } showResults = true; requestString = client.getRequestAsString(); responseString = client.getResponseAsString(); } catch (Exception e) { log.error(e.getMessage(), e); } }
TokenResponse tokenResponse = tokenClient.exec();
TokenResponse tokenResponse = tokenClient.exec();
TokenResponse tokenResponse = tokenClient.exec();
TokenResponse tokenResponse = tokenClient.exec();
TokenResponse tokenResponse = tokenClient.exec();
getRequest().setAuthPassword(clientSecret); return exec();
TokenResponse response1 = tokenClient.exec();
TokenResponse response1 = tokenClient.exec();
TokenResponse tokenResponse = tokenClient.exec();
TokenResponse tokenResponse = tokenClient.exec();
TokenResponse response1 = tokenClient.exec();
TokenResponse tokenResponse = tokenClient.exec();
TokenResponse tokenResponse = tokenClient.exec();
TokenResponse tokenResponse = tokenClient.exec();