Skip to content

Commit

Permalink
Update GitHub authorization method
Browse files Browse the repository at this point in the history
GitHub deprecated the use of query parameter for authentication header. The new way is passing it via HTTP header.

See https://developer.github.com/changes/2020-02-10-deprecating-auth-through-query-param/

Fixes #12
  • Loading branch information
jrbasso committed May 6, 2021
1 parent 80b0dbe commit 690267b
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/github.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
#include <jansson.h>
#include <curl/curl.h>

#define GITHUB_GIST_URL "https://api.github.com/gists?access_token=%s"
#define ENTERPRISE_GITHUB_GIST_URL "%s/gists?access_token=%s"

#define GITHUB_GIST_URL_ANONYMOUS "https://api.github.com/gists"
#define GITHUB_GIST_URL "https://api.github.com/gists"
#define ENTERPRISE_GITHUB_GIST_URL "%s/gists"

#define ENV_ACCESS_TOKEN_KEY "GISTIT_TOKEN"
#define ENV_GITHUB_API_URL "GISTIT_API_URL"
Expand All @@ -15,7 +13,7 @@ struct github_response *github_submit(json_t *content)
{
CURL *curl;
CURLcode res;
char url[100], userAgent[40], *token, *api_url;
char url[100], userAgent[40], authorizationHeader[100], *token, *api_url;
struct github_response *response = NULL;
struct curl_slist *headers = NULL;
long code;
Expand All @@ -31,16 +29,18 @@ struct github_response *github_submit(json_t *content)
sprintf(userAgent, "User-Agent: %s/%s", PACKAGE_NAME, PACKAGE_VERSION);
headers = curl_slist_append(headers, userAgent);
api_url = getenv(ENV_GITHUB_API_URL);
if (api_url != NULL) {
snprintf(url, sizeof(url), ENTERPRISE_GITHUB_GIST_URL, api_url);
} else {
sprintf(url, GITHUB_GIST_URL);
}

token = getenv(ENV_ACCESS_TOKEN_KEY);
if (token != NULL) {
if (api_url == NULL) {
sprintf(url, GITHUB_GIST_URL, token);
} else {
sprintf(url, ENTERPRISE_GITHUB_GIST_URL, api_url, token);
}
} else {
sprintf(url, GITHUB_GIST_URL_ANONYMOUS);
snprintf(authorizationHeader, sizeof(authorizationHeader), "Authorization: token %s", token);
headers = curl_slist_append(headers, authorizationHeader);
}

curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
Expand Down

0 comments on commit 690267b

Please sign in to comment.