Line data Source code
1 : // Copyright (C) 2009 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.account; 16 : 17 : import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_EXTERNAL; 18 : import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_GERRIT; 19 : import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_MAILTO; 20 : 21 : import com.google.common.base.Strings; 22 : import com.google.gerrit.common.Nullable; 23 : import com.google.gerrit.server.account.externalids.ExternalId; 24 : import com.google.gerrit.server.account.externalids.ExternalIdKeyFactory; 25 : import com.google.inject.Inject; 26 : import com.google.inject.Singleton; 27 : import java.util.Optional; 28 : 29 : /** 30 : * Information for {@link AccountManager#authenticate(AuthRequest)}. 31 : * 32 : * <p>Callers should populate this object with as much information as possible about the user 33 : * account. For example, OpenID authentication might return registration information including a 34 : * display name for the user, and an email address for them. These fields however are optional, as 35 : * not all OpenID providers return them, and not all non-OpenID systems can use them. 36 : */ 37 : public class AuthRequest { 38 : @Singleton 39 : public static class Factory { 40 : private final ExternalIdKeyFactory externalIdKeyFactory; 41 : 42 : @Inject 43 152 : public Factory(ExternalIdKeyFactory externalIdKeyFactory) { 44 152 : this.externalIdKeyFactory = externalIdKeyFactory; 45 152 : } 46 : 47 : public AuthRequest create(ExternalId.Key externalIdKey) { 48 0 : return new AuthRequest(externalIdKey, externalIdKeyFactory); 49 : } 50 : 51 : /** Create a request for a local username, such as from LDAP. */ 52 : public AuthRequest createForUser(String userName) { 53 16 : AuthRequest r = 54 : new AuthRequest( 55 16 : externalIdKeyFactory.create(SCHEME_GERRIT, userName), externalIdKeyFactory); 56 16 : r.setUserName(userName); 57 16 : return r; 58 : } 59 : 60 : /** Create a request for an external username. */ 61 : public AuthRequest createForExternalUser(String userName) { 62 1 : AuthRequest r = 63 : new AuthRequest( 64 1 : externalIdKeyFactory.create(SCHEME_EXTERNAL, userName), externalIdKeyFactory); 65 1 : r.setUserName(userName); 66 1 : return r; 67 : } 68 : 69 : /** 70 : * Create a request for an email address registration. 71 : * 72 : * <p>This type of request should be used only to attach a new email address to an existing user 73 : * account. 74 : */ 75 : public AuthRequest createForEmail(String email) { 76 14 : AuthRequest r = 77 14 : new AuthRequest(externalIdKeyFactory.create(SCHEME_MAILTO, email), externalIdKeyFactory); 78 14 : r.setEmailAddress(email); 79 14 : return r; 80 : } 81 : } 82 : 83 : private final ExternalIdKeyFactory externalIdKeyFactory; 84 : 85 : private ExternalId.Key externalId; 86 : private String password; 87 : private String displayName; 88 : private String emailAddress; 89 19 : private Optional<String> userName = Optional.empty(); 90 : private boolean skipAuthentication; 91 : private String authPlugin; 92 : private String authProvider; 93 : private boolean authProvidesAccountActiveStatus; 94 : private boolean active; 95 : 96 19 : private AuthRequest(ExternalId.Key externalId, ExternalIdKeyFactory externalIdKeyFactory) { 97 19 : this.externalId = externalId; 98 19 : this.externalIdKeyFactory = externalIdKeyFactory; 99 19 : } 100 : 101 : public ExternalId.Key getExternalIdKey() { 102 18 : return externalId; 103 : } 104 : 105 : @Nullable 106 : public String getLocalUser() { 107 15 : if (externalId.isScheme(SCHEME_GERRIT)) { 108 15 : return externalId.id(); 109 : } 110 1 : return null; 111 : } 112 : 113 : public void setLocalUser(String localUser) { 114 0 : if (externalId.isScheme(SCHEME_GERRIT)) { 115 0 : externalId = externalIdKeyFactory.create(SCHEME_GERRIT, localUser); 116 : } 117 0 : } 118 : 119 : public String getPassword() { 120 0 : return password; 121 : } 122 : 123 : public void setPassword(String pass) { 124 1 : password = pass; 125 1 : } 126 : 127 : public String getDisplayName() { 128 15 : return displayName; 129 : } 130 : 131 : public void setDisplayName(String name) { 132 5 : displayName = name != null && name.length() > 0 ? name : null; 133 5 : } 134 : 135 : public String getEmailAddress() { 136 18 : return emailAddress; 137 : } 138 : 139 : public void setEmailAddress(String email) { 140 14 : emailAddress = email != null && email.length() > 0 ? email : null; 141 14 : } 142 : 143 : public Optional<String> getUserName() { 144 15 : return userName; 145 : } 146 : 147 : public void setUserName(@Nullable String user) { 148 16 : userName = Optional.ofNullable(Strings.emptyToNull(user)); 149 16 : } 150 : 151 : public boolean isSkipAuthentication() { 152 0 : return skipAuthentication; 153 : } 154 : 155 : public void setSkipAuthentication(boolean skip) { 156 0 : skipAuthentication = skip; 157 0 : } 158 : 159 : public String getAuthPlugin() { 160 0 : return authPlugin; 161 : } 162 : 163 : public void setAuthPlugin(String authPlugin) { 164 0 : this.authPlugin = authPlugin; 165 0 : } 166 : 167 : public String getAuthProvider() { 168 0 : return authProvider; 169 : } 170 : 171 : public void setAuthProvider(String authProvider) { 172 0 : this.authProvider = authProvider; 173 0 : } 174 : 175 : public boolean authProvidesAccountActiveStatus() { 176 1 : return authProvidesAccountActiveStatus; 177 : } 178 : 179 : public void setAuthProvidesAccountActiveStatus(boolean authProvidesAccountActiveStatus) { 180 1 : this.authProvidesAccountActiveStatus = authProvidesAccountActiveStatus; 181 1 : } 182 : 183 : public boolean isActive() { 184 1 : return active; 185 : } 186 : 187 : public void setActive(Boolean isActive) { 188 1 : this.active = isActive; 189 1 : } 190 : }