0ac6aa730073d36f9f89654809b1d04f29a24b31
[utils] / security / usermgt / src / main / java / org / wamblee / security / authentication / JaasUserAccessor.java
1 /*
2  * Copyright 2005-2010 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.security.authentication;
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     /**
34      * Class of the JAAS user principal.
35      */
36     private Class userPrincipalClass;
37
38     /**
39      * Constructs user accessor.
40      * 
41      * @param aAdmin
42      *            User administration.
43      * @param aUserClassName
44      *            Class name of the user principal.
45      */
46     public JaasUserAccessor(String aUserClassName) {
47         try {
48             userPrincipalClass = Class.forName(aUserClassName);
49
50             if (!Principal.class.isAssignableFrom(userPrincipalClass)) {
51                 throw new IllegalArgumentException("Specified class '" +
52                     aUserClassName + "' is not a subclass of '" +
53                     Principal.class.getName());
54             }
55         } catch (ClassNotFoundException e) {
56             throw new RuntimeException(e);
57         }
58     }
59
60     @Override
61     public String getCurrentUser() {
62         Subject subject = Subject.getSubject(AccessController.getContext());
63
64         if (subject == null) {
65             return null;
66         }
67
68         Principal userPrincipal = getUserPrincipal(subject);
69
70         return userPrincipal.getName();
71     }
72
73     /**
74      * Gets the user principal from the subject.
75      * 
76      * @param aSubject
77      *            Subject.
78      * 
79      * @return User principal.
80      * 
81      * @throws IllegalArgumentException
82      *             In case there is a duplicate principal or the principal was
83      *             not found.
84      */
85     private Principal getUserPrincipal(Subject aSubject) {
86         Set<Principal> principals = aSubject.getPrincipals();
87         Principal userPrincipal = null;
88
89         for (Principal principal : principals) {
90             if (principal.getClass().equals(userPrincipalClass)) {
91                 if (userPrincipal != null) {
92                     throw new IllegalArgumentException(
93                         "Multiple principals for class '" + userPrincipalClass +
94                             "', subject: " + aSubject);
95                 }
96
97                 userPrincipal = principal;
98             }
99         }
100
101         if (userPrincipal == null) {
102             throw new IllegalArgumentException(
103                 "No user principal found for class '" + userPrincipalClass +
104                     "', subject: " + aSubject);
105         }
106
107         return userPrincipal;
108     }
109 }