Skip to content

Instantly share code, notes, and snippets.

@seratch
Created August 11, 2021 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seratch/1bcbddca9a78c778b60da88ca43c0c32 to your computer and use it in GitHub Desktop.
Save seratch/1bcbddca9a78c778b60da88ca43c0c32 to your computer and use it in GitHub Desktop.
Sign in with Slack (OpenID Connect) Example App in Kotlin
_metadata:
major_version: 1
minor_version: 1
display_information:
name: my-openid-connect-app
oauth_config:
redirect_urls:
- https://{your-domain-here}/slack/oauth_redirect
scopes:
user:
- openid
- email
- profile
plugins {
id("org.jetbrains.kotlin.jvm") version "1.5.21"
id("application")
}
repositories {
mavenCentral()
}
dependencies {
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("com.slack.api:bolt-jetty:1.10.0-RC1")
implementation("com.auth0:java-jwt:3.18.1")
implementation("org.slf4j:slf4j-simple:1.7.32") // or logback-classic
}
application {
mainClassName = "MyAppKt" // add "Kt" suffix for main function source file
}
import com.auth0.jwt.JWT
import com.slack.api.Slack
import com.slack.api.bolt.App
import com.slack.api.bolt.jetty.SlackAppServer
fun main() {
System.setProperty("org.slf4j.simpleLogger.log.com.slack.api", "debug")
// export SLACK_CLIENT_ID=
// export SLACK_CLIENT_SECRET=
// export SLACK_USER_SCOPES=openid,email,profile
// export SLACK_INSTALL_PATH=install
// export SLACK_REDIRECT_URI_PATH=oauth_redirect
// export SLACK_REDIRECT_URI=https://{your-domain-here}/slack/oauth_redirect
val app = App().asOpenIDConnectApp(true)
val slack = Slack.getInstance()
app.openIDConnectSuccess { request, response, token ->
val logger = request.context.logger
val claims = JWT.decode(token.idToken).claims
logger.info("id_token claims: {}", claims)
// Store the data in your database
// Example demonstrating openid.connect.userInfo API call with the issued access token
val teamId = claims["https://slack.com/team_id"]?.asString()
val userInfo = slack.methods(token.accessToken, teamId).openIDConnectUserInfo { it }
logger.info("usersInfo: {}", userInfo)
// Example demonstrating how to refresh the token afterwards
if (token.refreshToken != null) {
// refreshing the access token for the first time
val refreshedToken = slack.methods(null, teamId).openIDConnectToken { it
.clientId(System.getenv("SLACK_CLIENT_ID"))
.clientSecret(System.getenv("SLACK_CLIENT_SECRET"))
.grantType("refresh_token")
.refreshToken(token.refreshToken)
}
val userInfo = slack.methods(refreshedToken.accessToken, teamId).openIDConnectUserInfo { it }
logger.info("usersInfo by a refreshed token: {}", userInfo)
}
// Redirect the user to the next step
response.statusCode = 302
response.headers["Location"] = listOf("https://slack.com/")
response
}
val apps = mutableMapOf(
"/slack/" to app
)
val server = SlackAppServer(apps)
server.start() // http://localhost:3000/slack/install
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment