org.springframework.security.oauth2.provider.approval.Approval Java Examples

The following examples show how to use org.springframework.security.oauth2.provider.approval.Approval. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AccessConfirmationController.java    From OpenESPI-DataCustodian-java with Apache License 2.0 6 votes vote down vote up
@RequestMapping("/oauth/confirm_access")
public ModelAndView getAccessConfirmation(Map<String, Object> model, Principal principal) throws Exception {
	AuthorizationRequest clientAuth = (AuthorizationRequest) model.remove("authorizationRequest");
	ClientDetails client = clientDetailsService.loadClientByClientId(clientAuth.getClientId());
	model.put("auth_request", clientAuth);
	model.put("client", client);
	Map<String, String> scopes = new LinkedHashMap<String, String>();
	for (String scope : clientAuth.getScope()) {
		scopes.put(OAuth2Utils.SCOPE_PREFIX + scope, "false");  //Spring Security OAuth2 2.0.0.M2 change
	}
	for (Approval approval : approvalStore.getApprovals(principal.getName(), client.getClientId())) {
		if (clientAuth.getScope().contains(approval.getScope())) {
			scopes.put(OAuth2Utils.SCOPE_PREFIX + approval.getScope(),
					approval.getStatus() == ApprovalStatus.APPROVED ? "true" : "false");
		}
	}
	model.put("scopes", scopes);
	return new ModelAndView("access_confirmation", model);
}
 
Example #2
Source File: AccessConfirmationController.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
@RequestMapping("/oauth/confirm_access")
public ModelAndView getAccessConfirmation(Map<String, Object> model, Principal principal) throws Exception {
    AuthorizationRequest clientAuth = (AuthorizationRequest) model.remove("authorizationRequest");
    ClientDetails client = clientDetailsService.loadClientByClientId(clientAuth.getClientId());
    model.put("auth_request", clientAuth);
    model.put("client", client);
    Map<String, String> scopes = new LinkedHashMap<String, String>();
    for (String scope : clientAuth.getScope()) {
        scopes.put(OAuth2Utils.SCOPE_PREFIX + scope, "false");
    }
    for (Approval approval : approvalStore.getApprovals(principal.getName(), client.getClientId())) {
        if (clientAuth.getScope().contains(approval.getScope())) {
            scopes.put(OAuth2Utils.SCOPE_PREFIX + approval.getScope(),
                    approval.getStatus() == Approval.ApprovalStatus.APPROVED ? "true" : "false");
        }
    }
    model.put("scopes", scopes);
    return new ModelAndView("access_confirmation", model); // 订阅 appproval 页面
}
 
Example #3
Source File: MongoApprovalStore.java    From spring-security-mongo with MIT License 6 votes vote down vote up
@Override
public boolean revokeApprovals(final Collection<Approval> approvals) {
    boolean success = true;

    final Collection<MongoApproval> mongoApprovals = transformToMongoApproval(approvals);

    for (final MongoApproval mongoApproval : mongoApprovals) {
        if (handleRevocationsAsExpiry) {
            final boolean updateResult = mongoApprovalRepository.updateExpiresAt(LocalDateTime.now(), mongoApproval);
            if (!updateResult) {
                success = false;
            }

        }
        else {
            final boolean deleteResult = mongoApprovalRepository.deleteByUserIdAndClientIdAndScope(mongoApproval);

            if (!deleteResult) {
                success = false;
            }
        }
    }
    return success;
}
 
Example #4
Source File: MongoApprovalStoreTest.java    From spring-security-mongo with MIT License 6 votes vote down vote up
@Test
public void shouldGetApprovals() {
    //Given
    final String userId = string().next();
    final String clientId = string().next();

    //And
    final List<MongoApproval> expectedMongoApprovals = list(ofMongoApproval()).next();
    given(mongoApprovalRepository.findByUserIdAndClientId(userId, clientId)).willReturn(expectedMongoApprovals);

    //When
    final Collection<Approval> approvals = mongoApprovalStore.getApprovals(userId, clientId);

    //Then
    assertThat(approvals).hasSameSizeAs(expectedMongoApprovals);
}
 
Example #5
Source File: MongoApprovalStoreTest.java    From spring-security-mongo with MIT License 6 votes vote down vote up
@Test
public void shouldReturnFalseWhenHandleRevocationsAsExpiryIsTrueAndThereIsNothingToDelete() {
    //Given
    final List<Approval> approvals = list(ofApproval()).next();

    //And
    mongoApprovalStore.setHandleRevocationsAsExpiry(true);

    //And
    given(mongoApprovalRepository.updateExpiresAt(any(LocalDateTime.class), any(MongoApproval.class))).willReturn(false);

    //When
    final boolean result = mongoApprovalStore.revokeApprovals(approvals);

    //Then
    assertThat(result).isFalse();
    verify(mongoApprovalRepository, never()).deleteByUserIdAndClientIdAndScope(any(MongoApproval.class));
}
 
