re base code
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / http / config / BasicAuthorization.java
1 package org.openecomp.sdc.common.http.config;
2
3 import fj.data.Either;
4 import org.apache.commons.lang3.StringUtils;
5 import org.openecomp.sdc.security.SecurityUtil;
6
7 public class BasicAuthorization {
8     private String userName;
9     private String password;
10
11     public BasicAuthorization() {
12     }
13
14     public BasicAuthorization(BasicAuthorization basicAuthorization) {
15         setUserName(basicAuthorization.userName);
16         setPassword(basicAuthorization.password, false);
17     }
18
19     public void setUserName(String userName) {
20         validate(userName);
21         this.userName = userName;
22     }
23
24     public void setPassword(String password) {
25         setPassword(password, true);
26     }
27
28     public String getUserName() {
29         return userName;
30     }
31
32     public String getPassword() {
33         return password;
34     }
35
36     
37     private void setPassword(String password, boolean isEncoded) {
38         validate(password);
39         if(isEncoded) {
40             Either<String, String> passkey = SecurityUtil.INSTANCE.decrypt(password);
41             if(passkey.isLeft()) {
42                 this.password = passkey.left().value();
43             }
44             else {
45                 throw new IllegalArgumentException(passkey.right().value());
46             }
47         }
48         else {
49             this.password = password;
50         }
51     }
52
53     @Override
54     public int hashCode() {
55         final int prime = 31;
56         int result = 1;
57         result = prime * result + ((password == null) ? 0 : password.hashCode());
58         result = prime * result + ((userName == null) ? 0 : userName.hashCode());
59         return result;
60     }
61
62     @Override
63     public boolean equals(Object obj) {
64         if (this == obj)
65             return true;
66         if (obj == null)
67             return false;
68         if (getClass() != obj.getClass())
69             return false;
70         BasicAuthorization other = (BasicAuthorization) obj;
71         if (password == null) {
72             if (other.password != null)
73                 return false;
74         }
75         else if (!password.equals(other.password))
76             return false;
77         if (userName == null) {
78             if (other.userName != null)
79                 return false;
80         }
81         else if (!userName.equals(other.userName))
82             return false;
83         return true;
84     }
85
86     @Override
87     public String toString() {
88         StringBuilder builder = new StringBuilder();
89         builder.append("BasicAuthentication [userName=");
90         builder.append(userName);
91         builder.append("]");
92         return builder.toString();
93     }
94
95     private void validate(String str) {
96         if(StringUtils.isEmpty(str)) {
97             throw new IllegalArgumentException("BasicAuthorization username and/or password cannot be empty");
98         }
99     }
100 }