Пример #1
0
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            CreateIdentityPoolResponse createIdentityPoolResponse = new CreateIdentityPoolResponse();

            context.Read();
            int currentDepth = context.CurrentDepth;

            while (context.ReadAtDepth(currentDepth))
            {
                if (context.TestExpression("AllowUnauthenticatedIdentities", currentDepth))
                {
                    BoolUnmarshaller instance = BoolUnmarshaller.Instance;
                    createIdentityPoolResponse.AllowUnauthenticatedIdentities = instance.Unmarshall(context);
                }
                else if (context.TestExpression("CognitoIdentityProviders", currentDepth))
                {
                    ListUnmarshaller <CognitoIdentityProviderInfo, CognitoIdentityProviderInfoUnmarshaller> listUnmarshaller = new ListUnmarshaller <CognitoIdentityProviderInfo, CognitoIdentityProviderInfoUnmarshaller>(CognitoIdentityProviderInfoUnmarshaller.Instance);
                    createIdentityPoolResponse.CognitoIdentityProviders = listUnmarshaller.Unmarshall(context);
                }
                else if (context.TestExpression("DeveloperProviderName", currentDepth))
                {
                    StringUnmarshaller instance2 = StringUnmarshaller.Instance;
                    createIdentityPoolResponse.DeveloperProviderName = instance2.Unmarshall(context);
                }
                else if (context.TestExpression("IdentityPoolId", currentDepth))
                {
                    StringUnmarshaller instance3 = StringUnmarshaller.Instance;
                    createIdentityPoolResponse.IdentityPoolId = instance3.Unmarshall(context);
                }
                else if (context.TestExpression("IdentityPoolName", currentDepth))
                {
                    StringUnmarshaller instance4 = StringUnmarshaller.Instance;
                    createIdentityPoolResponse.IdentityPoolName = instance4.Unmarshall(context);
                }
                else if (context.TestExpression("OpenIdConnectProviderARNs", currentDepth))
                {
                    ListUnmarshaller <string, StringUnmarshaller> listUnmarshaller2 = new ListUnmarshaller <string, StringUnmarshaller>(StringUnmarshaller.Instance);
                    createIdentityPoolResponse.OpenIdConnectProviderARNs = listUnmarshaller2.Unmarshall(context);
                }
                else if (context.TestExpression("SamlProviderARNs", currentDepth))
                {
                    ListUnmarshaller <string, StringUnmarshaller> listUnmarshaller3 = new ListUnmarshaller <string, StringUnmarshaller>(StringUnmarshaller.Instance);
                    createIdentityPoolResponse.SamlProviderARNs = listUnmarshaller3.Unmarshall(context);
                }
                else if (context.TestExpression("SupportedLoginProviders", currentDepth))
                {
                    DictionaryUnmarshaller <string, string, StringUnmarshaller, StringUnmarshaller> dictionaryUnmarshaller = new DictionaryUnmarshaller <string, string, StringUnmarshaller, StringUnmarshaller>(StringUnmarshaller.Instance, StringUnmarshaller.Instance);
                    createIdentityPoolResponse.SupportedLoginProviders = dictionaryUnmarshaller.Unmarshall(context);
                }
            }
            return(createIdentityPoolResponse);
        }
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            CreateIdentityPoolResponse response = new CreateIdentityPoolResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("AllowUnauthenticatedIdentities", targetDepth))
                {
                    var unmarshaller = BoolUnmarshaller.Instance;
                    response.AllowUnauthenticatedIdentities = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("DeveloperProviderName", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.DeveloperProviderName = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("IdentityPoolId", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.IdentityPoolId = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("IdentityPoolName", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.IdentityPoolName = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("OpenIdConnectProviderARNs", targetDepth))
                {
                    var unmarshaller = new ListUnmarshaller <string, StringUnmarshaller>(StringUnmarshaller.Instance);
                    response.OpenIdConnectProviderARNs = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("SupportedLoginProviders", targetDepth))
                {
                    var unmarshaller = new DictionaryUnmarshaller <string, string, StringUnmarshaller, StringUnmarshaller>(StringUnmarshaller.Instance, StringUnmarshaller.Instance);
                    response.SupportedLoginProviders = unmarshaller.Unmarshall(context);
                    continue;
                }
            }


            return(response);
        }
Пример #3
0
        //Tests GetCognitoAWSCredentials
        public async void TestGetCognitoAWSCredentials()
        {
            string password     = "******";
            string poolRegion   = user.UserPool.PoolID.Substring(0, user.UserPool.PoolID.IndexOf("_"));
            string providerName = "cognito-idp." + poolRegion + ".amazonaws.com/" + user.UserPool.PoolID;

            AuthFlowResponse context =
                await user.StartWithSrpAuthAsync(new InitiateSrpAuthRequest()
            {
                Password = password
            }).ConfigureAwait(false);

            //Create identity pool
            identityClient = new AmazonCognitoIdentityClient(clientCredentials, clientRegion);
            CreateIdentityPoolResponse poolResponse =
                await identityClient.CreateIdentityPoolAsync(new CreateIdentityPoolRequest()
            {
                AllowUnauthenticatedIdentities = false,
                CognitoIdentityProviders       = new List <CognitoIdentityProviderInfo>()
                {
                    new CognitoIdentityProviderInfo()
                    {
                        ProviderName = providerName, ClientId = user.ClientID
                    }
                },
                IdentityPoolName = "TestIdentityPool" + DateTime.Now.ToString("yyyyMMdd_HHmmss"),
            }).ConfigureAwait(false);

            identityPoolId = poolResponse.IdentityPoolId;

            //Create role for identity pool
            managementClient = new AmazonIdentityManagementServiceClient(clientCredentials, clientRegion);
            CreateRoleResponse roleResponse = managementClient.CreateRoleAsync(new CreateRoleRequest()
            {
                RoleName = "_TestRole_" + DateTime.Now.ToString("yyyyMMdd_HHmmss"),
                AssumeRolePolicyDocument = "{\"Version\": \"2012-10-17\",\"Statement\": [{\"Effect" +
                                           "\": \"Allow\",\"Principal\": {\"Federated\": \"cognito-identity.amazonaws.com\"}," +
                                           "\"Action\": \"sts:AssumeRoleWithWebIdentity\"}]}"
            }).Result;

            roleName = roleResponse.Role.RoleName;

            //Create and attach policy for role
            CreatePolicyResponse policyResponse = managementClient.CreatePolicyAsync(new CreatePolicyRequest()
            {
                PolicyDocument = "{\"Version\": \"2012-10-17\",\"Statement\": " +
                                 "[{\"Effect\": \"Allow\",\"Action\": [\"mobileanalytics:PutEvents\",\"cog" +
                                 "nito-sync:*\",\"cognito-identity:*\",\"s3:*\"],\"Resource\": [\"*\"]}]}",
                PolicyName = "_Cognito_" + DateTime.Now.ToString("yyyyMMdd_HHmmss"),
            }).Result;

            policyArn = policyResponse.Policy.Arn;

            AttachRolePolicyRequest attachRequest = new AttachRolePolicyRequest()
            {
                PolicyArn = policyArn,
                RoleName  = roleName
            };
            AttachRolePolicyResponse attachRolePolicyResponse = managementClient.AttachRolePolicyAsync(attachRequest).Result;

            //Set the role for the identity pool
            await identityClient.SetIdentityPoolRolesAsync(new SetIdentityPoolRolesRequest()
            {
                IdentityPoolId = identityPoolId,
                Roles          = new Dictionary <string, string>()
                {
                    { "authenticated", roleResponse.Role.Arn },
                    { "unauthenticated", roleResponse.Role.Arn }
                },
            }).ConfigureAwait(false);

            //Create and test credentials
            CognitoAWSCredentials credentials = user.GetCognitoAWSCredentials(identityPoolId, clientRegion);

            using (var client = new AmazonS3Client(credentials, Amazon.RegionEndpoint.USEast1))
            {
                int tries = 0;
                ListBucketsResponse bucketsResponse = null;
                var       retryLimit    = 5;
                Exception lastException = null;

                for (; tries < retryLimit; tries++)
                {
                    try
                    {
                        bucketsResponse = await client.ListBucketsAsync(new ListBucketsRequest()).ConfigureAwait(false);

                        Assert.Equal(bucketsResponse.HttpStatusCode, System.Net.HttpStatusCode.OK);
                        break;
                    }
                    catch (Exception ex)
                    {
                        lastException = ex;
                        System.Threading.Thread.Sleep(3000);
                    }
                }

                if (tries == retryLimit && lastException != null)
                {
                    throw lastException;
                }
            }
        }