@sonatel-os/openapi-runtime 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +106 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -73,28 +73,121 @@ console.log(response.body);
|
|
|
73
73
|
|
|
74
74
|
-----
|
|
75
75
|
|
|
76
|
+
You are absolutely right. The previous README was too high-level regarding the specific configuration keys for `openapi.auth.yml` and how `save()` orchestrates the magic.
|
|
77
|
+
|
|
78
|
+
Here is the detailed **"Magic Authentication"** section to replace the short one in your README. It documents the YAML structure and the manual binding methods based on your source code.
|
|
79
|
+
|
|
80
|
+
-----
|
|
81
|
+
|
|
76
82
|
## 🔐 Magic Auto-Authentication
|
|
77
83
|
|
|
78
|
-
|
|
84
|
+
The runtime provides three ways to handle authentication: **Implicit (Env Vars)**, **Explicit (YAML)**, and **Manual (Code)**.
|
|
79
85
|
|
|
80
|
-
|
|
86
|
+
When you call `runtime.save()`, it automatically attempts to configure authentication in this order:
|
|
81
87
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
| `bearerAuth` (in `products` spec) | `OAR_PRODUCTS_BEARERAUTH` |
|
|
85
|
-
| `apiKey` (in `legacy` spec) | `OAR_LEGACY_APIKEY` |
|
|
86
|
-
| `oauth2` (ClientId) | `OAUTH_CLIENT_ID_PRODUCTS_OAUTH2` |
|
|
88
|
+
1. **YAML Config:** Checks for `openapi.auth.yml` in your project root.
|
|
89
|
+
2. **Environment Variables:** Scans `process.env` for matching naming conventions.
|
|
87
90
|
|
|
88
|
-
|
|
89
|
-
If your spec is named `eyone` and uses `BearerAuth`:
|
|
91
|
+
### 1\. Environment Variables (Zero Config)
|
|
90
92
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
93
|
+
If your OpenAPI spec has `securitySchemes` defined, the runtime automatically looks for environment variables matching this pattern:
|
|
94
|
+
|
|
95
|
+
`OAR_<SPEC_NAME>_<SCHEME_NAME>`
|
|
96
|
+
|
|
97
|
+
* **API Key:** `OAR_MYAPI_APIKEY`
|
|
98
|
+
* **Bearer Token:** `OAR_MYAPI_BEARERAUTH` (or `BEARER_MYAPI_BEARERAUTH`)
|
|
99
|
+
* **Basic Auth:** `BASIC_USER_MYAPI_BASICAUTH` and `BASIC_PASS_MYAPI_BASICAUTH`
|
|
100
|
+
* **OAuth2 (Client Creds):**
|
|
101
|
+
* `OAUTH_CLIENT_ID_MYAPI_OAUTH`
|
|
102
|
+
* `OAUTH_CLIENT_SECRET_MYAPI_OAUTH`
|
|
103
|
+
* `OAUTH_TOKEN_URL_MYAPI_OAUTH`
|
|
104
|
+
|
|
105
|
+
*(Note: Spec names and Scheme names are converted to Uppercase and sanitized).*
|
|
106
|
+
|
|
107
|
+
-----
|
|
108
|
+
|
|
109
|
+
### 2\. The `openapi.auth.yml` File (Advanced)
|
|
110
|
+
|
|
111
|
+
For more control (e.g., mapping custom env var names, setting specific scopes, or defining audiences), create a file named **`openapi.auth.yml`** in your project root.
|
|
112
|
+
|
|
113
|
+
The runtime loads this file during `save()`. You can use `${VAR_NAME}` syntax to inject environment variables.
|
|
114
|
+
|
|
115
|
+
```yaml
|
|
116
|
+
# openapi.auth.yml
|
|
117
|
+
|
|
118
|
+
auth:
|
|
119
|
+
# Matches the 'name' you passed to runtime.save({ name: 'products', ... })
|
|
120
|
+
products:
|
|
121
|
+
schemes:
|
|
122
|
+
# Matches the name in components.securitySchemes in your Swagger file
|
|
123
|
+
bearerAuth:
|
|
124
|
+
type: http
|
|
125
|
+
scheme: bearer
|
|
126
|
+
tokenFromEnv: MY_CUSTOM_TOKEN_VAR # Maps to process.env.MY_CUSTOM_TOKEN_VAR
|
|
127
|
+
|
|
128
|
+
legacyApiKey:
|
|
129
|
+
type: apiKey
|
|
130
|
+
in: header
|
|
131
|
+
name: X-API-KEY
|
|
132
|
+
valueFromEnv: LEGACY_KEY_VAR
|
|
133
|
+
|
|
134
|
+
auth0:
|
|
135
|
+
type: oauth2
|
|
136
|
+
flow: client_credentials
|
|
137
|
+
tokenUrl: https://my-tenant.auth0.com/oauth/token
|
|
138
|
+
clientIdFromEnv: AUTH0_CLIENT_ID
|
|
139
|
+
clientSecretFromEnv: AUTH0_CLIENT_SECRET
|
|
140
|
+
audience: https://api.orange-sonatel.sn
|
|
141
|
+
scope: "read:products write:orders"
|
|
94
142
|
```
|
|
95
143
|
|
|
96
|
-
|
|
144
|
+
**Supported Config Keys:**
|
|
145
|
+
|
|
146
|
+
| Type | Config Keys |
|
|
147
|
+
| :--- | :--- |
|
|
148
|
+
| **apiKey** | `in` (header/query), `name`, `value` (hardcoded), `valueFromEnv` |
|
|
149
|
+
| **http (bearer)** | `token`, `tokenFromEnv` |
|
|
150
|
+
| **http (basic)** | `username`, `password`, `usernameFromEnv`, `passwordFromEnv` |
|
|
151
|
+
| **oauth2** | `flow` (only 'client\_credentials'), `tokenUrl`, `clientIdFromEnv`, `clientSecretFromEnv`, `scope`, `audience` |
|
|
152
|
+
|
|
153
|
+
-----
|
|
154
|
+
|
|
155
|
+
### 3\. Manual Binding (Programmatic)
|
|
97
156
|
|
|
157
|
+
If you need dynamic tokens (e.g., retrieved from a database) or want to skip the auto-configuration, you can bind providers manually using `setAuth`.
|
|
158
|
+
|
|
159
|
+
This is useful for Client Components or complex server-side logic.
|
|
160
|
+
|
|
161
|
+
```javascript
|
|
162
|
+
import { save, setAuth, bearer, apiKey } from '@sonatel-os/openapi-runtime';
|
|
163
|
+
|
|
164
|
+
// 1. Load the spec (disable autoAuth if you want full manual control)
|
|
165
|
+
await save({ name: 'products', specName: 'products.json', autoAuth: false });
|
|
166
|
+
|
|
167
|
+
// 2. Define providers
|
|
168
|
+
// The keys here must match the security scheme names in your Swagger definition.
|
|
169
|
+
setAuth('products', {
|
|
170
|
+
// Simple Bearer
|
|
171
|
+
bearerAuth: bearer({
|
|
172
|
+
token: 'my-hardcoded-jwt'
|
|
173
|
+
}),
|
|
174
|
+
|
|
175
|
+
// Dynamic Bearer (Async) - Great for rotating tokens
|
|
176
|
+
internalAuth: bearer({
|
|
177
|
+
getToken: async () => {
|
|
178
|
+
const token = await db.getValidToken();
|
|
179
|
+
return token;
|
|
180
|
+
}
|
|
181
|
+
}),
|
|
182
|
+
|
|
183
|
+
// API Key
|
|
184
|
+
apiKeyAuth: apiKey({
|
|
185
|
+
in: 'header',
|
|
186
|
+
name: 'X-Special-Key',
|
|
187
|
+
getValue: () => process.env.SPECIAL_KEY
|
|
188
|
+
})
|
|
189
|
+
});
|
|
190
|
+
```
|
|
98
191
|
-----
|
|
99
192
|
|
|
100
193
|
## 🛠️ Features for Power Users
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sonatel-os/openapi-runtime",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "The dynamic, zero-codegen OpenAPI SDK. Load specs at runtime, mock responses, validate payloads, and execute requests without generating a single file.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Sonatel Open Source <opensource@sonatel.com>",
|