-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
musab.bozkurt
committed
Feb 3, 2024
1 parent
7453848
commit 7e11c08
Showing
16 changed files
with
991 additions
and
10 deletions.
There are no files selected for viewing
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
63 changes: 63 additions & 0 deletions
63
src/main/java/mb/oauth2authorizationserver/config/CustomPasswordAuthenticationConverter.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,63 @@ | ||
package mb.oauth2authorizationserver.config; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import mb.oauth2authorizationserver.utils.SecurityUtil; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.OAuth2ErrorCodes; | ||
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; | ||
import org.springframework.security.web.authentication.AuthenticationConverter; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.*; | ||
|
||
public class CustomPasswordAuthenticationConverter implements AuthenticationConverter { | ||
|
||
@Nullable | ||
@Override | ||
public Authentication convert(HttpServletRequest request) { | ||
String grantType = request.getParameter(OAuth2ParameterNames.GRANT_TYPE); | ||
|
||
if (!"custom_password".equals(grantType)) { | ||
return null; | ||
} | ||
|
||
MultiValueMap<String, String> parameters = SecurityUtil.getParameters(request); | ||
|
||
// scope (OPTIONAL) | ||
String scope = parameters.getFirst(OAuth2ParameterNames.SCOPE); | ||
if (StringUtils.hasText(scope) && parameters.get(OAuth2ParameterNames.SCOPE).size() != 1) { | ||
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST); | ||
} | ||
|
||
// username (REQUIRED) | ||
String username = parameters.getFirst(OAuth2ParameterNames.USERNAME); | ||
if (!StringUtils.hasText(username) || parameters.get(OAuth2ParameterNames.USERNAME).size() != 1) { | ||
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST); | ||
} | ||
|
||
// password (REQUIRED) | ||
String password = parameters.getFirst(OAuth2ParameterNames.PASSWORD); | ||
if (!StringUtils.hasText(password) || parameters.get(OAuth2ParameterNames.PASSWORD).size() != 1) { | ||
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST); | ||
} | ||
|
||
Set<String> requestedScopes = null; | ||
if (StringUtils.hasText(scope)) { | ||
requestedScopes = new HashSet<>(Arrays.asList(StringUtils.delimitedListToStringArray(scope, " "))); | ||
} | ||
|
||
Map<String, Object> additionalParameters = new HashMap<>(); | ||
parameters.forEach((key, value) -> { | ||
if (!key.equals(OAuth2ParameterNames.GRANT_TYPE) && !key.equals(OAuth2ParameterNames.SCOPE)) { | ||
additionalParameters.put(key, value.getFirst()); | ||
} | ||
}); | ||
|
||
Authentication clientPrincipal = SecurityContextHolder.getContext().getAuthentication(); | ||
return new CustomPasswordAuthenticationToken(clientPrincipal, requestedScopes, additionalParameters); | ||
} | ||
} |
Oops, something went wrong.