From d06eff23a62f9ea98b52885423ba9ba723b54583 Mon Sep 17 00:00:00 2001 From: bsatoriu Date: Tue, 16 Jan 2024 13:07:30 -0800 Subject: [PATCH] Remove obsolete token handler code --- maap/utils/HTTPServerHandler.py | 56 --------------------------------- maap/utils/TokenHandler.py | 39 ----------------------- test/test_MAAP.py | 6 ---- 3 files changed, 101 deletions(-) delete mode 100644 maap/utils/HTTPServerHandler.py delete mode 100644 maap/utils/TokenHandler.py diff --git a/maap/utils/HTTPServerHandler.py b/maap/utils/HTTPServerHandler.py deleted file mode 100644 index 8f29419..0000000 --- a/maap/utils/HTTPServerHandler.py +++ /dev/null @@ -1,56 +0,0 @@ -from http.server import BaseHTTPRequestHandler -import requests -import json - -REDIRECT_URL = 'http://localhost:8080/' - - -class HTTPServerHandler(BaseHTTPRequestHandler): - - """ - HTTP Server callbacks to handle Earthdata OAuth redirects - """ - def __init__(self, request, address, server, a_id): - self.app_id = a_id - super().__init__(request, address, server) - - def do_GET(self): - - EARTHDATA_API_AUTH_URI = 'https://uat.urs.earthdata.nasa.gov/oauth/token' - - self.send_response(200) - self.send_header('Content-type', 'text/html') - self.end_headers() - if 'code' in self.path: - self.auth_code = self.path.split('=')[1] - self.wfile.write(bytes('

You may now close this window.

', 'utf-8')) - self.server.access_token = self.get_access_token_from_url( - EARTHDATA_API_AUTH_URI, self.auth_code) - - def get_access_token_from_url(self, url, code): - """ - Parse the access token from Earthdata's response - Args: - url: the Earthdata api oauth URI containing valid client_id - code: Earthdata auth_code argument - Returns: - a string containing the access key - """ - - body = { - 'grant_type': 'authorization_code', - 'code': code, - 'redirect_uri': REDIRECT_URL - } - - r = requests.post(url, data=body, auth=('edl_client_name', 'edl_client_password')) - - if r.status_code == 401: - return "unauthorized" - else: - j = json.loads(r.text) - return j['access_token'] - - # Disable logging from the HTTP Server - def log_message(self, format, *args): - return diff --git a/maap/utils/TokenHandler.py b/maap/utils/TokenHandler.py deleted file mode 100644 index 160187e..0000000 --- a/maap/utils/TokenHandler.py +++ /dev/null @@ -1,39 +0,0 @@ -from http.server import HTTPServer -from webbrowser import open_new -from maap.utils.HTTPServerHandler import HTTPServerHandler - -REDIRECT_URL = 'http://localhost:8080/' -PORT = 8080 - - -# Command-line SSO work in progress -# Known issues: -# 1) browser window is spawned during execution; investigating running chrome in headless mode -# 2) credentials are required as input on initial authentication; -# investigating CAS python libraries to avoid this concern. -class TokenHandler: - """ - Functions used to handle Earthdata oAuth - """ - def __init__(self, a_id): - self._id = a_id - - def get_access_token(self): - """ - Fetches the access key using an HTTP server to handle oAuth - requests - Args: - appId: The URS assigned App ID - """ - - access_uri = ('https://uat.urs.earthdata.nasa.gov/oauth/' - + 'authorize?client_id=' + self._id + '&redirect_uri=' - + REDIRECT_URL + "&response_type=code") - - open_new(access_uri) - http_server = HTTPServer( - ('localhost', PORT), - lambda request, address, server: HTTPServerHandler( - request, address, server, self._id)) - http_server.handle_request() - return http_server.access_token \ No newline at end of file diff --git a/test/test_MAAP.py b/test/test_MAAP.py index b057260..5dcc082 100644 --- a/test/test_MAAP.py +++ b/test/test_MAAP.py @@ -1,6 +1,5 @@ from unittest import TestCase from maap.maap import MAAP -from maap.utils.TokenHandler import TokenHandler from unittest.mock import MagicMock import re @@ -64,11 +63,6 @@ def test_genFromEarthdata(self): 'data_center="MAAP Data Management Team", '\ 'bounding_box="-35.4375,-55.6875,-80.4375,37.6875")') - def test_TokenHandler(self): - th = TokenHandler("a-K9YbTr8h112zW5pLV8Fw") - token = th.get_access_token() - self.assertTrue(token != 'unauthorized' and len(token) > 0) - def test_uploadFiles(self): self.maap._upload_s3 = MagicMock(return_value=None) result = self.maap.uploadFiles(['test/s3-upload-testfile1.txt', 'test/s3-upload-testfile2.txt'])