Clientside GitHub Authentication

2020-07-11 09:28发布

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?

2条回答
孤傲高冷的网名
2楼-- · 2020-07-11 10:05

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.

查看更多
乱世女痞
3楼-- · 2020-07-11 10:08

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

查看更多
登录 后发表回答