public GatewayAddressCache(
            Uri serviceEndpoint,
            Protocol protocol,
            IAuthorizationTokenProvider tokenProvider,
            UserAgentContainer userAgent,
            IServiceConfigurationReader serviceConfigReader,
            long suboptimalPartitionForceRefreshIntervalInSeconds = 600,
            HttpMessageHandler messageHandler = null,
            ApiType apiType = ApiType.None)
        {
            this.addressEndpoint                     = new Uri(serviceEndpoint + "/" + Paths.AddressPathSegment);
            this.protocol                            = protocol;
            this.tokenProvider                       = tokenProvider;
            this.serviceEndpoint                     = serviceEndpoint;
            this.serviceConfigReader                 = serviceConfigReader;
            this.serverPartitionAddressCache         = new AsyncCache <PartitionKeyRangeIdentity, PartitionAddressInformation>();
            this.suboptimalServerPartitionTimestamps = new ConcurrentDictionary <PartitionKeyRangeIdentity, DateTime>();
            this.suboptimalMasterPartitionTimestamp  = DateTime.MaxValue;

            this.suboptimalPartitionForceRefreshIntervalInSeconds = suboptimalPartitionForceRefreshIntervalInSeconds;

            this.httpClient     = messageHandler == null ? new HttpClient() : new HttpClient(messageHandler);
            this.protocolFilter =
                string.Format(CultureInfo.InvariantCulture,
                              GatewayAddressCache.protocolFilterFormat,
                              Constants.Properties.Protocol,
                              GatewayAddressCache.ProtocolString(this.protocol));

            // Set requested API version header for version enforcement.
            this.httpClient.DefaultRequestHeaders.Add(HttpConstants.HttpHeaders.Version,
                                                      HttpConstants.Versions.CurrentVersion);

            this.httpClient.AddUserAgentHeader(userAgent);
            this.httpClient.AddApiTypeHeader(apiType);
        }
        public GatewayAddressCache(
            Uri serviceEndpoint,
            Protocol protocol,
            IAuthorizationTokenProvider tokenProvider,
            IServiceConfigurationReader serviceConfigReader,
            CosmosHttpClient httpClient,
            long suboptimalPartitionForceRefreshIntervalInSeconds = 600,
            bool enableTcpConnectionEndpointRediscovery           = false)
        {
            this.addressEndpoint                        = new Uri(serviceEndpoint + "/" + Paths.AddressPathSegment);
            this.protocol                               = protocol;
            this.tokenProvider                          = tokenProvider;
            this.serviceEndpoint                        = serviceEndpoint;
            this.serviceConfigReader                    = serviceConfigReader;
            this.serverPartitionAddressCache            = new AsyncCache <PartitionKeyRangeIdentity, PartitionAddressInformation>();
            this.suboptimalServerPartitionTimestamps    = new ConcurrentDictionary <PartitionKeyRangeIdentity, DateTime>();
            this.serverPartitionAddressToPkRangeIdMap   = new ConcurrentDictionary <ServerKey, HashSet <PartitionKeyRangeIdentity> >();
            this.suboptimalMasterPartitionTimestamp     = DateTime.MaxValue;
            this.enableTcpConnectionEndpointRediscovery = enableTcpConnectionEndpointRediscovery;

            this.suboptimalPartitionForceRefreshIntervalInSeconds = suboptimalPartitionForceRefreshIntervalInSeconds;

            this.httpClient = httpClient;

            this.protocolFilter =
                string.Format(CultureInfo.InvariantCulture,
                              GatewayAddressCache.protocolFilterFormat,
                              Constants.Properties.Protocol,
                              GatewayAddressCache.ProtocolString(this.protocol));
        }
Example #3
0
 public PartitionKeyRangeCache(IAuthorizationTokenProvider authorizationTokenProvider, IStoreModel storeModel, CollectionCache collectionCache)
 {
     this.routingMapCache = new AsyncCache <string, CollectionRoutingMap>(
         EqualityComparer <CollectionRoutingMap> .Default,
         StringComparer.Ordinal);
     this.authorizationTokenProvider = authorizationTokenProvider;
     this.storeModel      = storeModel;
     this.collectionCache = collectionCache;
 }
        /// <summary>
        /// Create new instance of <see cref="HttpRequestContext"/>
        /// </summary>
        /// <param name="traceContext"></param>
        public HttpRequestContext(TraceContext traceContext, IAuthorizationTokenProvider authorizationProvider, IHttpExtension httpExtension = null)
        {
            ArgumentValidator.ThrowIfNull(
                traceContext,
                nameof(traceContext));

            this.TraceContext          = traceContext;
            this.AuthorizationProvider = authorizationProvider;
            this.HttpExtension         = httpExtension;
        }
