Bump versions to 0.14.2-SNAPSHOT
[aaa.git] / aaa-tokenauthrealm / src / main / java / org / opendaylight / aaa / tokenauthrealm / auth / AuthenticationBuilder.java
1 /*
2  * Copyright (c) 2014, 2017 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.aaa.tokenauthrealm.auth;
9
10 import java.io.Serializable;
11 import java.util.Objects;
12 import java.util.Set;
13 import org.opendaylight.aaa.api.Authentication;
14 import org.opendaylight.aaa.api.Claim;
15
16 /**
17  * A builder for the authentication context.
18  *
19  * <p>
20  * The expiration DEFAULTS to 0.
21  *
22  * @author liemmn
23  */
24 public class AuthenticationBuilder {
25
26     private long expiration = 0L;
27     private final Claim claim;
28
29     public AuthenticationBuilder(Claim claim) {
30         this.claim = claim;
31     }
32
33     public AuthenticationBuilder setExpiration(long expiration) {
34         this.expiration = expiration;
35         return this;
36     }
37
38     public Authentication build() {
39         return new ImmutableAuthentication(this);
40     }
41
42     private static final class ImmutableAuthentication implements Authentication, Serializable {
43         private static final long serialVersionUID = 4919078164955609987L;
44         private int hashCode = 0;
45         long expiration = 0L;
46         Claim claim;
47
48         private ImmutableAuthentication(AuthenticationBuilder base) {
49             if (base.claim == null) {
50                 throw new IllegalStateException("The Claim is null.");
51             }
52             claim = new ClaimBuilder(base.claim).build();
53             expiration = base.expiration;
54
55             if (base.expiration < 0) {
56                 throw new IllegalStateException("The expiration is less than 0.");
57             }
58         }
59
60         @Override
61         public long expiration() {
62             return expiration;
63         }
64
65         @Override
66         public String clientId() {
67             return claim.clientId();
68         }
69
70         @Override
71         public String userId() {
72             return claim.userId();
73         }
74
75         @Override
76         public String user() {
77             return claim.user();
78         }
79
80         @Override
81         public String domain() {
82             return claim.domain();
83         }
84
85         @Override
86         public Set<String> roles() {
87             return claim.roles();
88         }
89
90         @Override
91         public boolean equals(Object object) {
92             if (this == object) {
93                 return true;
94             }
95             if (!(object instanceof Authentication)) {
96                 return false;
97             }
98             Authentication authentication = (Authentication) object;
99             return expiration == authentication.expiration()
100                     && Objects.equals(claim.roles(), authentication.roles())
101                     && Objects.equals(claim.domain(), authentication.domain())
102                     && Objects.equals(claim.userId(), authentication.userId())
103                     && Objects.equals(claim.user(), authentication.user())
104                     && Objects.equals(claim.clientId(), authentication.clientId());
105         }
106
107         @Override
108         public int hashCode() {
109             if (hashCode == 0) {
110                 hashCode = Objects.hash(expiration, claim);
111             }
112             return hashCode;
113         }
114
115         @Override
116         public String toString() {
117             return "expiration:" + expiration + "," + claim.toString();
118         }
119     }
120 }