示例#1
0
        static private async Task api(string[] args)
        {
            UserCognitoCredentials credentials = getSavedCredentials();

            if (credentials == null)
            {
                String password = GetPassword();
                credentials = getCognitoCredentials(args[0], password).Result;
                saveCredentials(credentials);
            }

            var signer  = new AWS4RequestSigner(credentials.getAccessKey(), credentials.getSecretKey());
            var request = new HttpRequestMessage {
                Method     = HttpMethod.Get,
                RequestUri = new Uri("https://ats.api.alexa.com/api?Action=TopSites&Count=5&CountryCode=" + args[2] + "&ResponseGroup=Country")
            };

            request.Headers.Add("x-api-key", args[1]);
            request.Headers.Add("x-amz-security-token", credentials.getSessionToken());

            request = await signer.Sign(request, "execute-api", "us-east-1");

            var client   = new HttpClient();
            var response = await client.SendAsync(request);

            var responseStr = await response.Content.ReadAsStringAsync();

            Console.WriteLine(responseStr);
        }
示例#2
0
        static private void saveCredentials(UserCognitoCredentials credentials)
        {
            IFormatter formatter = new BinaryFormatter();
            Stream     stream    = new FileStream(credentialsFile, FileMode.Create, FileAccess.Write);

            formatter.Serialize(stream, credentials);
            stream.Close();
        }
示例#3
0
        static private async Task <UserCognitoCredentials> getCognitoCredentials(String userEmail, String userPassword)
        {
            String cognitoUserPoolId     = "us-east-1_n8TiZp7tu";
            String cognitoClientId       = "6clvd0v40jggbaa5qid2h6hkqf";
            String cognitoIdentityPoolId = "us-east-1:bff024bb-06d0-4b04-9e5d-eb34ed07f884";

            Amazon.RegionEndpoint cognitoRegion = Amazon.RegionEndpoint.USEast1;

            AmazonCognitoIdentityProviderClient provider =
                new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.AnonymousAWSCredentials(), Amazon.RegionEndpoint.USEast1);
            CognitoUserPool userPool = new CognitoUserPool(cognitoUserPoolId, cognitoClientId, provider);
            CognitoUser     user     = new CognitoUser(userEmail, cognitoClientId, userPool, provider);

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

            String accessToken = context.AuthenticationResult.AccessToken;
            String idToken     = context.AuthenticationResult.IdToken;

            CognitoAWSCredentials credentials =
                user.GetCognitoAWSCredentials(cognitoIdentityPoolId, cognitoRegion);

            var identityClient = new AmazonCognitoIdentityClient(credentials, cognitoRegion);
            var idRequest      = new Amazon.CognitoIdentity.Model.GetIdRequest();

            idRequest.IdentityPoolId = cognitoIdentityPoolId;
            idRequest.Logins         = new Dictionary <string, string> {
                { "cognito-idp.us-east-1.amazonaws.com/" + cognitoUserPoolId, idToken }
            };
            var idResponseId = await identityClient.GetIdAsync(idRequest).ConfigureAwait(false);

            if (idResponseId.HttpStatusCode != System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine(String.Format("Failed to get credentials for identity. Status code: {0} ", idResponseId.HttpStatusCode));
                System.Environment.Exit(1);
            }

            var idResponseCredential = await identityClient.GetCredentialsForIdentityAsync(idResponseId.IdentityId, new Dictionary <string, string> {
                { "cognito-idp.us-east-1.amazonaws.com/" + cognitoUserPoolId, idToken }
            }).ConfigureAwait(false);

            if (idResponseCredential.HttpStatusCode != System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine(String.Format("Failed to get credentials for identity. Status code: {0} ", idResponseCredential.HttpStatusCode));
                System.Environment.Exit(1);
            }

            var cognitoCredentials = new UserCognitoCredentials(idResponseCredential.Credentials);

            return(cognitoCredentials);
        }
示例#4
0
        static private UserCognitoCredentials getSavedCredentials()
        {
            UserCognitoCredentials credentials = null;

            try {
                IFormatter formatter = new BinaryFormatter();
                Stream     stream    = new FileStream(credentialsFile, FileMode.Open, FileAccess.Read);
                credentials = (UserCognitoCredentials)formatter.Deserialize(stream);
                if (DateTime.UtcNow > credentials.getExpiration())
                {
                    Console.WriteLine(String.Format("Saved credentials expired ({0} older than {1})", credentials.getExpiration(), DateTime.UtcNow));
                    credentials = null;
                }
                else
                {
                    Console.WriteLine(String.Format("Using saved credentials. Valid until {0}", credentials.getExpiration()));
                }
            } catch (IOException) {
                credentials = null;
            }
            return(credentials);
        }