diff --git a/register-core-plus/pom.xml b/register-core-plus/pom.xml index a1dab23d2241b0bc4aeeb7d60a6a40836bc84d7b..31f9b5ff5b4fcb89100664301549426134663843 100644 --- a/register-core-plus/pom.xml +++ b/register-core-plus/pom.xml @@ -52,7 +52,7 @@ <dependency> <groupId>org.opengroup.osdu</groupId> <artifactId>core-plus-common-lib</artifactId> - <version>0.26.0</version> + <version>0.27.0-rc1</version> </dependency> <dependency> diff --git a/register-core-plus/src/main/java/org/opengroup/osdu/register/util/CorePlusServiceAccountImpl.java b/register-core-plus/src/main/java/org/opengroup/osdu/register/util/CorePlusServiceAccountImpl.java index 59dc9642fb33752874cf61d482833f368982b1fa..9dc807d071977838803b4a1b802d60db89691889 100644 --- a/register-core-plus/src/main/java/org/opengroup/osdu/register/util/CorePlusServiceAccountImpl.java +++ b/register-core-plus/src/main/java/org/opengroup/osdu/register/util/CorePlusServiceAccountImpl.java @@ -1,29 +1,28 @@ /* * Copyright 2017-2020, Schlumberger * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. */ package org.opengroup.osdu.register.util; -import lombok.SneakyThrows; import org.apache.http.impl.client.HttpClients; +import org.opengroup.osdu.core.googleidtoken.IGoogleIdTokenFactory; import org.opengroup.osdu.register.utils.IGoogleServiceAccount; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import lombok.SneakyThrows; @Component -public class CorePlusServiceAccountImpl implements IGoogleServiceAccount{ +public class CorePlusServiceAccountImpl implements IGoogleServiceAccount { @Autowired private IGoogleIdTokenFactory googleIdTokenFactory; @@ -39,4 +38,4 @@ public class CorePlusServiceAccountImpl implements IGoogleServiceAccount{ public String getPrivateKeyId(String keyString) { return this.googleIdTokenFactory.getPrivateKeyId(keyString); } -} \ No newline at end of file +} diff --git a/register-core-plus/src/main/java/org/opengroup/osdu/register/util/GoogleIdTokenProducer.java b/register-core-plus/src/main/java/org/opengroup/osdu/register/util/GoogleIdTokenProducer.java deleted file mode 100644 index 8e6552aed5a26cd9dbd4dd1b938006e9723fe681..0000000000000000000000000000000000000000 --- a/register-core-plus/src/main/java/org/opengroup/osdu/register/util/GoogleIdTokenProducer.java +++ /dev/null @@ -1,108 +0,0 @@ -package org.opengroup.osdu.register.util; - -import com.auth0.jwt.JWT; -import com.auth0.jwt.JWTCreator; -import com.auth0.jwt.algorithms.Algorithm; -import com.google.auth.oauth2.ServiceAccountCredentials; -import com.google.common.base.Strings; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import org.apache.http.NameValuePair; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.ContentType; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.util.EntityUtils; -import org.springframework.stereotype.Component; -import org.springframework.web.context.annotation.RequestScope; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.security.interfaces.RSAPrivateKey; -import java.util.*; -import java.util.concurrent.TimeUnit; - -@Component -@RequestScope -public class GoogleIdTokenProducer implements IGoogleIdTokenFactory{ - private static final String INVALID_INPUT = "Invalid inputs provided to getGoogleIdToken"; - private static final String INVALID_INPUT_KEY_ID = "Invalid input to get private key id"; - private static final String JWT_ALG = "RS256"; - private static final String JWT_TYPE = "JWT"; - private static final String ID_TOKEN = "id_token"; - private static final String GOOGLE_TOKEN_ENDPOINT = "https://www.googleapis.com/oauth2/v4/token"; - private static final String GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer"; - private static final String NO_ID_TOKEN_ERROR_MESSAGE = "User is not authorized to perform this operation."; - private static final String ERROR_GENERATING_MESSAGE = "Error generating google id token."; - private static final String ERROR_GETTING_KEY_ID = "Error getting private key id"; - private static final String CHARSET_NAME = "UTF-8"; - private final long expireDuration = 3600L; - - @Override - public String getGoogleIdToken(String keyFile, String audience, CloseableHttpClient httpClient) throws ServiceIdTokenException{ - if (Strings.isNullOrEmpty(keyFile) || Strings.isNullOrEmpty(audience)) { - throw new ServiceIdTokenException(INVALID_INPUT); - } - - try { - ServiceAccountCredentials credentials = ServiceAccountCredentials.fromStream(new ByteArrayInputStream(keyFile.getBytes(CHARSET_NAME))); - - Map<String, Object> jwtHeaders = new HashMap<>(); - jwtHeaders.put("kid", credentials.getPrivateKeyId()); - jwtHeaders.put("alg", JWT_ALG); - jwtHeaders.put("typ", JWT_TYPE); - - long currentMilliSec = System.currentTimeMillis(); - Date now = new Date(currentMilliSec); - Date expTime = new Date(currentMilliSec + TimeUnit.SECONDS.toMillis(expireDuration)); - - JWTCreator.Builder jwt = JWT.create().withHeader(jwtHeaders) - .withIssuedAt(now) - .withExpiresAt(expTime) - .withIssuer(credentials.getClientEmail()) - .withClaim("aud", GOOGLE_TOKEN_ENDPOINT) - .withClaim("target_audience", audience); - - RSAPrivateKey privateKey = (RSAPrivateKey) credentials.getPrivateKey(); - Algorithm algorithm = Algorithm.RSA256(null, privateKey); - - String signedJwt = jwt.sign(algorithm); - - List<NameValuePair> postParameters = new ArrayList<>(); - postParameters.add(new BasicNameValuePair("grant_type", GRANT_TYPE)); - postParameters.add(new BasicNameValuePair("assertion", signedJwt)); - - - HttpPost post = new HttpPost(GOOGLE_TOKEN_ENDPOINT); - post.setHeader("Content-Type", ContentType.APPLICATION_FORM_URLENCODED.getMimeType()); - post.setEntity(new UrlEncodedFormEntity(postParameters, CHARSET_NAME)); - - try(CloseableHttpResponse httpResponse = httpClient.execute(post)) { - JsonObject jsonContent = (new JsonParser()).parse(EntityUtils.toString(httpResponse.getEntity())).getAsJsonObject(); - - if (!jsonContent.has(ID_TOKEN)) { - throw new ServiceIdTokenException(NO_ID_TOKEN_ERROR_MESSAGE); - } - return jsonContent.get(ID_TOKEN).getAsString(); - } - } catch (Exception e) { - throw new ServiceIdTokenException(ERROR_GENERATING_MESSAGE, e); - } - } - - @Override - public String getPrivateKeyId(String keyFile) throws ServiceIdTokenException { - if (Strings.isNullOrEmpty(keyFile)) { - throw new ServiceIdTokenException(INVALID_INPUT_KEY_ID); - } - try { - ServiceAccountCredentials credentials = ServiceAccountCredentials.fromStream(new ByteArrayInputStream(keyFile.getBytes(CHARSET_NAME))); - return credentials.getPrivateKeyId(); - } catch (IOException e) { - throw new ServiceIdTokenException(ERROR_GETTING_KEY_ID, e); - } - } -} - diff --git a/register-core-plus/src/main/java/org/opengroup/osdu/register/util/IGoogleIdTokenFactory.java b/register-core-plus/src/main/java/org/opengroup/osdu/register/util/IGoogleIdTokenFactory.java deleted file mode 100644 index 8633f7a261449fac9738e641f8dc9dfa9afb52d0..0000000000000000000000000000000000000000 --- a/register-core-plus/src/main/java/org/opengroup/osdu/register/util/IGoogleIdTokenFactory.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.opengroup.osdu.register.util; - -import org.apache.http.impl.client.CloseableHttpClient; - -public interface IGoogleIdTokenFactory { - - String getGoogleIdToken(String keyFile, String audience, CloseableHttpClient httpClient) throws ServiceIdTokenException; - - String getPrivateKeyId(String keyFile) throws ServiceIdTokenException; -} diff --git a/register-core-plus/src/main/java/org/opengroup/osdu/register/util/ServiceIdTokenException.java b/register-core-plus/src/main/java/org/opengroup/osdu/register/util/ServiceIdTokenException.java deleted file mode 100644 index cc42c89d5526f6bc59771048f8e76357a28b5f10..0000000000000000000000000000000000000000 --- a/register-core-plus/src/main/java/org/opengroup/osdu/register/util/ServiceIdTokenException.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.opengroup.osdu.register.util; - - -public class ServiceIdTokenException extends Exception { - private static final long serialVersionUID = 4319650830086452892L; - - ServiceIdTokenException(String errorMessage) { - super(errorMessage); - } - - ServiceIdTokenException(String errorMessage, Exception e) { - super(errorMessage); - this.initCause(e); - } -} diff --git a/register-core-plus/src/main/java/org/opengroup/osdu/register/util/TokenService.java b/register-core-plus/src/main/java/org/opengroup/osdu/register/util/TokenService.java index d6bc18c61bc382cea73d95fcc7dde329694080e3..a4bea305b798d34e8e11d39037266792c45a0a2b 100644 --- a/register-core-plus/src/main/java/org/opengroup/osdu/register/util/TokenService.java +++ b/register-core-plus/src/main/java/org/opengroup/osdu/register/util/TokenService.java @@ -1,10 +1,10 @@ package org.opengroup.osdu.register.util; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.opengroup.osdu.auth.TokenProvider; import org.opengroup.osdu.core.common.util.IServiceAccountJwtClient; import org.springframework.stereotype.Service; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; @Slf4j @Service