private String getAccessToken()

in jdbc/src/main/java/software/amazon/timestream/jdbc/TimestreamAzureADCredentialsProvider.java [92:127]


  private String getAccessToken() throws SQLException {
    final String accessTokenEndpoint = "https://login.microsoftonline.com/" + this.tenantID + "/oauth2/token";

    final List<BasicNameValuePair> requestParameters =
      ImmutableList.of(new BasicNameValuePair("grant_type", "password"),
        new BasicNameValuePair("requested_token_type", "urn:ietf:params:oauth:token-type:saml2"),
        new BasicNameValuePair("username", userName),
        new BasicNameValuePair("password", password),
        new BasicNameValuePair("client_secret", clientSecret),
        new BasicNameValuePair("client_id", appID),
        new BasicNameValuePair("resource", appID));

    final HttpUriRequest accessTokenRequest = RequestBuilder
      .post()
      .setUri(accessTokenEndpoint)
      .addHeader("Accept", "application/json")
      .addHeader("Content-Type", "application/x-www-form-urlencoded")
      .setEntity(new UrlEncodedFormEntity(requestParameters, StandardCharsets.UTF_8))
      .build();

    try (CloseableHttpResponse response = this.httpClient.execute(accessTokenRequest)) {
      final StatusLine statusLine = response.getStatusLine();
      if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
        throw Error.createSQLException(LOGGER, Error.AAD_ACCESS_TOKEN_REQUEST_FAILED);
      }
      final HttpEntity responseEntity = response.getEntity();
      final String responseString = EntityUtils.toString(responseEntity, "UTF-8");
      final JsonNode jsonNode = OBJECT_MAPPER.readTree(responseString).get("access_token");
      if (jsonNode == null) {
        throw Error.createSQLException(LOGGER, Error.INVALID_AAD_ACCESS_TOKEN_RESPONSE);
      }
      return jsonNode.asText();
    } catch (IOException e) {
      throw Error.createSQLException(LOGGER, e, Error.AAD_ACCESS_TOKEN_ERROR);
    }
  }