001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.security.auth.http;
016    
017    import com.liferay.portal.kernel.util.Base64;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.util.LinkedHashMap;
023    import java.util.Map;
024    
025    /**
026     * @author Tomas Polesovsky
027     */
028    public class HttpAuthorizationHeader {
029    
030            public static final String AUTH_PARAMETER_NAME_NONCE = "nonce";
031    
032            public static final String AUTH_PARAMETER_NAME_PASSWORD = "password";
033    
034            public static final String AUTH_PARAMETER_NAME_REALM = "realm";
035    
036            public static final String AUTH_PARAMETER_NAME_RESPONSE = "response";
037    
038            public static final String AUTH_PARAMETER_NAME_URI = "uri";
039    
040            public static final String AUTH_PARAMETER_NAME_USERNAME = "username";
041    
042            public static final String SCHEME_BASIC = "Basic";
043    
044            public static final String SCHEME_DIGEST = "Digest";
045    
046            public HttpAuthorizationHeader(String scheme) {
047                    _scheme = scheme;
048            }
049    
050            public String getAuthParameter(String name) {
051                    return _authParameters.get(name);
052            }
053    
054            public Map<String, String> getAuthParameters() {
055                    return _authParameters;
056            }
057    
058            public String getScheme() {
059                    return _scheme;
060            }
061    
062            public void setAuthParameter(String name, String value) {
063                    _authParameters.put(name, value);
064            }
065    
066            public void setScheme(String scheme) {
067                    _scheme = scheme;
068            }
069    
070            @Override
071            public String toString() {
072                    if (_scheme.equals(SCHEME_BASIC) &&
073                            !Validator.isBlank(
074                                    getAuthParameter(AUTH_PARAMETER_NAME_USERNAME))) {
075    
076                            String userName = getAuthParameter(AUTH_PARAMETER_NAME_USERNAME);
077                            String password = getAuthParameter(AUTH_PARAMETER_NAME_PASSWORD);
078    
079                            String userNameAndPassword = userName + StringPool.COLON + password;
080    
081                            String encodedUserNameAndPassword = Base64.encode(
082                                    userNameAndPassword.getBytes());
083    
084                            return SCHEME_BASIC + StringPool.SPACE + encodedUserNameAndPassword;
085                    }
086    
087                    StringBundler sb = new StringBundler(_authParameters.size() * 6 + 2);
088    
089                    sb.append(_scheme);
090                    sb.append(StringPool.SPACE);
091    
092                    for (Map.Entry<String, String> entry : _authParameters.entrySet()) {
093                            sb.append(entry.getKey());
094                            sb.append(StringPool.EQUAL);
095                            sb.append(StringPool.QUOTE);
096                            sb.append(entry.getValue());
097                            sb.append(StringPool.QUOTE);
098                            sb.append(StringPool.COMMA_AND_SPACE);
099                    }
100    
101                    sb.setIndex(sb.index() - 1);
102    
103                    return sb.toString();
104            }
105    
106            private final Map<String, String> _authParameters = new LinkedHashMap<>();
107            private String _scheme;
108    
109    }