KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > jmx > auth > ASLoginDriverImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.admin.server.core.jmx.auth;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import javax.management.remote.JMXPrincipal JavaDoc;
30 import javax.security.auth.Subject JavaDoc;
31
32 import com.sun.enterprise.admin.common.constant.AdminConstants;
33 import com.sun.enterprise.security.auth.LoginContextDriver;
34 import com.sun.enterprise.security.auth.realm.Realm;
35 import com.sun.enterprise.util.i18n.StringManager;
36
37 public class ASLoginDriverImpl implements LoginDriver {
38
39     private static final String JavaDoc ASADMIN_GROUP = "asadmin";
40
41     private static Logger JavaDoc _logger =
42             Logger.getLogger(AdminConstants.kLoggerName);
43
44     private static StringManager _strings =
45             StringManager.getManager(ASLoginDriverImpl.class);
46
47     public ASLoginDriverImpl() {
48     }
49
50     public Subject JavaDoc login(String JavaDoc user, String JavaDoc password, String JavaDoc realm) {
51         LoginContextDriver.login(user, password, realm);
52         // Login succeeded, try authorization
53
authorize(user, password, realm);
54         /* **
55         ** TODO: The subject needs to be initialized properly
56         JMXPrincipal principal = new JMXPrincipal(user);
57         HashSet principalSet = new HashSet();
58         principalSet.add(principal);
59         Subject subj = new Subject(true, principalSet, new HashSet(),
60                 new HashSet());
61         return subj;
62         ** */

63         return null;
64     }
65
66     private void authorize(String JavaDoc user, String JavaDoc password, String JavaDoc realmName) {
67         boolean isAuthorized = false;
68         try {
69             boolean isValid = Realm.isValidRealm(realmName);
70             if (!isValid) {
71                 realmName = Realm.getDefaultRealm();
72             }
73             Realm realm = Realm.getInstance(realmName);
74             Enumeration JavaDoc groups = realm.getGroupNames(user);
75             while (groups != null && groups.hasMoreElements()) {
76                 String JavaDoc groupName = (String JavaDoc)groups.nextElement();
77                 if (ASADMIN_GROUP.equals(groupName)) {
78                     isAuthorized = true;
79                     break;
80                 }
81             }
82         } catch (Exception JavaDoc ee) {
83             _logger.log(Level.WARNING, "core.auth_failed", realmName);
84             _logger.log(Level.INFO, "core.auth_fail_exception", ee);
85             SecurityException JavaDoc se = new SecurityException JavaDoc(
86                     _strings.getString("admin.auth.failed"));
87             se.initCause(ee);
88             throw se;
89         }
90         if (!isAuthorized) {
91             throw new SecurityException JavaDoc(
92                     _strings.getString("admin.auth.failed.nogroup"));
93         }
94         return;
95     }
96
97 }
98
Popular Tags