|
36 | 36 | import lombok.extern.slf4j.Slf4j; |
37 | 37 |
|
38 | 38 | import java.io.IOException; |
| 39 | +import java.io.UnsupportedEncodingException; |
39 | 40 | import java.lang.reflect.Constructor; |
40 | 41 | import java.lang.reflect.InvocationTargetException; |
| 42 | +import java.net.URLEncoder; |
| 43 | +import java.nio.charset.StandardCharsets; |
41 | 44 | import java.util.*; |
42 | 45 | import java.util.concurrent.atomic.AtomicBoolean; |
43 | 46 | import java.util.regex.Pattern; |
@@ -314,26 +317,60 @@ private void initOAuthServicesIfNecessary() { |
314 | 317 | } |
315 | 318 | } |
316 | 319 |
|
| 320 | + /** |
| 321 | + * Get the Slack URL for beginning the OAuth flow, including the query |
| 322 | + * params necessary to identify this application to Slack. |
| 323 | + * |
| 324 | + * Appends the optional `redirect_uri` query param based on the provided |
| 325 | + * AppConfig to ensure that the correct OAuth redirect URI is selected in |
| 326 | + * cases where a Slack application may have multiple redirect URIs |
| 327 | + * associated with it. |
| 328 | + * |
| 329 | + * @param state The OAuth state param |
| 330 | + * @return The Slack URL to redirect users to for beginning the OAuth flow |
| 331 | + */ |
317 | 332 | public String getOauthInstallationUrl(String state) { |
318 | 333 | AppConfig config = config(); |
| 334 | + |
319 | 335 | if (config.getClientId() == null || config.getScope() == null || state == null) { |
320 | 336 | return null; |
| 337 | + } |
| 338 | + |
| 339 | + String scope = config.getScope() == null ? "" : config.getScope(); |
| 340 | + String redirectUriParam = redirectUriQueryParam(appConfig); |
| 341 | + |
| 342 | + if (config.isClassicAppPermissionsEnabled()) { |
| 343 | + // https://api.slack.com/authentication/migration |
| 344 | + return "https://slack.com/oauth/authorize" + |
| 345 | + "?client_id=" + config.getClientId() + |
| 346 | + "&scope=" + scope + |
| 347 | + "&state=" + state + |
| 348 | + redirectUriParam; |
321 | 349 | } else { |
322 | | - String scope = config.getScope() == null ? "" : config.getScope(); |
323 | | - if (config.isClassicAppPermissionsEnabled()) { |
324 | | - // https://api.slack.com/authentication/migration |
325 | | - return "https://slack.com/oauth/authorize" + |
326 | | - "?client_id=" + config.getClientId() + |
327 | | - "&scope=" + scope + |
328 | | - "&state=" + state; |
329 | | - } else { |
330 | | - String userScope = config.getUserScope() == null ? "" : config.getUserScope(); |
331 | | - return "https://slack.com/oauth/v2/authorize" + |
332 | | - "?client_id=" + config.getClientId() + |
333 | | - "&scope=" + scope + |
334 | | - "&user_scope=" + userScope + |
335 | | - "&state=" + state; |
336 | | - } |
| 350 | + String userScope = config.getUserScope() == null ? "" : config.getUserScope(); |
| 351 | + return "https://slack.com/oauth/v2/authorize" + |
| 352 | + "?client_id=" + config.getClientId() + |
| 353 | + "&scope=" + scope + |
| 354 | + "&user_scope=" + userScope + |
| 355 | + "&state=" + state + |
| 356 | + redirectUriParam; |
| 357 | + } |
| 358 | + } |
| 359 | + |
| 360 | + private String redirectUriQueryParam(AppConfig appConfig) { |
| 361 | + if (appConfig.getRedirectUri() == null) { |
| 362 | + return ""; |
| 363 | + } |
| 364 | + |
| 365 | + try { |
| 366 | + String urlEncodedRedirectUri = URLEncoder.encode( |
| 367 | + appConfig.getRedirectUri(), |
| 368 | + StandardCharsets.UTF_8.name() |
| 369 | + ); |
| 370 | + |
| 371 | + return String.format("&redirect_uri=%s", urlEncodedRedirectUri); |
| 372 | + } catch (UnsupportedEncodingException e) { |
| 373 | + return ""; |
337 | 374 | } |
338 | 375 | } |
339 | 376 |
|
|
0 commit comments