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