diff --git a/.gitignore b/.gitignore index bf2c255..2ce0a30 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ my_settings.py .lazy_login* .ipynb_checkpoints/ .ipynb_checkpoints/* +flask_session/ diff --git a/EagleEyev3/__init__.py b/EagleEyev3/__init__.py index b9b7592..eb3f694 100644 --- a/EagleEyev3/__init__.py +++ b/EagleEyev3/__init__.py @@ -138,6 +138,47 @@ class EagleEyev3(): 'current_user': self.current_user } + def logout(self): + """ + Revokes token. + + Returns: + dict: Dictionary containing the success status, response HTTP status code, and data. + """ + url = "https://auth.eagleeyenetworks.com/oauth2/revoke" + + payload = { + "token": self.access_token, + "token_type_hint": "access_token" + } + + headers = { + "Authorization": f"Bearer {self.access_token}", + "Accept": "application/json", + "Content-type": "application/json" + } + + # Send a POST request to obtain the base URL + response = requests.post(url, json=payload, headers=headers) + response_json = response.json() + + logging.info(f"{response.status_code} in logout") + + if response.status_code == 200: + success = True + else: + success = False + logging.info(f"call to logout: {response_json}") + + self.access_token = None + self.refresh_token = None + + return { + "success": success, + "response_http_status": response.status_code, + "data": response_json + } + def get_base_url(self, cascade=True): """ Obtains the base URL for the user. diff --git a/app.py b/app.py new file mode 100644 index 0000000..1406ebe --- /dev/null +++ b/app.py @@ -0,0 +1,85 @@ + +import json, requests +from flask import Flask, request, session, render_template, redirect, url_for +from flask_session import Session + +import logging +logger = logging.getLogger() + +#logger.setLevel('DEBUG') +logger.setLevel('INFO') +#logger.setLevel('WARN') +#logger.setLevel('ERROR') +#logger.setLevel('CRITICAL') + +from EagleEyev3 import EagleEyev3 + + +app = Flask(__name__) + +SESSION_TYPE = 'filesystem' +app.config.from_object(__name__) +Session(app) + +@app.route('/') +def index(): + + if 'een' in session: + een = session['een'] + else: + een = EagleEyev3() + session['een'] = een + + # using current_user as a proxy for an established valid session + if een.access_token == None: + base_url = "https://auth.eagleeyenetworks.com/oauth2/authorize" + path_url = f"?client_id={een.client_id}&response_type=code&scope=vms.all&redirect_uri={een.redirect_uri}" + return redirect(f"{base_url}{path_url}") + + # call this to check if session is actually valid + check = een.get_current_user() + if 'success' not in check or check['success'] == False: + base_url = "https://auth.eagleeyenetworks.com/oauth2/authorize" + path_url = f"?client_id={een.client_id}&response_type=code&scope=vms.all&redirect_uri={een.redirect_uri}" + return redirect(f"{base_url}{path_url}") + else: + logging.info(f"{check['success']} - check get_current_user") + + + values = { + "current_user": een.current_user + } + + return render_template('index.html', template_values=values) + + +@app.route('/login_callback') +def login_callback(): + # This is getting the ?code= querystring value from the HTTP request. + code = request.args.get('code') + + if 'een' in session: + een = session['een'] + else: + een = EagleEyev3() + + + if (code): + # use the include code parameter to complete login process + oauth_object = een.login_tokens(code) + + + return redirect(url_for('index')) + + +@app.route('/logout') +def logout(): + if 'een' in session: + een = session['een'] + een.logout() + + return redirect(url_for('index')) + + +if __name__ == '__main__': + app.run(host=een.server_host, port=een.server_port) diff --git a/server.py b/server.py deleted file mode 100644 index 6f4775f..0000000 --- a/server.py +++ /dev/null @@ -1,34 +0,0 @@ - -import json, requests -from flask import Flask, request -from EagleEyev3 import EagleEyev3 - -een = EagleEyev3() - -app = Flask(__name__) - -@app.route('/') -def index(): - - - base_url = "https://auth.eagleeyenetworks.com/oauth2/authorize" - path_url = f"?client_id={een.client_id}&response_type=code&scope=vms.all&redirect_uri={een.redirect_uri}" - - return f"Login with Eagle Eye Networks" - - - -@app.route('/login_callback') -def login_callback(): - # This is getting the ?code= querystring value from the HTTP request. - code = request.args.get('code') - - if (code): - # use the include code parameter to complete login process - oauth_object = een.login_tokens(code) - return f"Hello {een.current_user['firstName']} {een.current_user['lastName']}" - - - -if __name__ == '__main__': - app.run(host=een.server_host, port=een.server_port) diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..2c1fe4c --- /dev/null +++ b/templates/base.html @@ -0,0 +1,131 @@ + +{% if "X-PJAX" not in request.headers %} + + +
+ + + + + + + {% block title %} +