Skip to content
Snippets Groups Projects
Commit 93c32f3c authored by Rustam Lotsmanenko (EPAM)'s avatar Rustam Lotsmanenko (EPAM)
Browse files

Merge branch 'solxget_centeral' into 'master'

Delete locally copied files and use library instead

See merge request !479
parents a3184193 82e9d801
No related branches found
No related tags found
1 merge request!479Delete locally copied files and use library instead
Pipeline #270542 failed
......@@ -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>
......
/*
* 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
}
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);
}
}
}
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;
}
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);
}
}
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment