Skip to content

Commit

Permalink
Add support for token authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
skuroda committed Oct 9, 2012
1 parent cc39105 commit f0a5a09
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ Go to the "Packages" directory (`Preferences` / `Browse Packages…`). Then clon

# Options

If you're using OS X and have a keychain entry for github.com, no configuration is needed. Otherwise, edit the settings file (it should open automatically the first time you use a Gist command):
If you're using OS X and have a keychain entry for github.com, no configuration is needed. Otherwise, edit the settings file (it should open automatically the first time you use a Gist command). Note you must specifiy either username AND password or token.

* `"username": ""`

You need to enter your GitHub username here
You can enter your GitHub username here

* `"password": ""`

You need to enter your GitHub password here
You can enter your GitHub password here

* `"token": ""`

You can enter your GitHub token here. To generate a token, see "Generating Access Tokens"

* `"https_proxy": http://user:pass@proxy:port`

Expand Down Expand Up @@ -86,6 +90,23 @@ Use the `Gist` / `Add File To Gist` command to see a list of your Gists. Selecti
* Windows and Linux: "ctrl+shift+alt+g"
* OS X: "ctrl+shift+super+g"

# Generating Access Token
Adapted from [here](https://github.com/bgreenlee/sublime-github#generating-your-own-access-token)
Open up a Terminal window/shell (on OS X, Linux or Cygwin), and run:

curl -u username -d '{"scopes":["gist"]}' https://api.github.com/authorizations

where `username` is your GitHub username. You'll be prompted for your password first. Then you'll get back
a response that includes a 40-digit "token" value (e.g. `6423ba8429a152ff4a7279d1e8f4674029d3ef87`).
Go to Sublime Text 2 -> Preferences -> Package Settings -> Gist -> Settings - User,
and insert the token there. It should look like:

{
"token": "6423ba8429a152ff4a7279d1e8f4674029d3ef87"
}

Restart Sublime.

# Information

Source: https://github.com/condemil/Gist
Expand Down
23 changes: 21 additions & 2 deletions gist.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def __init__(self, code, response):
self.code = code
self.response = response

class MissingTokenException(Exception):
pass

def get_credentials():
username = settings.get('username')
password = settings.get('password')
Expand All @@ -55,6 +58,16 @@ def basic_auth_string():
auth_string = u'%s:%s' % get_credentials()
return auth_string.encode('utf-8')

def get_token():
token = settings.get('token')
if not token:
raise MissingTokenException()
return token

def token_auth_string():
auth_string = u'%s' % get_token()
return auth_string.encode('utf-8')

if sublime.platform() == 'osx':
# Keychain support
# Instead of Gist.sublime-settings, fetch username and password from the user's github.com keychain entry
Expand Down Expand Up @@ -246,7 +259,10 @@ def api_request_native(url, data=None, method=None):
request = urllib2.Request(url)
if method:
request.get_method = lambda: method
request.add_header('Authorization', 'Basic ' + base64.urlsafe_b64encode(basic_auth_string()))
try:
request.add_header('Authorization', 'token ' + token_auth_string())
except MissingTokenException:
request.add_header('Authorization', 'Basic ' + base64.urlsafe_b64encode(basic_auth_string()))
request.add_header('Accept', 'application/json')
request.add_header('Content-Type', 'application/json')

Expand Down Expand Up @@ -329,7 +345,10 @@ def mode(self):

@catch_errors
def run(self, edit):
get_credentials()
try:
get_token()
except MissingTokenException:
get_credentials()
regions = [region for region in self.view.sel() if not region.empty()]

if len(regions) == 0:
Expand Down

0 comments on commit f0a5a09

Please sign in to comment.