Example #6
Source File: MongoApprovalStoreTest.java    From spring-security-mongo with MIT License 6 votes vote down vote up
@Test
public void shouldRevokeApprovalsByUpdateWhenHandleRevocationsAsExpiryIsTrue() {
    //Given
    final List<Approval> approvals = list(ofApproval()).next();

    //And
    mongoApprovalStore.setHandleRevocationsAsExpiry(true);

    //And
    given(mongoApprovalRepository.updateExpiresAt(any(LocalDateTime.class), any(MongoApproval.class))).willReturn(true);

    //When
    final boolean result = mongoApprovalStore.revokeApprovals(approvals);

    //Then
    assertThat(result).isTrue();
    verify(mongoApprovalRepository, never()).deleteByUserIdAndClientIdAndScope(any(MongoApproval.class));
}
 
Example #7
Source File: MongoApprovalStoreTest.java    From spring-security-mongo with MIT License 6 votes vote down vote up
@Test
public void shouldReturnFalseWhenHandleRevocationsAsExpiryIsFalseAndThereIsNothingToDelete() {
    //Given
    final List<Approval> approvals = list(ofApproval()).next();

    //And
    mongoApprovalStore.setHandleRevocationsAsExpiry(false);

    //And
    given(mongoApprovalRepository.deleteByUserIdAndClientIdAndScope(any(MongoApproval.class))).willReturn(false);

    //When
    final boolean result = mongoApprovalStore.revokeApprovals(approvals);

    //Then
    assertThat(result).isFalse();
    verify(mongoApprovalRepository, never()).updateExpiresAt(any(LocalDateTime.class), any(MongoApproval.class));
}
 
Example #8
Source File: MongoApprovalStoreTest.java    From spring-security-mongo with MIT License 6 votes vote down vote up
@Test
public void shouldRevokeApprovalsByRemoveWhenHandleRevocationsAsExpiryIsFalse() {
    //Given
    final List<Approval> approvals = list(ofApproval()).next();

    //And
    mongoApprovalStore.setHandleRevocationsAsExpiry(false);

    //And
    given(mongoApprovalRepository.deleteByUserIdAndClientIdAndScope(any(MongoApproval.class))).willReturn(true);

    //When
    final boolean result = mongoApprovalStore.revokeApprovals(approvals);

    //Then
    assertThat(result).isTrue();
    verify(mongoApprovalRepository, never()).updateExpiresAt(any(LocalDateTime.class), any(MongoApproval.class));
}
 
Example #9
Source File: MongoApprovalStoreTest.java    From spring-security-mongo with MIT License 5 votes vote down vote up
@Test
public void shouldReturnFalseWhenSomeApprovalsFailedToUpdateOrInsert() {
    //Given
    final List<Approval> approvals = list(ofApproval()).next();

    //And
    given(mongoApprovalRepository.updateOrCreate(anyCollection())).willReturn(false);

    //When
    final boolean result = mongoApprovalStore.addApprovals(approvals);

    //Then
    assertThat(result).isFalse();
}
 
Example #10
Source File: IndexController.java    From spring-oauth-example with MIT License 5 votes vote down vote up
@PostMapping(value = "/approval/revoke")
public String revokeApproval(@ModelAttribute Approval approval) {
    approvalStore.revokeApprovals(asList(approval));
    tokenStore
            .findTokensByClientIdAndUserName(approval.getClientId(), approval.getUserId())
            .forEach(tokenStore::removeAccessToken);
    return "redirect:/";
}
 
Example #11
Source File: IndexController.java    From spring-oauth-example with MIT License 5 votes vote down vote up
@GetMapping("/")
public ModelAndView root(Map<String, Object> model, Principal principal) {
    List<Approval> approvals = clientDetailsService.listClientDetails().stream()
            .map(clientDetail -> approvalStore.getApprovals(principal.getName(), clientDetail.getClientId()))
            .flatMap(Collection::stream)
            .collect(Collectors.toList());
    model.put("approvals", approvals);
    model.put("clientDetails", clientDetailsService.listClientDetails());
    return new ModelAndView("index", model);
}
 
