Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clientside GitHub Authentication

I'm using a Javascript to do Basic Authentication with GitHub. For example, the following shell command gets a token from Github:

    curl -i -u uaername:password -k -d "{\"scopes\": [\"repo\"]}" https://api.github.com/authorizations

How do you achieve that with jQuery and AJAX?

like image 449
Nick Papamanolioudakis Avatar asked Aug 08 '13 11:08

Nick Papamanolioudakis


People also ask

How do I authenticate a GitHub request?

You can authenticate your request by adding a token. If you want to use the GitHub REST API for personal use, you can create a personal access token (PAT). The REST API operations used in this article require repo scope for personal access tokens. Other operations may require different scopes.

What is authorization callback URL in GitHub?

Set Authorization callback URL: Enter https://{$RED_HAT_QUAY_URL}/oauth2/github/callback as the Authorization callback URL.

How do I find my OAuth client ID GitHub?

Under your organization name, click Settings. In the developer settings sidebar, click OAuth Apps. On the OAuth Apps screen, click Register an application or New OAuth App. Give your application a name, such as "PingFederate Provisioning".


2 Answers

Including Basic Auth Data in HTTP Headers with jQuery

You can include basic auth details in the header using the Authorization field. You already understand how jQuery works. This snippet has the bits you're missing:

    let auth = btoa(username + ":" + password);

    jQuery.ajax({
        url: ...,
        headers: { Authorization: "Basic " + auth }
        ...
    });

Note: btoa and atob (pronounced B to A and A to B) are builtin functions, and convert to and from Base64. See the MDN docs for more information.

like image 90
Carl Smith Avatar answered Oct 18 '22 19:10

Carl Smith


Are you asking whether there is a way to get an oAuth token purely from the client side? If so, the answer is no.

But, you have some work arounds.

Github.js: https://github.com/michael/github

Gatekeeper is an open source server side component which can help with oAuth tokens management:

https://github.com/prose/gatekeeper

You could also use something like Firebase with simple login and in this case you don't need to manage any server side services:

https://www.firebase.com/docs/security/simple-login-github.html

like image 32
xrd Avatar answered Oct 18 '22 19:10

xrd