Skip to content

Add OpenID Connect document after v1.10 release #799

@seratch

Description

@seratch

Once we release v1.10, which includes the OpenID Connect support, we can add a new page to the document website. Its should be similar to https://slack.dev/java-slack-sdk/guides/app-distribution but some details are different.

A simple example app should look like this:

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.slack.api.Slack;
import com.slack.api.bolt.App;
import com.slack.api.bolt.AppConfig;
import com.slack.api.methods.MethodsClient;
import com.slack.api.methods.response.openid.connect.OpenIDConnectUserInfoResponse;
import org.slf4j.Logger;
import com.slack.api.bolt.jetty.SlackAppServer;

import java.util.HashMap;
import java.util.Map;

public class TestApp {

  public static void main(String[] args) throws Exception {

    // The configuration for OpenID Connect
    AppConfig config = new AppConfig();
    config.setClientId(System.getenv("SLACK_CLIENT_ID"));
    config.setClientSecret(System.getenv("SLACK_CLIENT_SECRET"));
    config.setOauthInstallPath("install"); // the default one is "start" for historical reason
    config.setOauthRedirectUriPath("oauth_redirect"); // the default one is "callback" for historical reason
    config.setRedirectUri("https://your-domain/slack/oauth_redirect");
    config.setUserScope("openid,email,profile");

    App app = new App().asOpenIDConnectApp(true);

    app.openIDConnectSuccess((req, resp, token) -> {
      Logger logger = req.getContext().getLogger();
      // Decode id_token in an openid.connect.token response
      DecodedJWT decoded = JWT.decode(token.getIdToken());
      Map<String, Claim> claims = decoded.getClaims();
      logger.info("claims: {}", claims);
      String teamId = claims.get("https://slack.com/team_id").asString();

      // Call openid.connect.userInfo using the given access token
      MethodsClient client = Slack.getInstance().methods();
      OpenIDConnectUserInfoResponse userInfo = client.openIDConnectUserInfo(r -> r.token(token.getAccessToken()));
      logger.info("userInfo: {}", userInfo);

      // Render the web page for the end-user
      String html = app.config().getOAuthRedirectUriPageRenderer().renderSuccessPage(
          null, req.getContext().getOauthCompletionUrl());
      resp.setBody(html);
      resp.setContentType("text/html; charset=utf-8");
      return resp;
    });

    // Start the web app
    Map<String, App> apps = new HashMap<>();
    apps.put("/slack/", app);
    SlackAppServer server = new SlackAppServer(app);
    server.start();
  }

}

For token rotation, the following success handler demonstrates how to perform API calls with it:

app.openIDConnectSuccess((req, resp, token) -> {
  Logger logger = req.getContext().getLogger();
  // Decode id_token in an openid.connect.token response
  DecodedJWT decoded = JWT.decode(token.getIdToken());
  Map<String, Claim> claims = decoded.getClaims();
  logger.info("claims: {}", claims);
  String teamId = claims.get("https://slack.com/team_id").asString();

  if (token.getRefreshToken() != null) {
    // Call openid.connect.userInfo using the given access token
    MethodsClient client = Slack.getInstance().methods();

    // run the first token rotation
    OpenIDConnectTokenResponse refreshedToken = client.openIDConnectToken(r -> r
        .clientId(config.getClientId())
        .clientSecret(config.getClientSecret())
        .grantType("refresh_token")
        .refreshToken(token.getRefreshToken())
    );

    // Call openid.connect.userInfo using the given access token
    MethodsClient teamIdWiredClient = Slack.getInstance().methods(refreshedToken.getAccessToken(), teamId);
    OpenIDConnectUserInfoResponse userInfo = teamIdWiredClient.openIDConnectUserInfo(r -> r.token(refreshedToken.getAccessToken()));
    logger.info("userInfo: {}", userInfo);

  } else {
    throw new RuntimeException("Invalid configuration - token rotation is not enabled");
  }

  // Render the web page for the end-user
  String html = app.config().getOAuthRedirectUriPageRenderer().renderSuccessPage(
      null, req.getContext().getOauthCompletionUrl());
  resp.setBody(html);
  resp.setContentType("text/html; charset=utf-8");
  return resp;
});

The page URLs

Requirements

Please make sure if this topic is specific to this SDK. For general questions/issues about Slack API platform or its server-side, could you submit questions at https://my.slack.com/help/requests/new instead. 🙇

Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you are agreeing to the those rules.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions