Update: Using another way to authenticate github module (#2029)

The github module only authenticate by query string, and this method is deprecated:
https://developer.github.com/changes/2019-11-05-deprecated-passwords-and-authorizations-api/#authenticating-using-query-parameters


There is no reason to remove it before the method stop working, so I've made possible to the user choose which authentication method he will use:

* The parameter token remain unchanged.
* If the parameter user is passed then the module will use the not deprecated method, passing user and token on the body of the requisition. Otherwise the module will use the deprecated method.

Co-authored-by: Lucas <araujo.lucasvale@gmail.com>

Fixes #2002
This commit is contained in:
Lucas Araújo 2020-03-26 08:50:42 -03:00 committed by GitHub
parent 15e79b09d3
commit 15496bfb4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 7 deletions

View File

@ -18,7 +18,6 @@ namespace modules {
bool build(builder* builder, const string& tag) const;
string get_format() const;
private:
void update_label(int);
int get_number_of_notification();
@ -30,11 +29,12 @@ namespace modules {
label_t m_label{};
label_t m_label_offline{};
string m_api_url;
string m_user;
string m_accesstoken{};
unique_ptr<http_downloader> m_http{};
bool m_empty_notifications{false};
std::atomic<bool> m_offline{false};
};
}
} // namespace modules
POLYBAR_NS_END

View File

@ -10,7 +10,7 @@ class http_downloader {
http_downloader(int connection_timeout = 5);
~http_downloader();
string get(const string& url);
string get(const string& url, const string& user = "", const string& password = "");
long response_code();
protected:
@ -25,6 +25,6 @@ namespace http_util {
decltype(auto) make_downloader(Args&&... args) {
return factory_util::unique<http_downloader>(forward<Args>(args)...);
}
}
} // namespace http_util
POLYBAR_NS_END

View File

@ -17,6 +17,7 @@ namespace modules {
github_module::github_module(const bar_settings& bar, string name_)
: timer_module<github_module>(bar, move(name_)), m_http(http_util::make_downloader()) {
m_accesstoken = m_conf.get(name(), "token");
m_user = m_conf.get(name(), "user", ""s);
m_api_url = m_conf.get(name(), "api-url", "https://api.github.com/"s);
if (m_api_url.back() != '/') {
m_api_url += '/';
@ -52,7 +53,12 @@ namespace modules {
}
string github_module::request() {
string content{m_http->get(m_api_url + "notifications?access_token=" + m_accesstoken)};
string content;
if (m_user.empty()) {
content = m_http->get(m_api_url + "notifications?access_token=" + m_accesstoken);
} else {
content = m_http->get(m_api_url + "notifications", m_user, m_accesstoken);
}
long response_code{m_http->response_code()};
switch (response_code) {

View File

@ -1,10 +1,12 @@
#include "utils/http.hpp"
#include <curl/curl.h>
#include <curl/easy.h>
#include <sstream>
#include "errors.hpp"
#include "settings.hpp"
#include "utils/http.hpp"
POLYBAR_NS
@ -23,10 +25,16 @@ http_downloader::~http_downloader() {
curl_easy_cleanup(m_curl);
}
string http_downloader::get(const string& url) {
string http_downloader::get(const string& url, const string& user, const string& password) {
std::stringstream out{};
curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &out);
if (!user.empty()) {
curl_easy_setopt(m_curl, CURLOPT_USERNAME, user.c_str());
}
if (!password.empty()) {
curl_easy_setopt(m_curl, CURLOPT_PASSWORD, password.c_str());
}
auto res = curl_easy_perform(m_curl);
if (res != CURLE_OK) {