Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e714bdf53c66739899c4438ef8fbb9ab9729b0d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*******************************************************************************
 * Copyright (c) 2005, 2008 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.security.auth;

import java.net.URL;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import org.eclipse.equinox.internal.security.auth.events.SecurityEventsManager;
import org.eclipse.equinox.internal.security.auth.nls.SecAuthMessages;
import org.eclipse.equinox.security.auth.ILoginContextListener;
import org.eclipse.equinox.security.auth.ILoginContext;

public class SecureContext implements ILoginContext {

	private String configName;
	private LoginContext loginContext;
	private CallbackHandler handler;

	private SecurityEventsManager eventsManager = new SecurityEventsManager();
	private boolean loggedIn = false;

	public SecureContext(String configugationName) {
		this(configugationName, null, null);
	}

	public SecureContext(String configugationName, URL configFile, CallbackHandler handler) {
		configName = configugationName;
		SecurePlatformInternal platform = SecurePlatformInternal.getInstance();
		if (configFile != null)
			platform.addConfigURL(configFile); // this call MUST be done before start()		
		platform.start();
		this.handler = handler;
	}

	public void login() throws LoginException {
		LoginContext context = getLoginContext();
		LoginException loginException = null;
		eventsManager.notifyLoginBegin(context.getSubject());
		try {
			context.login();
		} catch (LoginException e) {
			loginException = e;
		}
		// subject might have changed if login() was triggered
		eventsManager.notifyLoginEnd(context.getSubject(), loginException);
		if (loginException != null) {
			LoginException rtvException = new LoginException(SecAuthMessages.loginFailure);
			rtvException.initCause(loginException);
			throw rtvException;
		}
		loggedIn = true;
	}

	public void logout() throws LoginException {
		LoginContext context = getLoginContext();
		Subject subject = getLoginContext().getSubject();
		eventsManager.notifyLogoutBegin(subject);

		LoginException loginException = null;
		try {
			context.logout();
		} catch (LoginException e) {
			loginException = e;
		}
		eventsManager.notifyLogoutEnd(subject, loginException);
		loggedIn = false;
	}

	public Subject getSubject() throws LoginException {
		if (!loggedIn)
			login();
		return getLoginContext().getSubject();
	}

	public LoginContext getLoginContext() throws LoginException {
		if (loginContext != null)
			return loginContext;

		CallbackHandler callbackHandler;
		if (handler == null)
			callbackHandler = SecurePlatformInternal.getInstance().loadCallbackHandler(configName);
		else
			callbackHandler = handler;

		if (callbackHandler == null)
			loginContext = new LoginContext(configName);
		else
			loginContext = new LoginContext(configName, callbackHandler);
		return loginContext;
	}

	public void registerListener(ILoginContextListener listener) {
		eventsManager.addListener(listener);
	}

	public void unregisterListener(ILoginContextListener listener) {
		eventsManager.removeListener(listener);
	}

}

Back to the top