2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.common.http.config;
23 import fj.data.Either;
24 import org.apache.commons.lang3.StringUtils;
25 import org.onap.sdc.security.SecurityUtil;
27 public class BasicAuthorization {
28 private String userName;
29 private String password;
31 public BasicAuthorization() {
34 public BasicAuthorization(BasicAuthorization basicAuthorization) {
35 setUserName(basicAuthorization.userName);
36 setPassword(basicAuthorization.password, false);
39 public void setUserName(String userName) {
41 this.userName = userName;
44 public void setPassword(String password) {
45 setPassword(password, true);
48 public String getUserName() {
52 public String getPassword() {
57 private void setPassword(String password, boolean isEncoded) {
60 Either<String, String> passkey = SecurityUtil.INSTANCE.decrypt(password);
61 if(passkey.isLeft()) {
62 this.password = passkey.left().value();
65 throw new IllegalArgumentException(passkey.right().value());
69 this.password = password;
74 public int hashCode() {
77 result = prime * result + ((password == null) ? 0 : password.hashCode());
78 result = prime * result + ((userName == null) ? 0 : userName.hashCode());
83 public boolean equals(Object obj) {
88 if (getClass() != obj.getClass())
90 BasicAuthorization other = (BasicAuthorization) obj;
91 if (password == null) {
92 if (other.password != null)
95 else if (!password.equals(other.password))
97 if (userName == null) {
98 if (other.userName != null)
101 else if (!userName.equals(other.userName))
107 public String toString() {
108 StringBuilder builder = new StringBuilder();
109 builder.append("BasicAuthentication [userName=");
110 builder.append(userName);
112 return builder.toString();
115 private void validate(String str) {
116 if(StringUtils.isEmpty(str)) {
117 throw new IllegalArgumentException("BasicAuthorization username and/or password cannot be empty");