01: package org.drools.brms.server.security;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
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: import javax.security.auth.login.LoginException;
20:
21: import org.apache.log4j.Logger;
22: import org.drools.brms.client.rpc.SecurityService;
23: import org.jboss.seam.annotations.AutoCreate;
24: import org.jboss.seam.annotations.Name;
25: import org.jboss.seam.annotations.WebRemote;
26: import org.jboss.seam.contexts.Contexts;
27: import org.jboss.seam.security.Identity;
28:
29: /**
30: * This implements security related services.
31: * @author Michael Neale
32: */
33: @Name("org.drools.brms.client.rpc.SecurityService")
34: @AutoCreate
35: public class SecurityServiceImpl implements SecurityService {
36:
37: public static final String GUEST_LOGIN = "guest";
38: private static final Logger log = Logger
39: .getLogger(SecurityServiceImpl.class);
40:
41: @WebRemote
42: public boolean login(String userName, String password) {
43: log.info("Logging in user [" + userName + "]");
44: if (Contexts.isApplicationContextActive()) {
45: Identity.instance().setUsername(userName);
46: Identity.instance().setPassword(password);
47: try {
48: Identity.instance().authenticate();
49: } catch (LoginException e) {
50: log.error(e);
51: return false;
52: }
53: return Identity.instance().isLoggedIn();
54: } else {
55: return true;
56: }
57:
58: }
59:
60: @WebRemote
61: public String getCurrentUser() {
62: if (Contexts.isApplicationContextActive()) {
63: if (!Identity.instance().isLoggedIn()) {
64: //check to see if we can autologin
65: return checkAutoLogin();
66: }
67: return Identity.instance().getUsername();
68: } else {
69: return "SINGLE USER MODE (DEBUG) USE ONLY";
70: }
71: }
72:
73: /**
74: * This will return a auto login user name if it has been configured.
75: * Autologin means that its not really logged in, but a generic username will be used.
76: * Basically means security is bypassed.
77: *
78: */
79: private String checkAutoLogin() {
80: Identity id = Identity.instance();
81: id.setUsername(GUEST_LOGIN);
82: try {
83: id.authenticate();
84: } catch (LoginException e) {
85: return null;
86: }
87: if (id.isLoggedIn()) {
88: return id.getUsername();
89: } else {
90: return null;
91: }
92:
93: }
94:
95: }
|