def make_refresh_post(refresh_token): client = Client( token_endpoint='https://accounts.google.com/o/oauth2/token', resource_endpoint='https://www.googleapis.com/oauth2/v1', client_id=CLIENT_ID, client_secret=CLIENT_SECRET) params = {"grant_type": "refresh_token"} client.request_token(refresh_token=refresh_token, **params) return client.access_token, client.expires_in
def _make_code_post(code): client = Client( token_endpoint='https://accounts.google.com/o/oauth2/token', resource_endpoint='https://www.googleapis.com/oauth2/v1', client_id=CLIENT_ID, client_secret=CLIENT_SECRET) params = {"redirect_uri": REDIRECT_URI} client.request_token(code=code, **params) return client.access_token, client.refresh_token, client.expires_in
def authenticate(force=False): """ Returns an oauth token that can be passed to the server for identification. """ if not force: try: cur_time = int(time.time()) access_token, expires_at, refresh_token = get_storage() if cur_time < expires_at - 10: return access_token access_token, expires_in = make_refresh_post(refresh_token) update_storage(access_token, expires_in, refresh_token) return access_token except IOError as _: print('Performing authentication') except Exception as _: print('Performing authentication') print("Please enter your CalNet ID.") calnet_id = input("CalNet ID: ") c = Client(auth_endpoint='https://accounts.google.com/o/oauth2/auth', client_id=CLIENT_ID) url = c.auth_uri(scope="profile email", access_type='offline', name='ok-server', redirect_uri=REDIRECT_URI, login_hint='*****@*****.**' % (calnet_id)) webbrowser.open_new(url) host_name = REDIRECT_HOST port_number = REDIRECT_PORT done = False access_token = None refresh_token = None expires_in = None class CodeHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): """Respond to the GET request made by the OAuth""" path = urlparse(self.path) nonlocal access_token, refresh_token, expires_in, done qs = parse_qs(path.query) code = qs['code'][0] access_token, refresh_token, expires_in = _make_code_post(code) done = True self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(bytes(SUCCESS_HTML, "utf-8")) server_address = (host_name, port_number) httpd = http.server.HTTPServer(server_address, CodeHandler) httpd.handle_request() update_storage(access_token, expires_in, refresh_token) return access_token
def authenticate(force=False): """ Returns an oauth token that can be passed to the server for identification. """ if not force: try: cur_time = int(time.time()) access_token, expires_at, refresh_token = get_storage() if cur_time < expires_at - 10: return access_token access_token, expires_in = make_refresh_post(refresh_token) update_storage(access_token, expires_in, refresh_token) return access_token except IOError as _: print('Performing authentication') except Exception as _: print('Performing authentication') print("Please enter your CalNet ID.") calnet_id = input("CalNet ID: ") c = Client(auth_endpoint='https://accounts.google.com/o/oauth2/auth', client_id=CLIENT_ID) url = c.auth_uri(scope="profile email", access_type='offline', name='ok-server', redirect_uri=REDIRECT_URI, login_hint='*****@*****.**' % (calnet_id)) webbrowser.open_new(url) host_name = REDIRECT_HOST port_number = REDIRECT_PORT done = False access_token = None refresh_token = None expires_in = None class CodeHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): """Respond to the GET request made by the OAuth""" path = urlparse(self.path) nonlocal access_token, refresh_token, expires_in, done qs = parse_qs(path.query) code = qs['code'][0] access_token, refresh_token, expires_in = _make_code_post(code) done = True self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(bytes(SUCCESS_HTML, "utf-8")) def log_message(self, format, *args): return server_address = (host_name, port_number) httpd = http.server.HTTPServer(server_address, CodeHandler) httpd.handle_request() update_storage(access_token, expires_in, refresh_token) return access_token
def authenticate(force=False): """ Returns an oauth token that can be passed to the server for identification. """ if not force: try: cur_time = int(time.time()) access_token, expires_at, refresh_token = get_storage() if cur_time < expires_at - 10: return access_token access_token, expires_in = make_refresh_post(refresh_token) if not access_token and expires_in: raise AuthenticationException( "Authentication failed and returned an empty token.") update_storage(access_token, expires_in, refresh_token) return access_token except IOError as _: print('Performing authentication') except AuthenticationException as e: raise e # Let the main script handle this error except Exception as _: print('Performing authentication') check_ssl() print("Please enter your bCourses email.") email = input("bCourses email: ") c = Client(auth_endpoint='https://accounts.google.com/o/oauth2/auth', client_id=CLIENT_ID) url = c.auth_uri(scope="profile email", access_type='offline', name='ok-server', redirect_uri=REDIRECT_URI, login_hint=email) webbrowser.open_new(url) host_name = REDIRECT_HOST port_number = REDIRECT_PORT done = False access_token = None refresh_token = None expires_in = None class CodeHandler(http.server.BaseHTTPRequestHandler): def send_failure(self, message): self.send_response(400) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(bytes(failure_page(message), "utf-8")) def do_GET(self): """Respond to the GET request made by the OAuth""" nonlocal access_token, refresh_token, expires_in, done path = urlparse(self.path) qs = parse_qs(path.query) try: code = qs['code'][0] access_token, refresh_token, expires_in = _make_code_post(code) except KeyError: message = qs.get('error', 'Unknown') log.warning("No auth code provided {}".format(message)) done = True self.send_failure(message) return except Exception as e: # TODO : Catch just SSL errors log.warning("Could not obtain token", exc_info=True) done = True self.send_failure(e.message) return done = True self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(bytes(success_page(SERVER, email), "utf-8")) def log_message(self, format, *args): return server_address = (host_name, port_number) httpd = http.server.HTTPServer(server_address, CodeHandler) httpd.handle_request() update_storage(access_token, expires_in, refresh_token) return access_token