Search in sources :

Example 1 with Client

use of com.hw.helper.Client in project mt-auth by publicdevop2019.

the class BizClientIdempotentTest method create_client_then_update_w_same_changeId_two_times.

@Test
public void create_client_then_update_w_same_changeId_two_times() {
    ResponseEntity<DefaultOAuth2AccessToken> tokenResponse = action.getJwtPassword(ACCOUNT_USERNAME_ROOT, ACCOUNT_PASSWORD_ROOT);
    String bearer = tokenResponse.getBody().getValue();
    String s = UUID.randomUUID().toString();
    Client oldClient = action.getClientAsResource(CLIENT_ID_RESOURCE_ID);
    ResponseEntity<String> client1 = action.createClient(oldClient, s);
    Assert.assertEquals(HttpStatus.OK, client1.getStatusCode());
    oldClient.setAccessTokenValiditySeconds(120);
    String url = UserAction.proxyUrl + UserAction.SVC_NAME_AUTH + CLIENTS + "/" + client1.getHeaders().getLocation().toString();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setBearerAuth(bearer);
    String s2 = UUID.randomUUID().toString();
    headers.set("changeId", s2);
    headers.set("X-XSRF-TOKEN", "123");
    headers.add(HttpHeaders.COOKIE, "XSRF-TOKEN=123");
    oldClient.setVersion(0);
    HttpEntity<Client> request = new HttpEntity<>(oldClient, headers);
    ResponseEntity<String> exchange = action.restTemplate.exchange(url, HttpMethod.PUT, request, String.class);
    Assert.assertEquals(HttpStatus.OK, exchange.getStatusCode());
    oldClient.setVersion(1);
    ResponseEntity<String> exchange2 = action.restTemplate.exchange(url, HttpMethod.PUT, request, String.class);
    Assert.assertEquals(HttpStatus.OK, exchange2.getStatusCode());
}
Also used : Client(com.hw.helper.Client) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with Client

use of com.hw.helper.Client in project mt-auth by publicdevop2019.

the class BizClientIdempotentTest method create_client_then_delete_w_same_changeId_two_times.

@Test
public void create_client_then_delete_w_same_changeId_two_times() {
    ResponseEntity<DefaultOAuth2AccessToken> tokenResponse = action.getJwtPassword(ACCOUNT_USERNAME_ROOT, ACCOUNT_PASSWORD_ROOT);
    String bearer = tokenResponse.getBody().getValue();
    String s = UUID.randomUUID().toString();
    Client oldClient = action.getClientAsResource(CLIENT_ID_RESOURCE_ID);
    ResponseEntity<String> client1 = action.createClient(oldClient, s);
    Assert.assertEquals(HttpStatus.OK, client1.getStatusCode());
    oldClient.setAccessTokenValiditySeconds(120);
    String url = UserAction.proxyUrl + UserAction.SVC_NAME_AUTH + CLIENTS + "/" + client1.getHeaders().getLocation().toString();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setBearerAuth(bearer);
    String s2 = UUID.randomUUID().toString();
    headers.set("changeId", s2);
    headers.set("X-XSRF-TOKEN", "123");
    headers.add(HttpHeaders.COOKIE, "XSRF-TOKEN=123");
    HttpEntity<Client> request = new HttpEntity<>(oldClient, headers);
    ResponseEntity<String> exchange = action.restTemplate.exchange(url, HttpMethod.DELETE, request, String.class);
    Assert.assertEquals(HttpStatus.OK, exchange.getStatusCode());
    ResponseEntity<String> exchange2 = action.restTemplate.exchange(url, HttpMethod.DELETE, request, String.class);
    Assert.assertEquals(HttpStatus.OK, exchange2.getStatusCode());
}
Also used : Client(com.hw.helper.Client) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with Client

use of com.hw.helper.Client in project mt-auth by publicdevop2019.

the class BizClientIdempotentTest method create_client_then_update_w_same_changeId_two_times_concurrent.

