2 * Copyright (c) 2014, 2017 Hewlett-Packard Development Company, L.P. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.aaa.shiro.tokenauthrealm.auth;
11 import java.util.Base64;
12 import java.util.List;
14 import org.opendaylight.aaa.api.Authentication;
15 import org.opendaylight.aaa.api.AuthenticationException;
16 import org.opendaylight.aaa.api.Claim;
17 import org.opendaylight.aaa.api.CredentialAuth;
18 import org.opendaylight.aaa.api.PasswordCredentials;
19 import org.opendaylight.aaa.api.TokenAuth;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * An HTTP Basic authenticator. Note that this is provided as a Hydrogen
25 * backward compatible authenticator, but usage of this authenticator or HTTP
26 * Basic Authentication is highly discouraged due to its vulnerability.
29 * To obtain a token using the HttpBasicAuth Strategy, add a header to your HTTP
30 * request in the form:
31 * <code>Authorization: Basic BASE_64_ENCODED_CREDENTIALS</code>
34 * Where <code>BASE_64_ENCODED_CREDENTIALS</code> is the base 64 encoded value
35 * of the user's credentials in the following form: <code>user:password</code>
38 * For example, assuming the user is "admin" and the password is "admin":
39 * <code>Authorization: Basic YWRtaW46YWRtaW4=</code>
43 public class HttpBasicAuth implements TokenAuth {
45 public static final String AUTH_HEADER = "Authorization";
47 public static final String AUTH_SEP = ":";
49 public static final String BASIC_PREFIX = "Basic ";
51 // TODO relocate this constant
52 public static final String DEFAULT_DOMAIN = "sdn";
55 * username and password.
57 private static final int NUM_HEADER_CREDS = 2;
60 * username, password and domain.
62 private static final int NUM_TOKEN_CREDS = 3;
64 private static final Logger LOG = LoggerFactory.getLogger(HttpBasicAuth.class);
66 private final CredentialAuth<PasswordCredentials> credentialAuth;
68 public HttpBasicAuth(CredentialAuth<PasswordCredentials> credentialAuth) {
69 this.credentialAuth = credentialAuth;
72 private static boolean checkAuthHeaderFormat(final String authHeader) {
73 return authHeader != null && authHeader.startsWith(BASIC_PREFIX);
76 private static String extractAuthHeader(final Map<String, List<String>> headers) {
77 return headers.get(AUTH_HEADER).get(0);
80 private static String[] extractCredentialArray(final String authHeader) {
81 return new String(Base64.getDecoder().decode(authHeader.substring(BASIC_PREFIX.length())))
85 private static boolean verifyCredentialArray(final String[] creds) {
86 return creds != null && creds.length == NUM_HEADER_CREDS;
89 private static String[] addDomainToCredentialArray(final String[] creds) {
90 String[] newCredentialArray = new String[NUM_TOKEN_CREDS];
91 System.arraycopy(creds, 0, newCredentialArray, 0, creds.length);
92 newCredentialArray[2] = DEFAULT_DOMAIN;
93 return newCredentialArray;
96 private static Authentication generateAuthentication(
97 CredentialAuth<PasswordCredentials> credentialAuth, final String[] creds)
98 throws ArrayIndexOutOfBoundsException {
99 final PasswordCredentials pc = new PasswordCredentialBuilder().setUserName(creds[0])
100 .setPassword(creds[1]).setDomain(creds[2]).build();
101 final Claim claim = credentialAuth.authenticate(pc);
102 return new AuthenticationBuilder(claim).build();
106 public Authentication validate(final Map<String, List<String>> headers)
107 throws AuthenticationException {
108 if (headers.containsKey(AUTH_HEADER)) {
109 final String authHeader = extractAuthHeader(headers);
110 if (checkAuthHeaderFormat(authHeader)) {
112 String[] creds = extractCredentialArray(authHeader);
113 // If no domain was supplied then use the default one, which is
115 if (verifyCredentialArray(creds)) {
116 creds = addDomainToCredentialArray(creds);
118 // Assumes correct formatting in form Base64("user:password").
119 // Throws an exception if an unknown format is used.
121 return generateAuthentication(this.credentialAuth, creds);
122 } catch (ArrayIndexOutOfBoundsException e) {
123 final String message = "Login Attempt in Bad Format."
124 + " Please provide user:password in Base64 format.";
126 throw new AuthenticationException(message);