Search in sources :

Example 1 with OAuth2ClientEntity

use of org.codenergic.theskeleton.client.OAuth2ClientEntity in project theskeleton by codenergic.

the class UserOAuth2ClientApprovalStore method addApprovals.

@Override
public boolean addApprovals(Collection<Approval> approvals) {
    Objects.requireNonNull(approvals);
    approvals.forEach(a -> {
        UserOAuth2ClientApprovalEntity approval = approvalRepository.findByUserUsernameAndClientIdAndScope(a.getUserId(), a.getClientId(), a.getScope());
        UserOAuth2ClientApprovalEntity newApproval = new UserOAuth2ClientApprovalEntity();
        if (approval != null) {
            newApproval.setId(approval.getId());
            newApproval.setCreatedBy(approval.getCreatedBy());
            newApproval.setCreatedDate(approval.getCreatedDate());
        }
        newApproval.setUser(userRepository.findByUsername(a.getUserId())).setClient(new OAuth2ClientEntity().setId(a.getClientId())).setScope(a.getScope()).setApprovalStatus(a.getStatus());
        approvalRepository.save(newApproval);
    });
    return true;
}
Also used : OAuth2ClientEntity(org.codenergic.theskeleton.client.OAuth2ClientEntity) UserOAuth2ClientApprovalEntity(org.codenergic.theskeleton.user.UserOAuth2ClientApprovalEntity)

Example 2 with OAuth2ClientEntity

use of org.codenergic.theskeleton.client.OAuth2ClientEntity in project theskeleton by codenergic.

the class ProfileRestControllerTest method testFindProfileConnectedApps.

@Test
@WithMockUser("user123")
public void testFindProfileConnectedApps() throws Exception {
    final UserEntity user = new UserEntity().setUsername("user123");
    final OAuth2ClientEntity client = new OAuth2ClientEntity().setId("client123").setName("client123");
    final UserOAuth2ClientApprovalEntity approval1 = new UserOAuth2ClientApprovalEntity().setUser(user).setClient(client).setScope("scope1").setApprovalStatus(Approval.ApprovalStatus.APPROVED);
    final UserOAuth2ClientApprovalEntity approval2 = new UserOAuth2ClientApprovalEntity().setUser(user).setClient(client).setScope("scope2").setApprovalStatus(Approval.ApprovalStatus.DENIED);
    when(profileService.findOAuth2ClientApprovalByUsername("user123")).thenReturn(Arrays.asList(approval1, approval2));
    MockHttpServletRequestBuilder request = get("/api/profile/connected-apps").contentType(MediaType.APPLICATION_JSON);
    MockHttpServletResponse response = mockMvc.perform(request).andDo(document("user-profile-connected-apps-view")).andReturn().getResponse();
    assertThat(response.getStatus()).isEqualTo(200);
    CollectionType collectionType = objectMapper.getTypeFactory().constructCollectionType(List.class, UserOAuth2ClientApprovalRestData.class);
    List<UserOAuth2ClientApprovalRestData> returnedData = objectMapper.readValue(response.getContentAsByteArray(), collectionType);
    assertThat(returnedData).hasSize(1);
    UserOAuth2ClientApprovalRestData restData = returnedData.get(0);
    assertThat(restData.getClientId()).isEqualTo(client.getId());
    assertThat(restData.getClientName()).isEqualTo(client.getName());
    assertThat(restData.getUsername()).isEqualTo(user.getUsername());
    ImmutableMap<String, Approval.ApprovalStatus> scopeAndStatus = restData.getScopeAndStatus();
    assertThat(scopeAndStatus).hasSize(2);
    assertThat(scopeAndStatus).containsEntry(approval1.getScope(), approval1.getApprovalStatus());
    assertThat(scopeAndStatus).containsEntry(approval2.getScope(), approval2.getApprovalStatus());
    verify(profileService).findOAuth2ClientApprovalByUsername("user123");
}
Also used : OAuth2ClientEntity(org.codenergic.theskeleton.client.OAuth2ClientEntity) UserOAuth2ClientApprovalRestData(org.codenergic.theskeleton.user.UserOAuth2ClientApprovalRestData) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) UserOAuth2ClientApprovalEntity(org.codenergic.theskeleton.user.UserOAuth2ClientApprovalEntity) UserEntity(org.codenergic.theskeleton.user.UserEntity) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 3 with OAuth2ClientEntity

use of org.codenergic.theskeleton.client.OAuth2ClientEntity in project theskeleton by codenergic.

the class OAuth2ClientServiceImpl method generateSecret.

