You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.7 KiB

6 years ago
  1. /*
  2. * Copyright 2018 Red Hat, Inc. and/or its affiliates
  3. * and other contributors as indicated by the @author tags.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.keycloak.social.discord;
  18. import org.jboss.logging.Logger;
  19. import org.keycloak.broker.oidc.AbstractOAuth2IdentityProvider;
  20. import org.keycloak.broker.oidc.OAuth2IdentityProviderConfig;
  21. import org.keycloak.broker.oidc.mappers.AbstractJsonUserAttributeMapper;
  22. import org.keycloak.broker.provider.BrokeredIdentityContext;
  23. import org.keycloak.broker.provider.IdentityBrokerException;
  24. import org.keycloak.broker.provider.util.SimpleHttp;
  25. import org.keycloak.broker.social.SocialIdentityProvider;
  26. import org.keycloak.events.EventBuilder;
  27. import org.keycloak.models.KeycloakSession;
  28. import org.keycloak.social.linkedin.LinkedInIdentityProvider;
  29. import com.fasterxml.jackson.databind.JsonNode;
  30. /**
  31. * @author <a href="mailto:wadahiro@gmail.com">Hiroyuki Wada</a>
  32. */
  33. public class DiscordIdentityProvider extends AbstractOAuth2IdentityProvider<OAuth2IdentityProviderConfig>
  34. implements SocialIdentityProvider<OAuth2IdentityProviderConfig> {
  35. private static final Logger log = Logger.getLogger(LinkedInIdentityProvider.class);
  36. public static final String AUTH_URL = "https://discordapp.com/api/oauth2/authorize";
  37. public static final String TOKEN_URL = "https://discordapp.com/api/oauth2/token";
  38. public static final String PROFILE_URL = "https://discordapp.com/api/users/@me";
  39. public static final String DEFAULT_SCOPE = "identify email";
  40. public DiscordIdentityProvider(KeycloakSession session, OAuth2IdentityProviderConfig config) {
  41. super(session, config);
  42. config.setAuthorizationUrl(AUTH_URL);
  43. config.setTokenUrl(TOKEN_URL);
  44. config.setUserInfoUrl(PROFILE_URL);
  45. }
  46. @Override
  47. protected boolean supportsExternalExchange() {
  48. return true;
  49. }
  50. @Override
  51. protected String getProfileEndpointForValidation(EventBuilder event) {
  52. return PROFILE_URL;
  53. }
  54. @Override
  55. protected BrokeredIdentityContext extractIdentityFromProfile(EventBuilder event, JsonNode profile) {
  56. BrokeredIdentityContext user = new BrokeredIdentityContext(getJsonProperty(profile, "id"));
  57. user.setUsername(getJsonProperty(profile, "username"));
  58. // user.setName(getJsonProperty(profile, "username"));
  59. user.setEmail(getJsonProperty(profile, "email"));
  60. user.setIdpConfig(getConfig());
  61. user.setIdp(this);
  62. AbstractJsonUserAttributeMapper.storeUserProfileForMapper(user, profile, getConfig().getAlias());
  63. return user;
  64. }
  65. @Override
  66. protected BrokeredIdentityContext doGetFederatedIdentity(String accessToken) {
  67. log.debug("doGetFederatedIdentity()");
  68. try {
  69. JsonNode profile = SimpleHttp.doGet(PROFILE_URL, session).header("Authorization", "Bearer " + accessToken).asJson();
  70. // System.out.println(profile.toString());
  71. return extractIdentityFromProfile(null, profile);
  72. } catch (Exception e) {
  73. throw new IdentityBrokerException("Could not obtain user profile from discord.", e);
  74. }
  75. }
  76. @Override
  77. protected String getDefaultScopes() {
  78. return DEFAULT_SCOPE;
  79. }
  80. }