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(Claim claim) {
33 public AuthenticationBuilder setExpiration(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(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(Object object) {
95 if (!(object instanceof Authentication)) {
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());
108 public int hashCode() {
110 hashCode = Objects.hash(expiration, claim);
116 public String toString() {
117 return "expiration:" + expiration + "," + claim.toString();