-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove BaseController, change Principal to Authenticationand un…
…it test cases added
- Loading branch information
1 parent
87e8a5d
commit 15425be
Showing
12 changed files
with
412 additions
and
64 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
...lipse/tractusx/managedidentitywallets/config/security/CustomAuthenticationEntryPoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* ******************************************************************************* | ||
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* ****************************************************************************** | ||
*/ | ||
|
||
package org.eclipse.tractusx.managedidentitywallets.config.security; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.Setter; | ||
import org.eclipse.tractusx.managedidentitywallets.constant.StringPool; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.OAuth2Error; | ||
import org.springframework.security.oauth2.server.resource.BearerTokenError; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* The type Custom authentication entry point. | ||
*/ | ||
@Setter | ||
public final class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
||
private String realmName; | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) { | ||
HttpStatus status = HttpStatus.UNAUTHORIZED; | ||
Map<String, String> parameters = new LinkedHashMap<>(); | ||
|
||
if (this.realmName != null) { | ||
parameters.put("realm", this.realmName); | ||
} | ||
|
||
if (authException instanceof OAuth2AuthenticationException) { | ||
OAuth2Error error = ((OAuth2AuthenticationException) authException).getError(); | ||
parameters.put("error", error.getErrorCode()); | ||
if (StringUtils.hasText(error.getDescription())) { | ||
parameters.put("error_description", error.getDescription()); | ||
} | ||
|
||
if (StringUtils.hasText(error.getUri())) { | ||
parameters.put("error_uri", error.getUri()); | ||
} | ||
|
||
if (error instanceof BearerTokenError bearerTokenError) { | ||
if (StringUtils.hasText(bearerTokenError.getScope())) { | ||
parameters.put("scope", bearerTokenError.getScope()); | ||
} | ||
|
||
status = ((BearerTokenError) error).getHttpStatus(); | ||
} | ||
} | ||
|
||
if (authException.getMessage().contains(StringPool.BPN_NOT_FOUND)) { | ||
status = HttpStatus.FORBIDDEN; | ||
} | ||
|
||
String wwwAuthenticate = computeWWWAuthenticateHeaderValue(parameters); | ||
response.addHeader(HttpHeaders.WWW_AUTHENTICATE, wwwAuthenticate); | ||
response.setStatus(status.value()); | ||
} | ||
|
||
private static String computeWWWAuthenticateHeaderValue(Map<String, String> parameters) { | ||
StringBuilder wwwAuthenticate = new StringBuilder(); | ||
wwwAuthenticate.append("Bearer"); | ||
if (!parameters.isEmpty()) { | ||
wwwAuthenticate.append(" "); | ||
int i = 0; | ||
for (Map.Entry<String, String> entry : parameters.entrySet()) { | ||
wwwAuthenticate.append(entry.getKey()).append("=\"").append(entry.getValue()).append("\""); | ||
if (i != parameters.size() - 1) { | ||
wwwAuthenticate.append(", "); | ||
} | ||
i++; | ||
} | ||
} | ||
return wwwAuthenticate.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.