Exemple #1
0
 public GenesysAttributeBinding(string name, Type type, GenesysTokenProvider tokenProvider, GenesysTokenContext tokenContext)
 {
     _parameterName = name;
     _parameterType = type;
     _tokenProvider = tokenProvider;
     _tokenContext  = tokenContext;
 }
Exemple #2
0
        public virtual async Task <IGenesysAccessToken> GetTokenAsync(DateTime date
                                                                      , GenesysTokenContext tokenContext
                                                                      , IReadOnlyDictionary <string, object> webjobBindingData)
        {
            var credentials = await GetClientCredentialsAsync(tokenContext);

            if (credentials == null)
            {
                throw new ArgumentNullException("Cant get token with nullable client credentials.");
            }
            if (string.IsNullOrEmpty(credentials.ClientId))
            {
                throw new ArgumentException("ClientId is not defined.");
            }
            if (string.IsNullOrEmpty(credentials.ClientSecret))
            {
                throw new ArgumentException("ClientSecret is not defined.");
            }
            ValidateEnvironment(credentials.Environment);

            using (var client = new HttpClient())
            {
                var token = await tokenContext.TokenTable.GetAuthTokenAsync(client, date, credentials);

                return(token);
            }
        }
Exemple #3
0
        public virtual Task <IGenesysClientCredentials> GetClientCredentialsAsync(GenesysTokenContext tokenContext)
        {
            IGenesysClientCredentials credentials = new GenesysClientCredentials
            {
                ClientId     = _configuration[tokenContext.ClientId ?? GenesysConfigNames.ClientId],
                ClientSecret = _configuration[tokenContext.ClientSecret ?? GenesysConfigNames.ClientSecret],
                Environment  = _configuration[tokenContext.Environment ?? GenesysConfigNames.Environment],
                OrgId        = _configuration[GenesysConfigNames.OrgId],
            };

            //_configuration.GetSection(GenesysConfigNames.CredentialsSection).Get<GenesysClientCredentials>();

            return(Task.FromResult(credentials));
        }
        public async Task <IBinding> TryCreateAsync(BindingProviderContext context)
        {
            var parameter     = context.Parameter;
            var parameterName = context.Parameter.Name;
            var parameterType = context.Parameter.ParameterType;

            var attribute = parameter.GetCustomAttribute <GenesysAttribute>();

            if (attribute == null)
            {
                return(null);
            }

            if (parameterType != typeof(string) && parameterType != typeof(IGenesysAccessToken))
            {
                throw new InvalidOperationException("Can't bind entity to type '" + parameterType + "'.");
            }

            var connectionString = GetConnectionString(attribute.Connection);

            if (!TableStorageAccount.TryParse(connectionString, out TableStorageAccount tableStorageAccount))
            {
                throw new InvalidOperationException($"Storage account connection string for '{IConfigurationExtensions.GetPrefixedConnectionStringName(attribute.Connection)}' is invalid");
            }

            var tableClient = tableStorageAccount.CreateCloudTableClient();
            var table       = tableClient.GetTableReference(attribute.TokenTable ?? GenesysConfigNames.GenesysTokensTable);
            await table.CreateIfNotExistsAsync();

            var tokenCtx = new GenesysTokenContext
            {
                TokenTable   = table,
                ClientId     = attribute.ClientId,
                ClientSecret = attribute.ClientSecret,
                Environment  = attribute.Environment,
            };

            IBinding binding = new GenesysAttributeBinding(parameterName, parameterType, _tokenProvider, tokenCtx);

            return(binding);
        }