8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: AuthLoginThread.java,v 1.2 2008/06/25 05:41:53 qcheng Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
83290b0a7a824bc40f3decd65067d2a871524bd3Jonathan Scudder/**
83290b0a7a824bc40f3decd65067d2a871524bd3Jonathan Scudder * Portions Copyrighted 2013 ForgeRock, Inc.
83290b0a7a824bc40f3decd65067d2a871524bd3Jonathan Scudder */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.authentication.internal;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.IOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.security.auth.callback.Callback;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.security.auth.callback.CallbackHandler;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.security.auth.callback.UnsupportedCallbackException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.security.auth.login.LoginException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The class <code>AuthLoginThread</code> provides the needed synchronization
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * for JAAS's callback mechanism. This class starts a new thread and waits for
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * user's authentication information to be submitted. Used by a state less
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * protocol like HTTP.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class AuthLoginThread extends Thread implements CallbackHandler {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
47f571afc30ebc13dadb1d84c9deb522ffa7ac34Jonathan Scudder private AuthContext authContext;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Constructor for this class. Since it is protected, only classes in this
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * package can instantiate this object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected AuthLoginThread(AuthContext ctx) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthContext.authDebug.message("AuthLoginThread::Constructor");
47f571afc30ebc13dadb1d84c9deb522ffa7ac34Jonathan Scudder authContext = ctx;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Method that call's JAAS's <code>LoginContext</code>'s
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>login()</code> method.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void run() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthContext.authDebug.message("AuthLoginThread::run()");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
47f571afc30ebc13dadb1d84c9deb522ffa7ac34Jonathan Scudder authContext.loginContext.login();
47f571afc30ebc13dadb1d84c9deb522ffa7ac34Jonathan Scudder authContext.setLoginStatus(AuthContext.AUTH_SUCCESS);
83290b0a7a824bc40f3decd65067d2a871524bd3Jonathan Scudder AuthContext.authDebug.message("AuthLoginThread::run() successful login");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (LoginException le) {
47f571afc30ebc13dadb1d84c9deb522ffa7ac34Jonathan Scudder authContext.setLoginStatus(AuthContext.AUTH_FAILED);
47f571afc30ebc13dadb1d84c9deb522ffa7ac34Jonathan Scudder authContext.loginException = le;
83290b0a7a824bc40f3decd65067d2a871524bd3Jonathan Scudder AuthContext.authDebug.message("AuthLoginThread::run() exception during login; " + le);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Method that implements JAAS's <code>CallbackHandler</code> interface.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This method receives the authentication information requests from the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * plug-ins and sends it to <code>AuthContext</code>, and similarly
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * accepts the submited authentication information from <code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * AuthContext</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and sends it to the plug-ins.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
83290b0a7a824bc40f3decd65067d2a871524bd3Jonathan Scudder public void handle(Callback[] callback) throws IOException, UnsupportedCallbackException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthContext.authDebug.message("AuthLoginThread::handle()");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Clear the previously submitted information
47f571afc30ebc13dadb1d84c9deb522ffa7ac34Jonathan Scudder authContext.submittedInformation = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Set the required information variable
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster synchronized (this) {
47f571afc30ebc13dadb1d84c9deb522ffa7ac34Jonathan Scudder authContext.informationRequired = callback;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // wake up threads waiting for this variable
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.notify();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
83290b0a7a824bc40f3decd65067d2a871524bd3Jonathan Scudder AuthContext.authDebug.message("AuthLoginThread::handle() sent notify to wake up sleeping threads");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // check if the requested information is ready
47f571afc30ebc13dadb1d84c9deb522ffa7ac34Jonathan Scudder while (authContext.submittedInformation == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // wait for the variable to be set
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthContext.authDebug.message("AuthLoginThread::handle() "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + "waiting for Callbacks to be submitted");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster synchronized (this) {
47f571afc30ebc13dadb1d84c9deb522ffa7ac34Jonathan Scudder if (authContext.submittedInformation == null)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.wait();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthContext.authDebug.message("AuthLoginThread::handle() "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + "woke up from waiting for Callbacks to be submitted");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (InterruptedException ie) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // do nothing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Update the shared state and return the requested information
47f571afc30ebc13dadb1d84c9deb522ffa7ac34Jonathan Scudder authContext.loginContext.updateSharedState(authContext.submittedInformation);
47f571afc30ebc13dadb1d84c9deb522ffa7ac34Jonathan Scudder callback = authContext.submittedInformation;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}