1
|
/*
|
2
|
* This class has been taken from org.springframework.security.oauth.examples.sparklr
|
3
|
*
|
4
|
* ----------------------------------------------------------------------------------
|
5
|
*
|
6
|
* Copyright 2002-2011 the original author or authors.
|
7
|
*
|
8
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
* you may not use this file except in compliance with the License.
|
10
|
* You may obtain a copy of the License at
|
11
|
*
|
12
|
* http://www.apache.org/licenses/LICENSE-2.0
|
13
|
*
|
14
|
* Unless required by applicable law or agreed to in writing, software
|
15
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
17
|
* See the License for the specific language governing permissions and
|
18
|
* limitations under the License.
|
19
|
*/
|
20
|
package eu.etaxonomy.cdm.remote.oauth2;
|
21
|
|
22
|
import java.util.Collection;
|
23
|
|
24
|
import org.springframework.security.core.Authentication;
|
25
|
import org.springframework.security.oauth2.provider.AuthorizationRequest;
|
26
|
import org.springframework.security.oauth2.provider.ClientDetails;
|
27
|
import org.springframework.security.oauth2.provider.ClientDetailsService;
|
28
|
import org.springframework.security.oauth2.provider.ClientRegistrationException;
|
29
|
import org.springframework.security.oauth2.provider.approval.ApprovalStoreUserApprovalHandler;
|
30
|
|
31
|
/**
|
32
|
* @author a.kohlbecker
|
33
|
* @date Oct 6, 2016
|
34
|
*
|
35
|
*/
|
36
|
public class CdmUserApprovalHandler extends ApprovalStoreUserApprovalHandler {
|
37
|
|
38
|
private boolean useApprovalStore = true;
|
39
|
|
40
|
private ClientDetailsService clientDetailsService;
|
41
|
|
42
|
/**
|
43
|
* Service to load client details (optional) for auto approval checks.
|
44
|
*
|
45
|
* @param clientDetailsService a client details service
|
46
|
*/
|
47
|
@Override
|
48
|
public void setClientDetailsService(ClientDetailsService clientDetailsService) {
|
49
|
this.clientDetailsService = clientDetailsService;
|
50
|
super.setClientDetailsService(clientDetailsService);
|
51
|
}
|
52
|
|
53
|
/**
|
54
|
* @param useApprovalStore the useTokenServices to set
|
55
|
*/
|
56
|
public void setUseApprovalStore(boolean useApprovalStore) {
|
57
|
this.useApprovalStore = useApprovalStore;
|
58
|
}
|
59
|
|
60
|
/**
|
61
|
* Allows automatic approval for a white list of clients in the implicit grant case.
|
62
|
*
|
63
|
* @param authorizationRequest The authorization request.
|
64
|
* @param userAuthentication the current user authentication
|
65
|
*
|
66
|
* @return An updated request if it has already been approved by the current user.
|
67
|
*/
|
68
|
@Override
|
69
|
public AuthorizationRequest checkForPreApproval(AuthorizationRequest authorizationRequest,
|
70
|
Authentication userAuthentication) {
|
71
|
|
72
|
boolean approved = false;
|
73
|
// If we are allowed to check existing approvals this will short circuit the decision
|
74
|
if (useApprovalStore) {
|
75
|
authorizationRequest = super.checkForPreApproval(authorizationRequest, userAuthentication);
|
76
|
approved = authorizationRequest.isApproved();
|
77
|
}
|
78
|
else {
|
79
|
if (clientDetailsService != null) {
|
80
|
Collection<String> requestedScopes = authorizationRequest.getScope();
|
81
|
try {
|
82
|
ClientDetails client = clientDetailsService
|
83
|
.loadClientByClientId(authorizationRequest.getClientId());
|
84
|
for (String scope : requestedScopes) {
|
85
|
if (client.isAutoApprove(scope)) {
|
86
|
approved = true;
|
87
|
break;
|
88
|
}
|
89
|
}
|
90
|
}
|
91
|
catch (ClientRegistrationException e) {
|
92
|
}
|
93
|
}
|
94
|
}
|
95
|
authorizationRequest.setApproved(approved);
|
96
|
|
97
|
return authorizationRequest;
|
98
|
|
99
|
}
|
100
|
|
101
|
}
|