Пример #1
0
        public static GetSsoTokenResponse GetSsoToken(IAmazonSSOOIDC client, GetSsoTokenRequest request, IGetSsoTokenContext context)
        {
            var registerClientRequest = new RegisterClientRequest()
            {
                ClientName = request.ClientName,
                ClientType = request.ClientType,
            };

            InternalSDKUtils.ApplyValues(registerClientRequest, request.AdditionalProperties);

            var registerClientResponse = client.RegisterClient(registerClientRequest);


            var startDeviceAuthorizationRequest = new StartDeviceAuthorizationRequest()
            {
                ClientSecret = registerClientResponse.ClientSecret,
                ClientId     = registerClientResponse.ClientId,
                StartUrl     = request.StartUrl,
            };

            InternalSDKUtils.ApplyValues(startDeviceAuthorizationRequest, request.AdditionalProperties);

            var startDeviceAuthorizationResponse = client.StartDeviceAuthorization(startDeviceAuthorizationRequest);


            // Spec: The expiration time must be calculated by adding the number of seconds
            // returned by StartDeviceAuthorization (ExpiresIn) to the current time.
            DateTime deviceCodeExpiration = DateTime.UtcNow.AddSeconds(startDeviceAuthorizationResponse.ExpiresIn);

            request.SsoVerificationCallback(new SsoVerificationArguments()
            {
                UserCode                = startDeviceAuthorizationResponse.UserCode,
                VerificationUri         = startDeviceAuthorizationResponse.VerificationUri,
                VerificationUriComplete = startDeviceAuthorizationResponse.VerificationUriComplete,
            });


            var createTokenRequest = new CreateTokenRequest()
            {
                ClientId     = registerClientResponse.ClientId,
                ClientSecret = registerClientResponse.ClientSecret,
                GrantType    = CreateTokenGrantType,
                DeviceCode   = startDeviceAuthorizationResponse.DeviceCode,
            };

            InternalSDKUtils.ApplyValues(request, request.AdditionalProperties);

            var ssoToken = PollForSsoToken(client,
                                           createTokenRequest,
                                           startDeviceAuthorizationResponse.Interval,
                                           deviceCodeExpiration,
                                           context);

            return(new GetSsoTokenResponse()
            {
                AccessToken = ssoToken.AccessToken,
                ExpiresAt = DateTime.UtcNow.AddSeconds(ssoToken.ExpiresIn),
            });
        }
        void ICoreAmazonS3.Delete(string bucketName, string objectKey, IDictionary <string, object> additionalProperties)
        {
            var request = new DeleteObjectRequest
            {
                BucketName = bucketName,
                Key        = objectKey
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);
            this.DeleteObject(request);
        }
        Task ICoreAmazonS3.DeleteAsync(string bucketName, string objectKey, IDictionary <string, object> additionalProperties, CancellationToken cancellationToken)
        {
            var request = new DeleteObjectRequest
            {
                BucketName = bucketName,
                Key        = objectKey
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);
            return(this.DeleteObjectAsync(request, cancellationToken));
        }
        public void SetHttpVerb()
        {
            var request = new GetPreSignedUrlRequest();
            var values  = new Dictionary <string, object> {
                { "Verb", "DELETE" }
            };

            InternalSDKUtils.ApplyValues(request, values);

            Assert.AreEqual(request.Verb, HttpVerb.DELETE);
        }
        Stream ICoreAmazonS3.GetObjectStream(string bucketName, string objectKey, IDictionary <string, object> additionalProperties)
        {
            var request = new GetObjectRequest()
            {
                BucketName = bucketName,
                Key        = objectKey
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);
            return(this.GetObject(request).ResponseStream);
        }
        IAsyncResult ICoreAmazonS3.BeginGetObjectStream(string bucketName, string objectKey, IDictionary <string, object> additionalProperties, AsyncCallback callback, object state)
        {
            var request = new GetObjectRequest()
            {
                BucketName = bucketName,
                Key        = objectKey
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);

            return(this.BeginGetObject(request, callback, state));
        }
        async Task <Stream> ICoreAmazonS3.GetObjectStreamAsync(string bucketName, string objectKey, IDictionary <string, object> additionalProperties, CancellationToken cancellationToken)
        {
            var request = new GetObjectRequest()
            {
                BucketName = bucketName,
                Key        = objectKey
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);

            return((await this.GetObjectAsync(request, cancellationToken).ConfigureAwait(false)).ResponseStream);
        }
        void ICoreAmazonS3.UploadObjectFromStream(string bucketName, string objectKey, Stream stream, IDictionary <string, object> additionalProperties)
        {
            var transfer = new Amazon.S3.Transfer.TransferUtility(this);
            var request  = new Amazon.S3.Transfer.TransferUtilityUploadRequest
            {
                BucketName  = bucketName,
                Key         = objectKey,
                InputStream = stream
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);
            transfer.Upload(request);
        }
        Task ICoreAmazonS3.UploadObjectFromStreamAsync(string bucketName, string objectKey, Stream stream, IDictionary <string, object> additionalProperties, CancellationToken cancellationToken)
        {
            var transfer = new Amazon.S3.Transfer.TransferUtility(this);
            var request  = new Amazon.S3.Transfer.TransferUtilityUploadRequest
            {
                BucketName  = bucketName,
                Key         = objectKey,
                InputStream = stream
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);
            return(transfer.UploadAsync(request, cancellationToken));
        }
        void ICoreAmazonS3.UploadObjectFromFilePath(string bucketName, string objectKey, string filepath, IDictionary <string, object> additionalProperties)
        {
            var transfer = new Amazon.S3.Transfer.TransferUtility(this);
            var request  = new Amazon.S3.Transfer.TransferUtilityUploadRequest
            {
                BucketName = bucketName,
                Key        = objectKey,
                FilePath   = filepath
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);
            transfer.Upload(request);
        }
        string ICoreAmazonS3.GeneratePreSignedURL(string bucketName, string objectKey, DateTime expiration, IDictionary <string, object> additionalProperties)
        {
            var request = new GetPreSignedUrlRequest
            {
                BucketName = bucketName,
                Key        = objectKey,
                Expires    = expiration
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);


            return(this.GetPreSignedURL(request));
        }
        Task ICoreAmazonS3.DeletesAsync(string bucketName, IEnumerable <string> objectKeys, IDictionary <string, object> additionalProperties, CancellationToken cancellationToken)
        {
            var request = new DeleteObjectsRequest
            {
                BucketName = bucketName,
            };

            foreach (var key in objectKeys)
            {
                request.AddKey(key);
            }
            InternalSDKUtils.ApplyValues(request, additionalProperties);
            return(this.DeleteObjectsAsync(request, cancellationToken));
        }
        IAsyncResult ICoreAmazonS3.BeginUploadObjectFromFilePath(string bucketName, string objectKey, string filepath, IDictionary <string, object> additionalProperties, AsyncCallback callback, object state)
        {
            var transfer = new Amazon.S3.Transfer.TransferUtility(this);
            var request  = new Amazon.S3.Transfer.TransferUtilityUploadRequest
            {
                BucketName = bucketName,
                Key        = objectKey,
                FilePath   = filepath
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);

            return(transfer.BeginUpload(request, callback, state));
        }
        void ICoreAmazonS3.Deletes(string bucketName, IEnumerable <string> objectKeys, IDictionary <string, object> additionalProperties)
        {
            var request = new DeleteObjectsRequest
            {
                BucketName = bucketName
            };

            foreach (var key in objectKeys)
            {
                request.AddKey(key);
            }
            InternalSDKUtils.ApplyValues(request, additionalProperties);
            this.DeleteObjects(request);
        }
        Task ICoreAmazonS3.DownloadToFilePathAsync(string bucketName, string objectKey, string filepath, IDictionary <string, object> additionalProperties, CancellationToken cancellationToken)
        {
            var transfer = new Amazon.S3.Transfer.TransferUtility(this);

            var request = new Amazon.S3.Transfer.TransferUtilityDownloadRequest
            {
                BucketName = bucketName,
                Key        = objectKey,
                FilePath   = filepath
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);

            return(transfer.DownloadAsync(request, cancellationToken));
        }
        public void SetAutoCloseStream()
        {
            var request = new PutObjectRequest();
            var values  = new Dictionary <string, object> {
                { "AutoCloseStream", false }
            };

            InternalSDKUtils.ApplyValues(request, values);

            Assert.AreEqual(request.AutoCloseStream, false);

            request = new PutObjectRequest();
            values  = new Dictionary <string, object> {
                { "AutoCloseStream", true }
            };

            InternalSDKUtils.ApplyValues(request, values);

            Assert.AreEqual(request.AutoCloseStream, true);
        }
        IList <string> ICoreAmazonS3.GetAllObjectKeys(string bucketName, string prefix, IDictionary <string, object> additionalProperties)
        {
            var    keys   = new List <string>();
            string marker = null;

            do
            {
                var request = new ListObjectsRequest
                {
                    BucketName = bucketName,
                    Prefix     = prefix,
                    Marker     = marker
                };
                InternalSDKUtils.ApplyValues(request, additionalProperties);

                var listResponse = this.ListObjects(request);
                keys.AddRange(listResponse.S3Objects.Select(o => o.Key));
                marker = listResponse.NextMarker;
            } while (!string.IsNullOrEmpty(marker));

            return(keys);
        }
Пример #18
0
        public static async Task <ImmutableCredentials> CredentialsFromSsoAccessTokenAsync(IAmazonSSO client, string accountId,
                                                                                           string roleName, string accessToken,
                                                                                           IDictionary <string, object> additionalProperties)
        {
            var request = new GetRoleCredentialsRequest()
            {
                AccessToken = accessToken,
                AccountId   = accountId,
                RoleName    = roleName,
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);

            var response = await client.GetRoleCredentialsAsync(request).ConfigureAwait(false);

            DateTime credentialsExpiration =
                AWSSDKUtils.ConvertFromUnixEpochMilliseconds(response.RoleCredentials.Expiration);

            return(new SSOImmutableCredentials(
                       response.RoleCredentials.AccessKeyId,
                       response.RoleCredentials.SecretAccessKey,
                       response.RoleCredentials.SessionToken,
                       credentialsExpiration));
        }