/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of OmiLAXR. */ using System.Collections.Generic; using System.ComponentModel; using UnityEngine; namespace OmiLAXR.Endpoints { /// /// Loads Bearer token authentication configuration from a JSON file in StreamingAssets. /// Automatically configures a BearerEndpoint with loaded credentials. /// /// /// Loads Bearer token authentication configuration from a JSON file. /// /// /// Loading priority: /// - WebGL builds: /// 1. If URL contains "?bearer=https://..." (or analogous key), /// load the JSON config from that remote URL. /// 2. Otherwise expect "?endpoint=..." and "?token=..." in the URL. /// /// - Non-WebGL builds: /// 1. Command line arguments (-endpoint=..., -token=...). /// 2. JSON file next to the executable (AppDomain.CurrentDomain.BaseDirectory). /// 3. JSON file in Application.persistentDataPath. /// 4. JSON file in Application.streamingAssetsPath. /// /// Subclasses implement ApplyConfig(TC, TE) to actually configure the endpoint. /// [RequireComponent(typeof(BearerAuthEndpoint))] [AddComponentMenu("OmiLAXR / 6) Endpoints / Bearer Auth Loader")] [DefaultExecutionOrder(-1001)] // Ensure early initialization in the component lifecycle [Description( "Loads bearer token authentication configuration using the following strategy:\n\n" + "▶ WebGL builds:\n" + " 1. If the URL contains '?${urlQuery}=https://...' → load JSON config from that remote URL.\n" + " 2. Otherwise, expect '?endpoint=...' and '?token=...' parameters in the URL query.\n\n" + "▶ Non-WebGL builds:\n" + " 1. From command line arguments: -endpoint=..., -token=...\n" + " 2. From bearer.json next to the executable.\n" + " 3. From bearer.json in Application.persistentDataPath.\n" + " 4. From bearer.json in Application.streamingAssetsPath.\n\n" + "The first valid source in this order is used to configure the BearerAuthEndpoint." )] public class BearerAuthLoader : AuthLoader { /// /// Provides the default filename (including extension) used to discover /// bearer token configuration when loading from disk or remote sources. /// protected override string DefaultAuthFileName => "bearer.json"; /// /// Attempts to create a BearerAuth configuration from key/value pairs. /// Expects the following keys: /// - "endpoint": the API base URL or service endpoint /// - "token": the bearer token string to be used for authorization /// Returns true when both keys are present and a BearerAuth instance was created. /// /// Source dictionary (from CLI args, URL query, or JSON). /// Output BearerAuth configuration when successful; default otherwise. /// True on success; false when required keys are missing. protected override bool TryBuildConfig(IDictionary values, out BearerAuth config) { if (!values.ContainsKey("endpoint") || !values.ContainsKey("token")) { config = default; return false; } config = new BearerAuth(values["endpoint"], values["token"]); return true; } /// /// Applies the parsed bearer token configuration to the target endpoint. /// Delegates to the endpoint to store and activate the provided credentials. /// /// The BearerAuthEndpoint to configure. /// The validated BearerAuth configuration. protected override void ApplyConfig(BearerAuthEndpoint endpoint, BearerAuth config) { endpoint.SetAuthConfig(config); } } }