Ejemplo n.º 1
0
        public ServiceContext CreateServiceContext(AuthorizationToken authToken, IHubCommunicator hubCommunicator)
        {
            var      tokens            = authToken.Token.Split(new[] { Authenticator.TokenSeparator }, StringSplitOptions.None);
            var      accessToken       = tokens[0];
            var      accessTokenSecret = tokens[1];
            var      companyId         = tokens[2];
            var      expiresAt         = tokens[3];
            DateTime expiresDate;

            if (DateTime.TryParse(expiresAt, out expiresDate) == false)
            {
                //EventManager.TokenValidationFailed(authToken.Token, "Terminal Quickbooks token is invalid");
                throw new ArgumentException("Terminal Quickbooks token is invalid", nameof(expiresAt));
            }
            // Token renew should fit into 151-180 days period,
            // See https://developer.intuit.com/docs/0100_accounting/0060_authentication_and_authorization/connect_from_within_your_app#/manage
            //
            if (DateTime.Now > expiresDate.AddDays(-30) && DateTime.Now <= expiresDate)
            {
                authToken = _authenticator.RefreshAuthToken(authToken).Result;
                var tokenDto = new AuthorizationTokenDTO
                {
                    Id = authToken.Id.ToString(),
                    ExternalAccountId = authToken.ExternalAccountId,
                    Token             = authToken.Token
                };

                hubCommunicator.RenewToken(tokenDto);

                // After token refresh we need to get new accessToken and accessTokenSecret from it
                tokens            = authToken.Token.Split(new[] { Authenticator.TokenSeparator }, StringSplitOptions.None);
                accessToken       = tokens[0];
                accessTokenSecret = tokens[1];
            }

            if (DateTime.Now > expiresDate)
            {
                var message = "Quickbooks token is expired. Please, get the new one";
                //EventManager.TokenValidationFailed(authToken.Token, message);
                throw new TerminalQuickbooksTokenExpiredException(message);
            }

            var oauthValidator = new OAuthRequestValidator(
                accessToken,
                accessTokenSecret,
                Authenticator.ConsumerKey,
                Authenticator.ConsumerSecret);

            return(new ServiceContext(AppToken, companyId, IntuitServicesType.QBO, oauthValidator));
        }
        private async Task <AuthorizationToken> RefreshTokenImpl(AuthorizationToken auth)
        {
            AuthorizationToken result;

            try
            {
                result = await RefreshToken(auth);
            }
            catch
            {
                throw new AuthorizationTokenExpiredOrInvalidException();
            }
            await _hubCommunicator.RenewToken(Mapper.Map <AuthorizationTokenDTO>(result));

            return(result);
        }