Update SDC with new 'security-util-lib' version
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / http / config / BasicAuthorization.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.common.http.config;
21
22 import fj.data.Either;
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.sdc.security.SecurityUtil;
25
26 public class BasicAuthorization {
27
28     private String userName;
29     private String password;
30
31     public BasicAuthorization() {
32     }
33
34     public BasicAuthorization(BasicAuthorization basicAuthorization) {
35         setUserName(basicAuthorization.userName);
36         setPassword(basicAuthorization.password, false);
37     }
38
39     public String getUserName() {
40         return userName;
41     }
42
43     public void setUserName(String userName) {
44         validate(userName);
45         this.userName = userName;
46     }
47
48     public String getPassword() {
49         return password;
50     }
51
52     public void setPassword(String password) {
53         setPassword(password, true);
54     }
55
56     private void setPassword(String password, boolean isEncoded) {
57         validate(password);
58         if (isEncoded) {
59             Either<String, String> passkey = SecurityUtil.decrypt(password);
60             if (passkey.isLeft()) {
61                 this.password = passkey.left().value();
62             } else {
63                 throw new IllegalArgumentException(passkey.right().value());
64             }
65         } else {
66             this.password = password;
67         }
68     }
69
70     @Override
71     public int hashCode() {
72         final int prime = 31;
73         int result = 1;
74         result = prime * result + ((password == null) ? 0 : password.hashCode());
75         result = prime * result + ((userName == null) ? 0 : userName.hashCode());
76         return result;
77     }
78
79     @Override
80     public boolean equals(Object obj) {
81         if (this == obj) {
82             return true;
83         }
84         if (obj == null) {
85             return false;
86         }
87         if (getClass() != obj.getClass()) {
88             return false;
89         }
90         BasicAuthorization other = (BasicAuthorization) obj;
91         if (password == null) {
92             if (other.password != null) {
93                 return false;
94             }
95         } else if (!password.equals(other.password)) {
96             return false;
97         }
98         if (userName == null) {
99             if (other.userName != null) {
100                 return false;
101             }
102         } else if (!userName.equals(other.userName)) {
103             return false;
104         }
105         return true;
106     }
107
108     @Override
109     public String toString() {
110         StringBuilder builder = new StringBuilder();
111         builder.append("BasicAuthentication [userName=");
112         builder.append(userName);
113         builder.append("]");
114         return builder.toString();
115     }
116
117     private void validate(String str) {
118         if (StringUtils.isEmpty(str)) {
119             throw new IllegalArgumentException("BasicAuthorization username and/or password cannot be empty");
120         }
121     }
122 }