]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7cd8075ca12f52c5cad70d99bb809b612e2207af
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  *//**
24  *  This document is a part of the source code and related artifacts
25  *  for CollectionSpace, an open source collections management system
26  *  for museums and related institutions:
27
28  *  http://www.collectionspace.org
29  *  http://wiki.collectionspace.org
30
31  *  Copyright 2009 University of California at Berkeley
32
33  *  Licensed under the Educational Community License (ECL), Version 2.0.
34  *  You may not use this file except in compliance with this License.
35
36  *  You may obtain a copy of the ECL 2.0 License at
37
38  *  https://source.collectionspace.org/collection-space/LICENSE.txt
39
40  *  Unless required by applicable law or agreed to in writing, software
41  *  distributed under the License is distributed on an "AS IS" BASIS,
42  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43  *  See the License for the specific language governing permissions and
44  *  limitations under the License.
45  */
46
47 package org.collectionspace.services.authorization.spring;
48
49 import java.nio.charset.StandardCharsets;
50 import java.util.HashMap;
51 import java.util.Map;
52
53 import javax.xml.bind.DatatypeConverter;
54
55 import org.springframework.security.oauth2.provider.AuthorizationRequest;
56 import org.springframework.security.oauth2.provider.ClientDetails;
57 import org.springframework.security.oauth2.provider.ClientDetailsService;
58 import org.springframework.security.oauth2.provider.TokenRequest;
59 import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory;
60
61 /**
62  * An OAuth2RequestFactory that expects the password to be base64 encoded. This implementation
63  * copies the parameters, decodes the password if present, and passes the result to
64  * DefaultOAuth2RequestFactory.
65  */
66 public class CSpaceOAuth2RequestFactory extends DefaultOAuth2RequestFactory {
67     private final String PASSWORD_PARAMETER = "password";
68     
69     public CSpaceOAuth2RequestFactory(ClientDetailsService clientDetailsService) {
70         super(clientDetailsService);
71     }
72
73     @Override
74     public AuthorizationRequest createAuthorizationRequest(
75             Map<String, String> authorizationParameters) {
76         return super.createAuthorizationRequest(decodePassword(authorizationParameters));
77     }
78
79     @Override
80     public TokenRequest createTokenRequest(
81             Map<String, String> requestParameters,
82             ClientDetails authenticatedClient) {
83         return super.createTokenRequest(decodePassword(requestParameters), authenticatedClient);
84     }
85     
86     private Map<String, String> decodePassword(Map<String, String> parameters) {
87         if (parameters.containsKey(PASSWORD_PARAMETER)) {
88             String base64EncodedPassword = parameters.get(PASSWORD_PARAMETER);
89             String password = new String(DatatypeConverter.parseBase64Binary(base64EncodedPassword), StandardCharsets.UTF_8);
90
91             Map<String, String> parametersCopy = new HashMap<String, String>(parameters);
92
93             parametersCopy.put(PASSWORD_PARAMETER, password);
94
95             return parametersCopy;
96         }
97
98         return parameters;
99     }
100 }