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=========================================================
20 package org.openecomp.sdc.common.http.config;
22 import fj.data.Either;
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.sdc.security.SecurityUtil;
26 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 String getUserName() {
43 public void setUserName(String userName) {
45 this.userName = userName;
48 public String getPassword() {
52 public void setPassword(String password) {
53 setPassword(password, true);
56 private void setPassword(String password, boolean isEncoded) {
59 Either<String, String> passkey = SecurityUtil.decrypt(password);
60 if (passkey.isLeft()) {
61 this.password = passkey.left().value();
63 throw new IllegalArgumentException(passkey.right().value());
66 this.password = password;
71 public int hashCode() {
74 result = prime * result + ((password == null) ? 0 : password.hashCode());
75 result = prime * result + ((userName == null) ? 0 : userName.hashCode());
80 public boolean equals(Object obj) {
87 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)) {
98 if (userName == null) {
99 if (other.userName != null) {
102 } else if (!userName.equals(other.userName)) {
109 public String toString() {
110 StringBuilder builder = new StringBuilder();
111 builder.append("BasicAuthentication [userName=");
112 builder.append(userName);
114 return builder.toString();
117 private void validate(String str) {
118 if (StringUtils.isEmpty(str)) {
119 throw new IllegalArgumentException("BasicAuthorization username and/or password cannot be empty");