Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for token authentication

  • Loading branch information...
commit f0a5a090fde4ea69fe0ea976f8ae54be2f54148e 1 parent cc39105
@skuroda skuroda authored
Showing with 45 additions and 5 deletions.
  1. +24 −3 README.md
  2. +21 −2 gist.py
View
27 README.md
@@ -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`
@@ -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
View
23 gist.py
@@ -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')
@@ -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
@@ -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')
@@ -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:
Please sign in to comment.
Something went wrong with that request. Please try again.