|
1 | 1 | package com.patternknife.securityhelper.oauth2.client.integration.auth;
|
2 | 2 |
|
3 | 3 |
|
| 4 | +import com.patternknife.securityhelper.oauth2.api.config.response.error.message.SecurityUserExceptionMessage; |
4 | 5 | import com.patternknife.securityhelper.oauth2.api.config.security.KnifeHttpHeaders;
|
5 | 6 | import jakarta.xml.bind.DatatypeConverter;
|
6 | 7 | import lombok.SneakyThrows;
|
|
47 | 48 | import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
48 | 49 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
49 | 50 |
|
| 51 | + |
| 52 | + |
| 53 | +/* |
| 54 | +* Functions ending with |
| 55 | +* "ORIGINAL" : '/oauth2/token' |
| 56 | +* "EXPOSED" : '/api/v1/traditional-oauth/token' |
| 57 | +* */ |
50 | 58 | @ExtendWith(RestDocumentationExtension.class)
|
51 | 59 | @ExtendWith(SpringExtension.class)
|
52 | 60 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@@ -432,6 +440,134 @@ public void test_SameAppTokensUseSameAccessToken_ORIGINAL() throws Exception {
|
432 | 440 | }
|
433 | 441 | }
|
434 | 442 |
|
| 443 | + @Test |
| 444 | + public void testLoginWithInvalidCredentials_ORIGINAL() throws Exception { |
| 445 | + |
| 446 | + |
| 447 | + MvcResult result = mockMvc.perform(RestDocumentationRequestBuilders.post("/oauth2/token") |
| 448 | + .header(HttpHeaders.AUTHORIZATION, basicHeader) |
| 449 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 450 | + .param("grant_type", "password") |
| 451 | + .param("username", testUserName + "wrongcredential") |
| 452 | + .param("password", testUserPassword)) |
| 453 | + .andExpect(status().isUnauthorized()) // 401 |
| 454 | + .andDo(document( "{class-name}/{method-name}/oauth-access-token", |
| 455 | + preprocessRequest(new AccessTokenMaskingPreprocessor()), |
| 456 | + preprocessResponse(new AccessTokenMaskingPreprocessor(), prettyPrint()), |
| 457 | + requestHeaders( |
| 458 | + headerWithName(HttpHeaders.AUTHORIZATION).description("Connect the received client_id and client_secret with ':', use the base64 function, and write Basic at the beginning. ex) Basic base64(client_id:client_secret)"), |
| 459 | + headerWithName(KnifeHttpHeaders.APP_TOKEN).optional().description("Not having a value does not mean you cannot log in, but cases without an App-Token value share the same access_token. Please include it as a required value according to the device-specific session policy.") |
| 460 | + ), |
| 461 | + formParameters( |
| 462 | + parameterWithName("grant_type").description("Uses the password method among Oauth2 grant_types. Please write password."), |
| 463 | + parameterWithName("username").description("This is the user's email address."), |
| 464 | + parameterWithName("password").description("This is the user's password.") |
| 465 | + ))) |
| 466 | + .andReturn(); |
| 467 | + |
| 468 | + |
| 469 | + String responseString = result.getResponse().getContentAsString(); |
| 470 | + JSONObject jsonResponse = new JSONObject(responseString); |
| 471 | + String userMessage = jsonResponse.getString("userMessage"); |
| 472 | + |
| 473 | + assertEquals(userMessage, SecurityUserExceptionMessage.AUTHENTICATION_LOGIN_FAILURE.getMessage()); |
| 474 | + |
| 475 | + |
| 476 | + |
| 477 | + result = mockMvc.perform(RestDocumentationRequestBuilders.post("/oauth2/token") |
| 478 | + .header(HttpHeaders.AUTHORIZATION, "Basic " + DatatypeConverter.printBase64Binary((appUserClientId + "wrongcred:" + appUserClientSecret).getBytes("UTF-8"))) |
| 479 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 480 | + .param("grant_type", "password") |
| 481 | + .param("username", testUserName) |
| 482 | + .param("password", testUserPassword)) |
| 483 | + .andExpect(status().isUnauthorized()) // 401 |
| 484 | + .andDo(document( "{class-name}/{method-name}/oauth-access-token", |
| 485 | + preprocessRequest(new AccessTokenMaskingPreprocessor()), |
| 486 | + preprocessResponse(new AccessTokenMaskingPreprocessor(), prettyPrint()), |
| 487 | + requestHeaders( |
| 488 | + headerWithName(HttpHeaders.AUTHORIZATION).description("Connect the received client_id and client_secret with ':', use the base64 function, and write Basic at the beginning. ex) Basic base64(client_id:client_secret)"), |
| 489 | + headerWithName(KnifeHttpHeaders.APP_TOKEN).optional().description("Not having a value does not mean you cannot log in, but cases without an App-Token value share the same access_token. Please include it as a required value according to the device-specific session policy.") |
| 490 | + ), |
| 491 | + formParameters( |
| 492 | + parameterWithName("grant_type").description("Uses the password method among Oauth2 grant_types. Please write password."), |
| 493 | + parameterWithName("username").description("This is the user's email address."), |
| 494 | + parameterWithName("password").description("This is the user's password.") |
| 495 | + ))) |
| 496 | + .andReturn(); |
| 497 | + |
| 498 | + |
| 499 | + responseString = result.getResponse().getContentAsString(); |
| 500 | + jsonResponse = new JSONObject(responseString); |
| 501 | + userMessage = jsonResponse.getString("userMessage"); |
| 502 | + |
| 503 | + assertEquals(userMessage, SecurityUserExceptionMessage.WRONG_CLIENT_ID_SECRET.getMessage()); |
| 504 | + } |
| 505 | + |
| 506 | + |
| 507 | + @Test |
| 508 | + public void testLoginWithInvalidCredentials_EXPOSE() throws Exception { |
| 509 | + |
| 510 | + MvcResult result = mockMvc.perform(RestDocumentationRequestBuilders.post("/api/v1/traditional-oauth/token") |
| 511 | + .header(HttpHeaders.AUTHORIZATION, basicHeader) |
| 512 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 513 | + .param("grant_type", "password") |
| 514 | + .param("username", testUserName + "wrongcredential") |
| 515 | + .param("password", testUserPassword)) |
| 516 | + .andExpect(status().isUnauthorized()) // 401 |
| 517 | + .andDo(document( "{class-name}/{method-name}/oauth-access-token", |
| 518 | + preprocessRequest(new AccessTokenMaskingPreprocessor()), |
| 519 | + preprocessResponse(new AccessTokenMaskingPreprocessor(), prettyPrint()), |
| 520 | + requestHeaders( |
| 521 | + headerWithName(HttpHeaders.AUTHORIZATION).description("Connect the received client_id and client_secret with ':', use the base64 function, and write Basic at the beginning. ex) Basic base64(client_id:client_secret)"), |
| 522 | + headerWithName(KnifeHttpHeaders.APP_TOKEN).optional().description("Not having a value does not mean you cannot log in, but cases without an App-Token value share the same access_token. Please include it as a required value according to the device-specific session policy.") |
| 523 | + ), |
| 524 | + formParameters( |
| 525 | + parameterWithName("grant_type").description("Uses the password method among Oauth2 grant_types. Please write password."), |
| 526 | + parameterWithName("username").description("This is the user's email address."), |
| 527 | + parameterWithName("password").description("This is the user's password.") |
| 528 | + ))) |
| 529 | + .andReturn(); |
| 530 | + |
| 531 | + |
| 532 | + String responseString = result.getResponse().getContentAsString(); |
| 533 | + JSONObject jsonResponse = new JSONObject(responseString); |
| 534 | + String userMessage = jsonResponse.getString("userMessage"); |
| 535 | + |
| 536 | + assertEquals(userMessage, SecurityUserExceptionMessage.AUTHENTICATION_LOGIN_FAILURE.getMessage()); |
| 537 | + |
| 538 | + |
| 539 | + |
| 540 | + result = mockMvc.perform(RestDocumentationRequestBuilders.post("/api/v1/traditional-oauth/token") |
| 541 | + .header(HttpHeaders.AUTHORIZATION, "Basic " + DatatypeConverter.printBase64Binary((appUserClientId + "wrongcred:" + appUserClientSecret).getBytes("UTF-8"))) |
| 542 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 543 | + .param("grant_type", "password") |
| 544 | + .param("username", testUserName) |
| 545 | + .param("password", testUserPassword)) |
| 546 | + .andExpect(status().isUnauthorized()) // 401 |
| 547 | + .andDo(document( "{class-name}/{method-name}/oauth-access-token", |
| 548 | + preprocessRequest(new AccessTokenMaskingPreprocessor()), |
| 549 | + preprocessResponse(new AccessTokenMaskingPreprocessor(), prettyPrint()), |
| 550 | + requestHeaders( |
| 551 | + headerWithName(HttpHeaders.AUTHORIZATION).description("Connect the received client_id and client_secret with ':', use the base64 function, and write Basic at the beginning. ex) Basic base64(client_id:client_secret)"), |
| 552 | + headerWithName(KnifeHttpHeaders.APP_TOKEN).optional().description("Not having a value does not mean you cannot log in, but cases without an App-Token value share the same access_token. Please include it as a required value according to the device-specific session policy.") |
| 553 | + ), |
| 554 | + formParameters( |
| 555 | + parameterWithName("grant_type").description("Uses the password method among Oauth2 grant_types. Please write password."), |
| 556 | + parameterWithName("username").description("This is the user's email address."), |
| 557 | + parameterWithName("password").description("This is the user's password.") |
| 558 | + ))) |
| 559 | + .andReturn(); |
| 560 | + |
| 561 | + |
| 562 | + responseString = result.getResponse().getContentAsString(); |
| 563 | + jsonResponse = new JSONObject(responseString); |
| 564 | + userMessage = jsonResponse.getString("userMessage"); |
| 565 | + |
| 566 | + assertEquals(userMessage, SecurityUserExceptionMessage.WRONG_CLIENT_ID_SECRET.getMessage()); |
| 567 | + } |
| 568 | + |
| 569 | + |
| 570 | + |
435 | 571 | private static class AccessTokenMaskingPreprocessor implements OperationPreprocessor {
|
436 | 572 |
|
437 | 573 | @Override
|
|
0 commit comments