Search in sources :

Example 1 with MultiAuthorizationTypeIterator

use of com.amplifyframework.core.model.auth.MultiAuthorizationTypeIterator in project amplify-android by aws-amplify.

the class MultiAuthModeStrategy method authTypesFor.

@Override
public AuthorizationTypeIterator authTypesFor(@NonNull ModelSchema modelSchema, @NonNull ModelOperation operation) {
    final List<AuthRule> applicableRules = new ArrayList<>();
    Consumer<List<AuthRule>> filterAuthRules = authRules -> {
        for (AuthRule rule : authRules) {
            if (rule.getOperationsOrDefault().contains(operation)) {
                applicableRules.add(rule);
            }
        }
    };
    filterAuthRules.accept(modelSchema.getAuthRules());
    for (ModelField field : modelSchema.getFields().values()) {
        filterAuthRules.accept(field.getAuthRules());
    }
    return new MultiAuthorizationTypeIterator(applicableRules);
}
Also used : Consumer(com.amplifyframework.core.Consumer) List(java.util.List) AuthorizationTypeIterator(com.amplifyframework.core.model.auth.AuthorizationTypeIterator) MultiAuthorizationTypeIterator(com.amplifyframework.core.model.auth.MultiAuthorizationTypeIterator) NonNull(androidx.annotation.NonNull) AuthRule(com.amplifyframework.core.model.AuthRule) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelOperation(com.amplifyframework.core.model.ModelOperation) ModelField(com.amplifyframework.core.model.ModelField) ArrayList(java.util.ArrayList) MultiAuthorizationTypeIterator(com.amplifyframework.core.model.auth.MultiAuthorizationTypeIterator) ModelField(com.amplifyframework.core.model.ModelField) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) AuthRule(com.amplifyframework.core.model.AuthRule)

Example 2 with MultiAuthorizationTypeIterator

use of com.amplifyframework.core.model.auth.MultiAuthorizationTypeIterator in project amplify-android by aws-amplify.

the class MultiAuthorizationTypeIteratorTest method testOwnerAndGroupRules.

/**
 * Verify that if there are mixed owner and group rules, we return the auth types in the correct order.
 * We're verifying that:
 * - both owner rules are processed first (userPools and oidc) with isOwner = true.
 * - both group rules are processed next (userPools and oidc) withg isOwner = false.
 *
 * isOwner is used in the code to determine whether a request may need the owner parameter added.
 */
@Test
public void testOwnerAndGroupRules() {
    Iterator<AuthorizationType> expectedAuthTypes = Arrays.asList(AuthorizationType.AMAZON_COGNITO_USER_POOLS, AuthorizationType.OPENID_CONNECT, AuthorizationType.AMAZON_COGNITO_USER_POOLS, AuthorizationType.OPENID_CONNECT).iterator();
    Iterator<Boolean> expectedIsOwnerFlags = Arrays.asList(true, true, false, false).iterator();
    List<AuthRule> authRules = Arrays.asList(buildOwnerRule(null, null, null, null), buildOwnerRule(AuthStrategy.Provider.USER_POOLS, "differentOwnerField", null, CREATE_DELETE_OPERATIONS), buildOwnerRule(AuthStrategy.Provider.OIDC, "differentOwner", "myClaim", null), buildGroupRule(null, null, null, null, null), buildGroupRule(AuthStrategy.Provider.OIDC, "myGroupField", "someClaim", Collections.singletonList("group1"), null));
    MultiAuthorizationTypeIterator actualAuthTypeIterator = new MultiAuthorizationTypeIterator(authRules);
    assertIteratorState(expectedAuthTypes, expectedIsOwnerFlags, actualAuthTypeIterator);
}
Also used : MultiAuthorizationTypeIterator(com.amplifyframework.core.model.auth.MultiAuthorizationTypeIterator) AuthorizationType(com.amplifyframework.api.aws.AuthorizationType) AuthRule(com.amplifyframework.core.model.AuthRule) Test(org.junit.Test)

Example 3 with MultiAuthorizationTypeIterator