Example #12
Source File: IndexController.java    From cola with MIT License 5 votes vote down vote up
@RequestMapping("/")
public ModelAndView root(Map<String, Object> model, Principal principal) {

	List<Approval> approvals = clientDetailsService.listClientDetails().stream()
			.map(clientDetails -> approvalStore.getApprovals(principal.getName(), clientDetails.getClientId()))
			.flatMap(Collection::stream)
			.collect(Collectors.groupingBy(Approval::getClientId)).entrySet().stream().map((entry) -> {
				//合并
				Optional<Approval> sum = entry.getValue().stream().reduce((v1, v2) -> {
					v1.setScope(v1.getScope() + "," + v2.getScope());
					return v1;
				});
				return sum.get();
			}).collect(Collectors.toList());
	model.put("approvals", approvals);

	MultiValueMap<String, Connection<?>> connections = connectionRepository.findAllConnections();
	List<Map<String, Object>> connectionMap = connections.entrySet().stream().map(entry -> {
		Map<String, Object> connection = new HashMap<>();
		connection.put("provider", entry.getKey());
		if (entry.getValue().isEmpty()) {
			connection.put("connected", false);
			connection.put("displayName", "");
		} else {
			connection.put("connected", true);
			connection.put("displayName", entry.getValue().get(0).getDisplayName());
		}
		return connection;
	}).collect(Collectors.toList());
	model.put("connections", connectionMap);
	model.put("name", principal.getName());
	return new ModelAndView("index", model);

}
 
Example #13
Source File: MongoApprovalStoreTest.java    From spring-security-mongo with MIT License 5 votes vote down vote up
@Test
public void shouldAddApprovals() {
    //Given
    final List<Approval> approvals = list(ofApproval()).next();

    //And
    given(mongoApprovalRepository.updateOrCreate(anyCollection())).willReturn(true);

    //When
    final boolean result = mongoApprovalStore.addApprovals(approvals);

    //Then
    assertThat(result).isTrue();
}
 
Example #14
Source File: MongoApprovalStore.java    From spring-security-mongo with MIT License 5 votes vote down vote up
private List<MongoApproval> transformToMongoApproval(final Collection<Approval> approvals) {
    return approvals.stream().map(approval -> new MongoApproval(UUID.randomUUID().toString(),
            approval.getUserId(),
            approval.getClientId(),
            approval.getScope(),
            isNull(approval.getStatus()) ? Approval.ApprovalStatus.APPROVED: approval.getStatus(),
            convertTolocalDateTimeFrom(approval.getExpiresAt()),
            convertTolocalDateTimeFrom(approval.getLastUpdatedAt()))).collect(Collectors.toList());
}
 
Example #15
Source File: MongoApprovalStore.java    From spring-security-mongo with MIT License 5 votes vote down vote up
private List<Approval> transformToApprovals(final List<MongoApproval> mongoApprovals) {
    return mongoApprovals.stream().map(mongoApproval -> new Approval(mongoApproval.getUserId(),
            mongoApproval.getClientId(),
            mongoApproval.getScope(),
            Date.from(mongoApproval.getExpiresAt().atZone(ZoneId.systemDefault()).toInstant()),
            mongoApproval.getStatus(),
            Date.from(mongoApproval.getLastUpdatedAt().atZone(ZoneId.systemDefault()).toInstant())))
            .collect(Collectors.toList());
}
 
Example #16
Source File: IndexController.java    From cola with MIT License 5 votes vote down vote up
@RequestMapping(value = "/approval/revoke", method = RequestMethod.POST)
public String revokeApproval(@ModelAttribute Approval approval) {

	approvalStore.revokeApprovals(asList(approval));
	tokenStore.findTokensByClientIdAndUserName(approval.getClientId(), approval.getUserId())
			.forEach(tokenStore::removeAccessToken);
	return "redirect:/";
}
 
Example #17
Source File: SecurityRDG.java    From spring-security-mongo with MIT License 4 votes vote down vote up
public static Generator<Approval> ofApproval() {
    return () -> approvalBuilder().build();
}
 
Example #18
Source File: ApprovalBuilder.java    From spring-security-mongo with MIT License 4 votes vote down vote up
public Approval build() {
    return new Approval(userId, clientId, scope, expiresIn, status);
}
 
Example #19
Source File: MongoApprovalStore.java    From spring-security-mongo with MIT License 4 votes vote down vote up
@Override
public Collection<Approval> getApprovals(final String userId,
                                         final String clientId) {
    final List<MongoApproval> mongoApprovals = mongoApprovalRepository.findByUserIdAndClientId(userId, clientId);
    return transformToApprovals(mongoApprovals);
}
 
Example #20
Source File: MongoApprovalStore.java    From spring-security-mongo with MIT License 4 votes vote down vote up
@Override
public boolean addApprovals(final Collection<Approval> approvals) {
    final Collection<MongoApproval> mongoApprovals = transformToMongoApproval(approvals);

    return mongoApprovalRepository.updateOrCreate(mongoApprovals);
}