|
12 | 12 | * information: "Portions copyright [year] [name of copyright owner]".
|
13 | 13 | *
|
14 | 14 | * Copyright 2013-2016 ForgeRock AS.
|
| 15 | + * Portions Copyright 2024 Wren Security |
15 | 16 | */
|
16 | 17 |
|
17 | 18 | package org.forgerock.jaspi.modules.iwa;
|
|
23 | 24 |
|
24 | 25 | import javax.security.auth.Subject;
|
25 | 26 | import javax.security.auth.callback.CallbackHandler;
|
26 |
| -import javax.security.auth.message.AuthException; |
27 | 27 | import javax.security.auth.message.AuthStatus;
|
28 | 28 | import javax.security.auth.message.MessagePolicy;
|
29 | 29 | import java.security.Principal;
|
30 | 30 | import java.util.Arrays;
|
31 | 31 | import java.util.Collection;
|
32 |
| -import java.util.HashMap; |
33 | 32 | import java.util.Map;
|
34 | 33 |
|
35 | 34 | import org.forgerock.caf.authentication.api.AsyncServerAuthModule;
|
|
38 | 37 | import org.forgerock.http.protocol.Request;
|
39 | 38 | import org.forgerock.http.protocol.Response;
|
40 | 39 | import org.forgerock.http.protocol.Status;
|
| 40 | +import org.forgerock.jaspi.modules.iwa.wdsso.Base64; |
41 | 41 | import org.forgerock.jaspi.modules.iwa.wdsso.WDSSO;
|
42 | 42 | import org.forgerock.util.promise.Promise;
|
43 | 43 |
|
|
49 | 49 | public class IWAModule implements AsyncServerAuthModule {
|
50 | 50 |
|
51 | 51 | private static final String IWA_FAILED = "iwa-failed";
|
| 52 | + private static final String NEGOTIATE_AUTH_SCHEME = "Negotiate"; |
52 | 53 |
|
53 |
| - private CallbackHandler handler; |
54 |
| - private Map options; |
| 54 | + private Map<String, Object> options; |
55 | 55 |
|
56 | 56 | @Override
|
57 | 57 | public String getModuleId() {
|
58 | 58 | return "IWA";
|
59 | 59 | }
|
60 | 60 |
|
61 |
| - /** |
62 |
| - * {@inheritDoc} |
63 |
| - */ |
64 | 61 | @Override
|
65 | 62 | public void initialize(MessagePolicy requestPolicy, MessagePolicy responsePolicy, CallbackHandler handler,
|
66 | 63 | Map<String, Object> options) throws AuthenticationException {
|
67 |
| - this.handler = handler; |
68 | 64 | this.options = options;
|
69 | 65 | }
|
70 | 66 |
|
71 |
| - /** |
72 |
| - * {@inheritDoc} |
73 |
| - */ |
74 | 67 | @Override
|
75 | 68 | public Collection<Class<?>> getSupportedMessageTypes() {
|
76 |
| - return Arrays.asList(new Class<?>[]{Request.class, Response.class}); |
| 69 | + return Arrays.asList(new Class<?>[]{ Request.class, Response.class }); |
77 | 70 | }
|
78 | 71 |
|
79 | 72 | /**
|
80 | 73 | * Validates the request by checking the Authorization header in the request for a IWA token and processes that
|
81 | 74 | * for authentication.
|
82 |
| - * |
83 |
| - * @param messageInfo {@inheritDoc} |
84 |
| - * @param clientSubject {@inheritDoc} |
85 |
| - * @param serviceSubject {@inheritDoc} |
86 |
| - * @return {@inheritDoc} |
87 | 75 | */
|
88 | 76 | @Override
|
89 | 77 | public Promise<AuthStatus, AuthenticationException> validateRequest(MessageInfoContext messageInfo,
|
90 | 78 | Subject clientSubject, Subject serviceSubject) {
|
91 |
| - |
92 |
| - LOG.debug("IWAModule: validateRequest START"); |
93 |
| - |
94 | 79 | Request request = messageInfo.getRequest();
|
95 | 80 | Response response = messageInfo.getResponse();
|
96 | 81 |
|
97 |
| - String httpAuthorization = request.getHeaders().getFirst("Authorization"); |
| 82 | + String authorizationHeader = request.getHeaders().getFirst("Authorization"); |
98 | 83 |
|
| 84 | + if (authorizationHeader == null || authorizationHeader.isEmpty()) { |
| 85 | + LOG.debug("IWAModule: Authorization Header NOT set in request."); |
| 86 | + |
| 87 | + response.getHeaders().put("WWW-Authenticate", NEGOTIATE_AUTH_SCHEME); |
| 88 | + response.setStatus(Status.UNAUTHORIZED); |
| 89 | + response.setEntity(Map.of("failure", true, "recason", IWA_FAILED)); |
| 90 | + return newResultPromise(SEND_CONTINUE); |
| 91 | + } |
| 92 | + |
| 93 | + // Handle only negotiate authentication requests |
| 94 | + if (!authorizationHeader.startsWith(NEGOTIATE_AUTH_SCHEME)) { |
| 95 | + return newResultPromise(SEND_FAILURE); |
| 96 | + } |
| 97 | + |
| 98 | + LOG.debug("IWAModule: Negotiate authorization header set in request."); |
| 99 | + |
| 100 | + // Check SPNEGO token |
| 101 | + byte[] spnegoToken = extractSpnegoToken(authorizationHeader); |
| 102 | + if (spnegoToken == null) { |
| 103 | + return newExceptionPromise(new AuthenticationException("Invalid SPNEGO token")); |
| 104 | + } |
| 105 | + |
| 106 | + // Ignore NTLM over SPNEGO |
| 107 | + if (spnegoToken[0] == 'N' && spnegoToken[1] == 'T' && spnegoToken[2] == 'L' && spnegoToken[3] == 'M') { |
| 108 | + return newResultPromise(SEND_FAILURE); |
| 109 | + } |
| 110 | + |
| 111 | + // Perform Kerberos authentication |
99 | 112 | try {
|
100 |
| - if (httpAuthorization == null || "".equals(httpAuthorization)) { |
101 |
| - LOG.debug("IWAModule: Authorization Header NOT set in request."); |
102 |
| - |
103 |
| - response.getHeaders().put("WWW-Authenticate", "Negotiate"); |
104 |
| - response.setStatus(Status.UNAUTHORIZED); |
105 |
| - Map<String, Object> entity = new HashMap<>(); |
106 |
| - entity.put("failure", true); |
107 |
| - entity.put("recason", IWA_FAILED); |
108 |
| - response.setEntity(entity); |
109 |
| - |
110 |
| - return newResultPromise(SEND_CONTINUE); |
111 |
| - } else { |
112 |
| - LOG.debug("IWAModule: Authorization Header set in request."); |
113 |
| - try { |
114 |
| - final String username = new WDSSO().process(options, messageInfo, request); |
115 |
| - LOG.debug("IWAModule: IWA successful with username, {}", username); |
116 |
| - |
117 |
| - clientSubject.getPrincipals().add(new Principal() { |
118 |
| - public String getName() { |
119 |
| - return username; |
120 |
| - } |
121 |
| - }); |
122 |
| - } catch (Exception e) { |
123 |
| - LOG.debug("IWAModule: IWA has failed. {}", e.getMessage()); |
124 |
| - return newExceptionPromise(new AuthenticationException("IWA has failed")); |
125 |
| - } |
| 113 | + final String username = new WDSSO().process(options, messageInfo, spnegoToken); |
| 114 | + LOG.debug("IWAModule: IWA successful with username, {}", username); |
126 | 115 |
|
127 |
| - return newResultPromise(SUCCESS); |
128 |
| - } |
129 |
| - } finally { |
130 |
| - LOG.debug("IWAModule: validateRequest END"); |
| 116 | + clientSubject.getPrincipals().add(new Principal() { |
| 117 | + @Override |
| 118 | + public String getName() { |
| 119 | + return username; |
| 120 | + } |
| 121 | + }); |
| 122 | + return newResultPromise(SUCCESS); |
| 123 | + } catch (Exception e) { |
| 124 | + LOG.debug("IWAModule: IWA has failed. {}", e.getMessage()); |
| 125 | + return newExceptionPromise(new AuthenticationException("IWA has failed")); |
131 | 126 | }
|
132 | 127 | }
|
133 | 128 |
|
134 |
| - /** |
135 |
| - * Always returns AuthStatus.SEND_SUCCESS. |
136 |
| - * |
137 |
| - * @param messageInfo {@inheritDoc} |
138 |
| - * @param serviceSubject {@inheritDoc} |
139 |
| - * @return {@inheritDoc} |
140 |
| - */ |
141 | 129 | @Override
|
142 |
| - public Promise<AuthStatus, AuthenticationException> secureResponse(MessageInfoContext messageInfo, |
143 |
| - Subject serviceSubject) { |
| 130 | + public Promise<AuthStatus, AuthenticationException> secureResponse(MessageInfoContext messageInfo, Subject serviceSubject) { |
144 | 131 | return newResultPromise(SEND_SUCCESS);
|
145 | 132 | }
|
146 | 133 |
|
147 |
| - /** |
148 |
| - * {@inheritDoc} |
149 |
| - */ |
150 | 134 | @Override
|
151 | 135 | public Promise<Void, AuthenticationException> cleanSubject(MessageInfoContext messageInfo, Subject subject) {
|
152 | 136 | return newResultPromise(null);
|
153 | 137 | }
|
| 138 | + |
| 139 | + /** |
| 140 | + * Extract SPNEGO token from the specified negotiate authorization header. |
| 141 | + * @param header Authorization header to extract SPNEGO token. |
| 142 | + * @return Extracted token or null when extraction fails. |
| 143 | + */ |
| 144 | + private byte[] extractSpnegoToken(String header) { |
| 145 | + try { |
| 146 | + return Base64.decode(header.substring(NEGOTIATE_AUTH_SCHEME.length()).trim()); |
| 147 | + } catch (Exception e) { |
| 148 | + LOG.error("IWAModule: Failed to extract SPNEGO token from authorization header"); |
| 149 | + return null; |
| 150 | + } |
| 151 | + } |
| 152 | + |
154 | 153 | }
|
0 commit comments