1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.security.oauth2.provider.approval;
18
19 import java.util.Collection;
20 import java.util.Date;
21 import java.util.HashSet;
22
23 import org.springframework.security.oauth2.common.OAuth2AccessToken;
24 import org.springframework.security.oauth2.provider.OAuth2Authentication;
25 import org.springframework.security.oauth2.provider.approval.Approval.ApprovalStatus;
26 import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
27 import org.springframework.security.oauth2.provider.token.TokenStore;
28
29
30
31
32
33
34
35
36
37
38
39 public class TokenApprovalStore implements ApprovalStore {
40
41 private TokenStore store;
42
43
44
45
46 public void setTokenStore(TokenStore store) {
47 this.store = store;
48 }
49
50
51
52
53
54
55
56 @Override
57 public boolean addApprovals(Collection<Approval> approvals) {
58 return true;
59 }
60
61
62
63
64
65
66 @Override
67 public boolean revokeApprovals(Collection<Approval> approvals) {
68 boolean success = true;
69 for (Approval approval : approvals) {
70 Collection<OAuth2AccessToken> tokens = store.findTokensByClientIdAndUserName(approval.getClientId(), approval.getUserId());
71 for (OAuth2AccessToken token : tokens) {
72 OAuth2Authentication authentication = store.readAuthentication(token);
73 if (authentication != null
74 && approval.getClientId().equals(authentication.getOAuth2Request().getClientId())) {
75 store.removeAccessToken(token);
76 }
77 }
78 }
79 return success;
80 }
81
82
83
84
85
86
87
88 @Override
89 public Collection<Approval> getApprovals(String userId, String clientId) {
90 Collection<Approval> result = new HashSet<Approval>();
91 Collection<OAuth2AccessToken> tokens = store.findTokensByClientIdAndUserName(clientId, userId);
92 for (OAuth2AccessToken token : tokens) {
93 OAuth2Authentication authentication = store.readAuthentication(token);
94 if (authentication != null) {
95 Date expiresAt = token.getExpiration();
96 for (String scope : token.getScope()) {
97 result.add(new Approval(userId, clientId, scope, expiresAt, ApprovalStatus.APPROVED));
98 }
99 }
100 }
101 return result;
102 }
103
104 }