Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Give up on CORS POST for github.

  • Loading branch information...
commit 99a2cbb9c337bba32ec91fde782684e10c1db5ed 1 parent 8142677
@jdm authored
View
30 cgi-bin/githubapi.cgi
@@ -8,6 +8,9 @@ from StringIO import StringIO
import gzip
import ConfigParser
import base64
+import urllib
+import logging
+import sys
cgitb.enable()
print "Content-Type: text/html;charset=utf-8"
@@ -16,16 +19,8 @@ print
form = cgi.FieldStorage()
url = urllib2.unquote(form.getfirst('url', ''))
-#data = {}
-#parts = url.split('?')
-#if len(parts) > 1:
-# params = parts[1].split('&')
-# for param in params:
-# param_data = param.split('=')
-# data[param_data[0]] = param_data[1]
-
config = ConfigParser.RawConfigParser()
-config.read('config')
+config.read('./config')
try:
user = config.get('github', 'username')
passw = config.get('github', 'password')
@@ -33,13 +28,22 @@ except ConfigParser.NoSectionError:
user = None
passw = None
-req = urllib2.Request('https://api.github.com/%s' % url, None, {'Accept': 'application/vnd.github.raw',
- 'Accept-Encoding': 'gzip'})
-if user and passw:
+token = form.getfirst('access_token', None)
+data = form.getfirst('data', None)
+method = form.getfirst('method', 'GET')
+
+opener = urllib2.build_opener(urllib2.HTTPHandler)
+req = urllib2.Request('https://api.github.com/%s' % url, data, {'Accept': 'application/vnd.github.raw',
+ 'Accept-Encoding': 'gzip',
+ 'Content-Type': 'application/x-www-form-urlencoded'})
+req.get_method = lambda: method
+if token:
+ req.add_header("Authorization", "token %s" % token)
+elif user and passw:
base64string = base64.standard_b64encode('%s:%s' % (user, passw)).replace('\n', '')
req.add_header("Authorization", "Basic %s" % base64string)
-f = urllib2.urlopen(req)
+f = opener.open(req)
if f.info().get('Content-Encoding') == 'gzip':
buf = StringIO(f.read())
f = gzip.GzipFile(fileobj=buf)
View
5 cgi-bin/oauth_redirect.cgi
@@ -5,6 +5,7 @@ import cgitb
import cgi
import urllib2
import ConfigParser
+import os
cgitb.enable()
form = cgi.FieldStorage()
@@ -12,7 +13,7 @@ url = urllib2.unquote(form.getfirst('url', ''))
code = form.getfirst('code', '')
config = ConfigParser.RawConfigParser()
-config.read('config')
+config.read('./config')
id = config.get('github', 'app_id')
secret = config.get('github', 'app_secret')
@@ -20,7 +21,7 @@ data = {'client_id': id, 'redirect_uri': url, 'client_secret': secret, 'code': c
req = urllib2.Request('https://github.com/login/oauth/access_token?' +
'client_id=%s&redirect_uri=%s&client_secret=%s&code=%s' %
- (id, url, secret, code))
+ (id, 'http://%s%s?url=%s' % (os.environ['SERVER_NAME'], os.environ['REQUEST_URI'], url), secret, code))
f = urllib2.urlopen(req)
results = f.read()
View
25 taskmanagement.js
@@ -14,7 +14,7 @@ if (!check_query('access_token')) {
path = path.substr(0, path.lastIndexOf('/') + 1);
path += 'cgi-bin/oauth_redirect.cgi?url=' + encodeURIComponent(window.location.protocol + '//' + window.location.host + window.location.pathname);
redirect = window.location.protocol + '//' + window.location.host + path;
- clientID = location.hostname == "localhost" ? "73fd92744901e18828bb" : "7b9eaa72a4f18972fea5";
+ clientID = location.hostname == "localhost" ? "73fd92744901e18828bb" : "014a8157d5f002c9a3f7";
window.location = "https://github.com/login/oauth/authorize?scope=public_repo&client_id=" + clientID + "&redirect_uri=" + redirect;
}
@@ -23,20 +23,11 @@ window.onload = function() {
};
function _request(method, url, data, success, error) {
- data['access_token'] = githubToken;
- $.ajax({type: method,
- url: url,
- data: data,
- beforeSend: function(xhr) {
- xhr.setRequestHeader("Authorization", "token " + githubToken);
- xhr.setRequestHeader("Accept", 'application/vnd.github.raw');
- },
- success: function(response) { callback(JSON.parse(response)); },
- error: error(),
- /*contentType: 'application/json',*/
- contentType: 'application/x-www-form-urlencoded',
- dataType: 'json'
- });
+ $.getJSON('cgi-bin/githubapi.cgi?url=' + encodeURIComponent(url) +
+ '&access_token=' + githubToken +
+ '&data=' + JSON.stringify(data) +
+ '&method=' + method,
+ success, error);
}
/* success is a function that is passed the API response data specified at
@@ -48,7 +39,7 @@ function submitTask(summary, body, success, error) {
body: body,
labels: [config['tag']]
};
- _request('POST', 'https://api.github.com/repos/' + config['owner'] + '/' + config['repo'] + '/issues',
+ _request('POST', 'repos/' + config['owner'] + '/' + config['repo'] + '/issues',
data, success, error || function() { console.log('Error creating task'); });
}
@@ -61,6 +52,6 @@ function editTask(id, summary, body, success, error) {
body: body,
labels: [config['tag']]
};
- _request('PATCH', 'https://api.github.com/repos/' + config['owner'] + '/' + config['repo'] + '/issues/' + id,
+ _request('PATCH', 'repos/' + config['owner'] + '/' + config['repo'] + '/issues/' + id,
data, success, error || function() { console.log('Error editing task'); });
}
Please sign in to comment.
Something went wrong with that request. Please try again.