/// <summary> /// Constructs a new Rest instance that will use the given credentials to authenticate calls /// </summary> /// <param name="credentials"></param> /// <param name="userAgentUrl"></param> /// <param name="userAgentVersion"></param> public Rest(Credentials.Credentials credentials, string userAgentUrl, string userAgentVersion) { _credentials = credentials; _userAgent = userAgentUrl; _userAgentVersion = userAgentVersion; Http = new HttpClient(HttpHandler); Http.DefaultRequestHeaders.Add("User-Agent", $"DiscordBot ({_userAgent}, {_userAgentVersion})"); Http.DefaultRequestHeaders.Add("Authorization", _credentials.AuthToken); Channels = new Channels.ChannelRoutes(this); }
// // Updates the fields with the current values // private void UpdateFieldProperties(Credentials.Credentials credentials) { // We use simple credentials Simple simpleCredentials = credentials as Simple; // Just update the values textBox_User.Text = (credentials == null ? string.Empty : simpleCredentials.User); textBox_Password.Text = (credentials == null ? string.Empty : simpleCredentials.Password); // Set the buttons state UpdateAuthenticateButton(); }
// // Starts authenticating against the Jira server // public static Credentials.Credentials Authenticate(Logging.Logging logger) { // Get our data string serverUrl = Server.Names.Url[(int)Server.Names.Type.Jira]; string serverName = Server.Names.Type.Jira.ToString(); logger.Log("Starting authentication"); logger.Log(" * Name: {0}", serverName); logger.Log(" * Url: {0}", serverUrl); // Kick off the RB authentation Dialogs.SimpleAuthentication authDialog = new Dialogs.SimpleAuthentication(serverName, serverUrl, Jira.Authenticate, logger); authDialog.ShowDialog(); // Load in our credentials object Credentials.Credentials rbCredentials = Credentials.Credentials.Create(serverUrl, logger); return(rbCredentials); }
/// <summary> /// Constructs a new Gateway instance using the provided <see cref="Credentials.Credentials"/> /// </summary> /// <param name="credentials"></param> public Gateway(Credentials.Credentials credentials, Configuration config) { Credentials = credentials; Configuration = config; GatewayTokenSource = new CancellationTokenSource(); Http.Gateway.GatewayRoutes.Encoding = config.Encoding; Rest = new Rest(Credentials, config.UserAgentUrl, config.Version); Connector = new Connector(this, Credentials, Configuration); InitializeProxy(config); if (Http.Gateway.GatewayRoutes.Encoding == WebSocketting.WebSocketMessageEncoding.Json) { Connector.OnRawTextMessage += Connector_OnTextMessage; } else { Connector.OnRawBinaryMessage += Connector_OnBinaryMessage; } }
// // Populates any existing credentials // private void PopulateExistingCredentials(string server) { // Load our credentials, since this can take a while do it on another thread BackgroundWorker authThread = new BackgroundWorker(); // Called when we need to trigger the authentication authThread.DoWork += (object objectSender, DoWorkEventArgs args) => { m_logger.Log("Starting generation of Simple credentials"); args.Result = Credentials.Credentials.Create(server, m_logger); }; authThread.RunWorkerCompleted += (object objectSender, RunWorkerCompletedEventArgs args) => { // Set up the default state before we try to load the credentials UpdateAuthenticateDialogState(true); // Try and load in the credentials Credentials.Credentials credentials = null; if (args.Error != null) { m_logger.Log("Exception thrown when trying to load the existing credentials for {0}\n\n{1}\n", server, args.Error.Message); string message = string.Format("Exception thrown when trying to load the existing credentials for {0}\n\nException: {1}\n\nDescription: {2}", server, args.Error.GetType().Name, args.Error.Message); MessageBox.Show(this, message, @"Unable to authenticate", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { credentials = args.Result as Credentials.Credentials; m_logger.Log("Credentials generated"); } // Load our credentials UpdateFieldProperties(credentials); }; // Kick off the request authThread.RunWorkerAsync(); }
// // User wants to clear the credentials // private void button_ClearAuthenitcation_Click(object sender, EventArgs e) { Credentials.Credentials clearedCredentials = Credentials.Credentials.Clear(textBox_Server.Text, m_logger); UpdateFieldProperties(clearedCredentials); }
/// <summary> /// Creates a <see cref="Gateway.Gateway"/> object using the provided <see cref="Credentials.Credentials"/> object. /// This gateway can be used to create a connection with <see cref="Gateway.Gateway.ConnectAsync"/>. /// </summary> /// <param name="credentials"><see cref="Credentials.Credentials"/> object containing authorization information for the gateway API</param> /// <returns></returns> public static Gateway.Gateway CreateGateway(Credentials.Credentials credentials, Configuration config) { return(new Gateway.Gateway(credentials, config)); }
/// <summary> /// Constructs a new gateway connector for the given gateway. /// The connector connects with the provided <see cref="Credentials.Credentials"/> /// </summary> /// <param name="gateway">Gateway instance hosting this connector</param> /// <param name="credentials">Credentials with which to connect to the Discord Gateway API</param> public Connector(Gateway gateway, Credentials.Credentials credentials, Configuration config) { Gateway = gateway; Credentials = credentials; Configuration = config; }