diff --git a/auth_token_generator/pom.xml b/auth_token_generator/pom.xml index 57a8048..ed20ffc 100644 --- a/auth_token_generator/pom.xml +++ b/auth_token_generator/pom.xml @@ -1,4 +1,21 @@ + 4.0.0 @@ -8,11 +25,10 @@ 3.3.4 - io.entgra - auth_token_getter - 0.0.1-SNAPSHOT - auth_token_getter - auth_token_getter + o.entgra.oauth.token.generator + oauth-token-generator + 1.0-SNAPSHOT + oauth_token_generator diff --git a/auth_token_generator/src/main/java/io/entgra/auth_token_getter/AuthTokenGetterApplication.java b/auth_token_generator/src/main/java/io/entgra/auth_token_generator/AuthTokenGeneratorApplication.java similarity index 86% rename from auth_token_generator/src/main/java/io/entgra/auth_token_getter/AuthTokenGetterApplication.java rename to auth_token_generator/src/main/java/io/entgra/auth_token_generator/AuthTokenGeneratorApplication.java index 128beee..ff7a7ed 100644 --- a/auth_token_generator/src/main/java/io/entgra/auth_token_getter/AuthTokenGetterApplication.java +++ b/auth_token_generator/src/main/java/io/entgra/auth_token_generator/AuthTokenGeneratorApplication.java @@ -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); } } diff --git a/auth_token_generator/src/main/java/io/entgra/auth_token_getter/client/AuthFeignClient.java b/auth_token_generator/src/main/java/io/entgra/auth_token_generator/client/AuthFeignClient.java similarity index 96% rename from auth_token_generator/src/main/java/io/entgra/auth_token_getter/client/AuthFeignClient.java rename to auth_token_generator/src/main/java/io/entgra/auth_token_generator/client/AuthFeignClient.java index 32d35f3..d371f6a 100644 --- a/auth_token_generator/src/main/java/io/entgra/auth_token_getter/client/AuthFeignClient.java +++ b/auth_token_generator/src/main/java/io/entgra/auth_token_generator/client/AuthFeignClient.java @@ -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; diff --git a/auth_token_generator/src/main/java/io/entgra/auth_token_getter/exceptions/ErrorResponse.java b/auth_token_generator/src/main/java/io/entgra/auth_token_generator/exceptions/ErrorResponse.java similarity index 95% rename from auth_token_generator/src/main/java/io/entgra/auth_token_getter/exceptions/ErrorResponse.java rename to auth_token_generator/src/main/java/io/entgra/auth_token_generator/exceptions/ErrorResponse.java index 91ce3f4..14d16ef 100644 --- a/auth_token_generator/src/main/java/io/entgra/auth_token_getter/exceptions/ErrorResponse.java +++ b/auth_token_generator/src/main/java/io/entgra/auth_token_generator/exceptions/ErrorResponse.java @@ -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; diff --git a/auth_token_generator/src/main/java/io/entgra/auth_token_getter/service/TokenService.java b/auth_token_generator/src/main/java/io/entgra/auth_token_generator/service/TokenService.java similarity index 77% rename from auth_token_generator/src/main/java/io/entgra/auth_token_getter/service/TokenService.java rename to auth_token_generator/src/main/java/io/entgra/auth_token_generator/service/TokenService.java index a346025..729e1e9 100644 --- a/auth_token_generator/src/main/java/io/entgra/auth_token_getter/service/TokenService.java +++ b/auth_token_generator/src/main/java/io/entgra/auth_token_generator/service/TokenService.java @@ -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 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 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); } - } diff --git a/auth_token_generator/src/main/java/io/entgra/auth_token_getter/util/TokenDataHolder.java b/auth_token_generator/src/main/java/io/entgra/auth_token_generator/util/TokenDataHolder.java similarity index 95% rename from auth_token_generator/src/main/java/io/entgra/auth_token_getter/util/TokenDataHolder.java rename to auth_token_generator/src/main/java/io/entgra/auth_token_generator/util/TokenDataHolder.java index a9f6a32..7e4dbd4 100644 --- a/auth_token_generator/src/main/java/io/entgra/auth_token_getter/util/TokenDataHolder.java +++ b/auth_token_generator/src/main/java/io/entgra/auth_token_generator/util/TokenDataHolder.java @@ -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; diff --git a/auth_token_generator/src/main/resources/application.properties b/auth_token_generator/src/main/resources/application.properties index 804b5fa..d16b05d 100644 --- a/auth_token_generator/src/main/resources/application.properties +++ b/auth_token_generator/src/main/resources/application.properties @@ -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 = "" diff --git a/auth_token_generator/src/test/java/io/entgra/auth_token_generator/AuthTokenGeneratorApplicationTests.java b/auth_token_generator/src/test/java/io/entgra/auth_token_generator/AuthTokenGeneratorApplicationTests.java new file mode 100644 index 0000000..58f073d --- /dev/null +++ b/auth_token_generator/src/test/java/io/entgra/auth_token_generator/AuthTokenGeneratorApplicationTests.java @@ -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() { + } + +} diff --git a/auth_token_generator/src/test/java/io/entgra/auth_token_getter/AuthTokenGetterApplicationTests.java b/auth_token_generator/src/test/java/io/entgra/auth_token_getter/AuthTokenGetterApplicationTests.java deleted file mode 100644 index bdd8ffb..0000000 --- a/auth_token_generator/src/test/java/io/entgra/auth_token_getter/AuthTokenGetterApplicationTests.java +++ /dev/null @@ -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() { - } - -}