forked from galaxyproject/bioblend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Metaverse-Colab-for-Fusion-Energy/add_sess…
…ion_handling Add session handling
- Loading branch information
Showing
5 changed files
with
275 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
Cookie handler for Authelia authentication using the Galaxy API | ||
""" | ||
|
||
import getpass | ||
import logging as log | ||
import sys | ||
from http.cookiejar import LWPCookieJar | ||
from pathlib import Path | ||
from pprint import pprint | ||
|
||
import requests | ||
from galaxy_api import * | ||
|
||
AUTH_HOSTNAME = "auth.service.org" | ||
API_HOSTNAME = "galaxy.service.org" | ||
cookie_path = Path(".galaxy_auth.txt") | ||
cookie_jar = LWPCookieJar(cookie_path) | ||
|
||
|
||
class ExpiredCookies(Exception): | ||
pass | ||
|
||
|
||
class NoCookies(Exception): | ||
pass | ||
|
||
|
||
def main(): | ||
try: | ||
cookie_jar.load() # raises OSError | ||
if not cookie_jar: # if empty due to expirations | ||
raise ExpiredCookies() | ||
except OSError: | ||
print("No cached session found, please authenticate") | ||
prompt_authentication() | ||
except ExpiredCookies: | ||
print("Session has expired, please authenticate") | ||
prompt_authentication() | ||
run_examples() | ||
|
||
|
||
def prompt_authentication(): | ||
# -------------------------------------------------------------------------- | ||
# Prompt for username and password | ||
|
||
username = input("Please enter username: ") | ||
password = getpass.getpass(f"Please enter password for {username}: ") | ||
|
||
# -------------------------------------------------------------------------- | ||
# Prepare authentication packet and authenticate session using Authelia | ||
|
||
login_body = { | ||
"username": username, | ||
"password": password, | ||
"requestMethod": "GET", | ||
"keepMeLoggedIn": True, | ||
"targetURL": API_HOSTNAME, | ||
} | ||
|
||
with requests.sessions.Session() as session: | ||
session.cookies = cookie_jar | ||
session.verify = True | ||
|
||
auth = session.post(f"https://{AUTH_HOSTNAME}/api/firstfactor", json=login_body) | ||
|
||
response = session.get(f"https://{AUTH_HOSTNAME}/api/user/info") | ||
if response.status_code != 200: | ||
print("Authentication failed") | ||
sys.exit() | ||
else: | ||
pprint(response.json()) | ||
session.cookies.save() | ||
|
||
|
||
def run_examples(): | ||
GALAXY_KEY = "user_api_key" | ||
WORKFLOW_NAME = "workflow_name" | ||
with requests.sessions.Session() as session: | ||
session.cookies = cookie_jar | ||
|
||
print("Running demo to demonstrate how to use the Galaxy API with Authelia") | ||
|
||
print("Getting workflows from Galaxy") | ||
response = get_workflows(f"https://{API_HOSTNAME}", GALAXY_KEY, session=session) | ||
print(response) | ||
|
||
print("Getting inputs for a workflow") | ||
response = get_inputs(f"https://{API_HOSTNAME}", GALAXY_KEY, WORKFLOW_NAME, session=session) | ||
print(response) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.