|
14 | 14 | package org.eclipse.jetty.security;
|
15 | 15 |
|
16 | 16 | import java.util.Collection;
|
| 17 | +import java.util.List; |
17 | 18 |
|
18 | 19 | import org.eclipse.jetty.security.Authenticator.Configuration;
|
19 | 20 | import org.eclipse.jetty.security.authentication.BasicAuthenticator;
|
|
26 | 27 | import org.eclipse.jetty.security.internal.DeferredAuthenticationState;
|
27 | 28 | import org.eclipse.jetty.server.Context;
|
28 | 29 | import org.eclipse.jetty.server.Server;
|
| 30 | +import org.eclipse.jetty.util.StringUtil; |
29 | 31 | import org.eclipse.jetty.util.ssl.SslContextFactory;
|
30 | 32 |
|
31 | 33 | /**
|
@@ -53,27 +55,74 @@ public class DefaultAuthenticatorFactory implements Authenticator.Factory
|
53 | 55 | @Override
|
54 | 56 | public Authenticator getAuthenticator(Server server, Context context, Configuration configuration)
|
55 | 57 | {
|
56 |
| - String auth = configuration.getAuthenticationType(); |
57 |
| - Authenticator authenticator = null; |
| 58 | + String auth = StringUtil.asciiToUpperCase(configuration.getAuthenticationType()); |
| 59 | + if (auth == null) |
| 60 | + return null; |
58 | 61 |
|
59 |
| - if (Authenticator.BASIC_AUTH.equalsIgnoreCase(auth)) |
60 |
| - authenticator = new BasicAuthenticator(); |
61 |
| - else if (Authenticator.DIGEST_AUTH.equalsIgnoreCase(auth)) |
62 |
| - authenticator = new DigestAuthenticator(); |
63 |
| - else if (Authenticator.FORM_AUTH.equalsIgnoreCase(auth)) |
64 |
| - authenticator = new FormAuthenticator(); |
65 |
| - else if (Authenticator.SPNEGO_AUTH.equalsIgnoreCase(auth)) |
66 |
| - authenticator = new SPNEGOAuthenticator(); |
67 |
| - else if (Authenticator.NEGOTIATE_AUTH.equalsIgnoreCase(auth)) // see Bug #377076 |
68 |
| - authenticator = new SPNEGOAuthenticator(Authenticator.NEGOTIATE_AUTH); |
69 |
| - if (Authenticator.CERT_AUTH2.equalsIgnoreCase(auth)) |
| 62 | + return switch (auth) |
70 | 63 | {
|
71 |
| - Collection<SslContextFactory> sslContextFactories = server.getBeans(SslContextFactory.class); |
72 |
| - if (sslContextFactories.size() != 1) |
73 |
| - throw new IllegalStateException("SslClientCertAuthenticator requires a single SslContextFactory instances."); |
74 |
| - authenticator = new SslClientCertAuthenticator(sslContextFactories.iterator().next()); |
75 |
| - } |
| 64 | + case Authenticator.BASIC_AUTH -> new BasicAuthenticator(); |
| 65 | + case Authenticator.DIGEST_AUTH -> new DigestAuthenticator(); |
| 66 | + case Authenticator.FORM_AUTH -> new FormAuthenticator(); |
| 67 | + case Authenticator.SPNEGO_AUTH -> new SPNEGOAuthenticator(); |
| 68 | + case Authenticator.NEGOTIATE_AUTH -> new SPNEGOAuthenticator(Authenticator.NEGOTIATE_AUTH); // see Bug #377076 |
| 69 | + case Authenticator.MULTI_AUTH -> getMultiAuthenticator(server, context, configuration); |
| 70 | + case Authenticator.CERT_AUTH, Authenticator.CERT_AUTH2 -> |
| 71 | + { |
| 72 | + Collection<SslContextFactory> sslContextFactories = server.getBeans(SslContextFactory.class); |
| 73 | + if (sslContextFactories.size() != 1) |
| 74 | + throw new IllegalStateException("SslClientCertAuthenticator requires a single SslContextFactory instances."); |
| 75 | + yield new SslClientCertAuthenticator(sslContextFactories.iterator().next()); |
| 76 | + } |
| 77 | + default -> null; |
| 78 | + }; |
| 79 | + } |
76 | 80 |
|
77 |
| - return authenticator; |
| 81 | + private Authenticator getMultiAuthenticator(Server server, Context context, Authenticator.Configuration configuration) |
| 82 | + { |
| 83 | + SecurityHandler securityHandler = SecurityHandler.getCurrentSecurityHandler(); |
| 84 | + if (securityHandler == null) |
| 85 | + return null; |
| 86 | + |
| 87 | + String auth = configuration.getAuthenticationType(); |
| 88 | + if (Authenticator.MULTI_AUTH.equalsIgnoreCase(auth)) |
| 89 | + { |
| 90 | + MultiAuthenticator multiAuthenticator = new MultiAuthenticator(); |
| 91 | + |
| 92 | + String authenticatorConfig = configuration.getParameter("org.eclipse.jetty.security.multi.authenticators"); |
| 93 | + for (String config : StringUtil.csvSplit(authenticatorConfig)) |
| 94 | + { |
| 95 | + String[] parts = config.split(":"); |
| 96 | + if (parts.length != 2) |
| 97 | + throw new IllegalArgumentException(); |
| 98 | + |
| 99 | + String authType = parts[0].trim(); |
| 100 | + String pathSpec = parts[1].trim(); |
| 101 | + |
| 102 | + Authenticator.Configuration.Wrapper authConfig = new Authenticator.Configuration.Wrapper(configuration) |
| 103 | + { |
| 104 | + @Override |
| 105 | + public String getAuthenticationType() |
| 106 | + { |
| 107 | + return authType; |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + Authenticator authenticator = null; |
| 112 | + List<Authenticator.Factory> authenticatorFactories = securityHandler.getKnownAuthenticatorFactories(); |
| 113 | + for (Authenticator.Factory factory : authenticatorFactories) |
| 114 | + { |
| 115 | + authenticator = factory.getAuthenticator(server, context, authConfig); |
| 116 | + if (authenticator != null) |
| 117 | + break; |
| 118 | + } |
| 119 | + |
| 120 | + if (authenticator == null) |
| 121 | + throw new IllegalStateException(); |
| 122 | + multiAuthenticator.addAuthenticator(pathSpec, authenticator); |
| 123 | + } |
| 124 | + return multiAuthenticator; |
| 125 | + } |
| 126 | + return null; |
78 | 127 | }
|
79 | 128 | }
|
0 commit comments