Line data Source code
1 : // Copyright (C) 2021 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.externalids; 16 : 17 : import com.google.common.annotations.VisibleForTesting; 18 : import com.google.gerrit.common.Nullable; 19 : import com.google.gerrit.server.config.AuthConfig; 20 : import com.google.inject.ImplementedBy; 21 : import javax.inject.Inject; 22 : import javax.inject.Singleton; 23 : 24 : @Singleton 25 : public class ExternalIdKeyFactory { 26 : @ImplementedBy(ConfigImpl.class) 27 : public interface Config { 28 : boolean isUserNameCaseInsensitive(); 29 : } 30 : 31 : /** 32 : * Default implementation {@link Config} 33 : * 34 : * <p>Internally in google we are using different implementation. 35 : */ 36 : @Singleton 37 : public static class ConfigImpl implements Config { 38 : private final boolean isUserNameCaseInsensitive; 39 : 40 : @VisibleForTesting 41 : @Inject 42 153 : public ConfigImpl(AuthConfig authConfig) { 43 153 : this.isUserNameCaseInsensitive = authConfig.isUserNameCaseInsensitive(); 44 153 : } 45 : 46 : @Override 47 : public boolean isUserNameCaseInsensitive() { 48 153 : return isUserNameCaseInsensitive; 49 : } 50 : } 51 : 52 : private final boolean isUserNameCaseInsensitive; 53 : 54 : @Inject 55 153 : public ExternalIdKeyFactory(Config config) { 56 153 : this.isUserNameCaseInsensitive = config.isUserNameCaseInsensitive(); 57 153 : } 58 : 59 : /** 60 : * Creates an external ID key. 61 : * 62 : * @param scheme the scheme name, must not contain colons (':'). E.g. {@link 63 : * ExternalId#SCHEME_USERNAME}. 64 : * @param id the external ID, must not contain colons (':') 65 : * @return the created external ID key 66 : */ 67 : public ExternalId.Key create(@Nullable String scheme, String id) { 68 153 : return create(scheme, id, isUserNameCaseInsensitive); 69 : } 70 : 71 : /** 72 : * Creates an external ID key. 73 : * 74 : * @param scheme the scheme name, must not contain colons (':'). E.g. {@link 75 : * ExternalId#SCHEME_USERNAME}. 76 : * @param id the external ID, must not contain colons (':') 77 : * @param userNameCaseInsensitive whether the external ID key is matched case insensitively 78 : * @return the created external ID key 79 : */ 80 : public ExternalId.Key create( 81 : @Nullable String scheme, String id, boolean userNameCaseInsensitive) { 82 153 : if (scheme != null 83 153 : && (scheme.equals(ExternalId.SCHEME_USERNAME) || scheme.equals(ExternalId.SCHEME_GERRIT))) { 84 153 : return ExternalId.Key.create(scheme, id, userNameCaseInsensitive); 85 : } 86 : 87 151 : return ExternalId.Key.create(scheme, id, false); 88 : } 89 : 90 : /** 91 : * Parses an external ID key from its String representation 92 : * 93 : * @param externalId String representation of external ID key (e.g. username:johndoe) 94 : * @return the external Id key object 95 : */ 96 : public ExternalId.Key parse(String externalId) { 97 151 : int c = externalId.indexOf(':'); 98 151 : if (c < 1 || c >= externalId.length() - 1) { 99 1 : return create(null, externalId); 100 : } 101 151 : return create(externalId.substring(0, c), externalId.substring(c + 1)); 102 : } 103 : }