11import sys , os
22from bottle import route , request , response , run , hook , abort , redirect , error , install , auth_basic
33import simplejson as json
4+ import random
45import logging
56import datetime
67import 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+
1639def 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+
2173def 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
0 commit comments