/// <summary> /// Logs the user into NVivo Platforms /// </summary> /// <param name="parentControl">The control or form to be set as the owner of the dialog</param> /// <returns>True if authenticated, false if the user cancelled</returns> public bool Login(Control parentControl) { LoginForm loginForm = new LoginForm(); if (loginForm.ShowDialog((parentControl)) == DialogResult.OK) { // Show progress bar while executing the job in it's Shown event handler using (LoginProgressForm progressForm = new LoginProgressForm()) { progressForm.Shown += async(sender, e) => { LoginProgressForm thisForm = (LoginProgressForm)sender; try { myAuth0ServiceProxy = new Auth0ServiceProxy(new RestClientFactory()); myAuth0ServiceProxy.CredentialsChanged += Auth0ServiceProxy_CredentialsChanged; Credentials credentials = await myAuth0ServiceProxy.ExchangeAuthorizationCodeForAccessToken(loginForm.AuthorizationCode); VerifyCloudAccountStateIsActive(credentials.AccessToken); myCredentialStorageService.Credentials = credentials; myCredentialStorageService.Save(); } catch (Exception ex) { // Wrap all other exceptions into AuthenticationException as they are thrown during login process thisForm.Exception = !(ex is AuthenticationException) ? new AuthenticationException(string.Empty, ex) : ex; } finally { thisForm.Close(); } }; progressForm.ShowDialog(parentControl); if (progressForm.Exception != null) { ExceptionDispatchInfo.Capture(progressForm.Exception).Throw(); } } return(true); } if (!String.IsNullOrWhiteSpace(loginForm.ErrorMessage)) { throw new AuthenticationException(loginForm.ErrorMessage); } return(false); }
/// <summary> /// Make use of the stored credentials, if any, to authenticate against the Uluru Cloud services. /// </summary> /// <param name="parentControl">The control or form to be set as the owner for the progress dialog</param> /// <returns>True if authentication was successful, false if no stored credentials or the stored credentials /// are invalid.</returns> public bool AuthenticateFromStoredCredentials(Control parentControl) { // if plugin first started, we don't require CheckIfCredentialsFileExists // as if the file is not there, we will treat that scenario as a new user has not logged in; if (!myIsFirstRun) { // if the data file is deleted in-session, invalidate re-authentication // and letting NVivo handles it if (!myCredentialStorageService.CheckIfCredentialsFileExists()) { return(false); } } else { myIsFirstRun = false; } // if the user has no stored credentials if (!(myCredentialStorageService.Credentials is Credentials credentials)) { return(false); } // Show progress bar while executing the job in it's Shown event handler using (LoginProgressForm progressForm = new LoginProgressForm()) { progressForm.Shown += async(sender, e) => { LoginProgressForm thisForm = (LoginProgressForm)sender; try { // attempt refresh; the call to RefreshAccessToken will update the proxy instance's credential set myAuth0ServiceProxy = new Auth0ServiceProxy(new RestClientFactory(), credentials); myAuth0ServiceProxy.CredentialsChanged += Auth0ServiceProxy_CredentialsChanged; await myAuth0ServiceProxy.RefreshAccessToken(); } catch (Exception ex) { // Pass the exception to the LoginProgressForm thisForm.Exception = ex; myAuth0ServiceProxy = null; } finally { thisForm.Close(); } }; progressForm.ShowDialog(parentControl); // The stored credentials are bad, treat every exception at this stage as an AuthenticationException // and wrap them as the inner exception to rethrow and let NVivo handle error messages if (progressForm.Exception != null) { throw new AuthenticationException("", progressForm.Exception); } return(true); } }