@Test
public void create_client_then_update_w_same_changeId_two_times_concurrent() {
    ResponseEntity<DefaultOAuth2AccessToken> tokenResponse = action.getJwtPassword(ACCOUNT_USERNAME_ROOT, ACCOUNT_PASSWORD_ROOT);
    String bearer = tokenResponse.getBody().getValue();
    String s = UUID.randomUUID().toString();
    Client oldClient = action.getClientAsResource(CLIENT_ID_RESOURCE_ID);
    ResponseEntity<String> client1 = action.createClient(oldClient, s);
    Assert.assertEquals(HttpStatus.OK, client1.getStatusCode());
    oldClient.setAccessTokenValiditySeconds(120);
    String url = UserAction.proxyUrl + UserAction.SVC_NAME_AUTH + CLIENTS + "/" + client1.getHeaders().getLocation().toString();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setBearerAuth(bearer);
    String s2 = UUID.randomUUID().toString();
    headers.set("changeId", s2);
    headers.set("X-XSRF-TOKEN", "123");
    headers.add(HttpHeaders.COOKIE, "XSRF-TOKEN=123");
    oldClient.setVersion(0);
    HttpEntity<Client> request = new HttpEntity<>(oldClient, headers);
    Runnable runnable2 = () -> {
        ResponseEntity<String> exchange = action.restTemplate.exchange(url, HttpMethod.PUT, request, String.class);
        Assert.assertEquals(HttpStatus.OK, exchange.getStatusCode());
    };
    ArrayList<Runnable> runnables = new ArrayList<>();
    runnables.add(runnable2);
    runnables.add(runnable2);
    try {
        assertConcurrent("", runnables, 30000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : ArrayList(java.util.ArrayList) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Client(com.hw.helper.Client) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with Client

use of com.hw.helper.Client in project mt-auth by publicdevop2019.

the class BizClientIdempotentTest method create_client_then_delete_w_same_changeId_two_times_concurrent.

@Test
public void create_client_then_delete_w_same_changeId_two_times_concurrent() {
    ResponseEntity<DefaultOAuth2AccessToken> tokenResponse = action.getJwtPassword(ACCOUNT_USERNAME_ROOT, ACCOUNT_PASSWORD_ROOT);
    String bearer = tokenResponse.getBody().getValue();
    String s = UUID.randomUUID().toString();
    Client oldClient = action.getClientAsResource(CLIENT_ID_RESOURCE_ID);
    ResponseEntity<String> client1 = action.createClient(oldClient, s);
    Assert.assertEquals(HttpStatus.OK, client1.getStatusCode());
    oldClient.setAccessTokenValiditySeconds(120);
    String url = UserAction.proxyUrl + UserAction.SVC_NAME_AUTH + CLIENTS + "/" + client1.getHeaders().getLocation().toString();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setBearerAuth(bearer);
    String s2 = UUID.randomUUID().toString();
    headers.set("changeId", s2);
    headers.set("X-XSRF-TOKEN", "123");
    headers.add(HttpHeaders.COOKIE, "XSRF-TOKEN=123");
    HttpEntity<Client> request = new HttpEntity<>(oldClient, headers);
    Runnable runnable2 = () -> {
        ResponseEntity<String> exchange = action.restTemplate.exchange(url, HttpMethod.DELETE, request, String.class);
        Assert.assertEquals(HttpStatus.OK, exchange.getStatusCode());
    };
    ArrayList<Runnable> runnables = new ArrayList<>();
    runnables.add(runnable2);
    runnables.add(runnable2);
    try {
        assertConcurrent("", runnables, 30000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : ArrayList(java.util.ArrayList) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Client(com.hw.helper.Client) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with Client

use of com.hw.helper.Client in project mt-auth by publicdevop2019.

the class RefreshTokenTest method refresh_token_should_have_exp.

@Test
public void refresh_token_should_have_exp() {
    // create client supports refresh token
    Client clientRaw = ClientUtility.getClientRaw();
    String clientSecret = clientRaw.getClientSecret();
    HashSet<GrantTypeEnum> enums = new HashSet<>();
    enums.add(GrantTypeEnum.PASSWORD);
    enums.add(GrantTypeEnum.REFRESH_TOKEN);
    clientRaw.setResourceIds(Collections.singleton(AppConstant.CLIENT_ID_OAUTH2_ID));
    clientRaw.setGrantTypeEnums(enums);
    clientRaw.setTypes(new HashSet<>(List.of(ClientType.FIRST_PARTY)));
    clientRaw.setAccessTokenValiditySeconds(60);
    clientRaw.setRefreshTokenValiditySeconds(1000);
    ResponseEntity<String> client = ClientUtility.createClient(clientRaw);
    String clientId = client.getHeaders().getLocation().toString();
    Assert.assertEquals(HttpStatus.OK, client.getStatusCode());
    // get jwt
    ResponseEntity<DefaultOAuth2AccessToken> jwtPasswordWithClient = OAuth2Utility.getOAuth2PasswordToken(clientId, clientSecret, AppConstant.ACCOUNT_USERNAME_ADMIN, AppConstant.ACCOUNT_PASSWORD_ADMIN);
    Assert.assertEquals(HttpStatus.OK, jwtPasswordWithClient.getStatusCode());
    OAuth2RefreshToken refreshToken = jwtPasswordWithClient.getBody().getRefreshToken();
    String jwt = refreshToken.getValue();
    String jwtBody;
    try {
        jwtBody = jwt.split("\\.")[1];
    } catch (ArrayIndexOutOfBoundsException ex) {
        throw new IllegalArgumentException("malformed jwt token");
    }
    Base64.Decoder decoder = Base64.getDecoder();
    byte[] decode = decoder.decode(jwtBody);
    String s = new String(decode);
    Integer exp;
    try {
        Map<String, Object> var0 = TestContext.mapper.readValue(s, new TypeReference<Map<String, Object>>() {
        });
        exp = (Integer) var0.get("exp");
    } catch (IOException e) {
        throw new IllegalArgumentException("unable to find authorities in authorization header");
    }
    Assert.assertNotNull(exp);
}
Also used : OAuth2RefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken) Base64(java.util.Base64) IOException(java.io.IOException) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) GrantTypeEnum(com.hw.helper.GrantTypeEnum) Client(com.hw.helper.Client) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Client (com.hw.helper.Client)36 Test (org.junit.Test)30 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)23 HttpEntity (org.springframework.http.HttpEntity)18 HttpHeaders (org.springframework.http.HttpHeaders)18 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)12 HashSet (java.util.HashSet)7 ArrayList (java.util.ArrayList)6 GrantTypeEnum (com.hw.helper.GrantTypeEnum)4 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)4 MultiValueMap (org.springframework.util.MultiValueMap)4 ClientType (com.hw.helper.ClientType)3 SumTotal (com.hw.helper.SumTotal)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ResponseEntity (org.springframework.http.ResponseEntity)3 IOException (java.io.IOException)1 Base64 (java.util.Base64)1 Map (java.util.Map)1 Ignore (org.junit.Ignore)1 OAuth2RefreshToken (org.springframework.security.oauth2.common.OAuth2RefreshToken)1