2 * Copyright (c) 2014, 2017 Hewlett-Packard Development Company, L.P. and others. All rights reserved.
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
8 package org.opendaylight.aaa.tokenauthrealm.auth;
10 import java.io.Serializable;
11 import java.util.Objects;
13 import org.opendaylight.aaa.api.Authentication;
14 import org.opendaylight.aaa.api.Claim;
17 * A builder for the authentication context.
20 * The expiration DEFAULTS to 0.
24 public class AuthenticationBuilder {
26 private long expiration = 0L;
27 private final Claim claim;
29 public AuthenticationBuilder(final Claim claim) {
33 public AuthenticationBuilder setExpiration(final long expiration) {
34 this.expiration = expiration;
38 public Authentication build() {
39 return new ImmutableAuthentication(this);
42 private static final class ImmutableAuthentication implements Authentication, Serializable {
43 private static final long serialVersionUID = 4919078164955609987L;
44 private int hashCode = 0;
48 private ImmutableAuthentication(final AuthenticationBuilder base) {
49 if (base.claim == null) {
50 throw new IllegalStateException("The Claim is null.");
52 claim = new ClaimBuilder(base.claim).build();
53 expiration = base.expiration;
55 if (base.expiration < 0) {
56 throw new IllegalStateException("The expiration is less than 0.");
61 public long expiration() {
66 public String clientId() {
67 return claim.clientId();
71 public String userId() {
72 return claim.userId();
76 public String user() {
81 public String domain() {
82 return claim.domain();
86 public Set<String> roles() {
91 public boolean equals(final Object obj) {
92 return this == obj || obj instanceof Authentication other
93 && expiration == other.expiration()
94 && Objects.equals(claim.roles(), other.roles())
95 && Objects.equals(claim.domain(), other.domain())
96 && Objects.equals(claim.userId(), other.userId())
97 && Objects.equals(claim.user(), other.user())
98 && Objects.equals(claim.clientId(), other.clientId());
102 public int hashCode() {
104 hashCode = Objects.hash(expiration, claim);
110 public String toString() {
111 return "expiration:" + expiration + "," + claim.toString();