public ITranslationProvider CreateTranslationProvider( Uri translationProviderUri, string translationProviderState, ITranslationProviderCredentialStore credentialStore) { Log.Logger.Info("Attempting to create a new translation provider with URI: {0}", translationProviderUri); if (!SupportsTranslationProviderUri(translationProviderUri)) { Log.Logger.Error("Cannot handle URI {0}.", translationProviderUri); throw new Exception("Cannot handle URI."); } var credentials = credentialStore.GetCredential(translationProviderUri); if (credentials == null) { return(new TranslationProvider(new TranslationOptions())); } var options = JsonConvert.DeserializeObject <TranslationOptions>(translationProviderState); var genericCredentials = new GenericCredentials(credentials.Credential); if (options.UseBasicAuthentication) { options.ApiToken = SDLMTEdgeTranslatorHelper.GetAuthToken(options, genericCredentials); } else { options.ApiToken = genericCredentials["API-Key"]; SDLMTEdgeTranslatorHelper.VerifyBasicAPIToken(options, genericCredentials); } return(new TranslationProvider(options)); }
private bool AuthenticateCredentials(GenericCredentials credentials, bool showAlertOnFailure = true) { // Expire the old LPs in case we've changed the host and reset the API version SDLMTEdgeTranslatorHelper.ExpireLanguagePairs(); Options.ApiVersion = APIVersion.Unknown; string token; try { if (Options.UseBasicAuthentication) { token = SDLMTEdgeTranslatorHelper.GetAuthToken(Options, credentials); } else { token = APIKeyField.Text; SDLMTEdgeTranslatorHelper.VerifyBasicAPIToken(Options, credentials); } } catch (Exception e) { _logger.Error($"{Constants.AuthenticateCredentials}: {e.Message}\n {e.StackTrace}"); if (showAlertOnFailure) { DialogResult = DialogResult.None; MessageBox.Show(e.Message, PluginResources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } return(false); } Options.ApiToken = token; if (tabControl != null) { try { //avoiding to do binding when the window will be closed. Because removing and adding the tab will do the bind again. inRemoveLanguagesPreferencesTab = true; tabControl.Controls.RemoveByKey("LPTab"); if (Options?.ApiVersion == APIVersion.v2) { tabControl.Invoke(new Action(() => { if (LPTab != null) { tabControl.Controls.Add(LPTab); } })); } } finally { inRemoveLanguagesPreferencesTab = false; } } return(true); }
private bool AuthenticateCredentials(GenericCredentials credentials, bool showAlertOnFailure = true) { // Expire the old LPs in case we've changed the host and reset the API version SDLMTEdgeTranslatorHelper.ExpireLanguagePairs(); Options.ApiVersion = APIVersion.Unknown; string token; try { if (Options.UseBasicAuthentication) { token = SDLMTEdgeTranslatorHelper.GetAuthToken(Options, credentials); } else { token = APIKeyField.Text; SDLMTEdgeTranslatorHelper.VerifyBasicAPIToken(Options, credentials); } } catch (Exception e) { Log.Logger.Error($"{Constants.AuthenticateCredentials}: {e.Message}\n {e.StackTrace}"); if (showAlertOnFailure) { DialogResult = DialogResult.None; if (Environment.UserInteractive) { MessageBox.Show(e.Message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } return(false); } Options.ApiToken = token; if (tabControl != null) { tabControl.Controls.RemoveByKey("LPTab"); if (Options?.ApiVersion == APIVersion.v2) { tabControl.Invoke(new Action(() => { if (LPTab != null) { tabControl.Controls.Add(LPTab); } })); } } return(true); }
public void MtEdgeApi_VerifyAPIKey_NoExceptionThrown() { var credentials = new GenericCredentials(null, null); credentials["API-Key"] = apiKeyTranslationOptions.ApiToken; credentials["UseApiKey"] = "true"; try { SDLMTEdgeTranslatorHelper.VerifyBasicAPIToken(apiKeyTranslationOptions, credentials); } catch (Exception ex) { Assert.Fail("Expected no exception, but got: " + ex.Message); } }