1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.security.oauth.examples.sparklr.oauth;
18
19 import java.util.Collection;
20
21 import org.springframework.security.core.Authentication;
22 import org.springframework.security.oauth2.provider.AuthorizationRequest;
23 import org.springframework.security.oauth2.provider.ClientDetails;
24 import org.springframework.security.oauth2.provider.ClientDetailsService;
25 import org.springframework.security.oauth2.provider.ClientRegistrationException;
26 import org.springframework.security.oauth2.provider.approval.ApprovalStoreUserApprovalHandler;
27
28
29
30
31
32 public class SparklrUserApprovalHandler extends ApprovalStoreUserApprovalHandler {
33
34 private boolean useApprovalStore = true;
35
36 private ClientDetailsService clientDetailsService;
37
38
39
40
41
42
43 public void setClientDetailsService(ClientDetailsService clientDetailsService) {
44 this.clientDetailsService = clientDetailsService;
45 super.setClientDetailsService(clientDetailsService);
46 }
47
48
49
50
51 public void setUseApprovalStore(boolean useApprovalStore) {
52 this.useApprovalStore = useApprovalStore;
53 }
54
55
56
57
58
59
60
61
62
63 @Override
64 public AuthorizationRequest checkForPreApproval(AuthorizationRequest authorizationRequest,
65 Authentication userAuthentication) {
66
67 boolean approved = false;
68
69 if (useApprovalStore) {
70 authorizationRequest = super.checkForPreApproval(authorizationRequest, userAuthentication);
71 approved = authorizationRequest.isApproved();
72 }
73 else {
74 if (clientDetailsService != null) {
75 Collection<String> requestedScopes = authorizationRequest.getScope();
76 try {
77 ClientDetails client = clientDetailsService
78 .loadClientByClientId(authorizationRequest.getClientId());
79 for (String scope : requestedScopes) {
80 if (client.isAutoApprove(scope) || client.isAutoApprove("all")) {
81 approved = true;
82 break;
83 }
84 }
85 }
86 catch (ClientRegistrationException e) {
87 }
88 }
89 }
90 authorizationRequest.setApproved(approved);
91
92 return authorizationRequest;
93
94 }
95
96 }