4141import jakarta .servlet .ServletResponse ;
4242import jakarta .servlet .http .HttpServletRequest ;
4343import jakarta .servlet .http .HttpServletResponse ;
44+ import org .eclipse .jetty .client .Authentication ;
45+ import org .eclipse .jetty .ee9 .security .UserAuthentication ;
46+ import org .eclipse .jetty .ee9 .security .authentication .DeferredAuthentication ;
4447import org .eclipse .jetty .http .HttpHeader ;
48+ import org .eclipse .jetty .security .AuthenticationState ;
49+ import org .eclipse .jetty .security .Authenticator ;
4550import org .eclipse .jetty .security .ServerAuthException ;
46- import org .eclipse .jetty .security .UserAuthentication ;
51+ import org .eclipse .jetty .security .UserIdentity ;
4752import org .eclipse .jetty .security .authentication .BasicAuthenticator ;
48- import org .eclipse .jetty .security .authentication .DeferredAuthentication ;
4953import org .eclipse .jetty .security .authentication .LoginAuthenticator ;
50- import org .eclipse .jetty .server .Authentication ;
51- import org .eclipse .jetty .server .Authentication .User ;
52- import org .eclipse .jetty .server .UserIdentity ;
53- import org .eclipse .jetty .util .security .Constraint ;
5454
5555import java .io .IOException ;
5656import java .nio .charset .StandardCharsets ;
5757import java .util .Base64 ;
58+ import org .eclipse .jetty .server .Request ;
59+ import org .eclipse .jetty .server .Response ;
60+ import org .eclipse .jetty .util .Callback ;
5861
5962/**
6063 * Inspired from {@link BasicAuthenticator} but adapted for proxy auth.
@@ -65,63 +68,43 @@ public ProxyAuthenticator() {
6568 }
6669
6770 /* ------------------------------------------------------------ */
71+
6872 /**
69- * @see org.eclipse.jetty.security. Authenticator#getAuthMethod ()
73+ * @see Authenticator#getAuthenticationType ()
7074 */
7175 @ Override
72- public String getAuthMethod () {
73- return Constraint . __BASIC_AUTH ;
76+ public String getAuthenticationType () {
77+ return Authenticator . BASIC_AUTH ;
7478 }
7579
7680 /* ------------------------------------------------------------ */
81+
7782 /**
78- * @see org.eclipse.jetty.security. Authenticator#validateRequest(ServletRequest, ServletResponse, boolean )
83+ * @see Authenticator#validateRequest(Request, Response, Callback )
7984 */
8085 @ Override
81- public Authentication validateRequest (ServletRequest req , ServletResponse res , boolean mandatory ) throws ServerAuthException {
82- HttpServletRequest request = (HttpServletRequest ) req ;
83- HttpServletResponse response = (HttpServletResponse ) res ;
84- String credentials = request .getHeader (HttpHeader .PROXY_AUTHORIZATION .asString ());
85-
86- try {
87- if (!mandatory )
88- return new DeferredAuthentication (this );
86+ public AuthenticationState validateRequest (Request req , Response res , Callback callback ) {
87+ String credentials = req .getHeaders ().get (HttpHeader .PROXY_AUTHORIZATION );
88+ if (credentials != null ) {
89+ int space = credentials .indexOf (' ' );
90+ if (space > 0 ) {
91+ String method = credentials .substring (0 , space );
92+ if ("basic" .equalsIgnoreCase (method )) {
93+ credentials = credentials .substring (space + 1 );
94+ credentials = new String (Base64 .getDecoder ().decode (credentials ), StandardCharsets .ISO_8859_1 );
95+ int i = credentials .indexOf (':' );
96+ if (i > 0 ) {
97+ String username = credentials .substring (0 , i );
98+ String password = credentials .substring (i + 1 );
8999
90- if (credentials != null ) {
91- int space = credentials .indexOf (' ' );
92- if (space > 0 ) {
93- String method = credentials .substring (0 , space );
94- if ("basic" .equalsIgnoreCase (method )) {
95- credentials = credentials .substring (space + 1 );
96- credentials = new String (Base64 .getDecoder ().decode (credentials ), StandardCharsets .ISO_8859_1 );
97- int i = credentials .indexOf (':' );
98- if (i > 0 ) {
99- String username = credentials .substring (0 , i );
100- String password = credentials .substring (i + 1 );
101-
102- UserIdentity user = login (username , password , request );
103- if (user != null ) {
104- return new UserAuthentication (getAuthMethod (), user );
105- }
106- }
100+ return AuthenticationState .login (username , password , req , res );
107101 }
108102 }
109103 }
110-
111- if (DeferredAuthentication .isDeferred (response ))
112- return Authentication .UNAUTHENTICATED ;
113-
114- response .setHeader (HttpHeader .PROXY_AUTHENTICATE .asString (), "basic realm=\" " + _loginService .getName () + '"' );
115- response .sendError (HttpServletResponse .SC_PROXY_AUTHENTICATION_REQUIRED );
116- return Authentication .SEND_CONTINUE ;
117- } catch (IOException e ) {
118- throw new ServerAuthException (e );
119104 }
120- }
121105
122- @ Override
123- public boolean secureResponse ( ServletRequest req , ServletResponse res , boolean mandatory , User validatedUser ) throws ServerAuthException {
124- return true ;
106+ res . getHeaders (). add ( HttpHeader . PROXY_AUTHENTICATE . asString (), "basic realm= \" " + _loginService . getName () + '"' );
107+ res . setStatus ( HttpServletResponse . SC_PROXY_AUTHENTICATION_REQUIRED );
108+ return AuthenticationState . CHALLENGE ;
125109 }
126-
127110}
0 commit comments