feign_client_impl | updated with the requested changes.

pull/1/head
tharusha 1 month ago
parent 3a7d2db327
commit e803c4b992

@ -1,4 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you 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
*
* 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
@ -8,11 +25,10 @@
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.entgra</groupId>
<artifactId>auth_token_getter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>auth_token_getter</name>
<description>auth_token_getter</description>
<groupId>o.entgra.oauth.token.generator</groupId>
<artifactId>oauth-token-generator</artifactId>
<version>1.0-SNAPSHOT</version>
<name>oauth_token_generator</name>
<url/>
<licenses>
<license/>

@ -1,5 +1,3 @@
package io.entgra.auth_token_getter;
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
@ -17,6 +15,7 @@ package io.entgra.auth_token_getter;
* specific language governing permissions and limitations
* under the License.
*/
package io.entgra.auth_token_generator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@ -25,10 +24,10 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class AuthTokenGetterApplication {
public class AuthTokenGeneratorApplication {
public static void main(String[] args) {
SpringApplication.run(AuthTokenGetterApplication.class, args);
SpringApplication.run(AuthTokenGeneratorApplication.class, args);
}
}

@ -1,5 +1,3 @@
package io.entgra.auth_token_getter.client;
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
@ -17,6 +15,7 @@ package io.entgra.auth_token_getter.client;
* specific language governing permissions and limitations
* under the License.
*/
package io.entgra.auth_token_generator.client;
import feign.Param;
import org.springframework.cloud.openfeign.FeignClient;

@ -1,5 +1,3 @@
package io.entgra.auth_token_getter.exceptions;
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
@ -17,6 +15,7 @@ package io.entgra.auth_token_getter.exceptions;
* specific language governing permissions and limitations
* under the License.
*/
package io.entgra.auth_token_generator.exceptions;
import lombok.Data;

@ -1,5 +1,3 @@
package io.entgra.auth_token_getter.service;
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
@ -16,10 +14,12 @@ package io.entgra.auth_token_getter.service;
* KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.entgra.auth_token_generator.service;
import feign.FeignException;
import io.entgra.auth_token_getter.client.AuthFeignClient;
import io.entgra.auth_token_getter.util.TokenDataHolder;
import io.entgra.auth_token_generator.client.AuthFeignClient;
import io.entgra.auth_token_generator.util.TokenDataHolder;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@ -31,7 +31,7 @@ import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
import static io.entgra.auth_token_getter.exceptions.ErrorResponse.createErrorResponse;
import static io.entgra.auth_token_generator.exceptions.ErrorResponse.createErrorResponse;
@Slf4j
@Component
@ -43,49 +43,41 @@ public class TokenService {
@Autowired
private TokenDataHolder tokenDataHolder;
@Value("${client-id}")
@Value("${client_id}")
private String clientId;
@Value("${client-secret}")
@Value("${client_secret}")
private String clientSecret;
@Value("${grant-type}")
@Value("${grant_type}")
private String grantType;
@Value("${user-name}")
@Value("${user_name}")
private String userName;
@Value("${password}")
private String password;
@Value("${refresh-token}")
private String refreshToken;
@Value("${saml2-assertion}")
private String saml2Assertion;
// Method to fetch the token and store it in TokenDataHolder
public ResponseEntity<Object> fetchToken(String scope) {
// Basic validation for required fields
if (clientId == null || clientId.isEmpty()) {
if (StringUtils.isBlank(clientId)) {
return new ResponseEntity<>(createErrorResponse(400,
"Missing client ID",
"clientId is not configured."), HttpStatus.BAD_REQUEST);
}
if (clientSecret == null || clientSecret.isEmpty()) {
if (StringUtils.isBlank(clientSecret)) {
return new ResponseEntity<>(createErrorResponse(400,
"Missing client secret",
"clientSecret is not configured."), HttpStatus.BAD_REQUEST);
}
if (grantType == null || grantType.isEmpty()) {
if (StringUtils.isBlank(grantType)) {
return new ResponseEntity<>(createErrorResponse(400,
"Missing grant type",
"grantType is not configured."), HttpStatus.BAD_REQUEST);
}
if (scope == null || scope.isEmpty()) {
if (StringUtils.isBlank(scope)) {
return new ResponseEntity<>(createErrorResponse(400,
"Missing scope",
"Scope is required to fetch the token."), HttpStatus.BAD_REQUEST);
@ -97,50 +89,37 @@ public class TokenService {
String authHeader;
String body;
// Handle different grant types
switch (grantType) {
case "password":
case "password" -> {
authHeader = "Basic " + encodedAuth;
body = "grant_type=" + grantType + "&username=" + userName + "&password=" + password + "&scope=" + scope;
break;
case "client_credentials":
}
case "client_credentials" -> {
authHeader = "Basic " + encodedAuth;
body = "grant_type=client_credentials&scope=" + scope;
break;
case "refresh_token":
authHeader = "Basic " + encodedAuth;
body = "grant_type=refresh_token&refresh_token=" + refreshToken;
break;
case "urn:ietf:params:oauth:grant-type:saml2-bearer":
authHeader = "Basic " + encodedAuth;
body = "grant_type=urn:ietf:params:oauth:grant-type:saml2-bearer&assertion=" + saml2Assertion;
break;
case "iwa-ntlm":
}
case "iwa-ntlm" -> {
return new ResponseEntity<>(createErrorResponse(501,
"Not Implemented",
"IWA-NTLM grant type not handled directly."), HttpStatus.NOT_IMPLEMENTED);
}
default:
default -> {
return new ResponseEntity<>(createErrorResponse(400,
"Invalid grant type",
"Unsupported grant type: " + grantType), HttpStatus.BAD_REQUEST);
}
}
Map<String, String> response;
try {
// Call FeignClient to generate the token
response = authFeignClient.getToken(body, authHeader);
log.info("Request sent to OAuth2 server to get the token");
if (response != null) {
String accessToken = response.get("access_token");
if (accessToken != null) {
// Store access token and additional information in TokenDataHolder
tokenDataHolder.setAccessToken(accessToken);
tokenDataHolder.setTokenType(response.get("token_type"));
tokenDataHolder.setExpiresIn(Integer.parseInt(response.get("expires_in")));
@ -155,7 +134,6 @@ public class TokenService {
}
}
} catch (FeignException e) {
// Handle FeignException and return corresponding status code
String errorMessage = e.getMessage();
if (errorMessage != null && errorMessage.contains("[401]")) {
@ -181,10 +159,8 @@ public class TokenService {
e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
}
// Default error response if no other condition is met
return new ResponseEntity<>(createErrorResponse(500,
"Unknown Error",
"Failed to fetch the token for unknown reasons."), HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@ -1,5 +1,3 @@
package io.entgra.auth_token_getter.util;
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
@ -17,6 +15,7 @@ package io.entgra.auth_token_getter.util;
* specific language governing permissions and limitations
* under the License.
*/
package io.entgra.auth_token_generator.util;
import lombok.Data;
import org.springframework.stereotype.Component;

@ -1,12 +1,8 @@
#spring.application.name=auth_token_getter
auth_token_generation_uri=https://mgt.sg.local/
client-id=AtczeBBwunLMt7Ol4Xc7eNDEuXsa
client-secret=DX8dGTmN7elF3zQsSfbH4yjVm08a
grant-type=client_credentials
#grant-type=password
refresh-token=""
jwt-token =""
saml2-assertion=""
user-name =""
client_id=nmttDlrcDoNt0qE7p8rXxHiX2AMa
client_secret=H2xaw_kZ77JpmlX8cQmyAjVPK6Ma
grant_type=client_credentials
user_name =""
password = ""

@ -0,0 +1,30 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you 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
*
* 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 io.entgra.auth_token_generator;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class AuthTokenGeneratorApplicationTests {
@Test
void contextLoads() {
}
}

@ -1,13 +0,0 @@
package io.entgra.auth_token_getter;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class AuthTokenGetterApplicationTests {
@Test
void contextLoads() {
}
}
Loading…
Cancel
Save