use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse in project google-api-java-client by google.
the class CloudShellCredential method executeRefreshToken.
@Override
protected TokenResponse executeRefreshToken() throws IOException {
Socket socket = new Socket("localhost", this.getAuthPort());
socket.setSoTimeout(READ_TIMEOUT_MS);
TokenResponse token = new TokenResponse();
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(GET_AUTH_TOKEN_REQUEST);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// Ignore the size line
input.readLine();
Collection<Object> messageArray = jsonFactory.createJsonParser(input).parseArray(LinkedList.class, Object.class);
String accessToken = ((List<Object>) messageArray).get(ACCESS_TOKEN_INDEX).toString();
token.setAccessToken(accessToken);
} finally {
socket.close();
}
return token;
}
use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse in project hadoop-connectors by GoogleCloudDataproc.
the class HadoopCredentialsConfigurationTest method metadataServiceIsUsedByDefault.
@Test
public void metadataServiceIsUsedByDefault() throws Exception {
TokenResponse token = new TokenResponse().setAccessToken("metadata-test-token").setExpiresInSeconds(100L);
MockHttpTransport transport = mockTransport(jsonDataResponse(token));
GoogleCredentials credentials = getCredentials(transport);
credentials.refreshIfExpired();
assertThat(credentials).isInstanceOf(ComputeEngineCredentials.class);
assertThat(credentials.getAccessToken().getTokenValue()).isEqualTo("metadata-test-token");
}
use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse in project hadoop-connectors by GoogleCloudDataproc.
the class HadoopCredentialsConfigurationTest method userCredentials_credentialFactory_noNewRefreshToken.
@Test
public void userCredentials_credentialFactory_noNewRefreshToken() throws IOException {
// GIVEN
String initialRefreshToken = "FAKE_REFRESH_TOKEN";
String tokenServerUrl = "http://localhost/token";
configuration.set(getConfigKey(TOKEN_SERVER_URL_SUFFIX), tokenServerUrl);
configuration.setEnum(getConfigKey(AUTHENTICATION_TYPE_SUFFIX), AuthenticationType.USER_CREDENTIALS);
configuration.set(getConfigKey(AUTH_REFRESH_TOKEN_SUFFIX), initialRefreshToken);
configuration.set(getConfigKey(AUTH_CLIENT_ID_SUFFIX), "FAKE_CLIENT_ID");
configuration.set(getConfigKey(AUTH_CLIENT_SECRET_SUFFIX), "FAKE_CLIENT_SECRET");
long expireInSec = 300L;
String accessTokenAsString = "SlAV32hkKG";
TokenResponse tokenResponse = new TokenResponse().setAccessToken(accessTokenAsString).setExpiresInSeconds(expireInSec);
MockHttpTransport transport = mockTransport(jsonDataResponse(tokenResponse));
// WHEN
GoogleCredentials credentials = getCredentials(transport);
credentials.refresh();
// THEN
assertThat(credentials).isInstanceOf(UserCredentials.class);
UserCredentials userCredentials = (UserCredentials) credentials;
assertThat(userCredentials.getClientId()).isEqualTo("FAKE_CLIENT_ID");
assertThat(userCredentials.getClientSecret()).isEqualTo("FAKE_CLIENT_SECRET");
AccessToken accessToken = userCredentials.getAccessToken();
assertThat(accessToken).isNotNull();
// To avoid any timebase issue, we test a time range instead
assertThat(accessToken.getExpirationTime()).isGreaterThan(Date.from(Instant.now().plusSeconds(expireInSec - 10)));
assertThat(accessToken.getExpirationTime()).isLessThan(Date.from(Instant.now().plusSeconds(expireInSec + 10)));
String refreshToken = userCredentials.getRefreshToken();
assertThat(refreshToken).isEqualTo(initialRefreshToken);
}
use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse in project SORMAS-Project by hzi-braunschweig.
the class Oidc method requestAccessToken.
public static String requestAccessToken(String tokenEndpoint, String clientId, String clientSecret, List<String> scopes) throws Exception {
ClientParametersAuthentication clientAuth = new ClientParametersAuthentication(clientId, clientSecret);
try {
LOGGER.info(String.format("Requesting access token for client %s at %s with scope: %s", clientId, tokenEndpoint, scopes));
TokenResponse response = new ClientCredentialsTokenRequest(new NetHttpTransport(), new GsonFactory(), new GenericUrl(tokenEndpoint)).setClientAuthentication(clientAuth).setScopes(scopes).execute();
String token = response.getAccessToken();
if (token == null || token.isEmpty()) {
LOGGER.error("Could not retrieve access token.");
throw new Exception("Could not retrieve access token.");
}
return token;
} catch (IOException e) {
LOGGER.error("Unable to connect to Keycloak.", e);
throw e;
}
}
use of com.microsoft.identity.common.internal.providers.oauth2.TokenResponse in project isaac-api by isaacphysics.
the class TwitterAuthenticator method exchangeCode.
@Override
public String exchangeCode(final String authorizationCode) throws CodeExchangeException {
try {
AccessToken accessToken = twitter.getOAuthAccessToken(authorizationCode);
TokenResponse tokenResponse = new TokenResponse();
tokenResponse.setAccessToken(accessToken.getToken());
tokenResponse.setRefreshToken(accessToken.getTokenSecret());
tokenResponse.setExpiresInSeconds(Long.MAX_VALUE);
Builder builder = new AuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(), httpTransport, jsonFactory, new GenericUrl(TOKEN_EXCHANGE_URL), new ClientParametersAuthentication(clientId, clientSecret), clientId, AUTH_URL);
AuthorizationCodeFlow flow = builder.setDataStoreFactory(MemoryDataStoreFactory.getDefaultInstance()).build();
Credential credential = flow.createAndStoreCredential(tokenResponse, authorizationCode);
String internalReferenceToken = UUID.randomUUID().toString();
credentialStore.put(internalReferenceToken, credential);
flow.getCredentialDataStore().clear();
return internalReferenceToken;
} catch (IOException | TwitterException | IllegalStateException e) {
log.error("An error occurred during code exchange: " + e);
throw new CodeExchangeException();
}
}
Aggregations