1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.cas;
20
21 import org.apache.shiro.authc.AuthenticationException;
22 import org.apache.shiro.authc.AuthenticationInfo;
23 import org.apache.shiro.authc.AuthenticationToken;
24 import org.apache.shiro.authc.SimpleAuthenticationInfo;
25 import org.apache.shiro.authz.AuthorizationInfo;
26 import org.apache.shiro.authz.SimpleAuthorizationInfo;
27 import org.apache.shiro.realm.AuthorizingRealm;
28 import org.apache.shiro.subject.PrincipalCollection;
29 import org.apache.shiro.subject.SimplePrincipalCollection;
30 import org.apache.shiro.util.CollectionUtils;
31 import org.apache.shiro.util.StringUtils;
32 import org.jasig.cas.client.authentication.AttributePrincipal;
33 import org.jasig.cas.client.validation.*;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Map;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 @Deprecated
61 public class CasRealm extends AuthorizingRealm {
62
63
64 public static final String DEFAULT_REMEMBER_ME_ATTRIBUTE_NAME = "longTermAuthenticationRequestTokenUsed";
65 public static final String DEFAULT_VALIDATION_PROTOCOL = "CAS";
66
67 private static Logger log = LoggerFactory.getLogger(CasRealm.class);
68
69
70 private String casServerUrlPrefix;
71
72
73 private String casService;
74
75
76
77
78
79 private String validationProtocol = DEFAULT_VALIDATION_PROTOCOL;
80
81
82 private String rememberMeAttributeName = DEFAULT_REMEMBER_ME_ATTRIBUTE_NAME;
83
84
85 private TicketValidator ticketValidator;
86
87
88 private String defaultRoles;
89
90
91 private String defaultPermissions;
92
93
94 private String roleAttributeNames;
95
96
97 private String permissionAttributeNames;
98
99 public CasRealm() {
100 setAuthenticationTokenClass(CasToken.class);
101 }
102
103 @Override
104 protected void onInit() {
105 super.onInit();
106 ensureTicketValidator();
107 }
108
109 protected TicketValidator ensureTicketValidator() {
110 if (this.ticketValidator == null) {
111 this.ticketValidator = createTicketValidator();
112 }
113 return this.ticketValidator;
114 }
115
116 protected TicketValidator createTicketValidator() {
117 String urlPrefix = getCasServerUrlPrefix();
118 if ("saml".equalsIgnoreCase(getValidationProtocol())) {
119 return new Saml11TicketValidator(urlPrefix);
120 }
121 return new Cas20ServiceTicketValidator(urlPrefix);
122 }
123
124
125
126
127
128
129
130 @Override
131 @SuppressWarnings("unchecked")
132 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
133 CasToken./../../org/apache/shiro/cas/CasToken.html#CasToken">CasToken casToken = (CasToken) token;
134 if (token == null) {
135 return null;
136 }
137
138 String ticket = (String)casToken.getCredentials();
139 if (!StringUtils.hasText(ticket)) {
140 return null;
141 }
142
143 TicketValidator ticketValidator = ensureTicketValidator();
144
145 try {
146
147 Assertion casAssertion = ticketValidator.validate(ticket, getCasService());
148
149 AttributePrincipal casPrincipal = casAssertion.getPrincipal();
150 String userId = casPrincipal.getName();
151 log.debug("Validate ticket : {} in CAS server : {} to retrieve user : {}", new Object[]{
152 ticket, getCasServerUrlPrefix(), userId
153 });
154
155 Map<String, Object> attributes = casPrincipal.getAttributes();
156
157 casToken.setUserId(userId);
158 String rememberMeAttributeName = getRememberMeAttributeName();
159 String rememberMeStringValue = (String)attributes.get(rememberMeAttributeName);
160 boolean isRemembered = rememberMeStringValue != null && Boolean.parseBoolean(rememberMeStringValue);
161 if (isRemembered) {
162 casToken.setRememberMe(true);
163 }
164
165 List<Object> principals = CollectionUtils.asList(userId, attributes);
166 PrincipalCollection principalCollection = new SimplePrincipalCollection(principals, getName());
167 return new SimpleAuthenticationInfo(principalCollection, ticket);
168 } catch (TicketValidationException e) {
169 throw new CasAuthenticationException("Unable to validate ticket [" + ticket + "]", e);
170 }
171 }
172
173
174
175
176
177
178
179 @Override
180 @SuppressWarnings("unchecked")
181 protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
182
183 SimplePrincipalCollection principalCollection = (SimplePrincipalCollection) principals;
184 List<Object> listPrincipals = principalCollection.asList();
185 Map<String, String> attributes = (Map<String, String>) listPrincipals.get(1);
186
187 SimpleAuthorizationInfoorizationInfo">SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
188
189 addRoles(simpleAuthorizationInfo, split(defaultRoles));
190
191 addPermissions(simpleAuthorizationInfo, split(defaultPermissions));
192
193 List<String> attributeNames = split(roleAttributeNames);
194 for (String attributeName : attributeNames) {
195 String value = attributes.get(attributeName);
196 addRoles(simpleAuthorizationInfo, split(value));
197 }
198
199 attributeNames = split(permissionAttributeNames);
200 for (String attributeName : attributeNames) {
201 String value = attributes.get(attributeName);
202 addPermissions(simpleAuthorizationInfo, split(value));
203 }
204 return simpleAuthorizationInfo;
205 }
206
207
208
209
210
211
212
213 private List<String> split(String s) {
214 List<String> list = new ArrayList<String>();
215 String[] elements = StringUtils.split(s, ',');
216 if (elements != null && elements.length > 0) {
217 for (String element : elements) {
218 if (StringUtils.hasText(element)) {
219 list.add(element.trim());
220 }
221 }
222 }
223 return list;
224 }
225
226
227
228
229
230
231
232 private void addRoles(SimpleAuthorizationInfo simpleAuthorizationInfo, List<String> roles) {
233 for (String role : roles) {
234 simpleAuthorizationInfo.addRole(role);
235 }
236 }
237
238
239
240
241
242
243
244 private void addPermissions(SimpleAuthorizationInfo simpleAuthorizationInfo, List<String> permissions) {
245 for (String permission : permissions) {
246 simpleAuthorizationInfo.addStringPermission(permission);
247 }
248 }
249
250 public String getCasServerUrlPrefix() {
251 return casServerUrlPrefix;
252 }
253
254 public void setCasServerUrlPrefix(String casServerUrlPrefix) {
255 this.casServerUrlPrefix = casServerUrlPrefix;
256 }
257
258 public String getCasService() {
259 return casService;
260 }
261
262 public void setCasService(String casService) {
263 this.casService = casService;
264 }
265
266 public String getValidationProtocol() {
267 return validationProtocol;
268 }
269
270 public void setValidationProtocol(String validationProtocol) {
271 this.validationProtocol = validationProtocol;
272 }
273
274 public String getRememberMeAttributeName() {
275 return rememberMeAttributeName;
276 }
277
278 public void setRememberMeAttributeName(String rememberMeAttributeName) {
279 this.rememberMeAttributeName = rememberMeAttributeName;
280 }
281
282 public String getDefaultRoles() {
283 return defaultRoles;
284 }
285
286 public void setDefaultRoles(String defaultRoles) {
287 this.defaultRoles = defaultRoles;
288 }
289
290 public String getDefaultPermissions() {
291 return defaultPermissions;
292 }
293
294 public void setDefaultPermissions(String defaultPermissions) {
295 this.defaultPermissions = defaultPermissions;
296 }
297
298 public String getRoleAttributeNames() {
299 return roleAttributeNames;
300 }
301
302 public void setRoleAttributeNames(String roleAttributeNames) {
303 this.roleAttributeNames = roleAttributeNames;
304 }
305
306 public String getPermissionAttributeNames() {
307 return permissionAttributeNames;
308 }
309
310 public void setPermissionAttributeNames(String permissionAttributeNames) {
311 this.permissionAttributeNames = permissionAttributeNames;
312 }
313 }