Skip to content

Commit 523d335

Browse files
feature : authorization code module (in developement)
1 parent a223fc3 commit 523d335

File tree

10 files changed

+35
-9
lines changed

10 files changed

+35
-9
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ public class CommonDataSourceConfiguration {
189189
- **Customize the verification logic for UsernamePassword and Client as desired**
190190
- ``IOauth2AuthenticationHashCheckService``
191191

192+
## OAuth2 - ROPC
193+
* Refer to ``client/src/docs/asciidoc/api-app.adoc``
194+
195+
## OAuth2 - Authorization Code
196+
* Open the web browser by connecting to ``http://localhost:8370/oauth2/authorization?code=32132&grant_type=authorization_code&response_type=code&client_id=client_customer&redirect_uri=http%3A%2F%2Flocalhost%3A8370%2Fcallback1&scope=message.read&state=random-state&prompt=consent&access_type=offline&code_challenge=YOUR_CODE_CHALLENGE&code_challenge_method=S256``
197+
* Login with ``[email protected] / 1234 ``
198+
192199
## Running this App with Docker
193200
* Use the following module for Blue-Green deployment:
194201
* https://github.com/patternknife/docker-blue-green-runner

client/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ server.port=8370
33

44
spring.datasource.hikari.patternknife.url=jdbc:mysql://localhost:13506/sc_oauth2_pji?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true
55
spring.datasource.hikari.patternknife.username=root
6-
spring.datasource.hikari.patternknife.password=pJfV3Ug8Seigl9nArREG
6+
spring.datasource.hikari.patternknife.password=912031kdskdaaa
77

88

99
spring.datasource.hikari.patternknife.hikari.auto-commit=false

src/main/java/io/github/patternknife/securityhelper/oauth2/api/config/security/converter/auth/endpoint/AuthorizationCodeRequestAuthenticationConverter.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
import io.github.patternknife.securityhelper.oauth2.api.config.security.dao.KnifeAuthorizationConsentRepository;
5+
import io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.exception.KnifeOauth2AuthenticationException;
6+
import io.github.patternknife.securityhelper.oauth2.api.config.security.serivce.persistence.authorization.OAuth2AuthorizationServiceImpl;
57
import io.github.patternknife.securityhelper.oauth2.api.config.util.RequestOAuth2Distiller;
68
import jakarta.servlet.http.HttpServletRequest;
79
import lombok.RequiredArgsConstructor;
@@ -14,6 +16,8 @@
1416
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
1517

1618

19+
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
20+
import org.springframework.security.oauth2.server.authorization.OAuth2TokenType;
1721
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeRequestAuthenticationToken;
1822
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken;
1923
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
@@ -34,6 +38,7 @@ public final class AuthorizationCodeRequestAuthenticationConverter implements Au
3438
* */
3539
private final RegisteredClientRepository registeredClientRepository;
3640
private final KnifeAuthorizationConsentRepository knifeAuthorizationConsentRepository;
41+
private final OAuth2AuthorizationServiceImpl oAuth2AuthorizationService;
3742

3843
public void setClientAuthentication(String clientId) {
3944

@@ -54,10 +59,18 @@ public Authentication convert(HttpServletRequest request) {
5459
MultiValueMap<String, String> parameters = RequestOAuth2Distiller.getAuthorizationCodeSecurityAdditionalParameters(request);
5560

5661
// grant_type (REQUIRED)
57-
String grantType = parameters.getFirst(OAuth2ParameterNames.GRANT_TYPE);
58-
if (!AuthorizationGrantType.AUTHORIZATION_CODE.getValue().equals(grantType)) {
59-
// return null;
62+
63+
String authorizationCode = parameters.getFirst(OAuth2ParameterNames.CODE);
64+
if (authorizationCode == null) {
65+
throw new KnifeOauth2AuthenticationException("non -code");
66+
}
67+
68+
69+
OAuth2Authorization oAuth2Authorization = oAuth2AuthorizationService.findByToken(authorizationCode, new OAuth2TokenType("authorization_code"));
70+
if(oAuth2Authorization == null){
71+
throw new KnifeOauth2AuthenticationException("non -code");
6072
}
73+
String principalName = oAuth2Authorization.getPrincipalName();
6174

6275
// 클라이언트 인증 설정
6376
setClientAuthentication(parameters.getFirst(OAuth2ParameterNames.CLIENT_ID));

src/main/java/io/github/patternknife/securityhelper/oauth2/api/config/security/server/ServerConfig.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,20 @@ public SecurityFilterChain authorizationServerSecurityFilterChain(
122122
.oidc(Customizer.withDefaults())
123123
/*
124124
*
125-
* http://localhost:8370/oauth2/authorize?grant_type=authorization_code&response_type=code&client_id=client_customer&scope=read%20write&state=random-state&prompt=consent&access_type=offline
126-
* http://localhost:8370/oauth2/authorize?grant_type=authorization_code&response_type=code&client_id=client_customer&redirect_uri=http%3A%2F%2Flocalhost%3A8370%2Fcallback1&scope=read%20write&state=random-state&prompt=consent&access_type=offline&code_challenge=YOUR_CODE_CHALLENGE&code_challenge_method=S256
125+
*
126+
* http://localhost:8370/oauth2/authorization?code=32132&grant_type=authorization_code&response_type=code&client_id=client_customer&redirect_uri=http%3A%2F%2Flocalhost%3A8370%2Fcallback1&scope=message.read&state=random-state&prompt=consent&access_type=offline&code_challenge=YOUR_CODE_CHALLENGE&code_challenge_method=S256
127127
*
128128
* */
129129
// https://medium.com/@itsinil/oauth-2-1-pkce-%EB%B0%A9%EC%8B%9D-%EC%95%8C%EC%95%84%EB%B3%B4%EA%B8%B0-14500950cdbf
130130
.authorizationEndpoint(authorizationEndpoint ->
131131
authorizationEndpoint
132-
132+
// [1] User goes to the 'consentPage' below ('http://localhost:8370/oauth2/authorization?code=32132&grant_type=authorization_code&response_type=code&client_id=client_customer&redirect_uri=http%3A%2F%2Flocalhost%3A8370%2Fcallback1&scope=message.read&state=random-state&prompt=consent&access_type=offline&code_challenge=YOUR_CODE_CHALLENGE&code_challenge_method=S256')
133+
// [2] As you see 'KnifeAuthorizationCodeRequestConverterController', if the code parameter is NOT authenticated, it redirects you to the login page.
134+
// [3] If the login (/api/v1/traditional-oauth/authorization-code) in the 'src/main/resources/templates/login.html' is successful, it retries [1].
135+
// [4] Now you are on the consent page, check READ & WRITE and then press 'Submit'.
133136
.consentPage(CUSTOM_CONSENT_PAGE_URI)
137+
// [5]
138+
.authorizationRequestConverter(new AuthorizationCodeRequestAuthenticationConverter(registeredClientRepository, knifeAuthorizationConsentRepository, authorizationService))
134139
/* .authorizationRequestConverter(new AuthorizationCodeRequestAuthenticationConverter(registeredClientRepository, knifeAuthorizationConsentRepository))
135140
.authorizationRequestConverters(conveterList -> {
136141
conveterList.add(new AuthorizationCodeAuthenticationConverter(registeredClientRepository));
@@ -165,7 +170,7 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
165170
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
166171

167172
logger.error(exception.toString());
168-
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
173+
response.sendRedirect("login");
169174
}
170175
})
171176

client/src/main/resources/templates/consent.html renamed to src/main/resources/templates/consent.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ <h1 class="text-center text-primary">App permissions</h1>
4949
<form name="consent_form" method="post" th:action="${requestURI}">
5050
<input type="hidden" name="client_id" th:value="${clientId}">
5151
<input type="hidden" name="state" th:value="${state}">
52+
<input type="hidden" name="code" th:value="${code}">
5253
<input th:if="${userCode}" type="hidden" name="user_code" th:value="${userCode}">
5354

5455
<div th:each="scope: ${scopes}" class="form-check py-1">

client/src/main/resources/templates/login.html renamed to src/main/resources/templates/login.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1">
66
<title>Spring Authorization Server Sample</title>
77
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
8-
<link rel="stylesheet" href="/assets/css/signin.css" th:href="@{/assets/css/signin.css}" />
8+
<link rel="stylesheet" href="/css.css" th:href="@{/css/signin.css}" />
99
</head>
1010
<body>
1111
<div class="container">

0 commit comments

Comments
 (0)