Line data Source code
1 : // Copyright (C) 2012 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.auth; 16 : 17 : import static java.util.Objects.requireNonNull; 18 : 19 : import com.google.auto.value.AutoValue; 20 : import com.google.gerrit.common.Nullable; 21 : 22 : /** An authenticated user as specified by the AuthBackend. */ 23 : public class AuthUser { 24 : 25 : /** Globally unique identifier for the user. */ 26 : @AutoValue 27 0 : public abstract static class UUID { 28 : /** 29 : * A new unique identifier. 30 : * 31 : * @param uuid the unique identifier. 32 : * @return identifier instance. 33 : */ 34 : public static UUID create(String uuid) { 35 0 : return new AutoValue_AuthUser_UUID(uuid); 36 : } 37 : 38 : public abstract String uuid(); 39 : } 40 : 41 : private final UUID uuid; 42 : private final String username; 43 : 44 : /** 45 : * An authenticated user. 46 : * 47 : * @param uuid the globally unique ID. 48 : * @param username the name of the authenticated user. 49 : */ 50 0 : public AuthUser(UUID uuid, @Nullable String username) { 51 0 : this.uuid = requireNonNull(uuid); 52 0 : this.username = username; 53 0 : } 54 : 55 : /** Returns the globally unique identifier. */ 56 : public final UUID getUUID() { 57 0 : return uuid; 58 : } 59 : 60 : /** Returns the backend specific user name, or null if one does not exist. */ 61 : @Nullable 62 : public final String getUsername() { 63 0 : return username; 64 : } 65 : 66 : /** Returns {@code true} if {@link #getUsername()} is not null. */ 67 : public final boolean hasUsername() { 68 0 : return getUsername() != null; 69 : } 70 : 71 : @Override 72 : public boolean equals(Object obj) { 73 0 : if (obj instanceof AuthUser) { 74 0 : return getUUID().equals(((AuthUser) obj).getUUID()); 75 : } 76 0 : return false; 77 : } 78 : 79 : @Override 80 : public int hashCode() { 81 0 : return getUUID().hashCode(); 82 : } 83 : 84 : @Override 85 : public String toString() { 86 0 : return String.format("AuthUser[uuid=%s, username=%s]", getUUID(), getUsername()); 87 : } 88 : }