private async Task <(bool tokenSuccess, string token)> _TryGetTokenAsync(string accountId)
        {
            bool           tokenSuccess = false;
            string         token        = null;
            DateTimeOffset tokenExpires = default;

            if (_accountTokens.TryGetValue(accountId, out AccountToken cachedToken))
            {
                tokenSuccess = true;
                token        = cachedToken.Token;
                tokenExpires = cachedToken.TokenExpires;
            }

            if (token == null || tokenExpires == default || DateTimeOffset.Now > (tokenExpires - TimeSpan.FromSeconds(30)))
            {
                GraphTokenResult result = await _authProvider.GetTokenAsync(accountId).ConfigureAwait(false);

                tokenSuccess = result.Success;

                if (result.Success)
                {
                    token        = result.AccessToken;
                    tokenExpires = result.Expires;

                    // cache the token
                    _accountTokens[accountId] = new AccountToken(token, tokenExpires);
                }
            }

            return(tokenSuccess, token);
        }
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var auth = request.Headers.Authorization;

            if (auth == null)
            {
                return(await base.SendAsync(request, cancellationToken).ConfigureAwait(false));
            }

            var token = await _tokenProvider.GetTokenAsync(request, cancellationToken);

            request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, token);

            var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

            if (response.StatusCode != HttpStatusCode.Unauthorized)
            {
                return(response);
            }

            token = await _tokenProvider.GetNewTokenAsync(request, cancellationToken).ConfigureAwait(false);

            request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, token);

            return(await base.SendAsync(request, cancellationToken).ConfigureAwait(false));
        }
Beispiel #3
0
        public Task <string> Compress(string url)
        {
            if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
            {
                Response.StatusCode = 400;                                // bad request
                return(Task.FromResult("Error: url should be absolute")); // don't allocate an async state machine if user provided invalid input
            }
            return(CompressInternal());

            async Task <string> CompressInternal()
            {
                string userKey = await _authTokenProvider.GetTokenAsync(HttpContext, createIfNotExists : true).ConfigureAwait(false);

                long     newlyGeneratedLinkId;
                Sequence seq = await _sequenceProvider.GetSequenceAsync("linksSequence_" + userKey).ConfigureAwait(false);

                using (CancellationTokenSource source = new CancellationTokenSource(_defaultTimeout))
                {
                    seq = await seq.GetNextSequenceValue(source.Token).ConfigureAwait(false);

                    newlyGeneratedLinkId = seq.Value;
                }
                string             key    = String.Concat(userKey, "~", _converter.GenerateKey(newlyGeneratedLinkId));
                long               userId = _converter.RegenerateId(userKey);
                ShortenedLinkModel model  = new ShortenedLinkModel {
                    Id = new ShortenedLinkModelId {
                        LinkId = newlyGeneratedLinkId, UserId = userId
                    }, Key = key, Value = url
                };

                using (CancellationTokenSource source = new CancellationTokenSource(_defaultTimeout))
                    await _links.InsertOneAsync(model, _insertOptions, source.Token).ConfigureAwait(false);

                return(model.Key);
            }
        }