1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.security.authentication;
20
21 import java.io.IOException;
22 import javax.servlet.ServletRequest;
23 import javax.servlet.ServletResponse;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.eclipse.jetty.http.HttpHeader;
28 import org.eclipse.jetty.security.ServerAuthException;
29 import org.eclipse.jetty.security.UserAuthentication;
30 import org.eclipse.jetty.server.Authentication;
31 import org.eclipse.jetty.server.Authentication.User;
32 import org.eclipse.jetty.server.UserIdentity;
33 import org.eclipse.jetty.util.log.Log;
34 import org.eclipse.jetty.util.log.Logger;
35 import org.eclipse.jetty.util.security.Constraint;
36
37 public class SpnegoAuthenticator extends LoginAuthenticator
38 {
39 private static final Logger LOG = Log.getLogger(SpnegoAuthenticator.class);
40 private String _authMethod = Constraint.__SPNEGO_AUTH;
41
42 public SpnegoAuthenticator()
43 {
44 }
45
46
47
48
49
50 public SpnegoAuthenticator( String authMethod )
51 {
52 _authMethod = authMethod;
53 }
54
55 @Override
56 public String getAuthMethod()
57 {
58 return _authMethod;
59 }
60
61 @Override
62 public Authentication validateRequest(ServletRequest request, ServletResponse response, boolean mandatory) throws ServerAuthException
63 {
64 HttpServletRequest req = (HttpServletRequest)request;
65 HttpServletResponse res = (HttpServletResponse)response;
66
67 String header = req.getHeader(HttpHeader.AUTHORIZATION.asString());
68
69 if (!mandatory)
70 {
71 return new DeferredAuthentication(this);
72 }
73
74
75 if ( header == null )
76 {
77 try
78 {
79 if (DeferredAuthentication.isDeferred(res))
80 {
81 return Authentication.UNAUTHENTICATED;
82 }
83
84 LOG.debug("SpengoAuthenticator: sending challenge");
85 res.setHeader(HttpHeader.WWW_AUTHENTICATE.asString(), HttpHeader.NEGOTIATE.asString());
86 res.sendError(HttpServletResponse.SC_UNAUTHORIZED);
87 return Authentication.SEND_CONTINUE;
88 }
89 catch (IOException ioe)
90 {
91 throw new ServerAuthException(ioe);
92 }
93 }
94 else if (header != null && header.startsWith(HttpHeader.NEGOTIATE.asString()))
95 {
96 String spnegoToken = header.substring(10);
97
98 UserIdentity user = login(null,spnegoToken, request);
99
100 if ( user != null )
101 {
102 return new UserAuthentication(getAuthMethod(),user);
103 }
104 }
105
106 return Authentication.UNAUTHENTICATED;
107 }
108
109 @Override
110 public boolean secureResponse(ServletRequest request, ServletResponse response, boolean mandatory, User validatedUser) throws ServerAuthException
111 {
112 return true;
113 }
114
115 }