Skip to content
This repository was archived by the owner on Sep 22, 2020. It is now read-only.

Commit af31728

Browse files
basic auth working for orga membership and github users. jwt token implemented. token expires after 4hours
1 parent a98d512 commit af31728

4 files changed

Lines changed: 83 additions & 11 deletions

File tree

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,16 @@ cd github-pages-basic-auth-proxy
8888
sudo python3 setup.py install
8989
```
9090

91-
run in background on Port 8881
91+
Run Proxy
92+
93+
* proxy that allows only members of the organization to access page: (owner must be an GitHub Organization)
9294

9395
```
94-
cs-gh-proxy -e wsgi -u csgruenebe -gh https://comsysto.github.io/github-pages-basic-auth-proxy/086e41eb6ff7a50ad33ad742dbaa2e70b75740c4950fd5bbbdc71981e6fe88e3/ &
96+
$> cs-gh-proxy -e wsgi -p 8881 --authType onlyGitHubOrgUsers --owner comsysto --repository github-pages-basic-auth-proxy --obfuscator 086e41eb6ff7a50ad33ad742dbaa2e70b75740c4950fd5bbbdc71981e6fe88e3
9597
```
9698

99+
* proxy that allows all GitHub Users to access page: (owner can be GitHub Organization or normal user)
100+
101+
```
102+
$> cs-gh-proxy -e wsgi -p 8881 --authType allGitHubUsers --owner comsysto --repository github-pages-basic-auth-proxy --obfuscator 086e41eb6ff7a50ad33ad742dbaa2e70b75740c4950fd5bbbdc71981e6fe88e3
103+
```

cs_proxy/proxy.py

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import sys, os
22
from bottle import route, request, response, run, hook, abort, redirect, error, install, auth_basic
33
import simplejson as json
4+
import random
45
import logging
56
import datetime
67
import requests
8+
from requests.auth import HTTPBasicAuth
9+
from jose import jwt
10+
from jose.exceptions import JWSError
11+
import datetime
712

13+
#
14+
# global vars
15+
#
16+
owner = 0
17+
auth_type = 0
18+
jwt_secret = "%032x" % random.getrandbits(128)
819

920
#
1021
# HELPERS
@@ -13,11 +24,52 @@ def return_json(object, response):
1324
response.set_header('Content-Type', 'application/json')
1425
return json.dumps(object)
1526

27+
def create_jwt_token():
28+
return jwt.encode({'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=4)}, jwt_secret, algorithm='HS256')
29+
30+
31+
def valid_jwt_token(token):
32+
try:
33+
res = jwt.decode(token, jwt_secret, algorithms=['HS256'])
34+
print (res)
35+
return True
36+
except JWSError:
37+
return False
38+
1639
def check_pass(username, password):
40+
# FIXME: REMOVE ME IN PRODUCTION (JUST FOR DEMO)
1741
if username == 'bob' and password == '5678':
1842
return True
43+
#
44+
# First check if already valid JWT Token in Cookie
45+
#
46+
auth_cookie = request.get_cookie("cs-proxy-auth")
47+
if auth_cookie and valid_jwt_token(auth_cookie):
48+
print ('PROXY-AUTH: found valid JWT Token in cookie')
49+
return True
50+
51+
#
52+
# GitHub Basic Auth - also working with username + personal_access_token
53+
#
54+
print ('PROXY-AUTH: doing github basic auth - authType: {0}, owner: {1}'.format(auth_type, owner))
55+
basic_auth = HTTPBasicAuth(username, password)
56+
auth_response = requests.get('https://api.github.com/user', auth=basic_auth)
57+
if auth_response.status_code == 200:
58+
if auth_type == 'onlyGitHubOrgUsers':
59+
print ('PROXY-AUTH: doing org membership request')
60+
org_membership_response = requests.get('https://api.github.com/user/orgs', auth=basic_auth)
61+
if org_membership_response.status_code == 200:
62+
for org in org_membership_response.json():
63+
if org['login'] == owner:
64+
response.set_cookie("cs-proxy-auth", create_jwt_token())
65+
return True
66+
return False
67+
else:
68+
response.set_cookie("cs-proxy-auth", create_jwt_token())
69+
return True
1970
return False
2071

72+
2173
def normalize_proxy_url(url):
2274
print ('URL:')
2375
print (url)
@@ -57,23 +109,28 @@ def error500(error):
57109
def hello():
58110
return 'ok'
59111

112+
#
113+
# make args available in auth callback
114+
#
115+
global owner, auth_type
116+
owner = args.owner
117+
auth_type = args.authType
60118

61119
@route('/<url:re:.+>')
62120
@auth_basic(check_pass)
63121
def proxy_trough(url):
64-
return proxy_trough_helper('{0}{1}'.format(args.githubPagesUrl, normalize_proxy_url(url)))
122+
return proxy_trough_helper('https://{0}.github.io/{1}/{2}/{3}'.format(args.owner, args.repository, args.obfuscator, normalize_proxy_url(url)))
65123

66124
@route('/')
67125
@auth_basic(check_pass)
68126
def proxy_trough_root_page():
69-
return proxy_trough_helper('{0}{1}'.format(args.githubPagesUrl, '/index.html'))
70-
127+
return proxy_trough_helper('https://{0}.github.io/{1}/{2}/{3}'.format(args.owner, args.repository, args.obfuscator, '/index.html'))
71128

72129
#
73130
# RUN BY ENVIRONMENT
74131
#
75132
if args.environment == 'wsgi':
76-
run(host='localhost', port=8881, debug=True)
133+
run(host='localhost', port=args.port, debug=True)
77134
else:
78135
run(server='cgi')
79136

cs_proxy/run_proxy.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ def main():
1212
parser = argparse.ArgumentParser(description='comSysto GitHub Pages Auth Basic Proxy')
1313

1414
parser.add_argument("-e", "--environment", help='Which environment.', choices=['cgi', 'wsgi'])
15-
parser.add_argument("-gh", "--githubPagesUrl", help='baseUrl to gh-pages page e.g. https://foo.github.io/repo/2323/')
16-
parser.add_argument("-u", "--allowedUsers", help='allowed usernames.')
15+
parser.add_argument("-gho", "--owner", help='the owner of the repository. Either organizationname or username.')
16+
parser.add_argument("-ghr", "--repository", help='the repository name.')
17+
parser.add_argument("-obf", "--obfuscator", help='the subfolder-name in gh-pages branch used as obfuscator')
18+
parser.add_argument("-p", "--port", help='the port to run proxy e.g. 8881')
19+
parser.add_argument("-a", "--authType", help='how should users auth.', choices=['allGitHubUsers', 'onlyGitHubOrgUsers'] )
20+
1721

1822
args = parser.parse_args()
1923
if not args.environment:
2024
print ('USAGE')
21-
print (' proxy:')
22-
print (' $> cs-gh-proxy -e wsgi -u csgruenebe -gh https://comsysto.github.io/github-pages-basic-auth-proxy/086e41eb6ff7a50ad33ad742dbaa2e70b75740c4950fd5bbbdc71981e6fe88e3/ ')
25+
print (' proxy that allows only members of the organization to access page: (owner must be an GitHub Organization)')
26+
print (' $> cs-gh-proxy -e wsgi -p 8881 --authType onlyGitHubOrgUsers --owner comsysto --repository github-pages-basic-auth-proxy --obfuscator 086e41eb6ff7a50ad33ad742dbaa2e70b75740c4950fd5bbbdc71981e6fe88e3')
27+
print ('')
28+
print (' proxy that allows all GitHub Users to access page: (owner can be GitHub Organization or normal user)')
29+
print (' $> cs-gh-proxy -e wsgi -p 8881 --authType allGitHubUsers --owner comsysto --repository github-pages-basic-auth-proxy --obfuscator 086e41eb6ff7a50ad33ad742dbaa2e70b75740c4950fd5bbbdc71981e6fe88e3')
2330
print ('')
2431

2532
sys.exit(1)

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
'validators',
1717
'colorama',
1818
'bottle',
19-
'simplejson'
19+
'simplejson',
20+
'python-jose'
2021
],
2122
zip_safe=False,
2223
entry_points = {

0 commit comments

Comments
 (0)