X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Finternal%2Fhttp%2FCmsAuthenticator.java;h=e17a089fe05e15db48924216a5f3f6c1f998b2c1;hb=0e533d2562def311fdd7aa71f1d0d704e466861e;hp=307f928a5c9658f52bca202cf8d24f0655c0e403;hpb=921b6cf95420aafa6b9cebe107c927e8062ed865;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/internal/http/CmsAuthenticator.java b/org.argeo.cms/src/org/argeo/cms/internal/http/CmsAuthenticator.java index 307f928a5..e17a089fe 100644 --- a/org.argeo.cms/src/org/argeo/cms/internal/http/CmsAuthenticator.java +++ b/org.argeo.cms/src/org/argeo/cms/internal/http/CmsAuthenticator.java @@ -5,31 +5,24 @@ import javax.security.auth.login.LoginContext; import javax.security.auth.login.LoginException; import org.argeo.api.cms.CmsAuth; -import org.argeo.cms.auth.CurrentUser; +import org.argeo.cms.CurrentUser; import org.argeo.cms.auth.RemoteAuthCallbackHandler; import org.argeo.cms.auth.RemoteAuthRequest; import org.argeo.cms.auth.RemoteAuthResponse; import org.argeo.cms.auth.RemoteAuthUtils; -import org.argeo.util.CurrentSubject; import com.sun.net.httpserver.Authenticator; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpPrincipal; +/** An {@link Authenticator} implementation based on CMS authentication. */ public class CmsAuthenticator extends Authenticator { -// final static String HEADER_AUTHORIZATION = "Authorization"; -// final static String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate"; - -// private final static CmsLog log = CmsLog.getLog(CmsAuthenticator.class); - // TODO make it configurable private final String httpAuthRealm = "Argeo"; private final boolean forceBasic = false; @Override public Result authenticate(HttpExchange exch) { -// if (log.isTraceEnabled()) -// HttpUtils.logRequestHeaders(log, request); RemoteAuthHttpExchange remoteAuthExchange = new RemoteAuthHttpExchange(exch); ClassLoader currentThreadContextClassLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(CmsAuthenticator.class.getClassLoader()); @@ -38,9 +31,9 @@ public class CmsAuthenticator extends Authenticator { lc = CmsAuth.USER.newLoginContext(new RemoteAuthCallbackHandler(remoteAuthExchange, remoteAuthExchange)); lc.login(); } catch (LoginException e) { - // FIXME better analyse failure so as not to try endlessly - if (authIsRequired(remoteAuthExchange,remoteAuthExchange)) { - int statusCode = RemoteAuthUtils.askForWwwAuth(remoteAuthExchange, httpAuthRealm, forceBasic); + if (authIsRequired(remoteAuthExchange, remoteAuthExchange)) { + int statusCode = RemoteAuthUtils.askForWwwAuth(remoteAuthExchange, remoteAuthExchange, httpAuthRealm, + forceBasic); return new Authenticator.Retry(statusCode); } else { @@ -54,27 +47,12 @@ public class CmsAuthenticator extends Authenticator { Subject subject = lc.getSubject(); - CurrentSubject.callAs(subject, () -> { - RemoteAuthUtils.configureRequestSecurity(remoteAuthExchange); - return null; - }); -// Subject.doAs(subject, new PrivilegedAction() { -// -// @Override -// public Void run() { -// // TODO also set login context in order to log out ? -// RemoteAuthUtils.configureRequestSecurity(new ServletHttpRequest(request)); -// return null; -// } -// -// }); String username = CurrentUser.getUsername(subject); HttpPrincipal httpPrincipal = new HttpPrincipal(username, httpAuthRealm); return new Authenticator.Success(httpPrincipal); } - protected boolean authIsRequired(RemoteAuthRequest remoteAuthRequest, - RemoteAuthResponse remoteAuthResponse) { + protected boolean authIsRequired(RemoteAuthRequest remoteAuthRequest, RemoteAuthResponse remoteAuthResponse) { return true; }