2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
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.
15 * When distributing the software, include this License Header Notice in each
16 * file and include the License file at packager/legal/LICENSE.txt.
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.
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]"
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
41 package org.openhab.binding.lametrictime.api.authentication;
43 import java.io.IOException;
44 import java.io.InputStream;
46 import java.nio.charset.Charset;
47 import java.util.List;
50 import javax.annotation.Priority;
51 import javax.ws.rs.Priorities;
52 import javax.ws.rs.client.Client;
53 import javax.ws.rs.client.ClientBuilder;
54 import javax.ws.rs.client.ClientRequestContext;
55 import javax.ws.rs.client.ClientRequestFilter;
56 import javax.ws.rs.client.ClientResponseContext;
57 import javax.ws.rs.client.ClientResponseFilter;
58 import javax.ws.rs.client.Entity;
59 import javax.ws.rs.client.Invocation;
60 import javax.ws.rs.client.WebTarget;
61 import javax.ws.rs.core.Configuration;
62 import javax.ws.rs.core.HttpHeaders;
63 import javax.ws.rs.core.MediaType;
64 import javax.ws.rs.core.MultivaluedHashMap;
65 import javax.ws.rs.core.MultivaluedMap;
66 import javax.ws.rs.core.Response;
69 * Http Authentication filter that provides basic and digest authentication (based on RFC 2617).
71 * @author Miroslav Fuksa
73 @Priority(Priorities.AUTHENTICATION)
74 class HttpAuthenticationFilter implements ClientRequestFilter, ClientResponseFilter {
77 * Authentication type.
81 * Basic authentication.
86 private static final String REQUEST_PROPERTY_FILTER_REUSED = "org.openhab.binding.lametrictime.api.authentication.HttpAuthenticationFilter.reused";
87 private static final String REQUEST_PROPERTY_OPERATION = "org.openhab.binding.lametrictime.api.authentication.HttpAuthenticationFilter.operation";
90 * Encoding used for authentication calculations.
92 static final Charset CHARACTER_SET = Charset.forName("iso-8859-1");
94 private final HttpAuthenticationFeature.Mode mode;
96 private final BasicAuthenticator basicAuth;
99 * Create a new filter instance.
102 * @param basicCredentials Basic credentials (can be {@code null} if this filter does not work in the
103 * basic mode or if no default credentials are defined).
104 * @param digestCredentials Digest credentials (can be {@code null} if this filter does not work in the
105 * digest mode or if no default credentials are defined).
106 * @param configuration Configuration (non-{@code null}).
108 HttpAuthenticationFilter(HttpAuthenticationFeature.Mode mode, Credentials basicCredentials,
109 Configuration configuration) {
112 case BASIC_PREEMPTIVE:
113 case BASIC_NON_PREEMPTIVE:
114 this.basicAuth = new BasicAuthenticator(basicCredentials);
117 this.basicAuth = new BasicAuthenticator(basicCredentials);
120 throw new IllegalStateException("Not implemented.");
125 public void filter(ClientRequestContext request) throws IOException {
126 if ("true".equals(request.getProperty(REQUEST_PROPERTY_FILTER_REUSED))) {
130 if (request.getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) {
134 Type operation = null;
135 if (mode == HttpAuthenticationFeature.Mode.BASIC_PREEMPTIVE) {
136 basicAuth.filterRequest(request);
137 operation = Type.BASIC;
138 } else if (mode == HttpAuthenticationFeature.Mode.BASIC_NON_PREEMPTIVE) {
142 if (operation != null) {
143 request.setProperty(REQUEST_PROPERTY_OPERATION, operation);
148 public void filter(ClientRequestContext request, ClientResponseContext response) throws IOException {
149 if ("true".equals(request.getProperty(REQUEST_PROPERTY_FILTER_REUSED))) {
153 Type result = null; // which authentication is requested: BASIC or DIGEST
154 boolean authenticate;
156 if (response.getStatus() == Response.Status.UNAUTHORIZED.getStatusCode()) {
157 String authString = response.getHeaders().getFirst(HttpHeaders.WWW_AUTHENTICATE);
158 if (authString != null) {
159 final String upperCaseAuth = authString.trim().toUpperCase();
160 if (upperCaseAuth.startsWith("BASIC")) {
163 // unknown authentication -> this filter cannot authenticate with this method
169 authenticate = false;
172 if (mode == HttpAuthenticationFeature.Mode.BASIC_PREEMPTIVE) {
173 // do nothing -> 401 will be returned to the client
174 } else if (mode == HttpAuthenticationFeature.Mode.BASIC_NON_PREEMPTIVE) {
175 if (authenticate && result == Type.BASIC) {
176 basicAuth.filterResponseAndAuthenticate(request, response);
178 } else if (mode == HttpAuthenticationFeature.Mode.UNIVERSAL) {
180 boolean success = false;
182 // now we have the challenge response and we can authenticate
183 if (result == Type.BASIC) {
184 success = basicAuth.filterResponseAndAuthenticate(request, response);
190 private String getCacheKey(ClientRequestContext request) {
191 return request.getUri().toString() + ":" + request.getMethod();
195 * Repeat the {@code request} with provided {@code newAuthorizationHeader}
196 * and update the {@code response} with newest response data.
198 * @param request Request context.
199 * @param response Response context (will be updated with the new response data).
200 * @param newAuthorizationHeader {@code Authorization} header that should be added to the new request.
201 * @return {@code true} is the authentication was successful ({@code true} if 401 response code was not returned;
202 * {@code false} otherwise).
204 static boolean repeatRequest(ClientRequestContext request, ClientResponseContext response,
205 String newAuthorizationHeader) {
206 Client client = ClientBuilder.newClient(request.getConfiguration());
207 String method = request.getMethod();
208 MediaType mediaType = request.getMediaType();
209 URI lUri = request.getUri();
211 WebTarget resourceTarget = client.target(lUri);
213 Invocation.Builder builder = resourceTarget.request(mediaType);
215 MultivaluedMap<String, Object> newHeaders = new MultivaluedHashMap<String, Object>();
217 for (Map.Entry<String, List<Object>> entry : request.getHeaders().entrySet()) {
218 if (HttpHeaders.AUTHORIZATION.equals(entry.getKey())) {
221 newHeaders.put(entry.getKey(), entry.getValue());
224 newHeaders.add(HttpHeaders.AUTHORIZATION, newAuthorizationHeader);
225 builder.headers(newHeaders);
227 builder.property(REQUEST_PROPERTY_FILTER_REUSED, "true");
229 Invocation invocation;
230 if (request.getEntity() == null) {
231 invocation = builder.build(method);
233 invocation = builder.build(method, Entity.entity(request.getEntity(), request.getMediaType()));
235 Response nextResponse = invocation.invoke();
237 if (nextResponse.hasEntity()) {
238 response.setEntityStream(nextResponse.readEntity(InputStream.class));
240 MultivaluedMap<String, String> headers = response.getHeaders();
242 headers.putAll(nextResponse.getStringHeaders());
243 response.setStatus(nextResponse.getStatus());
245 return response.getStatus() != Response.Status.UNAUTHORIZED.getStatusCode();
249 * Credentials (username + password).
251 static class Credentials {
253 private final String username;
254 private final byte[] password;
257 * Create a new credentials from username and password as byte array.
259 * @param username Username.
260 * @param password Password as byte array.
262 Credentials(String username, byte[] password) {
263 this.username = username;
264 this.password = password;
268 * Create a new credentials from username and password as {@link String}.
270 * @param username Username.
271 * @param password {@code String} password.
273 Credentials(String username, String password) {
274 this.username = username;
275 this.password = password == null ? null : password.getBytes(CHARACTER_SET);
283 String getUsername() {
288 * Return password as byte array.
290 * @return Password string in byte array representation.
292 byte[] getPassword() {
297 private static Credentials extractCredentials(ClientRequestContext request, Type type) {
298 String usernameKey = null;
299 String passwordKey = null;
301 usernameKey = HttpAuthenticationFeature.HTTP_AUTHENTICATION_USERNAME;
302 passwordKey = HttpAuthenticationFeature.HTTP_AUTHENTICATION_PASSWORD;
303 } else if (type == Type.BASIC) {
304 usernameKey = HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_USERNAME;
305 passwordKey = HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_PASSWORD;
308 String userName = (String) request.getProperty(usernameKey);
309 if (userName != null && !userName.equals("")) {
311 Object password = request.getProperty(passwordKey);
312 if (password instanceof byte[]) {
313 pwdBytes = ((byte[]) password);
314 } else if (password instanceof String) {
315 pwdBytes = ((String) password).getBytes(CHARACTER_SET);
317 throw new RequestAuthenticationException("Passwort invalid.");
319 return new Credentials(userName, pwdBytes);
325 * Get credentials actual for the current request. Priorities in credentials selection are the following:
327 * <li>Basic/digest specific credentials defined in the request properties</li>
328 * <li>Common credentials defined in the request properties</li>
329 * <li>{@code defaultCredentials}</li>
332 * @param request Request from which credentials should be extracted.
333 * @param defaultCredentials Default credentials (can be {@code null}).
334 * @param type Type of requested credentials.
335 * @return Credentials or {@code null} if no credentials are found and {@code defaultCredentials} are {@code null}.
336 * @throws RequestAuthenticationException in case the {@code username} or {@code password} is invalid.
338 static Credentials getCredentials(ClientRequestContext request, Credentials defaultCredentials, Type type) {
339 Credentials commonCredentials = extractCredentials(request, type);
341 if (commonCredentials != null) {
342 return commonCredentials;
344 Credentials specificCredentials = extractCredentials(request, null);
346 return specificCredentials != null ? specificCredentials : defaultCredentials;