-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Start of impl for AuthProviderRegistry * Add authMode param to AuthProvider and AuthProviderRegistry * Create test
- Loading branch information
Showing
6 changed files
with
199 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
.../main/java/org/dataportabilityproject/gateway/PortabilityAuthServiceProviderRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2018 The Data-Portability Project Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.dataportabilityproject.gateway; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.dataportabilityproject.spi.gateway.auth.AuthDataGenerator; | ||
import org.dataportabilityproject.spi.gateway.auth.AuthServiceProvider; | ||
import org.dataportabilityproject.spi.gateway.auth.AuthServiceProviderRegistry; | ||
import org.dataportabilityproject.types.transfer.PortableType; | ||
|
||
public class PortabilityAuthServiceProviderRegistry implements AuthServiceProviderRegistry { | ||
|
||
private final ImmutableMap<String, AuthServiceProvider> authServiceProviderMap; | ||
private final ImmutableSet<String> supportedImportTypes; | ||
private final ImmutableSet<String> supportedExportTypes; | ||
|
||
// The parameters to the constructor are provided via dependency injection | ||
public PortabilityAuthServiceProviderRegistry(List<String> enabledServices, | ||
Map<String, AuthServiceProvider> serviceProviderMap){ | ||
ImmutableMap.Builder<String, AuthServiceProvider> serviceProviderBuilder = ImmutableMap | ||
.builder(); | ||
ImmutableSet.Builder<String> supportedImportTypesBuilder = ImmutableSet.builder(); | ||
ImmutableSet.Builder<String> supportedExportTypesBuilder = ImmutableSet.builder(); | ||
|
||
for(String service : enabledServices){ | ||
AuthServiceProvider authServiceProvider = serviceProviderMap.get(service); | ||
Preconditions.checkArgument(authServiceProvider != null, "AuthServiceProvider not found for [%s]", service); | ||
|
||
List<String> importTypes = authServiceProvider.getImportTypes(); | ||
List<String> exportTypes = authServiceProvider.getExportTypes(); | ||
|
||
for (String type : importTypes) { | ||
Preconditions.checkArgument(exportTypes.contains(type), | ||
"TransferDataType [%s] is available for import but not export in [%s] AuthServiceProvider", | ||
type, service); | ||
supportedImportTypesBuilder.add(type); | ||
} | ||
|
||
supportedExportTypesBuilder.addAll(exportTypes); | ||
serviceProviderBuilder.put(service, authServiceProvider); | ||
} | ||
|
||
authServiceProviderMap = serviceProviderBuilder.build(); | ||
supportedImportTypes = supportedImportTypesBuilder.build(); | ||
supportedExportTypes = supportedExportTypesBuilder.build(); | ||
} | ||
|
||
@Override | ||
public AuthDataGenerator getAuthDataGenerator(String serviceId, String transferDataType, AuthMode mode) { | ||
AuthServiceProvider provider = authServiceProviderMap.get(serviceId); | ||
Preconditions.checkArgument(provider!=null, "AuthServiceProvider not found for serviceId [%s]", serviceId); | ||
switch(mode) { | ||
case EXPORT: | ||
Preconditions.checkArgument(supportedExportTypes.contains(transferDataType), "AuthMode [%s] not valid for TransferDataType [%s]", mode, transferDataType); | ||
break; | ||
case IMPORT: | ||
Preconditions.checkArgument(supportedImportTypes.contains(transferDataType), "AuthMode [%s] not valid for TransferDataType [%s]", mode, transferDataType); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("AuthMode [" + mode + "] not supported"); | ||
} | ||
|
||
return provider.getAuthDataGenerator(transferDataType, mode); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...g/dataportabilityproject/spi/gateway/auth/PortabilityAuthServiceProviderRegistryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2018 The Data-Portability Project Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.dataportabilityproject.spi.gateway.auth; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import java.util.List; | ||
import org.dataportabilityproject.gateway.PortabilityAuthServiceProviderRegistry; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
public class PortabilityAuthServiceProviderRegistryTest { | ||
|
||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
@Test | ||
public void requireImportAndExportTest(){ | ||
List<String> supportedImportTypes = ImmutableList.of("photos", "contacts"); | ||
List<String> supportedExportTypes = ImmutableList.of("contacts"); | ||
|
||
AuthServiceProvider mockAuthProvider = mock(AuthServiceProvider.class); | ||
when(mockAuthProvider.getExportTypes()).thenReturn(supportedExportTypes); | ||
when(mockAuthProvider.getImportTypes()).thenReturn(supportedImportTypes); | ||
when(mockAuthProvider.getServiceId()).thenReturn("mockAuthProvider"); | ||
thrown.expect(IllegalArgumentException.class); | ||
thrown.expectMessage("available for import but not export"); | ||
|
||
AuthServiceProviderRegistry registry = new PortabilityAuthServiceProviderRegistry(ImmutableList.of("mockServiceProvider"), | ||
ImmutableMap.of("mockServiceProvider", mockAuthProvider)); | ||
} | ||
|
||
@Test | ||
public void serviceProviderNotFoundTest(){ | ||
AuthServiceProvider mockAuthProvider = mock(AuthServiceProvider.class); | ||
when(mockAuthProvider.getServiceId()).thenReturn("mockAuthProvider"); | ||
|
||
thrown.expect(IllegalArgumentException.class); | ||
thrown.expectMessage("AuthServiceProvider not found"); | ||
|
||
AuthServiceProviderRegistry registry = new PortabilityAuthServiceProviderRegistry(ImmutableList.of("ServiceDoesNotExist"), | ||
ImmutableMap.of("mockServiceProvider", mockAuthProvider)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters