Skip to content

Instantly share code, notes, and snippets.

@rotty3000
Last active April 8, 2025 15:47
Show Gist options
  • Save rotty3000/a6d6ad74f241324605d0c3aa6f7a711e to your computer and use it in GitHub Desktop.
Save rotty3000/a6d6ad74f241324605d0c3aa6f7a711e to your computer and use it in GitHub Desktop.
Client Extension ENV contract in Python
DXP Domains:
- localhost
DXP Main Domain: localhost
DXP Server Protocol: http
OAuth 2.0 Configuration for 'liferay-sample-etc-spring-boot-oauth-application-user-agent':
headless_server_audience: None
headless_server_client_id: None
headless_server_client_secret: None
headless_server_scopes: None
authorization_uri: /o/oauth2/authorize
introspection_uri: /o/oauth2/introspect
jwks_uri: /o/oauth2/jwks
token_uri: /o/oauth2/token
redirect_uris: ['/o/oauth2/redirect']
user_agent_audience: http://localhost:58081
user_agent_client_id: id-3230c153-65cc-803c-b37d-3012ee4fa5a
user_agent_scopes: Liferay.Headless.Admin.User.everything
Liferay.Headless.Admin.Workflow.everything
Authorization URI: /o/oauth2/authorize
Redirect URIs: ['/o/oauth2/redirect']
# How to use.
#
# Deploy the liferay-sample-etc-spring-boot sample to a locally running Liferay.
# Then execute the following (changing paths as necessary)
#
# LIFERAY_ROUTES_DXP=${LIFERAY_HOME}/routes/default/dxp \
# LIFERAY_ROUTES_CLIENT_EXTENSION=${LIFERAY_HOME}/routes/default/liferay-sample-etc-spring-boot \
# OAUTH_ERC=liferay-sample-etc-spring-boot-oauth-application-user-agent \
# python3 main.py
#
import os
def get_dxp_domains():
"""Retrieves the list of DXP domains from the com.liferay.lxc.dxp.domains file."""
domains_file_path = os.path.join(os.environ.get("LIFERAY_ROUTES_DXP", ""), "com.liferay.lxc.dxp.domains")
domains = []
if os.path.isfile(domains_file_path):
try:
with open(domains_file_path, 'r') as f:
for line in f:
domain = line.strip()
if domain:
domains.append(domain)
except IOError as e:
print(f"Error reading DXP domains file: {e}")
else:
print(f"Warning: DXP domains file not found at: {domains_file_path}")
return domains
def get_dxp_main_domain():
"""Retrieves the primary DXP domain from the com.liferay.lxc.dxp.main.domain file."""
main_domain_file_path = os.path.join(os.environ.get("LIFERAY_ROUTES_DXP", ""), "com.liferay.lxc.dxp.main.domain")
main_domain = None
if os.path.isfile(main_domain_file_path):
try:
with open(main_domain_file_path, 'r') as f:
main_domain = f.read().strip()
except IOError as e:
print(f"Error reading DXP main domain file: {e}")
else:
print(f"Warning: DXP main domain file not found at: {main_domain_file_path}")
return main_domain
def get_dxp_server_protocol():
"""Retrieves the DXP server protocol from the com.liferay.lxc.dxp.server.protocol file."""
protocol_file_path = os.path.join(os.environ.get("LIFERAY_ROUTES_DXP", ""), "com.liferay.lxc.dxp.server.protocol")
protocol = None
if os.path.isfile(protocol_file_path):
try:
with open(protocol_file_path, 'r') as f:
protocol = f.read().strip()
except IOError as e:
print(f"Error reading DXP server protocol file: {e}")
else:
print(f"Warning: DXP server protocol file not found at: {protocol_file_path}")
return protocol
import os
def load_client_extension_oauth2_config(reference_code):
"""Loads OAuth 2.0 configuration for a given application reference code."""
config = {}
base_path = os.environ.get("LIFERAY_ROUTES_CLIENT_EXTENSION", "")
def read_file_content(filename):
filepath = os.path.join(base_path, f"{reference_code}.{filename}")
if os.path.isfile(filepath):
try:
with open(filepath, 'r') as f:
return f.read().strip()
except IOError as e:
print(f"Error reading file {filepath}: {e}")
return None
# Headless Server Application
config['headless_server_audience'] = read_file_content("oauth2.headless.server.audience")
config['headless_server_client_id'] = read_file_content("oauth2.headless.server.client.id")
config['headless_server_client_secret'] = read_file_content("oauth2.headless.server.client.secret")
config['headless_server_scopes'] = read_file_content("oauth2.headless.server.scopes")
# OAuth 2.0 Endpoints (common to both types)
config['authorization_uri'] = read_file_content("oauth2.authorization.uri")
config['introspection_uri'] = read_file_content("oauth2.introspection.uri")
config['jwks_uri'] = read_file_content("oauth2.jwks.uri")
config['token_uri'] = read_file_content("oauth2.token.uri")
# Redirect URIs (User Agent)
redirect_uris_content = read_file_content("oauth2.redirect.uris")
config['redirect_uris'] = [uri.strip() for uri in redirect_uris_content.splitlines() if uri.strip()] if redirect_uris_content else None
# User Agent Application
config['user_agent_audience'] = read_file_content("oauth2.user.agent.audience")
config['user_agent_client_id'] = read_file_content("oauth2.user.agent.client.id")
config['user_agent_scopes'] = read_file_content("oauth2.user.agent.scopes")
return config
if __name__ == "__main__":
domains = get_dxp_domains()
if domains:
print("DXP Domains:")
for domain in domains:
print(f"- {domain}")
main_domain = get_dxp_main_domain()
if main_domain:
print(f"\nDXP Main Domain: {main_domain}")
protocol = get_dxp_server_protocol()
if protocol:
print(f"\nDXP Server Protocol: {protocol}")
# Replace 'your-app-reference-code' with the actual value from your client-extension.yaml
application_reference_code = os.environ.get("OAUTH_ERC", "your-app-reference-code")
oauth2_config = load_client_extension_oauth2_config(application_reference_code)
if oauth2_config:
print(f"\nOAuth 2.0 Configuration for '{application_reference_code}':")
for key, value in oauth2_config.items():
print(f" {key}: {value}")
# Example of accessing specific properties
if oauth2_config.get('authorization_uri'):
print(f"\nAuthorization URI: {oauth2_config['authorization_uri']}")
if oauth2_config.get('headless_server_client_id'):
print(f"Headless Server Client ID: {oauth2_config['headless_server_client_id']}")
if oauth2_config.get('redirect_uris'):
print(f"Redirect URIs: {oauth2_config['redirect_uris']}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment