8102b82fe22a6dd447b920d2abbf6f41869e3575
[utils] / security / src / main / java / org / wamblee / usermgt / JaasUserAccessor.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.usermgt;
18
19 import java.security.AccessController;
20 import java.security.Principal;
21 import java.util.Set;
22
23 import javax.security.auth.Subject;
24
25 /**
26  * Implementation of the user accessor that retrieves user information
27  * from JAAS. 
28  *
29  * @author Erik Brakkee
30  */
31 public class JaasUserAccessor implements UserAccessor {
32
33     /**
34      * User administration to use. 
35      */
36     private UserAdministration admin;
37
38     /**
39      * Class of the JAAS user principal. 
40      */
41     private Class userPrincipalClass;
42
43     /**
44      * Constructs user accessor. 
45      * @param aAdmin User administration. 
46      * @param aUserClassName Class name of the user principal. 
47      */
48     public JaasUserAccessor(UserAdministration aAdmin, String aUserClassName) {
49         admin = aAdmin;
50         try {
51             userPrincipalClass = Class.forName(aUserClassName);
52             if ( !Principal.class.isAssignableFrom(userPrincipalClass)) {
53                 throw new IllegalArgumentException("Specified class '" + aUserClassName + "' is not a subclass of '" +
54                         Principal.class.getName());
55             }
56         } catch (ClassNotFoundException e) {
57             throw new RuntimeException(e);
58         }
59     }
60
61     /*
62      * (non-Javadoc)
63      * 
64      * @see org.wamblee.usermgt.UserAccessor#getCurrentUser()
65      */
66     public User getCurrentUser() {
67         Subject subject = Subject.getSubject(AccessController.getContext());
68         if (subject == null) {
69             return null;
70         }
71         Principal userPrincipal = getUserPrincipal(subject);
72       
73         return admin.getUser(userPrincipal.getName());
74     }
75
76     /**
77      * Gets the user principal from the subject. 
78      * @param subject Subject. 
79      * @return User principal. 
80      * @throws IllegalArgumentException In case there is a duplicate principal or the principal was not found. 
81      */
82     private Principal getUserPrincipal(Subject subject) {
83         Set<Principal> principals = subject.getPrincipals();
84         Principal userPrincipal = null;  
85         for ( Principal principal: principals) { 
86             if ( principal.getClass().equals(userPrincipalClass)) { 
87                 if ( userPrincipal != null ) { 
88                     throw new IllegalArgumentException(
89                             "Multiple principals for class '" + userPrincipalClass + "', subject: " + subject);                   
90                 }
91                 userPrincipal = principal; 
92             }
93         }
94         if ( userPrincipal == null ) { 
95             throw new IllegalArgumentException(
96                     "No user principal found for class '" + userPrincipalClass + "', subject: " + subject);
97         }
98         return userPrincipal;
99     }
100
101 }