Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
Copyright (c) 2022 - 2026 ANSYS, Inc. and/or its affiliates.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
2 changes: 1 addition & 1 deletion examples/cfx_static_mixer/exec_cfx.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/cfx_static_mixer/project_setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/fluent_2d_heat_exchanger/project_setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/fluent_nozzle/project_setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/generic_api/project_setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/lsdyna_cylinder_plate/lsdyna_job.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/mapdl_linked_analyses/project_setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/mapdl_motorbike_frame/exec_mapdl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/mapdl_motorbike_frame/project_query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/mapdl_motorbike_frame/project_setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/mapdl_motorbike_frame/task_files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/mapdl_tyre_performance/project_setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/python_large_output/evaluate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/python_large_output/exec_python.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/python_large_output/project_setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/python_linked_multi_process_step/eval.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/python_linked_multi_process_step/project_setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/python_multi_process_step/eval.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/python_multi_process_step/project_setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/python_multi_process_step/task_files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/python_two_bar_truss_problem/evaluate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/python_two_bar_truss_problem/exec_python.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion examples/python_two_bar_truss_problem/project_setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion generate_resources.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
90 changes: 72 additions & 18 deletions oidc_pkce.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Script to demonstrate how to authenticate with an OIDC provider
using the Authorization Code Flow with PKCE.
"""Script to demonstrate OIDC authentication with Authorization Code Flow using PKCE.

Mostly inspired by:
- https://www.camiloterevinto.com/post/oauth-pkce-flow-from-python-desktop
Expand All @@ -10,35 +8,46 @@
import argparse
import base64
import hashlib
from http.server import BaseHTTPRequestHandler, HTTPServer
import os
import re
import socket
import webbrowser
from http.server import BaseHTTPRequestHandler, HTTPServer
from typing import Any
from urllib import parse
import webbrowser

import requests
from requests import Request


class OAuthHttpServer(HTTPServer):
"""HTTP server for OAuth authentication with authorization code storage."""

def __init__(self, *args, **kwargs):
"""Initialize the OAuth HTTP server.

Args:
*args: Variable length argument list passed to HTTPServer.
**kwargs: Arbitrary keyword arguments passed to HTTPServer.

"""
HTTPServer.__init__(self, *args, **kwargs)
self.authorization_code = ""


class OAuthHttpHandler(BaseHTTPRequestHandler):
def do_GET(self):
"""HTTP request handler for OAuth authentication callbacks."""

def do_GET(self):
"""Handle GET requests to capture OAuth authorization code."""
parsed = parse.urlparse(self.path)
qs = parse.parse_qs(parsed.query)
self.server.authorization_code = qs["code"][0]

self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
html_body = """
html_body = b"""
<html>
<head>
<script type="text/javascript">
Expand All @@ -49,16 +58,21 @@ def do_GET(self):
<p>Operation successful. You can safely close this window.</p>
</body>
</html>
""".encode(
"UTF-8"
)
"""
self.wfile.write(html_body)

def log_message(self, format, *args):
"""Suppress log messages from the HTTP server."""
return


def generate_code() -> tuple[str, str]:
"""Generate PKCE code verifier and code challenge for OAuth authentication.

Returns:
tuple[str, str]: A tuple containing (code_verifier, code_challenge).

"""
code_verifier = base64.urlsafe_b64encode(os.urandom(40)).decode("utf-8")
code_verifier = re.sub("[^a-zA-Z0-9]+", "", code_verifier)

Expand All @@ -70,8 +84,25 @@ def generate_code() -> tuple[str, str]:


def prepare_auth_url(
auth_url, redirect_uri, code_challenge, client_id="rep-cli", scope="openid"
auth_url: str,
redirect_uri: str,
code_challenge: str,
client_id: str = "rep-cli",
scope: str = "openid",
) -> str:
"""Prepare OAuth authorization URL with PKCE parameters.

Args:
auth_url: OAuth authorization endpoint URL.
redirect_uri: Redirect URI for OAuth callback.
code_challenge: PKCE code challenge string.
client_id: OAuth client ID. Defaults to "rep-cli".
scope: OAuth scope. Defaults to "openid".

Returns:
str: Complete authorization URL with query parameters.

"""
return (
Request(
method="GET",
Expand All @@ -91,7 +122,13 @@ def prepare_auth_url(
)


def find_free_port():
def find_free_port() -> int:
"""Find an available port for the local HTTP server.

Returns:
int: An available port number.

"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 0))
s.listen(1)
Expand All @@ -100,11 +137,19 @@ def find_free_port():
return port


def login(config: dict[str, Any]) -> str:
def login(config: dict[str, Any]) -> tuple[str, str, str]:
"""Perform OAuth login with PKCE flow.

Args:
config: Configuration dictionary containing OAuth parameters including
auth_url, client_id, scope, and token_url.

Returns:
tuple[str, str, str]: A tuple containing (access_token, refresh_token, id_token).

"""
port = find_free_port()
with OAuthHttpServer(("", port), OAuthHttpHandler) as httpd:

# print(f"Local HTTP server available at http://localhost:{httpd.server_port}")

code_verifier, code_challenge = generate_code()
Expand Down Expand Up @@ -148,7 +193,16 @@ def login(config: dict[str, Any]) -> str:
return access_token, refresh_token, id_token


def get_config(hps_url):
def get_config(hps_url: str) -> dict[str, Any]:
"""Retrieve OAuth configuration from HPS server.

Args:
hps_url: Base URL of the HPS server.

Returns:
dict: Configuration dictionary containing OAuth endpoints and settings.

"""
jms_url = hps_url.rstrip("/") + "/jms/api/v1"
response = requests.get(url=jms_url, verify=True)
assert response.status_code == 200, f"Failed to get config from JMS: {response.text}"
Expand All @@ -164,9 +218,9 @@ def get_config(hps_url):
)

response = requests.get(url=config["disco_endpoint"], verify=True)
assert (
response.status_code == 200
), f"Failed to get discovery from {config['disco_endpoint']}: {response.text}"
assert response.status_code == 200, (
f"Failed to get discovery from {config['disco_endpoint']}: {response.text}"
)
raw_disco = response.json()
# Fill specific endpoints from discovery
config["auth_url"] = raw_disco["authorization_endpoint"]
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/hps/client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/hps/client/__version__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/hps/client/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/hps/client/auth/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/hps/client/auth/api/auth_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/hps/client/auth/resource/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/hps/client/auth/resource/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/hps/client/auth/schema/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/hps/client/auth/schema/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/hps/client/authenticate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/hps/client/check_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# Copyright (C) 2022 - 2026 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
Expand Down
Loading