Refactor executable library handbooks and improve document reading

This commit is contained in:
cflmy committed 2026-11-08 22:19:11 +08:00
1 parent 16c44588d5
commit c50c515c4a
191 files changed
+29572 -7816

No files matched your search

+73 -21
View File
@@ -8,53 +8,105 @@ import:
table: lib/table.mq.md
---
Read [the security entry](index.mq.md) to distinguish credential/session operations, role checks and OIDC integration. This leaf owns the native authentication resource; endpoint metadata still controls route authorization. Prepare the native web plugin, keep secrets out of examples and logs, and verify both denied and accepted credentials before changing session behavior.
## _load
**p = > plugin.native_path name="web"**
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | No business value on completion; a missing native plugin reports the cause and exits this run. |
Resolve and load the native web plugin before resource operations; missing binaries stop this run.
We retain **`p` = [Locate the installed native plugin](plugin.native_path(name="web"))**.
1. `p`
> plugin.load path=`p`
We **[Load the native plugin required by this operation](plugin.load(path=`p`))**.
2. *
> print text=ext/security: native web plugin not found
> sys.exit code=1
****
We **[Print the prepared message](print(text="ext/security: native web plugin not found"))**.
We **[End this run with the selected exit status](sys.exit(code=1))**.
Return *None*.
# auth
+ `users`
+ `session_ttl`=3600
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| users | any | required | Configured user store used by authentication |
| session_ttl | any | 3600 | Session lifetime in seconds |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | Session/auth helper. Constructs a config object; `login` validates against the users table. To gate `/admin` on this app, use `app.auth users=…` instead. |
Use `users`, `session_ttl`=3600 as the inputs for this operation.
Session/auth helper. Constructs a config object; `login` validates against the users table. To gate `/admin` on this app, use `app.auth users=…` instead.
> _load
*> web_auth_new users=`users` session_ttl=`session_ttl`*
We **[Load the native plugin required by this operation](_load())**.
Return *[Session/auth helper. Constructs a config object; login validates against the users table. To gate /admin on this app, use app.auth users=… instead.](web_auth_new(users=`users`, session_ttl=`session_ttl`))*.
## login
+ `username`
+ `password`
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| username | any | required | User name in the configured user store |
| password | any | required | Credential supplied for verification or hashing; do not log it |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | Validate credentials against the users table and create a session. Returns `{ok, session_id, username, role}`. |
Use `username`, `password` as the inputs for this operation.
Validate credentials against the users table and create a session. Returns `{ok, session_id, username, role}`.
**users = [users](access(self, "users"))**
**ttl = [session_ttl](access(self, "session_ttl"))**
*> web_auth_login username=`username` password=`password` users=`users` session_ttl=`ttl`*
We retain **`users` = [Read users field](access(`self`, "users"))**.
We retain **`ttl` = [Read session_ttl field](access(`self`, "session_ttl"))**.
Return *[Validate credentials against the users table and create a session. Returns {ok, session_id, username, role}.](web_auth_login(username=`username`, password=`password`, users=`users`, session_ttl=`ttl`))*.
## check
+ `session_id`
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| session_id | any | required | Session identifier returned by login |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | Returns `{ok, username, role}` when the session is valid. |
Use `session_id` as the inputs for this operation.
Returns `{ok, username, role}` when the session is valid.
*> web_auth_check session_id=`session_id`*
Return *[Returns {ok, username, role} when the session is valid.](web_auth_check(session_id=`session_id`))*.
## logout
+ `session_id`
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| session_id | any | required | Session identifier returned by login |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | Destroy the session. |
Use `session_id` as the inputs for this operation.
Destroy the session.
*> web_auth_logout session_id=`session_id`*
Return *[Destroy the session.](web_auth_logout(session_id=`session_id`))*.
## hash_password
+ `password`
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| password | any | required | Credential supplied for verification or hashing; do not log it |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | Hash a plaintext password for storage in admin user tables (argon2id). Store the returned `hash` in the `password` column; login verifies automatically. |
Use `password` as the inputs for this operation.
Hash a plaintext password for storage in admin user tables (argon2id). Store the returned `hash` in the `password` column; login verifies automatically.
*> web_password_hash password=`password`*
Return *[Hash a plaintext password for storage in admin user tables (argon2id). Store the returned hash in the password column; login verifies automatically.](web_password_hash(password=`password`))*.
+48
View File
@@ -0,0 +1,48 @@
---
markup: occlusion
title: Security resources — authentication, roles and OIDC
import:
permissions_api: ext/security/rbac.mq.md
---
Use [auth](auth.mq.md) for credential/session operations, [rbac](rbac.mq.md) for role permissions,
and [OIDC](oidc.mq.md) for attaching an external identity provider to an existing web app.
Endpoint authorization remains explicit in the web artifact metadata; a script permission check
does not automatically protect an HTTP route.
The resources use the native web plugin. Build it with scripts/build-web-plugin.sh in this checkout.
This entry's deterministic permission check does not contact an identity provider or change users:
```sh
marqdo call ext/security/index.mq.md check_permission 'held="notes:read"' 'needed="notes:read"' --effects plugin --json
```
Constructors create receivers that are used by resource methods. The free function below makes
that pattern available to CLI callers without copying opaque object fields. For OIDC, configure
registered callback URLs and origin restrictions in oidc.mq.md; never copy production credentials
into a source example. Transport success and authorization success are distinct fields.
Develop native security policy in the existing web security implementation, then document the
public contract and exercise both allow and deny cases. Keep permission delimiters, wildcard
behavior, session lifetime and credential handling explicit. Use local fixtures for login and
OIDC tests; the reusable source must not require a live identity service merely to be inspected.
## check_permission
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| held | text | required | Comma-separated permissions held by the subject; wildcard follows the native RBAC policy |
| needed | text | required | Comma-separated required permissions interpreted by the documented RBAC checker |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | map | Native ok/allowed record; inspect allowed to decide authorization |
The inputs `held` and `needed` describe one explicit permission decision, not a role assignment
or a request to modify the user database. The native checker currently accepts any matching
required permission; held wildcard permits all. Use the leaf API when changing a role catalog.
The checker is an associated tool: it does not read self, so this workflow calls its definition
path directly rather than pretending it is an instance method. We return *[Evaluate this subject against the requested permission set](permissions_api.rbac.can(permissions=`held`, needed=`needed`))* .
The returned allowed field is the authorization decision; the outer CLI ok only reports whether
this function call completed. A denied decision remains a valid, observable result.
+35 -18
View File
@@ -7,28 +7,45 @@ import:
sys: lib/sys.mq.md
---
OIDC attaches provider configuration to an existing web application. Read [the security entry](index.mq.md) for the ownership boundaries before configuring a live identity provider. Registered callback URLs, origin checks and credentials require real deployment configuration; constructing a record alone does not validate a login flow. Keep provider failures distinct from permission decisions and exercise callbacks with controlled fixtures.
## _load
**p = > plugin.native_path name="web"**
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | No business value on completion; a missing native plugin reports the cause and exits this run. |
Resolve and load the native web plugin before resource operations; missing binaries stop this run.
We retain **`p` = [Locate the installed native plugin](plugin.native_path(name="web"))**.
1. `p`
> plugin.load path=`p`
We **[Load the native plugin required by this operation](plugin.load(path=`p`))**.
2. *
> print text=ext/security: native web plugin not found
> sys.exit code=1
****
We **[Print the prepared message](print(text="ext/security: native web plugin not found"))**.
We **[End this run with the selected exit status](sys.exit(code=1))**.
Return *None*.
## attach
+ `app`
+ `issuer`=None
+ `client_id`
+ `client_secret`
+ `redirect_uri`
+ `redirect_origins`=None
+ `scopes`="openid profile email"
+ `callback_path`=None
+ `authorize_url`=None
+ `token_url`=None
+ `userinfo_url`=None
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| app | any | required | Existing web application resource |
| issuer | any | None | OIDC issuer URL; None requires explicit provider endpoint configuration |
| client_id | any | required | Client identifier registered with the identity provider |
| client_secret | any | required | Client credential; supply through trusted runtime configuration |
| redirect_uri | any | required | Registered absolute callback URL or callback path |
| redirect_origins | any | None | Comma-separated callback origin allowlist when using a path callback |
| scopes | any | "openid profile email" | Space-separated identity-provider scopes |
| callback_path | any | None | Explicit callback route override or None for the implementation default |
| authorize_url | any | None | Authorization endpoint override |
| token_url | any | None | Token-exchange endpoint override |
| userinfo_url | any | None | User-info endpoint override |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | (`/oidc/callback`). Path mode resolves the absolute callback from the login request Host (`X-Forwarded-Proto` / `X-Forwarded-Host` when behind a reverse proxy) and stores it in the session so token exchange uses the same string. Optional `redirect_origins` is a comma-separated allowlist (`https://qdqc.com,https://www.qdqc.com`). When set, path-mode origins must match. The IdP must still register every absolute callback URL that will be used. |
Use `app`, `issuer`=None, `client_id`, `client_secret`, `redirect_uri`, `redirect_origins`=None, `scopes`="openid profile email", `callback_path`=None, `authorize_url`=None, `token_url`=None, `userinfo_url`=None as the inputs for this operation.
`redirect_uri` may be an absolute URL (`https://qdqc.com/oidc/callback`) or a path
(`/oidc/callback`). Path mode resolves the absolute callback from the login request
@@ -39,5 +56,5 @@ Optional `redirect_origins` is a comma-separated allowlist
(`https://qdqc.com,https://www.qdqc.com`). When set, path-mode origins must match.
The IdP must still register every absolute callback URL that will be used.
> _load
*> web_app_oidc app=`app` issuer=`issuer` client_id=`client_id` client_secret=`client_secret` redirect_uri=`redirect_uri` redirect_origins=`redirect_origins` scopes=`scopes` callback_path=`callback_path` authorize_url=`authorize_url` token_url=`token_url` userinfo_url=`userinfo_url`*
We **[Load the native plugin required by this operation](_load())**.
Return *[(/oidc/callback)](web_app_oidc(app=`app`, issuer=`issuer`, client_id=`client_id`, client_secret=`client_secret`, redirect_uri=`redirect_uri`, redirect_origins=`redirect_origins`, scopes=`scopes`, callback_path=`callback_path`, authorize_url=`authorize_url`, token_url=`token_url`, userinfo_url=`userinfo_url`))*.
+72 -25
View File
@@ -8,56 +8,103 @@ import:
table: lib/table.mq.md
---
Role and permission data are explicit inputs to the native web checker. The [security entry](index.mq.md) executes allow and deny cases without an identity provider. The can tool is associated with rbac rather than a receiver method because it reads only its arguments. Held wildcard and any-match needed permissions follow the native policy; do not reinterpret that as an all-permissions check.
## _load
**p = > plugin.native_path name="web"**
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | No business value on completion; a missing native plugin reports the cause and exits this run. |
Resolve and load the native web plugin before resource operations; missing binaries stop this run.
We retain **`p` = [Locate the installed native plugin](plugin.native_path(name="web"))**.
1. `p`
> plugin.load path=`p`
We **[Load the native plugin required by this operation](plugin.load(path=`p`))**.
2. *
> print text=ext/security: native web plugin not found
> sys.exit code=1
****
We **[Print the prepared message](print(text="ext/security: native web plugin not found"))**.
We **[End this run with the selected exit status](sys.exit(code=1))**.
Return *None*.
# rbac
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | Offline helpers for role ⊥ permission. Prefer `app.enable_rbac` + gates for HTTP; use these for scripts/tests. |
Offline helpers for role ⊥ permission. Prefer `app.enable_rbac` + gates for HTTP; use these for scripts/tests.
## can
+ `permissions`
+ `needed`
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| permissions | any | required | Comma-separated permission names held by the current role or subject |
| needed | any | required | Permission names required by the operation |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | Return `{ok, allowed}` — whether held permission CSV includes any of `needed` (CSV). Held `*` allows all. |
Use `permissions`, `needed` as the inputs for this operation.
Return `{ok, allowed}` — whether held permission CSV includes any of `needed` (CSV). Held `*` allows all.
> _load
*> web_rbac_can permissions=`permissions` needed=`needed`*
We **[Load the native plugin required by this operation](_load())**.
Return *[Return {ok, allowed} — whether held permission CSV includes any of needed (CSV). Held * allows all.](web_rbac_can(permissions=`permissions`, needed=`needed`))*.
## assign_role
+ `url`
+ `username`
+ `role`
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| url | any | required | Resource connection URL; scheme selects the documented backend |
| username | any | required | User name in the configured user store |
| role | any | required | Role name within the selected user store |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | Bind `username` (in `web_users`) to role name. |
Use `url`, `username`, `role` as the inputs for this operation.
Bind `username` (in `web_users`) to role name.
> _load
*> web_rbac_assign_role url=`url` username=`username` role=`role`*
We **[Load the native plugin required by this operation](_load())**.
Return *[Bind username (in web_users) to role name.](web_rbac_assign_role(url=`url`, username=`username`, role=`role`))*.
## create_role
+ `url`
+ `name`
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| url | any | required | Resource connection URL; scheme selects the documented backend |
| name | any | required | Name registered or looked up by this operation |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | Create a non-system role; returns `{ok, id, name}`. |
Use `url`, `name` as the inputs for this operation.
Create a non-system role; returns `{ok, id, name}`.
> _load
*> web_rbac_create_role url=`url` name=`name`*
We **[Load the native plugin required by this operation](_load())**.
Return *[Create a non-system role; returns {ok, id, name}.](web_rbac_create_role(url=`url`, name=`name`))*.
## set_role_permissions
+ `url`
+ `role`
+ `permissions`
+ `grantable`=""
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| url | any | required | Resource connection URL; scheme selects the documented backend |
| role | any | required | Role name within the selected user store |
| permissions | any | required | Comma-separated permission names held by the current role or subject |
| grantable | any | "" | Permission catalog limiting which permissions may be granted |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | Replace permissions on a non-system role (CSV). `grantable` restricts grants (anti-escalation); empty = full catalog. |
Use `url`, `role`, `permissions`, `grantable`="" as the inputs for this operation.
Replace permissions on a non-system role (CSV). `grantable` restricts grants (anti-escalation); empty = full catalog.
> _load
*> web_rbac_set_role_permissions url=`url` role=`role` permissions=`permissions` grantable=`grantable`*
We **[Load the native plugin required by this operation](_load())**.
Return *[Replace permissions on a non-system role (CSV). grantable restricts grants (anti-escalation); empty = full catalog.](web_rbac_set_role_permissions(url=`url`, role=`role`, permissions=`permissions`, grantable=`grantable`))*.