001/* 002 * SonarQube 003 * Copyright (C) 2009-2016 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020package org.sonar.api.server.authentication; 021 022import javax.annotation.CheckForNull; 023import javax.annotation.Nullable; 024import javax.annotation.concurrent.Immutable; 025 026import static com.google.common.base.Preconditions.checkArgument; 027import static org.apache.commons.lang.StringUtils.isNotBlank; 028 029/** 030 * User information provided by the Identity Provider to be register into the platform. 031 * 032 * @since 5.4 033 */ 034@Immutable 035public final class UserIdentity { 036 037 private final String providerLogin; 038 private final String login; 039 private final String name; 040 private final String email; 041 042 private UserIdentity(Builder builder) { 043 this.providerLogin = builder.providerLogin; 044 this.login = builder.login; 045 this.name = builder.name; 046 this.email = builder.email; 047 } 048 049 /** 050 * Non-blank user login for the related {@link IdentityProvider}. 051 */ 052 public String getProviderLogin() { 053 return providerLogin; 054 } 055 056 /** 057 * Non-blank user login, unique for the SonarQube platform. 058 * If two {@link IdentityProvider} define two users with the same login, then users are considered as identical. 059 */ 060 public String getLogin() { 061 return login; 062 } 063 064 /** 065 * Non-blank display name. Uniqueness is not mandatory, even it's recommended for easier search of users 066 * in webapp. 067 */ 068 public String getName() { 069 return name; 070 } 071 072 /** 073 * Optional non-blank email. If defined, then it must be unique among all the users defined by all 074 * {@link IdentityProvider}. If not unique, then authentication will fail. 075 */ 076 @CheckForNull 077 public String getEmail() { 078 return email; 079 } 080 081 public static Builder builder() { 082 return new Builder(); 083 } 084 085 public static class Builder { 086 private String providerLogin; 087 private String login; 088 private String name; 089 private String email; 090 091 private Builder() { 092 } 093 094 /** 095 * @see UserIdentity#getProviderLogin() 096 */ 097 public Builder setProviderLogin(String providerLogin) { 098 this.providerLogin = providerLogin; 099 return this; 100 } 101 102 /** 103 * @see UserIdentity#getLogin() () 104 */ 105 public Builder setLogin(String login) { 106 this.login = login; 107 return this; 108 } 109 110 /** 111 * @see UserIdentity#getName() 112 */ 113 public Builder setName(String name) { 114 this.name = name; 115 return this; 116 } 117 118 /** 119 * @see UserIdentity#getEmail() 120 */ 121 public Builder setEmail(@Nullable String email) { 122 this.email = email; 123 return this; 124 } 125 126 public UserIdentity build() { 127 validateProviderLogin(providerLogin); 128 validateLogin(login); 129 validateName(name); 130 validateEmail(email); 131 return new UserIdentity(this); 132 } 133 134 private static void validateProviderLogin(String providerLogin){ 135 checkArgument(isNotBlank(providerLogin), "Provider login must not be blank"); 136 checkArgument(providerLogin.length() <= 255, "Provider login size is incorrect (maximum 255 characters)"); 137 } 138 139 private static void validateLogin(String login){ 140 checkArgument(isNotBlank(login), "User login must not be blank"); 141 checkArgument(login.length() <= 255 && login.length() >= 3, "User login size is incorrect (Between 3 and 255 characters)"); 142 } 143 144 private static void validateName(String name){ 145 checkArgument(isNotBlank(name), "User name must not be blank"); 146 checkArgument(name.length() <= 200, "User name size is too big (200 characters max)"); 147 } 148 149 private static void validateEmail(@Nullable String email){ 150 checkArgument(email == null || email.length() <= 100, "User email size is too big (100 characters max)"); 151 } 152 } 153}