KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > security > StubLoginModule


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.activemq.security;
20
21 import org.apache.activemq.jaas.GroupPrincipal;
22 import org.apache.activemq.jaas.UserPrincipal;
23
24 import java.util.Map JavaDoc;
25
26 import javax.security.auth.Subject JavaDoc;
27 import javax.security.auth.callback.CallbackHandler JavaDoc;
28 import javax.security.auth.login.FailedLoginException JavaDoc;
29 import javax.security.auth.login.LoginException JavaDoc;
30 import javax.security.auth.spi.LoginModule JavaDoc;
31
32 public class StubLoginModule implements LoginModule JavaDoc {
33     public static final String JavaDoc ALLOW_LOGIN_PROPERTY = "org.apache.activemq.jaas.stubproperties.allow_login";
34     public static final String JavaDoc USERS_PROPERTY = "org.apache.activemq.jaas.stubproperties.users";
35     public static final String JavaDoc GROUPS_PROPERTY = "org.apache.activemq.jaas.stubproperties.groups";
36     
37     private Subject JavaDoc subject = null;
38     
39     private String JavaDoc userNames[] = null;
40     private String JavaDoc groupNames[] = null;
41     private boolean allowLogin = false;
42     
43     public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc callbackHandler, Map JavaDoc sharedState, Map JavaDoc options) {
44         String JavaDoc allowLoginString = (String JavaDoc)(options.get(ALLOW_LOGIN_PROPERTY));
45         String JavaDoc usersString = (String JavaDoc)(options.get(USERS_PROPERTY));
46         String JavaDoc groupsString = (String JavaDoc)(options.get(GROUPS_PROPERTY));
47         
48         this.subject = subject;
49         
50         allowLogin = Boolean.parseBoolean(allowLoginString);
51         userNames = usersString.split(",");
52         groupNames = groupsString.split(",");
53     }
54
55     public boolean login() throws LoginException JavaDoc {
56         if (!allowLogin) {
57             throw new FailedLoginException JavaDoc("Login was not allowed (as specified in configuration).");
58         }
59         
60         return true;
61     }
62     
63     public boolean commit() throws LoginException JavaDoc {
64         if (!allowLogin) {
65             throw new FailedLoginException JavaDoc("Login was not allowed (as specified in configuration).");
66         }
67         
68         for (int i = 0; i < userNames.length; ++i) {
69             if (userNames[i].length() > 0 ) {
70                 subject.getPrincipals().add(new UserPrincipal(userNames[i]));
71             }
72         }
73         
74         for (int i = 0; i < groupNames.length; ++i) {
75             if (groupNames[i].length() > 0) {
76                 subject.getPrincipals().add(new GroupPrincipal(groupNames[i]));
77             }
78         }
79         
80         return true;
81     }
82
83     public boolean abort() throws LoginException JavaDoc {
84         return true;
85     }
86
87     public boolean logout() throws LoginException JavaDoc {
88         subject.getPrincipals().clear();
89         
90         return true;
91     }
92
93 }
94
Popular Tags