aboutsummaryrefslogtreecommitdiffstats
path: root/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/BasicAuthorization.java
blob: 57750be0f1204b1dc0b3bda5286531b820fcf3e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package org.openecomp.sdc.common.http.config;

import fj.data.Either;
import org.apache.commons.lang3.StringUtils;
import org.openecomp.sdc.security.SecurityUtil;

public class BasicAuthorization {
    private String userName;
    private String password;

    public BasicAuthorization() {
    }

    public BasicAuthorization(BasicAuthorization basicAuthorization) {
        setUserName(basicAuthorization.userName);
        setPassword(basicAuthorization.password, false);
    }

    public void setUserName(String userName) {
        validate(userName);
        this.userName = userName;
    }

    public void setPassword(String password) {
        setPassword(password, true);
    }

    public String getUserName() {
        return userName;
    }

    public String getPassword() {
        return password;
    }

    
    private void setPassword(String password, boolean isEncoded) {
        validate(password);
        if(isEncoded) {
            Either<String, String> passkey = SecurityUtil.INSTANCE.decrypt(password);
            if(passkey.isLeft()) {
                this.password = passkey.left().value();
            }
            else {
                throw new IllegalArgumentException(passkey.right().value());
            }
        }
        else {
            this.password = password;
        }
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((password == null) ? 0 : password.hashCode());
        result = prime * result + ((userName == null) ? 0 : userName.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        BasicAuthorization other = (BasicAuthorization) obj;
        if (password == null) {
            if (other.password != null)
                return false;
        }
        else if (!password.equals(other.password))
            return false;
        if (userName == null) {
            if (other.userName != null)
                return false;
        }
        else if (!userName.equals(other.userName))
            return false;
        return true;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("BasicAuthentication [userName=");
        builder.append(userName);
        builder.append("]");
        return builder.toString();
    }

    private void validate(String str) {
        if(StringUtils.isEmpty(str)) {
            throw new IllegalArgumentException("BasicAuthorization username and/or password cannot be empty");
        }
    }
}