private CompletableFuture getAuthorizationHeader()

in src/main/java/com/spotify/github/v3/clients/GitHubClient.java [981:1012]


  private CompletableFuture<String> getAuthorizationHeader(final String path) {
    if (isJwtRequest(path) && getPrivateKey().isEmpty()) {
      throw new IllegalStateException("This endpoint needs a client with a private key for an App");
    }
    if (getAccessToken().isPresent()) {
      return completedFuture(String.format("token %s", token));
    } else if (getPrivateKey().isPresent()) {
      final String jwtToken;
      try {
        jwtToken = JwtTokenIssuer.fromPrivateKey(privateKey).getToken(appId);
      } catch (Exception e) {
        throw new RuntimeException("There was an error generating JWT token", e);
      }
      if (isJwtRequest(path)) {
        return completedFuture(String.format("Bearer %s", jwtToken));
      }
      if (installationId == null) {
        throw new RuntimeException("This endpoint needs a client with an installation ID");
      }
      try {
        return getInstallationToken(jwtToken, installationId)
            .thenApply(token -> String.format("token %s", token))
            .exceptionally(
                ex -> {
                  throw new RuntimeException("Could not generate access token for github app", ex);
                });
      } catch (Exception e) {
        throw new RuntimeException("Could not generate access token for github app", e);
      }
    }
    throw new RuntimeException("Not possible to authenticate. ");
  }