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)); }
public async Task <AuthorizationTokenDTO> GenerateInternalOAuthToken(CredentialsDTO curCredentials) { try { var authToken = await ObtainAuthToken(curCredentials); if (authToken == null) { return(new AuthorizationTokenDTO() { Error = "Unable to authenticate in DocuSign service, invalid login name or password." }); } var authorizationTokenDTO = new AuthorizationTokenDTO() { Token = JsonConvert.SerializeObject(authToken), ExternalAccountId = curCredentials.Username, AuthCompletedNotificationRequired = true }; string demoAccountStr = string.Empty; if (curCredentials.IsDemoAccount) { demoAccountStr = CloudConfigurationManager.GetSetting("DemoAccountAttributeString"); } if (authorizationTokenDTO.AdditionalAttributes == null) { authorizationTokenDTO.AdditionalAttributes = demoAccountStr; } else if (!authorizationTokenDTO.AdditionalAttributes.Contains(demoAccountStr)) { authorizationTokenDTO.AdditionalAttributes += demoAccountStr; } return(authorizationTokenDTO); } catch (Exception ex) { await _loggerService.ReportTerminalError(ex, curCredentials.Fr8UserId); return(new AuthorizationTokenDTO() { Error = "An error occurred while trying to authorize, please try again later." }); } }
/// <summary> /// Not all fields of token will be replaced in database! /// </summary> /// <param name="token"></param> public void RenewToken(AuthorizationTokenDTO token) { using (var uow = ObjectFactory.GetInstance <IUnitOfWork>()) { var authToken = uow.AuthorizationTokenRepository.FindTokenById(Guid.Parse(token.Id)); if (authToken == null) { return; } authToken.ExternalAccountId = token.ExternalAccountId; authToken.Token = token.Token; authToken.ExpiresAt = token.ExpiresAt; authToken.AdditionalAttributes = token.AdditionalAttributes; uow.SaveChanges(); } }
public static async Task <AuthorizationTokenDTO> DocuSign_AuthToken(BaseIntegrationTest integrationTest) { if (DocuSignToken == null) { var creds = new CredentialsDTO() { Username = "******", Password = "******", IsDemoAccount = true }; string endpoint = integrationTest.GetTerminalUrl() + "/authentication/token"; var jobject = await integrationTest.HttpPostAsync <CredentialsDTO, JObject>(endpoint, creds); DocuSignToken = JsonConvert.DeserializeObject <AuthorizationTokenDTO>(jobject.ToString()); Assert.IsTrue(string.IsNullOrEmpty(DocuSignToken.Error)); } return(DocuSignToken); }
/// <summary> /// Checks if google token is invalid. /// </summary> /// <param name="authTokenDO"></param> /// <returns></returns> protected override async Task <bool> CheckAuthentication() { if (!await base.CheckAuthentication()) { return(false); } var token = GetGoogleAuthToken(); // Post token to google api to check its validity // Variable needs for more readability. var result = Task.Run(async() => await _googleIntegration.IsTokenInfoValid(token)).Result; if (result == false) { // Tries to refresh token. If refresh is successful, updates current token silently try { var newToken = _googleIntegration.RefreshToken(token); var tokenDTO = new AuthorizationTokenDTO { Id = AuthorizationToken.Id, ExternalAccountId = AuthorizationToken.ExternalAccountId, Token = JsonConvert.SerializeObject(newToken) }; AuthorizationToken.Token = tokenDTO.Token; await HubCommunicator.RenewToken(tokenDTO); return(true); } catch (Exception exception) { var message = "Token is invalid and refresh failed with exception: " + exception.Message; Logger.GetLogger().Error(message); return(false); } } return(true); }
private static async Task PostToTerminalEventsEndPoint(string userId, TerminalDO authenticatedTerminal, AuthorizationTokenDTO authToken) { var restClient = ObjectFactory.GetInstance <IRestfulServiceClient>(); var terminalService = ObjectFactory.GetInstance <ITerminal>(); var headers = terminalService.GetRequestHeaders(authenticatedTerminal, userId); await restClient.PostAsync <object>( new Uri(authenticatedTerminal.Endpoint + "/terminals/" + authenticatedTerminal.Name + "/events"), new { fr8_user_id = userId, auth_token = authToken }, null, headers); }
public Task RenewToken(AuthorizationTokenDTO token) { return(Task.FromResult(0)); }
public IHttpActionResult RenewToken([FromBody] AuthorizationTokenDTO token) { _authorization.RenewToken(token); return(Ok()); }
public async Task RenewToken(AuthorizationTokenDTO token) { var url = $"{GetHubUrlWithApiVersion()}/authentication/renewtoken"; var uri = new Uri(url); await _restfulServiceClient.PostAsync(uri, token, null, await GetHMACHeader(uri, token)); }
public static void TerminalAuthenticationCompleted(string userId, TerminalDO authenticatedTerminal, AuthorizationTokenDTO authToken) { var handler = EventAuthenticationCompleted; if (handler != null) { handler(userId, authenticatedTerminal, authToken); } }
public Task RenewToken(AuthorizationTokenDTO token) { throw new NotImplementedException("Terminals can't communicate with an unknown hub"); }