use of org.keycloak.representations.AccessTokenResponse in project keycloak by keycloak.
the class UmaGrantTypeTest method testNoRefreshToken.
@Test
public void testNoRefreshToken() {
ClientResource client = getClient(getRealm());
ClientRepresentation clientRepresentation = client.toRepresentation();
clientRepresentation.getAttributes().put(OIDCConfigAttributes.USE_REFRESH_TOKEN, "false");
client.update(clientRepresentation);
AccessTokenResponse accessTokenResponse = getAuthzClient().obtainAccessToken("marta", "password");
AuthorizationResponse response = authorize(null, null, null, null, accessTokenResponse.getToken(), null, null, new PermissionRequest("Resource A", "ScopeA", "ScopeB"));
String rpt = response.getToken();
String refreshToken = response.getRefreshToken();
assertNotNull(rpt);
assertNull(refreshToken);
clientRepresentation.getAttributes().put(OIDCConfigAttributes.USE_REFRESH_TOKEN, "true");
client.update(clientRepresentation);
}
use of org.keycloak.representations.AccessTokenResponse in project keycloak by keycloak.
the class AccountLinkSpringBootTest method getToken.
private String getToken(OAuthClient.AccessTokenResponse response, Client httpClient) throws Exception {
log.info("target here is " + OAuthClient.AUTH_SERVER_ROOT);
String idpToken = httpClient.target(OAuthClient.AUTH_SERVER_ROOT).path("realms").path(REALM_NAME).path("broker").path(PARENT_REALM).path("token").request().header("Authorization", "Bearer " + response.getAccessToken()).get(String.class);
AccessTokenResponse res = JsonSerialization.readValue(idpToken, AccessTokenResponse.class);
return res.getToken();
}
use of org.keycloak.representations.AccessTokenResponse in project indy by Commonjava.
the class BasicAuthenticationOAuthTranslator method lookupToken.
private AccessTokenResponse lookupToken(final UserPass userPass) {
final URI uri = KeycloakUriBuilder.fromUri(config.getUrl()).path(ServiceUrlConstants.TOKEN_PATH).build(config.getRealm());
logger.debug("Looking up token at: {}", uri);
final HttpPost request = new HttpPost(uri);
final List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(USERNAME, userPass.getUser()));
params.add(new BasicNameValuePair(PASSWORD, userPass.getPassword()));
params.add(new BasicNameValuePair(OAuth2Constants.GRANT_TYPE, OAuth2Constants.PASSWORD));
final String authorization = BasicAuthHelper.createHeader(config.getServerResource(), config.getServerCredentialSecret());
request.setHeader(AUTHORIZATION_HEADER, authorization);
CloseableHttpClient client = null;
AccessTokenResponse tokenResponse = null;
try {
client = http.createClient(uri.getHost());
final UrlEncodedFormEntity form = new UrlEncodedFormEntity(params, "UTF-8");
request.setEntity(form);
CloseableHttpResponse response = client.execute(request);
logger.debug("Got response status: {}", response.getStatusLine());
if (response.getStatusLine().getStatusCode() == 200) {
try (InputStream in = response.getEntity().getContent()) {
final String json = IOUtils.toString(in);
logger.debug("Token response:\n\n{}\n\n", json);
tokenResponse = JsonSerialization.readValue(json, AccessTokenResponse.class);
}
}
} catch (IOException | IndyHttpException e) {
logger.error(String.format("Keycloak token request failed: %s", e.getMessage()), e);
} finally {
IOUtils.closeQuietly(client);
}
return tokenResponse;
}
use of org.keycloak.representations.AccessTokenResponse in project alfresco-repository by Alfresco.
the class IdentityServiceAuthenticationComponentTest method testAuthenticationPass.
@Test
public void testAuthenticationPass() {
when(mockAuthzClient.obtainAccessToken("username", "password")).thenReturn(new AccessTokenResponse());
authComponent.authenticateImpl("username", "password".toCharArray());
// Check that the authenticated user has been set
assertEquals("User has not been set as expected.", "username", authenticationContext.getCurrentUserName());
}
use of org.keycloak.representations.AccessTokenResponse in project keycloak by keycloak.
the class DirectAccessGrantsLoginModule method directGrantAuth.
protected Auth directGrantAuth(String username, String password) throws IOException, VerificationException {
String authServerBaseUrl = deployment.getAuthServerBaseUrl();
HttpPost post = new HttpPost(deployment.getTokenUrl());
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair(OAuth2Constants.GRANT_TYPE, OAuth2Constants.PASSWORD));
formparams.add(new BasicNameValuePair("username", username));
formparams.add(new BasicNameValuePair("password", password));
if (scope != null) {
formparams.add(new BasicNameValuePair(OAuth2Constants.SCOPE, scope));
}
ClientCredentialsProviderUtils.setClientCredentials(deployment, post, formparams);
UrlEncodedFormEntity form = new UrlEncodedFormEntity(formparams, "UTF-8");
post.setEntity(form);
HttpClient client = deployment.getClient();
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
if (status != 200) {
StringBuilder errorBuilder = new StringBuilder("Login failed. Invalid status: " + status);
if (entity != null) {
InputStream is = entity.getContent();
OAuth2ErrorRepresentation errorRep = JsonSerialization.readValue(is, OAuth2ErrorRepresentation.class);
errorBuilder.append(", OAuth2 error. Error: " + errorRep.getError()).append(", Error description: " + errorRep.getErrorDescription());
}
String error = errorBuilder.toString();
log.warn(error);
throw new IOException(error);
}
if (entity == null) {
throw new IOException("No Entity");
}
InputStream is = entity.getContent();
AccessTokenResponse tokenResponse = JsonSerialization.readValue(is, AccessTokenResponse.class);
// refreshToken will be saved to privateCreds of Subject for now
refreshToken = tokenResponse.getRefreshToken();
AdapterTokenVerifier.VerifiedTokens tokens = AdapterTokenVerifier.verifyTokens(tokenResponse.getToken(), tokenResponse.getIdToken(), deployment);
return postTokenVerification(tokenResponse.getToken(), tokens.getAccessToken());
}
Aggregations