Example #5
0
 public UsernamePasswordAuthenticationService(
     IUsernamePasswordAuthenticationProvider usernamePasswordAuthenticationProvider,
     IPermissionAuthorizationProvider permissionAuthorizationProvider,
     IAuthorizationTokenProvider authorizationTokenProvider
     )
 {
     _usernamePasswordAuthenticationProvider = usernamePasswordAuthenticationProvider;
     _permissionAuthorizationProvider        = permissionAuthorizationProvider;
     _authorizationTokenProvider             = authorizationTokenProvider;
 }
        public ClientCollectionCache(IStoreModel storeModel, IAuthorizationTokenProvider tokenProvider, RetryPolicy retryPolicy)
        {
            if (storeModel == null)
            {
                throw new ArgumentNullException("storeModel");
            }

            this.storeModel    = storeModel;
            this.tokenProvider = tokenProvider;
            this.retryPolicy   = retryPolicy;
        }
Example #7
0
        public AuthorizationController(IAuthorizationTokenProvider tokenProvider,
                                       ICommandHandler <CreateUserCommand> userCommandHandler,
                                       ISearchableRepository <UserInfo, UserInfo> searchRepository,
                                       IHashProvider hashProvider,
                                       IRepository <UserInfo, UserInfo> userRepository)
        {
            Condition.Requires(tokenProvider).IsNotNull();
            Condition.Requires(userCommandHandler).IsNotNull();
            Condition.Requires(searchRepository).IsNotNull();
            Condition.Requires(hashProvider).IsNotNull();
            Condition.Requires(userRepository).IsNotNull();

            m_TokenProvider      = tokenProvider;
            m_UserCommandHandler = userCommandHandler;
            m_SearchRepository   = searchRepository;
            m_HashProvider       = hashProvider;
        }
        public void Test_ExchangeServiceThrowsOnEmptyTokenAndNullTokenProvider()
        {
            Assert.ThrowsException <ArgumentNullException>(() =>
            {
                ExchangeService exchangeService = new ExchangeService(
                    string.Empty,
                    "*****@*****.**");
            });

            Assert.ThrowsException <ArgumentNullException>(() =>
            {
                IAuthorizationTokenProvider provider = null;
                ExchangeService exchangeService      = new ExchangeService(
                    provider,
                    "*****@*****.**");
            });
        }
Example #9
0
        public UserCommandHandler(IHashProvider hashProvider,
                                  IRepository <UserInfo, UserInfo> userRepository,
                                  IMessager messager,
                                  IAuthorizationTokenProvider tokenProvider,
                                  ISearchableRepository <UserInfo, UserInfo> searchRepository)
        {
            Condition.Requires(tokenProvider).IsNotNull();
            Condition.Requires(userRepository).IsNotNull();
            Condition.Requires(hashProvider).IsNotNull();
            Condition.Requires(messager).IsNotNull();
            Condition.Requires(searchRepository).IsNotNull();

            m_UserRepository   = userRepository;
            m_HashProvider     = hashProvider;
            m_Messager         = messager;
            m_TokenProvider    = tokenProvider;
            m_SearchRepository = searchRepository;
        }
Example #10
0
        public UserController(IAuthorizationTokenProvider tokenProvider,
                              IRepository <UserInfo, UserInfo> userRepository,
                              ISearchableRepository <UserInfo, UserInfo> searchRepository,
                              ICommandHandler <ResetPasswordCommand> commandHandler,
                              ICommandHandler <SetPasswordCommand> setPasswordHandler)
        {
            Condition.Requires(tokenProvider).IsNotNull();
            Condition.Requires(userRepository).IsNotNull();
            Condition.Requires(commandHandler).IsNotNull();
            Condition.Requires(searchRepository).IsNotNull();
            Condition.Requires(setPasswordHandler).IsNotNull();

            m_TokenProvider      = tokenProvider;
            m_UserRepository     = userRepository;
            m_CommandHandler     = commandHandler;
            m_SearchRepository   = searchRepository;
            m_SetPasswordHandler = setPasswordHandler;
        }
