aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/api/AthenzPrincipal.java
blob: 08d289e216b8e65f589d97eb81b0e1e50a2e6b74 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.athenz.api;

import java.security.Principal;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static java.util.Collections.emptyList;

/**
 * @author bjorncs
 */
public class AthenzPrincipal implements Principal {

    private final AthenzIdentity athenzIdentity;
    private final NToken nToken;
    private final List<AthenzRole> roles;

    public AthenzPrincipal(AthenzIdentity athenzIdentity) {
        this(athenzIdentity, null, emptyList());
    }

    public AthenzPrincipal(AthenzIdentity athenzIdentity, NToken nToken) {
        this(athenzIdentity, nToken, emptyList());
    }

    public AthenzPrincipal(AthenzIdentity identity, List<AthenzRole> roles) {
        this(identity, null, roles);
    }

    private AthenzPrincipal(AthenzIdentity athenzIdentity, NToken nToken, List<AthenzRole> roles) {
        this.athenzIdentity = athenzIdentity;
        this.nToken = nToken;
        this.roles = roles;
    }

    public AthenzIdentity getIdentity() {
        return athenzIdentity;
    }

    @Override
    public String getName() {
        return athenzIdentity.getFullName();
    }

    public AthenzDomain getDomain() {
        return athenzIdentity.getDomain();
    }

    public Optional<NToken> getNToken() {
        return Optional.ofNullable(nToken);
    }

    public List<AthenzRole> getRoles() {
        return roles;
    }

    @Override
    public String toString() {
        return "AthenzPrincipal{" +
                "athenzIdentity=" + athenzIdentity +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AthenzPrincipal principal = (AthenzPrincipal) o;
        return Objects.equals(athenzIdentity, principal.athenzIdentity);
    }

    @Override
    public int hashCode() {
        return Objects.hash(athenzIdentity);
    }
}