use of com.amplifyframework.core.model.auth.MultiAuthorizationTypeIterator in project amplify-android by aws-amplify.

the class MultiAuthorizationTypeIteratorTest method testAllRules.

/**
 * Test a schema with auth rules for each strategy is returned in the expected order.
 */
@Test
public void testAllRules() {
    Iterator<AuthorizationType> expectedAuthTypes = Arrays.asList(AuthorizationType.AMAZON_COGNITO_USER_POOLS, AuthorizationType.OPENID_CONNECT, AuthorizationType.AWS_IAM, AuthorizationType.API_KEY).iterator();
    Iterator<Boolean> expectedIsOwnerFlags = Arrays.asList(true, false, false, false).iterator();
    List<AuthRule> authRules = Arrays.asList(buildGroupRule(AuthStrategy.Provider.OIDC, null, null, null, null), buildPrivateRule(AuthStrategy.Provider.IAM, null), buildOwnerRule(null, null, null, null), buildPublicRule(null, null));
    MultiAuthorizationTypeIterator actualAuthTypeIterator = new MultiAuthorizationTypeIterator(authRules);
    assertIteratorState(expectedAuthTypes, expectedIsOwnerFlags, actualAuthTypeIterator);
}
Also used : MultiAuthorizationTypeIterator(com.amplifyframework.core.model.auth.MultiAuthorizationTypeIterator) AuthorizationType(com.amplifyframework.api.aws.AuthorizationType) AuthRule(com.amplifyframework.core.model.AuthRule) Test(org.junit.Test)

Example 4 with MultiAuthorizationTypeIterator

use of com.amplifyframework.core.model.auth.MultiAuthorizationTypeIterator in project amplify-android by aws-amplify.

the class MultiAuthorizationTypeIteratorTest method testMultiOwnerRules.

/**
 * If there are multiple owner based rules (a couple using userPools and one using oidc),
 * it should only return 2 auth types (one for userPools and one for oidc).
 */
@Test
public void testMultiOwnerRules() {
    Iterator<AuthorizationType> expectedAuthTypes = Arrays.asList(AuthorizationType.AMAZON_COGNITO_USER_POOLS, AuthorizationType.OPENID_CONNECT).iterator();
    Iterator<Boolean> expectedIsOwnerFlags = Arrays.asList(true, true).iterator();
    List<AuthRule> authRules = Arrays.asList(buildOwnerRule(AuthStrategy.Provider.OIDC, "differentOwner", "myClaim", Arrays.asList(ModelOperation.CREATE, ModelOperation.DELETE)), buildOwnerRule(null, null, null, null), buildOwnerRule(AuthStrategy.Provider.USER_POOLS, "differentOwnerField", null, Arrays.asList(ModelOperation.CREATE, ModelOperation.DELETE)));
    MultiAuthorizationTypeIterator actualAuthTypeIterator = new MultiAuthorizationTypeIterator(authRules);
    assertIteratorState(expectedAuthTypes, expectedIsOwnerFlags, actualAuthTypeIterator);
}
Also used : MultiAuthorizationTypeIterator(com.amplifyframework.core.model.auth.MultiAuthorizationTypeIterator) AuthorizationType(com.amplifyframework.api.aws.AuthorizationType) AuthRule(com.amplifyframework.core.model.AuthRule) Test(org.junit.Test)

Aggregations

AuthRule (com.amplifyframework.core.model.AuthRule)4 MultiAuthorizationTypeIterator (com.amplifyframework.core.model.auth.MultiAuthorizationTypeIterator)4 AuthorizationType (com.amplifyframework.api.aws.AuthorizationType)3 Test (org.junit.Test)3 NonNull (androidx.annotation.NonNull)1 Consumer (com.amplifyframework.core.Consumer)1 ModelField (com.amplifyframework.core.model.ModelField)1 ModelOperation (com.amplifyframework.core.model.ModelOperation)1 ModelSchema (com.amplifyframework.core.model.ModelSchema)1 AuthorizationTypeIterator (com.amplifyframework.core.model.auth.AuthorizationTypeIterator)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1