Example #11
0
        public GlobalAddressResolver(
            GlobalEndpointManager endpointManager,
            Protocol protocol,
            IAuthorizationTokenProvider tokenProvider,
            CollectionCache collectionCache,
            PartitionKeyRangeCache routingMapProvider,
            UserAgentContainer userAgentContainer,
            IServiceConfigurationReader serviceConfigReader,
            HttpMessageHandler messageHandler,
            ConnectionPolicy connectionPolicy,
            ApiType apiType)
        {
            this.endpointManager     = endpointManager;
            this.protocol            = protocol;
            this.tokenProvider       = tokenProvider;
            this.userAgentContainer  = userAgentContainer;
            this.collectionCache     = collectionCache;
            this.routingMapProvider  = routingMapProvider;
            this.serviceConfigReader = serviceConfigReader;
            this.messageHandler      = messageHandler;
            this.requestTimeout      = connectionPolicy.RequestTimeout;
            this.apiType             = apiType;

            int maxBackupReadEndpoints =
                !connectionPolicy.EnableReadRequestsFallback.HasValue || connectionPolicy.EnableReadRequestsFallback.Value
                ? GlobalAddressResolver.MaxBackupReadRegions : 0;

            this.enableTcpConnectionEndpointRediscovery = connectionPolicy.EnableTcpConnectionEndpointRediscovery;

            this.maxEndpoints = maxBackupReadEndpoints + 2; // for write and alternate write endpoint (during failover)

            this.addressCacheByEndpoint = new ConcurrentDictionary <Uri, EndpointCache>();

            foreach (Uri endpoint in endpointManager.WriteEndpoints)
            {
                this.GetOrAddEndpoint(endpoint);
            }

            foreach (Uri endpoint in endpointManager.ReadEndpoints)
            {
                this.GetOrAddEndpoint(endpoint);
            }
        }
        internal static Guid GetUserId(this HttpRequestMessage request)
        {
            try
            {
                string tokenHeader = request.Headers
                                     .FirstOrDefault(header => header.Key == AUTH_TOKEN_NAME)
                                     .Value.FirstOrDefault();

                if (string.IsNullOrEmpty(tokenHeader))
                {
                    throw new HttpException(401, ErrorMessages.UserNotAuthorized);
                }

                IAuthorizationTokenProvider tokenProvider = (IAuthorizationTokenProvider)WebApiConfig.Container.GetService(typeof(IAuthorizationTokenProvider));
                Guid userId = tokenProvider.ValidateAuthorizationToken(tokenHeader);

                return(userId);
            }
            catch (Exception)
            {
                throw new HttpException(401, ErrorMessages.UserNotAuthorized);
            }
        }
Example #13
0
 public StoreClient CreateStoreClient(
     IAddressResolver addressResolver,
     ISessionContainer sessionContainer,
     IServiceConfigurationReader serviceConfigurationReader,
     IAuthorizationTokenProvider authorizationTokenProvider,
     bool enableRequestDiagnostics   = false,
     bool enableReadRequestsFallback = false,
     bool useFallbackClient          = true,
     bool useMultipleWriteLocations  = false)
 {
     this.ThrowIfDisposed();
     if (useFallbackClient && this.fallbackClient != null)
     {
         return(new StoreClient(
                    addressResolver,
                    sessionContainer,
                    serviceConfigurationReader,
                    authorizationTokenProvider,
                    this.protocol,
                    // Use the fallback client instead of the default one.
                    this.fallbackClient,
                    enableRequestDiagnostics,
                    enableReadRequestsFallback,
                    useMultipleWriteLocations));
     }
     return(new StoreClient(
                addressResolver,
                sessionContainer,
                serviceConfigurationReader,
                authorizationTokenProvider,
                this.protocol,
                this.transportClient,
                enableRequestDiagnostics,
                enableReadRequestsFallback,
                useMultipleWriteLocations));
 }
 public AuthorizationTokenRequestModifier(IAuthorizationTokenProvider authorizationTokenProvider)
 {
     this.authorizationTokenProvider = authorizationTokenProvider;
 }