|
| 1 | +// Copyright (C) 2018 The Android Open Source Project |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.googlesource.gerrit.plugins.oauth; |
| 16 | + |
| 17 | +import com.google.common.base.CharMatcher; |
| 18 | +import com.google.gerrit.extensions.annotations.PluginName; |
| 19 | +import com.google.gerrit.extensions.auth.oauth.OAuthServiceProvider; |
| 20 | +import com.google.gerrit.extensions.auth.oauth.OAuthToken; |
| 21 | +import com.google.gerrit.extensions.auth.oauth.OAuthUserInfo; |
| 22 | +import com.google.gerrit.extensions.auth.oauth.OAuthVerifier; |
| 23 | +import com.google.gerrit.server.OutputFormat; |
| 24 | +import com.google.gerrit.server.config.CanonicalWebUrl; |
| 25 | +import com.google.gerrit.server.config.PluginConfig; |
| 26 | +import com.google.gerrit.server.config.PluginConfigFactory; |
| 27 | +import com.google.gson.JsonElement; |
| 28 | +import com.google.gson.JsonObject; |
| 29 | +import com.google.inject.Inject; |
| 30 | +import com.google.inject.Provider; |
| 31 | +import com.google.inject.Singleton; |
| 32 | +import java.io.IOException; |
| 33 | +import javax.servlet.http.HttpServletResponse; |
| 34 | +import org.scribe.builder.ServiceBuilder; |
| 35 | +import org.scribe.model.OAuthRequest; |
| 36 | +import org.scribe.model.Response; |
| 37 | +import org.scribe.model.Token; |
| 38 | +import org.scribe.model.Verb; |
| 39 | +import org.scribe.model.Verifier; |
| 40 | +import org.scribe.oauth.OAuthService; |
| 41 | +import org.slf4j.Logger; |
| 42 | +import org.slf4j.LoggerFactory; |
| 43 | + |
| 44 | +@Singleton |
| 45 | +class Office365OAuthService implements OAuthServiceProvider { |
| 46 | + private static final Logger log = LoggerFactory.getLogger(Office365OAuthService.class); |
| 47 | + static final String CONFIG_SUFFIX = "-office365-oauth"; |
| 48 | + private static final String OFFICE365_PROVIDER_PREFIX = "office365-oauth:"; |
| 49 | + private static final String PROTECTED_RESOURCE_URL = "https://graph.microsoft.com/v1.0/me"; |
| 50 | + private static final String SCOPE = |
| 51 | + "openid offline_access https://graph.microsoft.com/user.readbasic.all"; |
| 52 | + private final OAuthService service; |
| 53 | + private final String canonicalWebUrl; |
| 54 | + private final boolean useEmailAsUsername; |
| 55 | + |
| 56 | + @Inject |
| 57 | + Office365OAuthService( |
| 58 | + PluginConfigFactory cfgFactory, |
| 59 | + @PluginName String pluginName, |
| 60 | + @CanonicalWebUrl Provider<String> urlProvider) { |
| 61 | + PluginConfig cfg = cfgFactory.getFromGerritConfig(pluginName + CONFIG_SUFFIX); |
| 62 | + this.canonicalWebUrl = CharMatcher.is('/').trimTrailingFrom(urlProvider.get()) + "/"; |
| 63 | + this.useEmailAsUsername = cfg.getBoolean(InitOAuth.USE_EMAIL_AS_USERNAME, false); |
| 64 | + this.service = |
| 65 | + new ServiceBuilder() |
| 66 | + .provider(Office365Api.class) |
| 67 | + .apiKey(cfg.getString(InitOAuth.CLIENT_ID)) |
| 68 | + .apiSecret(cfg.getString(InitOAuth.CLIENT_SECRET)) |
| 69 | + .callback(canonicalWebUrl + "oauth") |
| 70 | + .scope(SCOPE) |
| 71 | + .build(); |
| 72 | + if (log.isDebugEnabled()) { |
| 73 | + log.debug("OAuth2: canonicalWebUrl={}", canonicalWebUrl); |
| 74 | + log.debug("OAuth2: scope={}", SCOPE); |
| 75 | + log.debug("OAuth2: useEmailAsUsername={}", useEmailAsUsername); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { |
| 81 | + OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); |
| 82 | + request.addHeader("Accept", "*/*"); |
| 83 | + request.addHeader("Authorization", "Bearer " + token.getToken()); |
| 84 | + Response response = request.send(); |
| 85 | + if (response.getCode() != HttpServletResponse.SC_OK) { |
| 86 | + throw new IOException( |
| 87 | + String.format( |
| 88 | + "Status %s (%s) for request %s", |
| 89 | + response.getCode(), response.getBody(), request.getUrl())); |
| 90 | + } |
| 91 | + JsonElement userJson = |
| 92 | + OutputFormat.JSON.newGson().fromJson(response.getBody(), JsonElement.class); |
| 93 | + if (log.isDebugEnabled()) { |
| 94 | + log.debug("User info response: {}", response.getBody()); |
| 95 | + } |
| 96 | + if (userJson.isJsonObject()) { |
| 97 | + JsonObject jsonObject = userJson.getAsJsonObject(); |
| 98 | + JsonElement id = jsonObject.get("id"); |
| 99 | + if (id == null || id.isJsonNull()) { |
| 100 | + throw new IOException(String.format("Response doesn't contain id field")); |
| 101 | + } |
| 102 | + JsonElement email = jsonObject.get("mail"); |
| 103 | + JsonElement name = jsonObject.get("displayName"); |
| 104 | + String login = null; |
| 105 | + |
| 106 | + if (useEmailAsUsername && !email.isJsonNull()) { |
| 107 | + login = email.getAsString().split("@")[0]; |
| 108 | + } |
| 109 | + return new OAuthUserInfo( |
| 110 | + OFFICE365_PROVIDER_PREFIX + id.getAsString() /*externalId*/, |
| 111 | + login /*username*/, |
| 112 | + email == null || email.isJsonNull() ? null : email.getAsString() /*email*/, |
| 113 | + name == null || name.isJsonNull() ? null : name.getAsString() /*displayName*/, |
| 114 | + null); |
| 115 | + } |
| 116 | + |
| 117 | + throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public OAuthToken getAccessToken(OAuthVerifier rv) { |
| 122 | + Verifier vi = new Verifier(rv.getValue()); |
| 123 | + Token to = service.getAccessToken(null, vi); |
| 124 | + OAuthToken result = new OAuthToken(to.getToken(), to.getSecret(), to.getRawResponse()); |
| 125 | + return result; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public String getAuthorizationUrl() { |
| 130 | + String url = service.getAuthorizationUrl(null); |
| 131 | + return url; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public String getVersion() { |
| 136 | + return service.getVersion(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public String getName() { |
| 141 | + return "Office365 OAuth2"; |
| 142 | + } |
| 143 | +} |
0 commit comments