Removed DOCUMENT ME comments that were generated and applied source code
[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  * Implementation of the user accessor that retrieves user information from
27  * JAAS.
28  * 
29  * @author Erik Brakkee
30  */
31 public class JaasUserAccessor implements UserAccessor {
32     /**
33      * User administration to use.
34      */
35     private UserAdministration admin;
36
37     /**
38      * Class of the JAAS user principal.
39      */
40     private Class userPrincipalClass;
41
42     /**
43      * Constructs user accessor.
44      * 
45      * @param aAdmin
46      *            User administration.
47      * @param aUserClassName
48      *            Class name of the user principal.
49      */
50     public JaasUserAccessor(UserAdministration aAdmin, String aUserClassName) {
51         admin = aAdmin;
52
53         try {
54             userPrincipalClass = Class.forName(aUserClassName);
55
56             if (!Principal.class.isAssignableFrom(userPrincipalClass)) {
57                 throw new IllegalArgumentException("Specified class '" +
58                     aUserClassName + "' is not a subclass of '" +
59                     Principal.class.getName());
60             }
61         } catch (ClassNotFoundException e) {
62             throw new RuntimeException(e);
63         }
64     }
65
66     /*
67      * (non-Javadoc)
68      * 
69      * @see org.wamblee.usermgt.UserAccessor#getCurrentUser()
70      */
71     public User getCurrentUser() {
72         Subject subject = Subject.getSubject(AccessController.getContext());
73
74         if (subject == null) {
75             return null;
76         }
77
78         Principal userPrincipal = getUserPrincipal(subject);
79
80         return admin.getUser(userPrincipal.getName());
81     }
82
83     /**
84      * Gets the user principal from the subject.
85      * 
86      * @param subject
87      *            Subject.
88      * 
89      * @return User principal.
90      * 
91      * @throws IllegalArgumentException
92      *             In case there is a duplicate principal or the principal was
93      *             not found.
94      */
95     private Principal getUserPrincipal(Subject subject) {
96         Set<Principal> principals = subject.getPrincipals();
97         Principal userPrincipal = null;
98
99         for (Principal principal : principals) {
100             if (principal.getClass().equals(userPrincipalClass)) {
101                 if (userPrincipal != null) {
102                     throw new IllegalArgumentException(
103                         "Multiple principals for class '" + userPrincipalClass +
104                             "', subject: " + subject);
105                 }
106
107                 userPrincipal = principal;
108             }
109         }
110
111         if (userPrincipal == null) {
112             throw new IllegalArgumentException(
113                 "No user principal found for class '" + userPrincipalClass +
114                     "', subject: " + subject);
115         }
116
117         return userPrincipal;
118     }
119 }