@Override
@Transactional
public OAuth2ClientEntity generateSecret(String clientId) {
    OAuth2ClientEntity client = findClientById(clientId);
    client.setClientSecret(passwordEncoder.encode(clientId));
    return client;
}
Also used : OAuth2ClientEntity(org.codenergic.theskeleton.client.OAuth2ClientEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with OAuth2ClientEntity

use of org.codenergic.theskeleton.client.OAuth2ClientEntity in project theskeleton by codenergic.

the class UserOauth2ClientApprovalStoreTest method testRevokeApprovals.

@Test
public void testRevokeApprovals() {
    assertThatThrownBy(() -> {
        approvalStore.revokeApprovals(null);
    }).isInstanceOf(NullPointerException.class);
    when(approvalRepository.findByUserUsernameAndClientIdAndScope(anyString(), anyString(), eq("read"))).thenReturn(new UserOAuth2ClientApprovalEntity().setUser(new UserEntity().setId("1")).setClient(new OAuth2ClientEntity().setId("2")).setScope("read").setApprovalStatus(ApprovalStatus.APPROVED));
    when(approvalRepository.findByUserUsernameAndClientIdAndScope(anyString(), anyString(), eq("write"))).thenReturn(null);
    List<Approval> approvals = new ArrayList<>();
    approvals.add(new Approval("", "", "write", new Date(), ApprovalStatus.APPROVED));
    for (int i = 0; i < 3; i++) {
        approvals.add(new Approval("", "", "read", new Date(), ApprovalStatus.APPROVED));
    }
    approvalStore.revokeApprovals(approvals);
    verify(approvalRepository, times(1)).findByUserUsernameAndClientIdAndScope(anyString(), anyString(), eq("write"));
    verify(approvalRepository, times(3)).findByUserUsernameAndClientIdAndScope(anyString(), anyString(), eq("read"));
    verify(approvalRepository, times(3)).delete(any(UserOAuth2ClientApprovalEntity.class));
}
Also used : OAuth2ClientEntity(org.codenergic.theskeleton.client.OAuth2ClientEntity) ArrayList(java.util.ArrayList) Approval(org.springframework.security.oauth2.provider.approval.Approval) Date(java.util.Date) Test(org.junit.Test)

Example 5 with OAuth2ClientEntity

use of org.codenergic.theskeleton.client.OAuth2ClientEntity in project theskeleton by codenergic.

the class UserOauth2ClientApprovalStoreTest method testGetApprovals.

@Test
@SuppressWarnings("serial")
public void testGetApprovals() {
    when(approvalRepository.findByUserUsernameAndClientId(anyString(), anyString())).thenReturn(Arrays.asList(new UserOAuth2ClientApprovalEntity() {

        {
            setCreatedDate(new Date());
        }
    }.setApprovalStatus(ApprovalStatus.APPROVED).setUser(new UserEntity()).setClient(new OAuth2ClientEntity()), new UserOAuth2ClientApprovalEntity() {

        {
            setCreatedDate(new Date());
        }
    }.setApprovalStatus(ApprovalStatus.DENIED).setUser(new UserEntity()).setClient(new OAuth2ClientEntity())));
    List<Approval> approvals = new ArrayList<>(approvalStore.getApprovals("1", "2"));
    assertThat(approvals.size()).isEqualTo(2);
    assertThat(approvals.get(0).getStatus()).isEqualTo(ApprovalStatus.APPROVED);
    assertThat(approvals.get(1).getStatus()).isEqualTo(ApprovalStatus.DENIED);
    verify(approvalRepository).findByUserUsernameAndClientId(anyString(), anyString());
}
Also used : OAuth2ClientEntity(org.codenergic.theskeleton.client.OAuth2ClientEntity) ArrayList(java.util.ArrayList) Approval(org.springframework.security.oauth2.provider.approval.Approval) Date(java.util.Date) Test(org.junit.Test)

Aggregations

OAuth2ClientEntity (org.codenergic.theskeleton.client.OAuth2ClientEntity)10 Test (org.junit.Test)6 Date (java.util.Date)3 UserOAuth2ClientApprovalEntity (org.codenergic.theskeleton.user.UserOAuth2ClientApprovalEntity)3 Approval (org.springframework.security.oauth2.provider.approval.Approval)3 CollectionType (com.fasterxml.jackson.databind.type.CollectionType)2 ArrayList (java.util.ArrayList)2 UserEntity (org.codenergic.theskeleton.user.UserEntity)2 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 WithMockUser (org.springframework.security.test.context.support.WithMockUser)2 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)2 Transactional (org.springframework.transaction.annotation.Transactional)2 UserOAuth2ClientApprovalRestData (org.codenergic.theskeleton.user.UserOAuth2ClientApprovalRestData)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1