]> git.basschouten.com Git - openhab-addons.git/blob
f457624d83bbbb88fcaffe397faa6aef78544ccd
[openhab-addons.git] /
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
5  *
6  * The contents of this file are subject to the terms of either the GNU
7  * General Public License Version 2 only ("GPL") or the Common Development
8  * and Distribution License("CDDL") (collectively, the "License").  You
9  * may not use this file except in compliance with the License.  You can
10  * obtain a copy of the License at
11  * http://glassfish.java.net/public/CDDL+GPL_1_1.html
12  * or packager/legal/LICENSE.txt.  See the License for the specific
13  * language governing permissions and limitations under the License.
14  *
15  * When distributing the software, include this License Header Notice in each
16  * file and include the License file at packager/legal/LICENSE.txt.
17  *
18  * GPL Classpath Exception:
19  * Oracle designates this particular file as subject to the "Classpath"
20  * exception as provided by Oracle in the GPL Version 2 section of the License
21  * file that accompanied this code.
22  *
23  * Modifications:
24  * If applicable, add the following below the License Header, with the fields
25  * enclosed by brackets [] replaced by your own identifying information:
26  * "Portions Copyright [year] [name of copyright owner]"
27  *
28  * Contributor(s):
29  * If you wish your version of this file to be governed by only the CDDL or
30  * only the GPL Version 2, indicate your decision by adding "[Contributor]
31  * elects to include this software in this distribution under the [CDDL or GPL
32  * Version 2] license."  If you don't indicate a single choice of license, a
33  * recipient has the option to distribute your version of this file under
34  * either the CDDL, the GPL Version 2 or to extend the choice of license to
35  * its licensees as provided above.  However, if you add GPL Version 2 code
36  * and therefore, elected the GPL Version 2 license, then the option applies
37  * only if the new code is made subject to such option by the copyright
38  * holder.
39  */
40 package org.openhab.binding.lametrictime.internal.api.authentication;
41
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.net.URI;
45 import java.nio.charset.Charset;
46 import java.util.List;
47 import java.util.Map;
48
49 import javax.annotation.Priority;
50 import javax.ws.rs.Priorities;
51 import javax.ws.rs.client.Client;
52 import javax.ws.rs.client.ClientBuilder;
53 import javax.ws.rs.client.ClientRequestContext;
54 import javax.ws.rs.client.ClientRequestFilter;
55 import javax.ws.rs.client.ClientResponseContext;
56 import javax.ws.rs.client.ClientResponseFilter;
57 import javax.ws.rs.client.Entity;
58 import javax.ws.rs.client.Invocation;
59 import javax.ws.rs.client.WebTarget;
60 import javax.ws.rs.core.Configuration;
61 import javax.ws.rs.core.HttpHeaders;
62 import javax.ws.rs.core.MediaType;
63 import javax.ws.rs.core.MultivaluedHashMap;
64 import javax.ws.rs.core.MultivaluedMap;
65 import javax.ws.rs.core.Response;
66
67 /**
68  * Http Authentication filter that provides basic and digest authentication (based on RFC 2617).
69  *
70  * @author Miroslav Fuksa
71  */
72 @Priority(Priorities.AUTHENTICATION)
73 class HttpAuthenticationFilter implements ClientRequestFilter, ClientResponseFilter {
74
75     /**
76      * Authentication type.
77      */
78     enum Type {
79         /**
80          * Basic authentication.
81          */
82         BASIC
83     }
84
85     private static final String REQUEST_PROPERTY_FILTER_REUSED = "org.openhab.binding.lametrictime.api.authentication.HttpAuthenticationFilter.reused";
86     private static final String REQUEST_PROPERTY_OPERATION = "org.openhab.binding.lametrictime.api.authentication.HttpAuthenticationFilter.operation";
87
88     /**
89      * Encoding used for authentication calculations.
90      */
91     static final Charset CHARACTER_SET = Charset.forName("iso-8859-1");
92
93     private final HttpAuthenticationFeature.Mode mode;
94
95     private final BasicAuthenticator basicAuth;
96
97     /**
98      * Create a new filter instance.
99      *
100      * @param mode Mode.
101      * @param basicCredentials Basic credentials (can be {@code null} if this filter does not work in the
102      *            basic mode or if no default credentials are defined).
103      * @param digestCredentials Digest credentials (can be {@code null} if this filter does not work in the
104      *            digest mode or if no default credentials are defined).
105      * @param configuration Configuration (non-{@code null}).
106      */
107     HttpAuthenticationFilter(HttpAuthenticationFeature.Mode mode, Credentials basicCredentials,
108             Configuration configuration) {
109         this.mode = mode;
110         switch (mode) {
111             case BASIC_PREEMPTIVE:
112             case BASIC_NON_PREEMPTIVE:
113                 this.basicAuth = new BasicAuthenticator(basicCredentials);
114                 break;
115             case UNIVERSAL:
116                 this.basicAuth = new BasicAuthenticator(basicCredentials);
117                 break;
118             default:
119                 throw new IllegalStateException("Not implemented.");
120         }
121     }
122
123     @Override
124     public void filter(ClientRequestContext request) throws IOException {
125         if ("true".equals(request.getProperty(REQUEST_PROPERTY_FILTER_REUSED))) {
126             return;
127         }
128
129         if (request.getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) {
130             return;
131         }
132
133         Type operation = null;
134         if (mode == HttpAuthenticationFeature.Mode.BASIC_PREEMPTIVE) {
135             basicAuth.filterRequest(request);
136             operation = Type.BASIC;
137         } else if (mode == HttpAuthenticationFeature.Mode.BASIC_NON_PREEMPTIVE) {
138             // do nothing
139         }
140
141         if (operation != null) {
142             request.setProperty(REQUEST_PROPERTY_OPERATION, operation);
143         }
144     }
145
146     @Override
147     public void filter(ClientRequestContext request, ClientResponseContext response) throws IOException {
148         if ("true".equals(request.getProperty(REQUEST_PROPERTY_FILTER_REUSED))) {
149             return;
150         }
151
152         Type result = null; // which authentication is requested: BASIC or DIGEST
153         boolean authenticate;
154
155         if (response.getStatus() == Response.Status.UNAUTHORIZED.getStatusCode()) {
156             String authString = response.getHeaders().getFirst(HttpHeaders.WWW_AUTHENTICATE);
157             if (authString != null) {
158                 final String upperCaseAuth = authString.trim().toUpperCase();
159                 if (upperCaseAuth.startsWith("BASIC")) {
160                     result = Type.BASIC;
161                 } else {
162                     // unknown authentication -> this filter cannot authenticate with this method
163                     return;
164                 }
165             }
166             authenticate = true;
167         } else {
168             authenticate = false;
169         }
170
171         if (mode == HttpAuthenticationFeature.Mode.BASIC_PREEMPTIVE) {
172             // do nothing -> 401 will be returned to the client
173         } else if (mode == HttpAuthenticationFeature.Mode.BASIC_NON_PREEMPTIVE) {
174             if (authenticate && result == Type.BASIC) {
175                 basicAuth.filterResponseAndAuthenticate(request, response);
176             }
177         } else if (mode == HttpAuthenticationFeature.Mode.UNIVERSAL) {
178             if (authenticate) {
179                 boolean success = false;
180
181                 // now we have the challenge response and we can authenticate
182                 if (result == Type.BASIC) {
183                     success = basicAuth.filterResponseAndAuthenticate(request, response);
184                 }
185             }
186         }
187     }
188
189     private String getCacheKey(ClientRequestContext request) {
190         return request.getUri().toString() + ":" + request.getMethod();
191     }
192
193     /**
194      * Repeat the {@code request} with provided {@code newAuthorizationHeader}
195      * and update the {@code response} with newest response data.
196      *
197      * @param request Request context.
198      * @param response Response context (will be updated with the new response data).
199      * @param newAuthorizationHeader {@code Authorization} header that should be added to the new request.
200      * @return {@code true} is the authentication was successful ({@code true} if 401 response code was not returned;
201      *         {@code false} otherwise).
202      */
203     static boolean repeatRequest(ClientRequestContext request, ClientResponseContext response,
204             String newAuthorizationHeader) {
205         Client client = ClientBuilder.newClient(request.getConfiguration());
206         String method = request.getMethod();
207         MediaType mediaType = request.getMediaType();
208         URI lUri = request.getUri();
209
210         WebTarget resourceTarget = client.target(lUri);
211
212         Invocation.Builder builder = resourceTarget.request(mediaType);
213
214         MultivaluedMap<String, Object> newHeaders = new MultivaluedHashMap<String, Object>();
215
216         for (Map.Entry<String, List<Object>> entry : request.getHeaders().entrySet()) {
217             if (HttpHeaders.AUTHORIZATION.equals(entry.getKey())) {
218                 continue;
219             }
220             newHeaders.put(entry.getKey(), entry.getValue());
221         }
222
223         newHeaders.add(HttpHeaders.AUTHORIZATION, newAuthorizationHeader);
224         builder.headers(newHeaders);
225
226         builder.property(REQUEST_PROPERTY_FILTER_REUSED, "true");
227
228         Invocation invocation;
229         if (request.getEntity() == null) {
230             invocation = builder.build(method);
231         } else {
232             invocation = builder.build(method, Entity.entity(request.getEntity(), request.getMediaType()));
233         }
234         Response nextResponse = invocation.invoke();
235
236         if (nextResponse.hasEntity()) {
237             response.setEntityStream(nextResponse.readEntity(InputStream.class));
238         }
239         MultivaluedMap<String, String> headers = response.getHeaders();
240         headers.clear();
241         headers.putAll(nextResponse.getStringHeaders());
242         response.setStatus(nextResponse.getStatus());
243
244         return response.getStatus() != Response.Status.UNAUTHORIZED.getStatusCode();
245     }
246
247     /**
248      * Credentials (username + password).
249      */
250     static class Credentials {
251
252         private final String username;
253         private final byte[] password;
254
255         /**
256          * Create a new credentials from username and password as byte array.
257          *
258          * @param username Username.
259          * @param password Password as byte array.
260          */
261         Credentials(String username, byte[] password) {
262             this.username = username;
263             this.password = password;
264         }
265
266         /**
267          * Create a new credentials from username and password as {@link String}.
268          *
269          * @param username Username.
270          * @param password {@code String} password.
271          */
272         Credentials(String username, String password) {
273             this.username = username;
274             this.password = password == null ? null : password.getBytes(CHARACTER_SET);
275         }
276
277         /**
278          * Return username.
279          *
280          * @return username.
281          */
282         String getUsername() {
283             return username;
284         }
285
286         /**
287          * Return password as byte array.
288          *
289          * @return Password string in byte array representation.
290          */
291         byte[] getPassword() {
292             return password;
293         }
294     }
295
296     private static Credentials extractCredentials(ClientRequestContext request, Type type) {
297         String usernameKey = null;
298         String passwordKey = null;
299         if (type == null) {
300             usernameKey = HttpAuthenticationFeature.HTTP_AUTHENTICATION_USERNAME;
301             passwordKey = HttpAuthenticationFeature.HTTP_AUTHENTICATION_PASSWORD;
302         } else if (type == Type.BASIC) {
303             usernameKey = HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_USERNAME;
304             passwordKey = HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_PASSWORD;
305         }
306
307         String userName = (String) request.getProperty(usernameKey);
308         if (userName != null && !userName.equals("")) {
309             byte[] pwdBytes;
310             Object password = request.getProperty(passwordKey);
311             if (password instanceof byte[]) {
312                 pwdBytes = ((byte[]) password);
313             } else if (password instanceof String) {
314                 pwdBytes = ((String) password).getBytes(CHARACTER_SET);
315             } else {
316                 throw new RequestAuthenticationException("Passwort invalid.");
317             }
318             return new Credentials(userName, pwdBytes);
319         }
320         return null;
321     }
322
323     /**
324      * Get credentials actual for the current request. Priorities in credentials selection are the following:
325      * <ol>
326      * <li>Basic/digest specific credentials defined in the request properties</li>
327      * <li>Common credentials defined in the request properties</li>
328      * <li>{@code defaultCredentials}</li>
329      * </ol>
330      *
331      * @param request Request from which credentials should be extracted.
332      * @param defaultCredentials Default credentials (can be {@code null}).
333      * @param type Type of requested credentials.
334      * @return Credentials or {@code null} if no credentials are found and {@code defaultCredentials} are {@code null}.
335      * @throws RequestAuthenticationException in case the {@code username} or {@code password} is invalid.
336      */
337     static Credentials getCredentials(ClientRequestContext request, Credentials defaultCredentials, Type type) {
338         Credentials commonCredentials = extractCredentials(request, type);
339
340         if (commonCredentials != null) {
341             return commonCredentials;
342         } else {
343             Credentials specificCredentials = extractCredentials(request, null);
344
345             return specificCredentials != null ? specificCredentials : defaultCredentials;
346         }
347     }
348 }