aefe447adc0df6b3b04e95d9aafeac6f596cadea
[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 package org.wamblee.usermgt;
17
18 import java.security.AccessController;
19 import java.security.Principal;
20
21 import java.util.Set;
22
23 import javax.security.auth.Subject;
24
25
26 /**
27  * Implementation of the user accessor that retrieves user information from
28  * JAAS.
29  *
30  * @author Erik Brakkee
31  */
32 public class JaasUserAccessor implements UserAccessor {
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
51         try {
52             userPrincipalClass = Class.forName(aUserClassName);
53
54             if (!Principal.class.isAssignableFrom(userPrincipalClass)) {
55                 throw new IllegalArgumentException("Specified class '"
56                     + aUserClassName + "' is not a subclass of '"
57                     + Principal.class.getName());
58             }
59         } catch (ClassNotFoundException e) {
60             throw new RuntimeException(e);
61         }
62     }
63
64     /*
65      * (non-Javadoc)
66      *
67      * @see org.wamblee.usermgt.UserAccessor#getCurrentUser()
68      */
69     /**
70      * DOCUMENT ME!
71      *
72      * @return DOCUMENT ME!
73      */
74     public User getCurrentUser() {
75         Subject subject = Subject.getSubject(AccessController.getContext());
76
77         if (subject == null) {
78             return null;
79         }
80
81         Principal userPrincipal = getUserPrincipal(subject);
82
83         return admin.getUser(userPrincipal.getName());
84     }
85
86     /**
87      * Gets the user principal from the subject.
88      *
89      * @param subject Subject.
90      *
91      * @return User principal.
92      *
93      * @throws IllegalArgumentException In case there is a duplicate principal
94      *         or the principal was not found.
95      */
96     private Principal getUserPrincipal(Subject subject) {
97         Set<Principal> principals    = subject.getPrincipals();
98         Principal      userPrincipal = null;
99
100         for (Principal principal : principals) {
101             if (principal.getClass().equals(userPrincipalClass)) {
102                 if (userPrincipal != null) {
103                     throw new IllegalArgumentException(
104                         "Multiple principals for class '" + userPrincipalClass
105                         + "', subject: " + subject);
106                 }
107
108                 userPrincipal = principal;
109             }
110         }
111
112         if (userPrincipal == null) {
113             throw new IllegalArgumentException(
114                 "No user principal found for class '" + userPrincipalClass
115                 + "', subject: " + subject);
116         }
117
118         return userPrincipal;
119     }
120 }