diff --git a/client_secret.json b/client_secret.json new file mode 100644 index 0000000..2104b71 --- /dev/null +++ b/client_secret.json @@ -0,0 +1 @@ +{"web":{"client_id":"377787187748-shuvi4iq5bi4gdet6q3ioataimobs4lh.apps.googleusercontent.com","project_id":"calendarwatch-1584185874753","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"Hu_YWmKsVKUcLwyeINYzdKfZ","redirect_uris":["http://localhost:1234"],"javascript_origins":["http://raphael.maenle.net","https://raphael.maenle.net"]}} diff --git a/index.html b/index.html new file mode 100644 index 0000000..0aab441 --- /dev/null +++ b/index.html @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + +
+ + + + diff --git a/index.js b/index.js new file mode 100644 index 0000000..f2e88d7 --- /dev/null +++ b/index.js @@ -0,0 +1,8 @@ +const express = require('express') +const path = require('path') +const PORT = process.env.PORT || 1234 + +express() + .use(express.static(path.join(__dirname,'public'))) + .get('/', (req, res) => res.render('index')) + .listen(PORT, () => console.log(`Listening on ${ PORT }`)) diff --git a/website.py b/website.py new file mode 100644 index 0000000..b62478a --- /dev/null +++ b/website.py @@ -0,0 +1,80 @@ +from google.oauth2 import id_token +from google.auth.transport import requests + +from http.server import HTTPServer, SimpleHTTPRequestHandler, BaseHTTPRequestHandler +import socketserver +import logging +import json + +Handler = SimpleHTTPRequestHandler + +class S(BaseHTTPRequestHandler): + def _set_headers(self): + self.send_response(200) + self.send_header('Content-type', 'text/html') + self.end_headers() + + def do_GET(self): + self._set_headers() + f = open("index.html", "r") + self.wfile.write(f.read().encode('utf-8')) + + def do_HEAD(self): + self._set_headers() + + def do_POST(self): + self._set_headers() + print("in post method") + self.data_string = self.rfile.read(int(self.headers['Content-Length'])) + print('checking client id') + checkClientId(self.data_string) + self.send_response(200) + self.end_headers() + self.wfile.write("Hello".encode('utf-8')) + return + +def run(server_class=HTTPServer, handler_class=S, port=1234): + logging.basicConfig(level=logging.INFO) + server_address = ('', port) + with socketserver.TCPServer(("", port), handler_class) as httpd: + print("serving at port", port) + httpd.serve_forever() + + +# (Receive token by HTTPS POST) +def checkClientId(token): + try: + + with open('client_secret.json', 'r') as json_file: + clientSecret = json.load(json_file) + CLIENT_ID = clientSecret["web"]["client_id"] + # Specify the CLIENT_ID of the app that accesses the backend: + idinfo = id_token.verify_oauth2_token(token, requests.Request(), CLIENT_ID) + + # Or, if multiple clients access the backend server: + # idinfo = id_token.verify_oauth2_token(token, requests.Request()) + # if idinfo['aud'] not in [CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]: + # raise ValueError('Could not verify audience.') + + if idinfo['iss'] not in ['accounts.google.com', 'https://accounts.google.com']: + raise ValueError('Wrong issuer.') + + # If auth request is from a G Suite domain: + # if idinfo['hd'] != GSUITE_DOMAIN_NAME: + # raise ValueError('Wrong hosted domain.') + + # ID token is valid. Get the user's Google Account ID from the decoded token. + userid = idinfo['sub'] + print(f"valid user id: {userid}") + except ValueError: + # ID token is invalid + print('invalid token') + pass + +if __name__ == '__main__': + from sys import argv + + if len(argv) == 2: + run(port=int(argv[1])) + else: + run() \ No newline at end of file