airlock-bot 0.3.2 → 0.4.0
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 +46 -33
- package/airlock.service +1 -0
- package/dist/airlock/context.d.ts.map +1 -1
- package/dist/airlock/context.js +5 -3
- package/dist/airlock/context.js.map +1 -1
- package/dist/airlock/tools.d.ts +8 -4
- package/dist/airlock/tools.d.ts.map +1 -1
- package/dist/airlock/tools.js +14 -3
- package/dist/airlock/tools.js.map +1 -1
- package/dist/allowlist/engine.d.ts +3 -0
- package/dist/allowlist/engine.d.ts.map +1 -1
- package/dist/allowlist/engine.js +32 -3
- package/dist/allowlist/engine.js.map +1 -1
- package/dist/audit/db.d.ts +0 -1
- package/dist/audit/db.d.ts.map +1 -1
- package/dist/audit/db.js +9 -7
- package/dist/audit/db.js.map +1 -1
- package/dist/audit/logger.d.ts +0 -1
- package/dist/audit/logger.d.ts.map +1 -1
- package/dist/audit/logger.js +0 -3
- package/dist/audit/logger.js.map +1 -1
- package/dist/audit/redactor.d.ts.map +1 -1
- package/dist/audit/redactor.js +6 -6
- package/dist/audit/redactor.js.map +1 -1
- package/dist/backend/airlock-adapter.d.ts +10 -1
- package/dist/backend/airlock-adapter.d.ts.map +1 -1
- package/dist/backend/airlock-adapter.js +219 -6
- package/dist/backend/airlock-adapter.js.map +1 -1
- package/dist/backend/cli/adapter.d.ts +4 -2
- package/dist/backend/cli/adapter.d.ts.map +1 -1
- package/dist/backend/cli/adapter.js +36 -11
- package/dist/backend/cli/adapter.js.map +1 -1
- package/dist/backend/cli/builder.d.ts +1 -1
- package/dist/backend/cli/builder.d.ts.map +1 -1
- package/dist/backend/cli/builder.js +28 -12
- package/dist/backend/cli/builder.js.map +1 -1
- package/dist/backend/exec-adapter.d.ts +1 -1
- package/dist/backend/exec-adapter.d.ts.map +1 -1
- package/dist/backend/exec-adapter.js +1 -5
- package/dist/backend/exec-adapter.js.map +1 -1
- package/dist/backend/factory.d.ts +2 -0
- package/dist/backend/factory.d.ts.map +1 -1
- package/dist/backend/factory.js +17 -9
- package/dist/backend/factory.js.map +1 -1
- package/dist/backend/http-adapter.d.ts +6 -3
- package/dist/backend/http-adapter.d.ts.map +1 -1
- package/dist/backend/http-adapter.js +24 -10
- package/dist/backend/http-adapter.js.map +1 -1
- package/dist/backend/mcp-adapter.d.ts +7 -2
- package/dist/backend/mcp-adapter.d.ts.map +1 -1
- package/dist/backend/mcp-adapter.js +74 -16
- package/dist/backend/mcp-adapter.js.map +1 -1
- package/dist/backend/openapi/adapter.d.ts +8 -2
- package/dist/backend/openapi/adapter.d.ts.map +1 -1
- package/dist/backend/openapi/adapter.js +196 -17
- package/dist/backend/openapi/adapter.js.map +1 -1
- package/dist/backend/openapi/parser.d.ts +15 -1
- package/dist/backend/openapi/parser.d.ts.map +1 -1
- package/dist/backend/openapi/parser.js +153 -11
- package/dist/backend/openapi/parser.js.map +1 -1
- package/dist/backend/types.d.ts +12 -1
- package/dist/backend/types.d.ts.map +1 -1
- package/dist/config/loader.d.ts.map +1 -1
- package/dist/config/loader.js +334 -50
- package/dist/config/loader.js.map +1 -1
- package/dist/config/mutator.d.ts.map +1 -1
- package/dist/config/mutator.js +13 -3
- package/dist/config/mutator.js.map +1 -1
- package/dist/config/profiles.d.ts +2 -1
- package/dist/config/profiles.d.ts.map +1 -1
- package/dist/config/profiles.js +50 -2
- package/dist/config/profiles.js.map +1 -1
- package/dist/config/provider-credentials.d.ts +4 -0
- package/dist/config/provider-credentials.d.ts.map +1 -0
- package/dist/config/provider-credentials.js +48 -0
- package/dist/config/provider-credentials.js.map +1 -0
- package/dist/config/schema.d.ts +2795 -8776
- package/dist/config/schema.d.ts.map +1 -1
- package/dist/config/schema.js +294 -122
- package/dist/config/schema.js.map +1 -1
- package/dist/config/watcher.d.ts.map +1 -1
- package/dist/config/watcher.js +3 -0
- package/dist/config/watcher.js.map +1 -1
- package/dist/config/yaml-merge.d.ts +9 -0
- package/dist/config/yaml-merge.d.ts.map +1 -0
- package/dist/config/yaml-merge.js +54 -0
- package/dist/config/yaml-merge.js.map +1 -0
- package/dist/configure-agent/cli.d.ts +26 -0
- package/dist/configure-agent/cli.d.ts.map +1 -1
- package/dist/configure-agent/cli.js +70 -35
- package/dist/configure-agent/cli.js.map +1 -1
- package/dist/configure-cli/cli.d.ts +6 -0
- package/dist/configure-cli/cli.d.ts.map +1 -1
- package/dist/configure-cli/cli.js +45 -14
- package/dist/configure-cli/cli.js.map +1 -1
- package/dist/configure-web/assistant.d.ts +49 -0
- package/dist/configure-web/assistant.d.ts.map +1 -0
- package/dist/configure-web/assistant.js +312 -0
- package/dist/configure-web/assistant.js.map +1 -0
- package/dist/configure-web/changes.d.ts +214 -0
- package/dist/configure-web/changes.d.ts.map +1 -0
- package/dist/configure-web/changes.js +317 -0
- package/dist/configure-web/changes.js.map +1 -0
- package/dist/configure-web/cli.d.ts +33 -11
- package/dist/configure-web/cli.d.ts.map +1 -1
- package/dist/configure-web/cli.js +704 -151
- package/dist/configure-web/cli.js.map +1 -1
- package/dist/discover/cli.js +2 -2
- package/dist/discover/cli.js.map +1 -1
- package/dist/discover/index.d.ts.map +1 -1
- package/dist/discover/index.js +16 -22
- package/dist/discover/index.js.map +1 -1
- package/dist/discover/openapi.d.ts.map +1 -1
- package/dist/discover/openapi.js +2 -0
- package/dist/discover/openapi.js.map +1 -1
- package/dist/discover/strategies/completion.d.ts.map +1 -1
- package/dist/discover/strategies/completion.js +19 -2
- package/dist/discover/strategies/completion.js.map +1 -1
- package/dist/discover/strategies/fig.d.ts +10 -1
- package/dist/discover/strategies/fig.d.ts.map +1 -1
- package/dist/discover/strategies/fig.js +24 -10
- package/dist/discover/strategies/fig.js.map +1 -1
- package/dist/discover/strategies/help-parser.d.ts.map +1 -1
- package/dist/discover/strategies/help-parser.js +9 -6
- package/dist/discover/strategies/help-parser.js.map +1 -1
- package/dist/events/payload-inspector.d.ts +25 -0
- package/dist/events/payload-inspector.d.ts.map +1 -0
- package/dist/events/payload-inspector.js +210 -0
- package/dist/events/payload-inspector.js.map +1 -0
- package/dist/events/protocol.d.ts +227 -0
- package/dist/events/protocol.d.ts.map +1 -0
- package/dist/events/protocol.js +422 -0
- package/dist/events/protocol.js.map +1 -0
- package/dist/events/registry.d.ts +57 -0
- package/dist/events/registry.d.ts.map +1 -0
- package/dist/events/registry.js +179 -0
- package/dist/events/registry.js.map +1 -0
- package/dist/events/schema-validator.d.ts +13 -0
- package/dist/events/schema-validator.d.ts.map +1 -0
- package/dist/events/schema-validator.js +54 -0
- package/dist/events/schema-validator.js.map +1 -0
- package/dist/events/service.d.ts +97 -0
- package/dist/events/service.d.ts.map +1 -0
- package/dist/events/service.js +918 -0
- package/dist/events/service.js.map +1 -0
- package/dist/events/stream-manager.d.ts +58 -0
- package/dist/events/stream-manager.d.ts.map +1 -0
- package/dist/events/stream-manager.js +94 -0
- package/dist/events/stream-manager.js.map +1 -0
- package/dist/events/upstream-client.d.ts +88 -0
- package/dist/events/upstream-client.d.ts.map +1 -0
- package/dist/events/upstream-client.js +437 -0
- package/dist/events/upstream-client.js.map +1 -0
- package/dist/gateway.d.ts +14 -0
- package/dist/gateway.d.ts.map +1 -1
- package/dist/gateway.js +197 -24
- package/dist/gateway.js.map +1 -1
- package/dist/hitl/approval-dashboard.d.ts +4 -0
- package/dist/hitl/approval-dashboard.d.ts.map +1 -1
- package/dist/hitl/approval-dashboard.js +62 -21
- package/dist/hitl/approval-dashboard.js.map +1 -1
- package/dist/hitl/approval-stream.d.ts +2 -0
- package/dist/hitl/approval-stream.d.ts.map +1 -1
- package/dist/hitl/approval-stream.js +10 -0
- package/dist/hitl/approval-stream.js.map +1 -1
- package/dist/hitl/engine.d.ts +18 -0
- package/dist/hitl/engine.d.ts.map +1 -1
- package/dist/hitl/engine.js +78 -27
- package/dist/hitl/engine.js.map +1 -1
- package/dist/hitl/formatter.d.ts.map +1 -1
- package/dist/hitl/formatter.js +6 -2
- package/dist/hitl/formatter.js.map +1 -1
- package/dist/hitl/grants.d.ts +8 -1
- package/dist/hitl/grants.d.ts.map +1 -1
- package/dist/hitl/grants.js +25 -4
- package/dist/hitl/grants.js.map +1 -1
- package/dist/hitl/parser.d.ts.map +1 -1
- package/dist/hitl/parser.js +6 -2
- package/dist/hitl/parser.js.map +1 -1
- package/dist/hitl/provider-factory.js +6 -1
- package/dist/hitl/provider-factory.js.map +1 -1
- package/dist/hitl/providers/composite.d.ts +0 -1
- package/dist/hitl/providers/composite.d.ts.map +1 -1
- package/dist/hitl/providers/composite.js +0 -3
- package/dist/hitl/providers/composite.js.map +1 -1
- package/dist/hitl/providers/dashboard.d.ts +1 -0
- package/dist/hitl/providers/dashboard.d.ts.map +1 -1
- package/dist/hitl/providers/dashboard.js +9 -2
- package/dist/hitl/providers/dashboard.js.map +1 -1
- package/dist/hitl/providers/ios.d.ts +0 -1
- package/dist/hitl/providers/ios.d.ts.map +1 -1
- package/dist/hitl/providers/ios.js +14 -27
- package/dist/hitl/providers/ios.js.map +1 -1
- package/dist/hitl/providers/macos.d.ts.map +1 -1
- package/dist/hitl/providers/macos.js +5 -0
- package/dist/hitl/providers/macos.js.map +1 -1
- package/dist/hitl/providers/telegram.d.ts.map +1 -1
- package/dist/hitl/providers/telegram.js +3 -1
- package/dist/hitl/providers/telegram.js.map +1 -1
- package/dist/hitl/providers/tui.d.ts +1 -1
- package/dist/hitl/providers/tui.d.ts.map +1 -1
- package/dist/hitl/providers/tui.js +56 -29
- package/dist/hitl/providers/tui.js.map +1 -1
- package/dist/hitl/providers/types.d.ts +11 -2
- package/dist/hitl/providers/types.d.ts.map +1 -1
- package/dist/hitl/runtime.js +1 -1
- package/dist/hitl/runtime.js.map +1 -1
- package/dist/hook/api.js +2 -1
- package/dist/hook/api.js.map +1 -1
- package/dist/index.js +15 -3
- package/dist/index.js.map +1 -1
- package/dist/introspect/cli.d.ts.map +1 -1
- package/dist/introspect/cli.js +5 -2
- package/dist/introspect/cli.js.map +1 -1
- package/dist/mcp-apps/ui.d.ts +69 -0
- package/dist/mcp-apps/ui.d.ts.map +1 -0
- package/dist/mcp-apps/ui.js +143 -0
- package/dist/mcp-apps/ui.js.map +1 -0
- package/dist/middleware/audit-args.d.ts +7 -0
- package/dist/middleware/audit-args.d.ts.map +1 -0
- package/dist/middleware/audit-args.js +23 -0
- package/dist/middleware/audit-args.js.map +1 -0
- package/dist/middleware/chain-builder.d.ts +2 -2
- package/dist/middleware/chain-builder.d.ts.map +1 -1
- package/dist/middleware/chain-builder.js +7 -3
- package/dist/middleware/chain-builder.js.map +1 -1
- package/dist/middleware/core/allowlist.d.ts.map +1 -1
- package/dist/middleware/core/allowlist.js +4 -4
- package/dist/middleware/core/allowlist.js.map +1 -1
- package/dist/middleware/core/arg-policy.d.ts +1 -0
- package/dist/middleware/core/arg-policy.d.ts.map +1 -1
- package/dist/middleware/core/arg-policy.js +34 -37
- package/dist/middleware/core/arg-policy.js.map +1 -1
- package/dist/middleware/core/command-policy.d.ts +4 -1
- package/dist/middleware/core/command-policy.d.ts.map +1 -1
- package/dist/middleware/core/command-policy.js +50 -4
- package/dist/middleware/core/command-policy.js.map +1 -1
- package/dist/middleware/core/exec-policy.d.ts.map +1 -1
- package/dist/middleware/core/exec-policy.js +5 -5
- package/dist/middleware/core/exec-policy.js.map +1 -1
- package/dist/middleware/core/execute.d.ts +5 -1
- package/dist/middleware/core/execute.d.ts.map +1 -1
- package/dist/middleware/core/execute.js +18 -15
- package/dist/middleware/core/execute.js.map +1 -1
- package/dist/middleware/core/hitl-gate.d.ts.map +1 -1
- package/dist/middleware/core/hitl-gate.js +34 -16
- package/dist/middleware/core/hitl-gate.js.map +1 -1
- package/dist/middleware/core/rate-limiter.d.ts.map +1 -1
- package/dist/middleware/core/rate-limiter.js +23 -7
- package/dist/middleware/core/rate-limiter.js.map +1 -1
- package/dist/middleware/core/schema-validator.d.ts +3 -0
- package/dist/middleware/core/schema-validator.d.ts.map +1 -1
- package/dist/middleware/core/schema-validator.js +31 -12
- package/dist/middleware/core/schema-validator.js.map +1 -1
- package/dist/middleware/core/upload-policy.d.ts +18 -0
- package/dist/middleware/core/upload-policy.d.ts.map +1 -0
- package/dist/middleware/core/upload-policy.js +98 -0
- package/dist/middleware/core/upload-policy.js.map +1 -0
- package/dist/middleware/detectors/injection-detector.d.ts.map +1 -1
- package/dist/middleware/detectors/injection-detector.js +8 -6
- package/dist/middleware/detectors/injection-detector.js.map +1 -1
- package/dist/middleware/detectors/sensitivity-classifier.d.ts.map +1 -1
- package/dist/middleware/detectors/sensitivity-classifier.js +19 -5
- package/dist/middleware/detectors/sensitivity-classifier.js.map +1 -1
- package/dist/middleware/post/untrusted-envelope.d.ts.map +1 -1
- package/dist/middleware/post/untrusted-envelope.js +4 -1
- package/dist/middleware/post/untrusted-envelope.js.map +1 -1
- package/dist/middleware/response.d.ts +33 -0
- package/dist/middleware/response.d.ts.map +1 -0
- package/dist/middleware/response.js +110 -0
- package/dist/middleware/response.js.map +1 -0
- package/dist/middleware/types.d.ts +11 -0
- package/dist/middleware/types.d.ts.map +1 -1
- package/dist/mobile/api.d.ts.map +1 -1
- package/dist/mobile/api.js +25 -32
- package/dist/mobile/api.js.map +1 -1
- package/dist/mobile/apns.d.ts +2 -2
- package/dist/mobile/apns.d.ts.map +1 -1
- package/dist/mobile/apns.js +13 -60
- package/dist/mobile/apns.js.map +1 -1
- package/dist/pool/client-credentials.d.ts +2 -0
- package/dist/pool/client-credentials.d.ts.map +1 -1
- package/dist/pool/client-credentials.js +18 -3
- package/dist/pool/client-credentials.js.map +1 -1
- package/dist/pool/credential-health.d.ts +2 -2
- package/dist/pool/credential-health.d.ts.map +1 -1
- package/dist/pool/credential-health.js +7 -2
- package/dist/pool/credential-health.js.map +1 -1
- package/dist/pool/http-client.d.ts +105 -16
- package/dist/pool/http-client.d.ts.map +1 -1
- package/dist/pool/http-client.js +712 -116
- package/dist/pool/http-client.js.map +1 -1
- package/dist/pool/oauth-callback-router.d.ts +35 -0
- package/dist/pool/oauth-callback-router.d.ts.map +1 -0
- package/dist/pool/oauth-callback-router.js +84 -0
- package/dist/pool/oauth-callback-router.js.map +1 -0
- package/dist/pool/oauth-cli.d.ts +10 -0
- package/dist/pool/oauth-cli.d.ts.map +1 -0
- package/dist/pool/oauth-cli.js +318 -0
- package/dist/pool/oauth-cli.js.map +1 -0
- package/dist/pool/oauth-lock.d.ts +149 -0
- package/dist/pool/oauth-lock.d.ts.map +1 -0
- package/dist/pool/oauth-lock.js +575 -0
- package/dist/pool/oauth-lock.js.map +1 -0
- package/dist/pool/oauth-provider.d.ts +231 -27
- package/dist/pool/oauth-provider.d.ts.map +1 -1
- package/dist/pool/oauth-provider.js +802 -142
- package/dist/pool/oauth-provider.js.map +1 -1
- package/dist/pool/oauth-refresh.d.ts +94 -0
- package/dist/pool/oauth-refresh.d.ts.map +1 -0
- package/dist/pool/oauth-refresh.js +477 -0
- package/dist/pool/oauth-refresh.js.map +1 -0
- package/dist/pool/oauth-store.d.ts +304 -0
- package/dist/pool/oauth-store.d.ts.map +1 -0
- package/dist/pool/oauth-store.js +684 -0
- package/dist/pool/oauth-store.js.map +1 -0
- package/dist/pool/pool.d.ts +37 -4
- package/dist/pool/pool.d.ts.map +1 -1
- package/dist/pool/pool.js +170 -8
- package/dist/pool/pool.js.map +1 -1
- package/dist/pool/required-mcps.d.ts +7 -0
- package/dist/pool/required-mcps.d.ts.map +1 -1
- package/dist/pool/required-mcps.js +40 -2
- package/dist/pool/required-mcps.js.map +1 -1
- package/dist/pool/sse-client.d.ts +17 -3
- package/dist/pool/sse-client.d.ts.map +1 -1
- package/dist/pool/sse-client.js +59 -6
- package/dist/pool/sse-client.js.map +1 -1
- package/dist/pool/status.d.ts +53 -0
- package/dist/pool/status.d.ts.map +1 -1
- package/dist/pool/stdio-client.d.ts +23 -3
- package/dist/pool/stdio-client.d.ts.map +1 -1
- package/dist/pool/stdio-client.js +84 -6
- package/dist/pool/stdio-client.js.map +1 -1
- package/dist/registry/airlock-policy.d.ts +1 -1
- package/dist/registry/airlock-policy.d.ts.map +1 -1
- package/dist/registry/registry.d.ts +19 -2
- package/dist/registry/registry.d.ts.map +1 -1
- package/dist/registry/registry.js +161 -9
- package/dist/registry/registry.js.map +1 -1
- package/dist/registry/sanitizer.d.ts +5 -0
- package/dist/registry/sanitizer.d.ts.map +1 -1
- package/dist/registry/sanitizer.js +17 -13
- package/dist/registry/sanitizer.js.map +1 -1
- package/dist/security/request.d.ts +19 -0
- package/dist/security/request.d.ts.map +1 -1
- package/dist/security/request.js +35 -1
- package/dist/security/request.js.map +1 -1
- package/dist/stdio-mode.d.ts.map +1 -1
- package/dist/stdio-mode.js +229 -44
- package/dist/stdio-mode.js.map +1 -1
- package/dist/tools/api.d.ts.map +1 -1
- package/dist/tools/api.js +49 -15
- package/dist/tools/api.js.map +1 -1
- package/dist/tools/exec.d.ts +1 -1
- package/dist/tools/exec.d.ts.map +1 -1
- package/dist/tools/http.d.ts +7 -3
- package/dist/tools/http.d.ts.map +1 -1
- package/dist/tools/http.js +179 -25
- package/dist/tools/http.js.map +1 -1
- package/dist/transport/agent-server.d.ts +29 -2
- package/dist/transport/agent-server.d.ts.map +1 -1
- package/dist/transport/agent-server.js +171 -27
- package/dist/transport/agent-server.js.map +1 -1
- package/dist/transport/http-server.d.ts +17 -4
- package/dist/transport/http-server.d.ts.map +1 -1
- package/dist/transport/http-server.js +106 -153
- package/dist/transport/http-server.js.map +1 -1
- package/dist/transport/keepalive.d.ts +11 -13
- package/dist/transport/keepalive.d.ts.map +1 -1
- package/dist/transport/keepalive.js +11 -13
- package/dist/transport/keepalive.js.map +1 -1
- package/dist/transport/oauth-callback.d.ts +19 -0
- package/dist/transport/oauth-callback.d.ts.map +1 -0
- package/dist/transport/oauth-callback.js +30 -0
- package/dist/transport/oauth-callback.js.map +1 -0
- package/dist/transport/session-limiter.d.ts +8 -3
- package/dist/transport/session-limiter.d.ts.map +1 -1
- package/dist/transport/session-limiter.js +8 -0
- package/dist/transport/session-limiter.js.map +1 -1
- package/dist/transport/sse-server.d.ts.map +1 -1
- package/dist/transport/sse-server.js +54 -24
- package/dist/transport/sse-server.js.map +1 -1
- package/dist/transport/stdio-server.d.ts +6 -0
- package/dist/transport/stdio-server.d.ts.map +1 -1
- package/dist/transport/stdio-server.js +19 -7
- package/dist/transport/stdio-server.js.map +1 -1
- package/dist/types.d.ts +0 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/uploads/api.d.ts +20 -0
- package/dist/uploads/api.d.ts.map +1 -0
- package/dist/uploads/api.js +280 -0
- package/dist/uploads/api.js.map +1 -0
- package/dist/uploads/refs.d.ts +48 -0
- package/dist/uploads/refs.d.ts.map +1 -0
- package/dist/uploads/refs.js +273 -0
- package/dist/uploads/refs.js.map +1 -0
- package/dist/uploads/resolve.d.ts +117 -0
- package/dist/uploads/resolve.d.ts.map +1 -0
- package/dist/uploads/resolve.js +581 -0
- package/dist/uploads/resolve.js.map +1 -0
- package/dist/uploads/store.d.ts +224 -0
- package/dist/uploads/store.d.ts.map +1 -0
- package/dist/uploads/store.js +1908 -0
- package/dist/uploads/store.js.map +1 -0
- package/dist/util/atomic-file.d.ts +6 -0
- package/dist/util/atomic-file.d.ts.map +1 -1
- package/dist/util/atomic-file.js +42 -2
- package/dist/util/atomic-file.js.map +1 -1
- package/dist/util/is-record.d.ts +3 -0
- package/dist/util/is-record.d.ts.map +1 -0
- package/dist/util/is-record.js +5 -0
- package/dist/util/is-record.js.map +1 -0
- package/dist/web/assets/airlock-shield-fPu067nB.png +0 -0
- package/dist/web/assets/favicon-DL7sWXGq.png +0 -0
- package/dist/web/assets/index-B_qY7DgQ.js +225 -0
- package/dist/web/assets/index-Bc_jHlFK.css +1 -0
- package/dist/web/assets/inter-cyrillic-ext-opsz-normal-CfYZpDVp.woff2 +0 -0
- package/dist/web/assets/inter-cyrillic-opsz-normal-BFQXbQ-f.woff2 +0 -0
- package/dist/web/assets/inter-greek-ext-opsz-normal-BmPQvyvo.woff2 +0 -0
- package/dist/web/assets/inter-greek-opsz-normal-BM0NlUE_.woff2 +0 -0
- package/dist/web/assets/inter-latin-ext-opsz-normal-DpK-iCPk.woff2 +0 -0
- package/dist/web/assets/inter-latin-opsz-normal-BwkfbSeq.woff2 +0 -0
- package/dist/web/assets/inter-vietnamese-opsz-normal-Ci9T9ieq.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
- package/dist/web/index.html +17 -0
- package/examples/airlock.yaml +3 -1
- package/examples/claude-code-setup.md +1 -1
- package/examples/docker-compose.yaml +3 -1
- package/examples/docker-gateway.yaml +2 -1
- package/examples/gateway.yaml +1 -1
- package/examples/profiles.yaml +5 -2
- package/examples/sandbox-presets.yaml +4 -1
- package/extensions/openclaw/index.ts +3 -3
- package/extensions/openclaw/openclaw.plugin.json +4 -4
- package/package.json +25 -10
- package/schema.json +2810 -2078
- package/dist/transport/mcp-normalizer.d.ts +0 -9
- package/dist/transport/mcp-normalizer.d.ts.map +0 -1
- package/dist/transport/mcp-normalizer.js +0 -12
- package/dist/transport/mcp-normalizer.js.map +0 -1
- package/extensions/openclaw/package-lock.json +0 -8548
|
@@ -1,17 +1,56 @@
|
|
|
1
|
-
import { readFile } from 'fs/promises';
|
|
2
|
-
import { join } from 'path';
|
|
3
1
|
import { execFile } from 'child_process';
|
|
4
|
-
import { createHash } from 'crypto';
|
|
2
|
+
import { createHash, randomBytes } from 'crypto';
|
|
5
3
|
import { createServer } from 'http';
|
|
4
|
+
import { setTimeout as delay } from 'timers/promises';
|
|
6
5
|
import { childLogger } from '../util/logger.js';
|
|
7
|
-
import { atomicWritePrivateFile } from '../util/atomic-file.js';
|
|
8
6
|
import { adaptiveExpiryBuffer } from './oauth-timing.js';
|
|
7
|
+
import { OAuthGrantStore, OAuthStoreConflictError, OAuthStoreReadOnlyError, grantIssuer, credentialFingerprint, drainGrantOperations, withGrantOperation, } from './oauth-store.js';
|
|
9
8
|
const log = childLogger('oauth');
|
|
10
|
-
|
|
9
|
+
let persistRetryMs = 30 * 1000;
|
|
10
|
+
/** Test hook: shorten the persistence retry interval (default 30 s). */
|
|
11
|
+
export function setPersistenceRetryMs(ms = 30 * 1000) {
|
|
12
|
+
persistRetryMs = ms;
|
|
13
|
+
}
|
|
11
14
|
const TOKEN_CHANGE_POLL_MS = 2 * 1000;
|
|
12
15
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
16
|
+
* Refresh at least this often while a refresh token is stored, even when the access token is
|
|
17
|
+
* long-lived or has no declared lifetime, so refresh-token inactivity windows never lapse on an
|
|
18
|
+
* idle gateway. Per provider: `oauth_max_refresh_interval_ms`.
|
|
19
|
+
*/
|
|
20
|
+
export const DEFAULT_MAX_REFRESH_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
21
|
+
function sameExceptIssuer(a, b) {
|
|
22
|
+
const ra = a;
|
|
23
|
+
const rb = b;
|
|
24
|
+
const keys = new Set([...Object.keys(ra), ...Object.keys(rb)]);
|
|
25
|
+
keys.delete('issuer');
|
|
26
|
+
for (const key of keys) {
|
|
27
|
+
if (JSON.stringify(ra[key]) !== JSON.stringify(rb[key]))
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* File-backed OAuth credential store for one provider (`~/.airlock/oauth/<server-id>.json`).
|
|
34
|
+
*
|
|
35
|
+
* It plays two roles:
|
|
36
|
+
*
|
|
37
|
+
* 1. The SDK-facing {@link OAuthClientProvider} used ONLY for interactive authorization (preparing
|
|
38
|
+
* a browser URL and exchanging the callback code). Everything an authorization attempt produces
|
|
39
|
+
* — a new client registration, PKCE verifier, discovery state, resource — is staged under
|
|
40
|
+
* `pending` and only replaces the stored grant when the code exchange succeeds. Starting a
|
|
41
|
+
* browser flow therefore never clobbers a grant that may still be refreshable.
|
|
42
|
+
* 2. The grant store for Airlock's own refresh engine (`oauth-refresh.ts`), which never goes
|
|
43
|
+
* through the SDK's `auth()` and so never triggers its credential invalidation.
|
|
44
|
+
*
|
|
45
|
+
* Invariant: nothing in here deletes a grant. `invalidateCredentials()` only discards staged
|
|
46
|
+
* pending state; an operator restore (`airlock oauth restore`) or a successful new authorization
|
|
47
|
+
* are the only ways a stored grant is replaced, and every replacement is backed up first.
|
|
48
|
+
*
|
|
49
|
+
* In-memory state vs disk: every mutation bumps `mutationGen`; a successful save records the
|
|
50
|
+
* generation it wrote in `persistedGen`. While they differ, memory is authoritative and `load()`
|
|
51
|
+
* does not read the file; a read that started before a mutation is discarded when it completes.
|
|
52
|
+
* Saves are compare-and-swap against the file version memory was derived from, so an operator
|
|
53
|
+
* restore (or any other writer) is never silently overwritten by stale memory.
|
|
15
54
|
*/
|
|
16
55
|
export class FileOAuthProvider {
|
|
17
56
|
serverId;
|
|
@@ -19,26 +58,103 @@ export class FileOAuthProvider {
|
|
|
19
58
|
preregisteredClientId;
|
|
20
59
|
preregisteredClientSecret;
|
|
21
60
|
relayCallbackUrl;
|
|
22
|
-
|
|
61
|
+
store;
|
|
23
62
|
data = {};
|
|
24
63
|
callbackServer;
|
|
25
|
-
|
|
64
|
+
rejectLoopbackCallback;
|
|
65
|
+
stopped = false;
|
|
26
66
|
browserOpenedAt = 0;
|
|
27
67
|
expectedState;
|
|
28
|
-
|
|
29
|
-
|
|
68
|
+
mutationGen = 0;
|
|
69
|
+
persistedGen = 0;
|
|
70
|
+
/** Grant revision memory was derived from (undefined: unknown). */
|
|
71
|
+
diskRev;
|
|
72
|
+
/**
|
|
73
|
+
* Grants that must be kept but could not be written anywhere yet (history and orphan journal
|
|
74
|
+
* both failed). Held in memory and retried with persistence; reported as persistence_pending.
|
|
75
|
+
*/
|
|
76
|
+
unpreserved = [];
|
|
77
|
+
/** The last save failed and memory holds changes that are not on disk yet. */
|
|
78
|
+
persistFailing = false;
|
|
30
79
|
persistenceRetryTimer;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
80
|
+
callbackMode;
|
|
81
|
+
callbackRouter;
|
|
82
|
+
/** Gateway mode: the code the pending authorization is waiting for. */
|
|
83
|
+
gatewayWait;
|
|
84
|
+
withdrawGatewayCallback;
|
|
85
|
+
/**
|
|
86
|
+
* Gateway mode only. The SDK sends this as the authorize request's `state`; the gateway routes
|
|
87
|
+
* the browser callback by it, so it must be present and unguessable for every authorization.
|
|
88
|
+
* Left undefined in loopback mode so existing flows keep the SDK's own behavior.
|
|
89
|
+
*/
|
|
90
|
+
state;
|
|
91
|
+
constructor(serverId, callbackPort, preregisteredClientId, preregisteredClientSecret, relayCallbackUrl, options = {}) {
|
|
34
92
|
this.serverId = serverId;
|
|
35
93
|
this.callbackPort = callbackPort;
|
|
36
94
|
this.preregisteredClientId = preregisteredClientId;
|
|
37
95
|
this.preregisteredClientSecret = preregisteredClientSecret;
|
|
38
96
|
this.relayCallbackUrl = relayCallbackUrl;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this.
|
|
97
|
+
this.store = new OAuthGrantStore(serverId, options.storage ?? 'writer', options.storeDir);
|
|
98
|
+
this.callbackMode = options.callbackMode ?? 'loopback';
|
|
99
|
+
this.callbackRouter = options.callbackRouter;
|
|
100
|
+
if (this.callbackMode === 'gateway') {
|
|
101
|
+
if (!this.relayCallbackUrl) {
|
|
102
|
+
throw new Error(`Provider "${serverId}" uses oauth_callback_mode: gateway without oauth_callback_url`);
|
|
103
|
+
}
|
|
104
|
+
this.state = () => randomBytes(32).toString('base64url');
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
get storageMode() {
|
|
108
|
+
return this.store.mode;
|
|
109
|
+
}
|
|
110
|
+
/** Whether this process may refresh/write this grant right now (holds the writer lock). */
|
|
111
|
+
canWrite() {
|
|
112
|
+
return this.store.canWrite();
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Run a grant operation (refresh exchange, authorization-code exchange) exclusively for this
|
|
116
|
+
* grant file within the process. See {@link withGrantOperation}.
|
|
117
|
+
*/
|
|
118
|
+
runExclusive(fn) {
|
|
119
|
+
return withGrantOperation(this.store.path, fn);
|
|
120
|
+
}
|
|
121
|
+
/** Set by {@link close}: no new token exchange may start. */
|
|
122
|
+
closing = false;
|
|
123
|
+
/**
|
|
124
|
+
* Shutdown: admit no new exchanges, wait (up to `timeoutMs`) for one already sent to be committed
|
|
125
|
+
* or parked, then make one last attempt to write anything still held only in memory. Returns
|
|
126
|
+
* false when something could not be made durable in time (it is logged).
|
|
127
|
+
*/
|
|
128
|
+
async close(timeoutMs) {
|
|
129
|
+
this.closing = true;
|
|
130
|
+
const drained = await drainGrantOperations(timeoutMs, this.store.path);
|
|
131
|
+
if (!drained) {
|
|
132
|
+
log.error({ serverId: this.serverId, timeoutMs }, 'OAuth exchange still in flight at shutdown deadline; its result may be lost');
|
|
133
|
+
}
|
|
134
|
+
const persisted = this.persistencePending ? await this.retryPersistenceNow() : true;
|
|
135
|
+
if (!persisted) {
|
|
136
|
+
log.error({ serverId: this.serverId }, 'OAuth grant changes could not be written before shutdown');
|
|
137
|
+
}
|
|
138
|
+
if (this.persistenceRetryTimer) {
|
|
139
|
+
clearTimeout(this.persistenceRetryTimer);
|
|
140
|
+
this.persistenceRetryTimer = undefined;
|
|
141
|
+
}
|
|
142
|
+
return drained && persisted;
|
|
143
|
+
}
|
|
144
|
+
/** Subscribe to "this process just acquired the OAuth writer lock". */
|
|
145
|
+
onWriterAcquired(cb) {
|
|
146
|
+
return this.store.onLockAcquired(cb);
|
|
147
|
+
}
|
|
148
|
+
/** A token change is held in memory because the disk write failed (retrying). */
|
|
149
|
+
get persistencePending() {
|
|
150
|
+
return ((this.persistFailing && this.mutationGen !== this.persistedGen) || this.unpreserved.length > 0);
|
|
151
|
+
}
|
|
152
|
+
/** The unresolved refresh exchange recorded in the grant, if any (as last loaded). */
|
|
153
|
+
get exchangeMarker() {
|
|
154
|
+
return this.data.exchange;
|
|
155
|
+
}
|
|
156
|
+
touched() {
|
|
157
|
+
this.mutationGen++;
|
|
42
158
|
}
|
|
43
159
|
get redirectUrl() {
|
|
44
160
|
if (this.relayCallbackUrl) {
|
|
@@ -55,87 +171,439 @@ export class FileOAuthProvider {
|
|
|
55
171
|
response_types: ['code'],
|
|
56
172
|
};
|
|
57
173
|
}
|
|
174
|
+
preregisteredClient() {
|
|
175
|
+
if (!this.preregisteredClientId)
|
|
176
|
+
return undefined;
|
|
177
|
+
return {
|
|
178
|
+
client_id: this.preregisteredClientId,
|
|
179
|
+
redirect_uris: [this.redirectUrl],
|
|
180
|
+
...(this.preregisteredClientSecret && {
|
|
181
|
+
client_secret: this.preregisteredClientSecret,
|
|
182
|
+
}),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
// ─── SDK-facing (interactive authorization only) ──────────────────────────────────────────────
|
|
58
186
|
async clientInformation() {
|
|
59
|
-
//
|
|
60
|
-
|
|
61
|
-
if (
|
|
62
|
-
return
|
|
63
|
-
client_id: this.preregisteredClientId,
|
|
64
|
-
redirect_uris: [this.redirectUrl],
|
|
65
|
-
...(this.preregisteredClientSecret && {
|
|
66
|
-
client_secret: this.preregisteredClientSecret,
|
|
67
|
-
}),
|
|
68
|
-
};
|
|
69
|
-
}
|
|
187
|
+
// Pre-registered credentials skip dynamic client registration.
|
|
188
|
+
const preregistered = this.preregisteredClient();
|
|
189
|
+
if (preregistered)
|
|
190
|
+
return preregistered;
|
|
70
191
|
await this.load();
|
|
71
|
-
const info = this.data.clientInfo;
|
|
192
|
+
const info = this.data.pending?.clientInfo ?? this.data.clientInfo;
|
|
72
193
|
if (info?.redirect_uris?.length && !info.redirect_uris.includes(this.redirectUrl)) {
|
|
194
|
+
// Register a new client for the next authorization, but keep the stored registration: the
|
|
195
|
+
// stored refresh token is bound to it and refresh does not involve a redirect URI.
|
|
73
196
|
log.info({
|
|
74
197
|
serverId: this.serverId,
|
|
75
198
|
redirectUrl: this.redirectUrl,
|
|
76
199
|
cachedRedirectUris: info.redirect_uris,
|
|
77
|
-
}, '
|
|
78
|
-
this.data = {};
|
|
79
|
-
await this.save();
|
|
200
|
+
}, 'Cached OAuth client registration uses a different redirect URI; the next authorization registers a new client (stored grant kept)');
|
|
80
201
|
return undefined;
|
|
81
202
|
}
|
|
82
|
-
return
|
|
203
|
+
return info;
|
|
83
204
|
}
|
|
84
205
|
async saveClientInformation(info) {
|
|
85
|
-
this.
|
|
86
|
-
|
|
206
|
+
if (this.preregisteredClientId)
|
|
207
|
+
return;
|
|
208
|
+
await this.loadForMutation();
|
|
209
|
+
if (this.data.pending?.clientInfo?.client_id === info.client_id) {
|
|
210
|
+
this.data.pending.clientInfo = info;
|
|
211
|
+
}
|
|
212
|
+
else if (this.data.clientInfo?.client_id === info.client_id || !this.data.tokens) {
|
|
213
|
+
// Same registration (e.g. the SDK back-stamping its issuer) or no grant to protect yet.
|
|
214
|
+
this.data.clientInfo = info;
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
// A new registration while a grant exists: stage it until the new authorization succeeds.
|
|
218
|
+
this.data.pending = { startedAt: Date.now(), ...this.data.pending, clientInfo: info };
|
|
219
|
+
}
|
|
220
|
+
this.touched();
|
|
221
|
+
await this.save('client-registration');
|
|
87
222
|
}
|
|
88
223
|
async tokens() {
|
|
89
224
|
await this.load();
|
|
90
225
|
return this.data.tokens;
|
|
91
226
|
}
|
|
227
|
+
/**
|
|
228
|
+
* Called by the SDK when an authorization-code exchange succeeds (or to back-stamp an issuer on
|
|
229
|
+
* the stored tokens). With an authorization attempt pending this commits the new grant — the
|
|
230
|
+
* only automatic way a stored grant is replaced. Without one, it behaves like a refresh commit.
|
|
231
|
+
*/
|
|
92
232
|
async saveTokens(tokens) {
|
|
93
|
-
|
|
94
|
-
|
|
233
|
+
try {
|
|
234
|
+
await this.loadForMutation();
|
|
235
|
+
}
|
|
236
|
+
catch (err) {
|
|
237
|
+
// The stored grant cannot be read, so nothing may be published on top of it. Tokens the
|
|
238
|
+
// issuer just issued are kept anyway (history, else orphan journal, else memory + retry).
|
|
239
|
+
if (tokens.refresh_token || tokens.access_token) {
|
|
240
|
+
await this.preserve({ tokens }, 'unobserved-save');
|
|
241
|
+
}
|
|
242
|
+
throw err;
|
|
243
|
+
}
|
|
244
|
+
const stamped = tokens;
|
|
245
|
+
const current = this.data.tokens;
|
|
246
|
+
if (current && sameExceptIssuer(current, stamped)) {
|
|
247
|
+
// Identical credentials; the SDK is only stamping the issuer (SEP-2352). Anything else (a
|
|
248
|
+
// different refresh token, a completed authorization) is a real commit below.
|
|
249
|
+
if (stamped.issuer && current.issuer !== stamped.issuer) {
|
|
250
|
+
this.data.tokens = { ...current, issuer: stamped.issuer };
|
|
251
|
+
this.data.issuer ??= stamped.issuer;
|
|
252
|
+
this.touched();
|
|
253
|
+
await this.persistGrant('issuer-stamp');
|
|
254
|
+
}
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const pending = this.data.pending;
|
|
258
|
+
if (pending) {
|
|
259
|
+
const previous = this.data;
|
|
260
|
+
const issuer = stamped.issuer ?? pending.issuer;
|
|
261
|
+
let clientInfo = pending.clientInfo ?? previous.clientInfo;
|
|
262
|
+
if (clientInfo && issuer && clientInfo.issuer !== issuer) {
|
|
263
|
+
clientInfo = { ...clientInfo, issuer };
|
|
264
|
+
}
|
|
265
|
+
this.data = {
|
|
266
|
+
clientInfo,
|
|
267
|
+
tokens,
|
|
268
|
+
obtainedAt: Date.now(),
|
|
269
|
+
...(issuer ? { issuer } : {}),
|
|
270
|
+
...(pending.discovery ? { discovery: pending.discovery } : {}),
|
|
271
|
+
...(pending.resource ? { resource: pending.resource } : {}),
|
|
272
|
+
};
|
|
273
|
+
this.touched();
|
|
274
|
+
log.info({
|
|
275
|
+
serverId: this.serverId,
|
|
276
|
+
issuer,
|
|
277
|
+
previousIssuer: grantIssuer(previous),
|
|
278
|
+
replacedGrant: Boolean(previous.tokens),
|
|
279
|
+
refreshTokenPresent: Boolean(tokens.refresh_token),
|
|
280
|
+
resource: pending.resource,
|
|
281
|
+
}, 'OAuth authorization completed; stored the new grant (previous grant kept in history)');
|
|
282
|
+
await this.persistGrant('authorization');
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
await this.commitRefresh(tokens, stamped.issuer ? { issuer: stamped.issuer } : {});
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* The SDK calls this on invalid_grant / invalid_client / unauthorized_client. Airlock never lets
|
|
289
|
+
* that delete a grant: an issuer outage, a clock problem, a second process racing a rotation, or
|
|
290
|
+
* a misrouted request (wrong AS, missing `resource`) all produce those errors for grants that
|
|
291
|
+
* are still good. Only staged authorization state is discarded.
|
|
292
|
+
*/
|
|
293
|
+
async invalidateCredentials(scope) {
|
|
95
294
|
await this.load();
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
295
|
+
if (this.diskRev === undefined)
|
|
296
|
+
return; // nothing observed: nothing staged to discard
|
|
297
|
+
log.warn({
|
|
298
|
+
serverId: this.serverId,
|
|
299
|
+
scope,
|
|
300
|
+
refreshTokenPresent: Boolean(this.data.tokens?.refresh_token),
|
|
301
|
+
}, 'OAuth SDK requested credential invalidation; keeping the stored grant (only staged authorization state is discarded)');
|
|
302
|
+
const pending = this.data.pending;
|
|
303
|
+
if (!pending)
|
|
304
|
+
return;
|
|
305
|
+
if (scope === 'all')
|
|
306
|
+
delete this.data.pending;
|
|
307
|
+
if (scope === 'client')
|
|
308
|
+
delete pending.clientInfo;
|
|
309
|
+
if (scope === 'verifier')
|
|
310
|
+
delete pending.codeVerifier;
|
|
311
|
+
if (scope === 'discovery')
|
|
312
|
+
delete pending.discovery;
|
|
313
|
+
if (scope === 'tokens')
|
|
314
|
+
return;
|
|
315
|
+
this.touched();
|
|
316
|
+
await this.save('authorization-reset').catch(() => { });
|
|
317
|
+
}
|
|
318
|
+
/** True when this provider can receive an interactive browser callback in this process. */
|
|
319
|
+
canHandleAuthCallback() {
|
|
320
|
+
return this.callbackMode !== 'gateway' || Boolean(this.callbackRouter);
|
|
321
|
+
}
|
|
322
|
+
redirectToAuthorization(url) {
|
|
323
|
+
if (this.stopped)
|
|
324
|
+
return Promise.resolve();
|
|
325
|
+
if (this.callbackMode === 'gateway') {
|
|
326
|
+
if (!this.callbackRouter) {
|
|
327
|
+
log.warn({ serverId: this.serverId }, 'Skipping browser OAuth launch: oauth_callback_mode: gateway requires the gateway HTTP listener');
|
|
328
|
+
return Promise.resolve();
|
|
329
|
+
}
|
|
330
|
+
// The SDK has stored a new PKCE verifier. Show the corresponding URL every time we
|
|
331
|
+
// supersede an attempt, so the operator always has a link that can complete the exchange.
|
|
332
|
+
this.armGatewayCallback(url.searchParams.get('state') ?? '');
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
const now = Date.now();
|
|
336
|
+
if (now - this.browserOpenedAt < 30_000) {
|
|
337
|
+
log.debug({ serverId: this.serverId }, 'Skipping duplicate browser open (debounced)');
|
|
338
|
+
return Promise.resolve();
|
|
339
|
+
}
|
|
340
|
+
this.browserOpenedAt = now;
|
|
341
|
+
// When using an HTTPS relay, wrap the state parameter with the local port
|
|
342
|
+
// so the relay can redirect back to localhost after the OAuth provider responds.
|
|
343
|
+
const originalState = url.searchParams.get('state') ?? '';
|
|
344
|
+
this.expectedState = originalState;
|
|
345
|
+
if (this.relayCallbackUrl) {
|
|
346
|
+
url.searchParams.set('state', `${this.callbackPort}.${originalState}`);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
log.info({ serverId: this.serverId, url: url.toString() }, 'Opening browser for OAuth authorization');
|
|
350
|
+
const cmd = process.platform === 'darwin' ? 'open' : 'xdg-open';
|
|
351
|
+
execFile(cmd, [url.toString()]);
|
|
352
|
+
return Promise.resolve();
|
|
353
|
+
}
|
|
354
|
+
/** Stage the PKCE verifier for a new authorization. The stored grant is not touched. */
|
|
355
|
+
async saveCodeVerifier(verifier) {
|
|
356
|
+
await this.loadForMutation();
|
|
357
|
+
this.data.pending = { startedAt: Date.now(), ...this.data.pending, codeVerifier: verifier };
|
|
358
|
+
delete this.data.codeVerifier;
|
|
359
|
+
this.touched();
|
|
360
|
+
await this.save('authorization-start');
|
|
361
|
+
}
|
|
362
|
+
async codeVerifier() {
|
|
363
|
+
await this.load();
|
|
364
|
+
return this.data.pending?.codeVerifier ?? this.data.codeVerifier ?? '';
|
|
365
|
+
}
|
|
366
|
+
/** Discovery state of the authorization attempt in progress (binds the callback leg's issuer). */
|
|
367
|
+
async discoveryState() {
|
|
368
|
+
await this.load();
|
|
369
|
+
return this.data.pending?.discovery;
|
|
370
|
+
}
|
|
371
|
+
async saveDiscoveryState(state) {
|
|
372
|
+
await this.loadForMutation();
|
|
373
|
+
this.data.pending = { startedAt: Date.now(), ...this.data.pending, discovery: state };
|
|
374
|
+
this.touched();
|
|
375
|
+
await this.save('authorization-discovery');
|
|
376
|
+
}
|
|
377
|
+
async saveResourceUrl(resourceUrl) {
|
|
378
|
+
await this.loadForMutation();
|
|
379
|
+
if (this.data.pending?.resource === resourceUrl)
|
|
380
|
+
return;
|
|
381
|
+
this.data.pending = { startedAt: Date.now(), ...this.data.pending, resource: resourceUrl };
|
|
382
|
+
this.touched();
|
|
383
|
+
await this.save('authorization-resource');
|
|
384
|
+
}
|
|
385
|
+
async saveAuthorizationServerUrl(issuer) {
|
|
386
|
+
await this.loadForMutation();
|
|
387
|
+
if (this.data.pending?.issuer === issuer)
|
|
388
|
+
return;
|
|
389
|
+
this.data.pending = { startedAt: Date.now(), ...this.data.pending, issuer };
|
|
390
|
+
this.touched();
|
|
391
|
+
await this.save('authorization-issuer');
|
|
392
|
+
}
|
|
393
|
+
/** Start a fresh authorization attempt: discard stale staged state, keep the stored grant. */
|
|
394
|
+
async beginAuthorization() {
|
|
395
|
+
await this.loadForMutation();
|
|
396
|
+
this.data.pending = { startedAt: Date.now() };
|
|
397
|
+
delete this.data.codeVerifier;
|
|
398
|
+
this.touched();
|
|
399
|
+
await this.save('authorization-start');
|
|
400
|
+
}
|
|
401
|
+
// ─── Refresh-engine facing ────────────────────────────────────────────────────────────────────
|
|
402
|
+
async grant() {
|
|
403
|
+
await this.loadForMutation();
|
|
404
|
+
return {
|
|
405
|
+
tokens: this.data.tokens,
|
|
406
|
+
clientInfo: this.preregisteredClient() ?? this.data.clientInfo,
|
|
407
|
+
issuer: grantIssuer(this.data),
|
|
408
|
+
discovery: this.data.discovery,
|
|
409
|
+
resource: this.data.resource,
|
|
410
|
+
obtainedAt: this.data.obtainedAt,
|
|
411
|
+
...(this.data.exchange ? { exchange: this.data.exchange } : {}),
|
|
412
|
+
...(this.diskRev !== undefined ? { revision: this.diskRev } : {}),
|
|
413
|
+
credentials: credentialFingerprint(this.data),
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Durably record that a refresh exchange is about to be sent. This is also the fencing check:
|
|
418
|
+
* the write re-verifies the writer lock. When it fails nothing may be sent — a refresh token
|
|
419
|
+
* must never be spent unless its successor can be committed.
|
|
420
|
+
*/
|
|
421
|
+
async beginExchange(marker) {
|
|
108
422
|
try {
|
|
109
|
-
await this.
|
|
423
|
+
await this.loadForMutation();
|
|
110
424
|
}
|
|
111
425
|
catch (err) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
426
|
+
return {
|
|
427
|
+
ok: false,
|
|
428
|
+
kind: 'unwritable',
|
|
429
|
+
message: err instanceof Error ? err.message : String(err),
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
const wasClean = this.mutationGen === this.persistedGen;
|
|
433
|
+
const previous = this.data.exchange;
|
|
434
|
+
this.data.exchange = marker;
|
|
435
|
+
this.touched();
|
|
436
|
+
try {
|
|
437
|
+
await this.save('refresh-start');
|
|
438
|
+
return { ok: true };
|
|
439
|
+
}
|
|
440
|
+
catch (err) {
|
|
441
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
442
|
+
if (err instanceof OAuthStoreConflictError)
|
|
443
|
+
return { ok: false, kind: 'conflict', message };
|
|
444
|
+
if (previous)
|
|
445
|
+
this.data.exchange = previous;
|
|
446
|
+
else
|
|
447
|
+
delete this.data.exchange;
|
|
448
|
+
this.touched();
|
|
449
|
+
// The file still holds the pre-marker state; if memory was clean before, it is clean again.
|
|
450
|
+
if (wasClean)
|
|
451
|
+
this.persistedGen = this.mutationGen;
|
|
452
|
+
return {
|
|
453
|
+
ok: false,
|
|
454
|
+
kind: err instanceof OAuthStoreReadOnlyError ? 'read_only' : 'unwritable',
|
|
455
|
+
message,
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/** Replace (or clear) the exchange marker after an exchange without a successor. */
|
|
460
|
+
async endExchange(next) {
|
|
461
|
+
await this.loadForMutation();
|
|
462
|
+
if (next)
|
|
463
|
+
this.data.exchange = next;
|
|
464
|
+
else
|
|
465
|
+
delete this.data.exchange;
|
|
466
|
+
this.touched();
|
|
467
|
+
await this.persistGrant(next ? 'refresh-ambiguous' : 'refresh-end');
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Adopt successors parked by a process that lost the writer lock mid-exchange (call while
|
|
471
|
+
* holding the lock, inside {@link runExclusive}). A successor whose predecessor is the stored
|
|
472
|
+
* refresh token replaces it; any other is archived to history, never dropped.
|
|
473
|
+
*/
|
|
474
|
+
async adoptOrphanedSuccessors() {
|
|
475
|
+
const orphans = await this.store.listOrphans();
|
|
476
|
+
if (orphans.length === 0)
|
|
477
|
+
return;
|
|
478
|
+
await this.loadForMutation();
|
|
479
|
+
for (const orphan of orphans) {
|
|
480
|
+
const current = this.data.tokens?.refresh_token;
|
|
481
|
+
// Adopt only while the stored credentials are exactly the ones the exchange started from.
|
|
482
|
+
// Revisions that only changed markers or staged state do not block adoption; any credential
|
|
483
|
+
// change (a later refresh — also with non-rotating refresh tokens — an authorization, a
|
|
484
|
+
// restore) does, and the parked successor is then archived instead.
|
|
485
|
+
// Orphans without a credential fingerprint (older format) are never adopted, only archived.
|
|
486
|
+
const matches = Boolean(current) &&
|
|
487
|
+
orphan.predecessorCredentials !== undefined &&
|
|
488
|
+
orphan.predecessorCredentials === credentialFingerprint(this.data);
|
|
489
|
+
if (matches) {
|
|
490
|
+
const { pending: _pending, exchange: _exchange, ...grant } = orphan.grant;
|
|
491
|
+
this.data = { ...grant, ...(this.data.pending ? { pending: this.data.pending } : {}) };
|
|
492
|
+
this.touched();
|
|
493
|
+
const result = await this.persistGrant('adopt-successor');
|
|
494
|
+
if (result !== 'persisted')
|
|
495
|
+
return; // keep the orphan file until the adoption is durable
|
|
496
|
+
log.warn({ serverId: this.serverId }, 'Adopted an OAuth successor grant left by a process that lost the writer lock mid-refresh');
|
|
497
|
+
}
|
|
498
|
+
else {
|
|
499
|
+
if (!(await this.store.archive(orphan.grant, 'orphan-successor'))) {
|
|
500
|
+
// No durable copy elsewhere: keep the orphan file itself, try again next time.
|
|
501
|
+
log.warn({ serverId: this.serverId }, 'Could not archive an orphaned OAuth successor; keeping it parked');
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
log.warn({ serverId: this.serverId }, 'Archived an orphaned OAuth successor that no longer matches the stored grant (kept in history)');
|
|
505
|
+
}
|
|
506
|
+
await this.store.removeOrphan(orphan.file);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Persist tokens obtained by refreshing the stored grant. With a `basis`, the commit is checked
|
|
511
|
+
* for coherence first: when the stored grant is no longer the one that was refreshed (a new
|
|
512
|
+
* authorization or a restore landed meanwhile) the result is archived instead of mixing one
|
|
513
|
+
* grant's refresh token with another grant's client registration.
|
|
514
|
+
*/
|
|
515
|
+
async commitRefresh(tokens, meta = {}, basis) {
|
|
516
|
+
// A fresh provider instance must load the persisted refresh token before handling a refresh
|
|
517
|
+
// response that may omit it.
|
|
518
|
+
await this.load();
|
|
519
|
+
if (basis) {
|
|
520
|
+
const currentClientId = this.preregisteredClient()?.client_id ?? this.data.clientInfo?.client_id;
|
|
521
|
+
// Coherent only while the stored credentials are exactly the ones the exchange started
|
|
522
|
+
// from (refresh + access token + client). A late result must never overwrite a newer
|
|
523
|
+
// access token (non-rotating refresh tokens) or another grant's registration.
|
|
524
|
+
const coherent = basis.credentials
|
|
525
|
+
? credentialFingerprint(this.data) === basis.credentials
|
|
526
|
+
: this.data.tokens?.refresh_token === basis.refreshToken &&
|
|
527
|
+
currentClientId === basis.clientInfo.client_id;
|
|
528
|
+
if (!coherent) {
|
|
529
|
+
await this.preserve({
|
|
530
|
+
clientInfo: basis.clientInfo,
|
|
531
|
+
tokens: tokens.refresh_token
|
|
532
|
+
? tokens
|
|
533
|
+
: { ...tokens, refresh_token: basis.refreshToken },
|
|
534
|
+
obtainedAt: Date.now(),
|
|
535
|
+
...(meta.issuer ? { issuer: meta.issuer } : {}),
|
|
536
|
+
...(meta.discovery ? { discovery: meta.discovery } : {}),
|
|
537
|
+
...(meta.resource ? { resource: meta.resource } : {}),
|
|
538
|
+
}, 'superseded-refresh');
|
|
539
|
+
log.warn({ serverId: this.serverId }, 'A different OAuth grant was stored while a refresh was in flight; kept the stored grant and archived the refresh result to history');
|
|
540
|
+
return 'superseded';
|
|
541
|
+
}
|
|
117
542
|
}
|
|
543
|
+
const previous = this.data.tokens;
|
|
544
|
+
const previousObtainedAt = this.data.obtainedAt;
|
|
545
|
+
const refreshTokenReturned = Boolean(tokens.refresh_token);
|
|
546
|
+
const refreshTokenRotated = Boolean(tokens.refresh_token &&
|
|
547
|
+
previous?.refresh_token &&
|
|
548
|
+
tokens.refresh_token !== previous.refresh_token);
|
|
549
|
+
// Preserve an existing refresh_token when a refresh response omits one. Many OAuth providers
|
|
550
|
+
// return refresh_token only on the initial authorization; overwriting would drop it.
|
|
551
|
+
if (!tokens.refresh_token && previous?.refresh_token) {
|
|
552
|
+
tokens = { ...tokens, refresh_token: previous.refresh_token };
|
|
553
|
+
}
|
|
554
|
+
const issuer = meta.issuer ?? previous?.issuer;
|
|
555
|
+
this.data.tokens = issuer ? { ...tokens, issuer } : tokens;
|
|
556
|
+
this.data.obtainedAt = Date.now();
|
|
557
|
+
if (meta.issuer) {
|
|
558
|
+
this.data.issuer = meta.issuer;
|
|
559
|
+
const client = this.data.clientInfo;
|
|
560
|
+
if (client && client.issuer && client.issuer !== meta.issuer) {
|
|
561
|
+
this.data.clientInfo = { ...client, issuer: meta.issuer };
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
if (meta.discovery)
|
|
565
|
+
this.data.discovery = meta.discovery;
|
|
566
|
+
if (meta.resource && !this.data.resource)
|
|
567
|
+
this.data.resource = meta.resource;
|
|
568
|
+
delete this.data.exchange; // committed in the same write that stores the successor
|
|
569
|
+
this.touched();
|
|
570
|
+
return this.persistGrant('refresh', {
|
|
571
|
+
refreshTokenReturned,
|
|
572
|
+
refreshTokenRotated,
|
|
573
|
+
accessExpiresInSec: tokens.expires_in,
|
|
574
|
+
previousTokenAgeSec: previousObtainedAt
|
|
575
|
+
? Math.floor((this.data.obtainedAt - previousObtainedAt) / 1000)
|
|
576
|
+
: undefined,
|
|
577
|
+
}, basis);
|
|
118
578
|
}
|
|
119
579
|
/**
|
|
120
|
-
* How long (ms) until the
|
|
121
|
-
* expires
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
* The caller drives proactive refresh: many OAuth servers signal access-token expiry with an
|
|
126
|
-
* application-level error rather than HTTP 401, so the reactive on-401 refresh never fires and the
|
|
127
|
-
* connection silently rots. Refreshing ahead of expiry avoids that entirely.
|
|
580
|
+
* How long (ms) until the grant should be proactively refreshed: `bufferMs` before the access
|
|
581
|
+
* token expires, and in any case no later than `maxIntervalMs` after the tokens were obtained (so
|
|
582
|
+
* a refresh token with an inactivity window is exercised even when the access token is
|
|
583
|
+
* long-lived or declares no lifetime). Returns `undefined` only without a refresh_token. Never
|
|
584
|
+
* negative. Tokens persisted before `obtainedAt` existed report `0` (refresh promptly).
|
|
128
585
|
*/
|
|
129
|
-
async msUntilRefresh(bufferMs) {
|
|
586
|
+
async msUntilRefresh(bufferMs, maxIntervalMs = DEFAULT_MAX_REFRESH_INTERVAL_MS) {
|
|
130
587
|
await this.load();
|
|
131
588
|
const tokens = this.data.tokens;
|
|
132
|
-
if (!tokens?.refresh_token
|
|
589
|
+
if (!tokens?.refresh_token)
|
|
133
590
|
return undefined;
|
|
134
591
|
const obtainedAt = this.data.obtainedAt ?? 0;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
592
|
+
let dueAt = obtainedAt + maxIntervalMs;
|
|
593
|
+
if (tokens.expires_in) {
|
|
594
|
+
const lifetimeMs = tokens.expires_in * 1000;
|
|
595
|
+
const effectiveBufferMs = adaptiveExpiryBuffer(lifetimeMs, bufferMs);
|
|
596
|
+
dueAt = Math.min(dueAt, obtainedAt + lifetimeMs - effectiveBufferMs);
|
|
597
|
+
}
|
|
598
|
+
return Math.max(0, dueAt - Date.now());
|
|
599
|
+
}
|
|
600
|
+
/** Whether the stored access token is past its declared expiry (unknown lifetime → false). */
|
|
601
|
+
async accessTokenExpired() {
|
|
602
|
+
await this.load();
|
|
603
|
+
const tokens = this.data.tokens;
|
|
604
|
+
if (!tokens?.expires_in || !this.data.obtainedAt)
|
|
605
|
+
return false;
|
|
606
|
+
return Date.now() >= this.data.obtainedAt + tokens.expires_in * 1000;
|
|
139
607
|
}
|
|
140
608
|
/** Opaque marker used to notice credentials completed by another Airlock process/tool. */
|
|
141
609
|
async tokenVersion() {
|
|
@@ -144,67 +612,39 @@ export class FileOAuthProvider {
|
|
|
144
612
|
return undefined;
|
|
145
613
|
return createHash('sha256').update(JSON.stringify(this.data.tokens)).digest('hex');
|
|
146
614
|
}
|
|
147
|
-
/** Wait until
|
|
615
|
+
/** Wait until the token file changes (another process, an operator restore, or our refresh). */
|
|
148
616
|
async waitForTokenChange(previous, signal) {
|
|
149
617
|
while (!signal.aborted) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
618
|
+
try {
|
|
619
|
+
await delay(TOKEN_CHANGE_POLL_MS, undefined, { ref: false, signal });
|
|
620
|
+
}
|
|
621
|
+
catch (err) {
|
|
622
|
+
if (signal.aborted)
|
|
623
|
+
return;
|
|
624
|
+
throw err;
|
|
625
|
+
}
|
|
158
626
|
if (signal.aborted)
|
|
159
627
|
return;
|
|
160
628
|
if ((await this.tokenVersion()) !== previous)
|
|
161
629
|
return;
|
|
162
630
|
}
|
|
163
631
|
}
|
|
164
|
-
async invalidateCredentials(scope) {
|
|
165
|
-
log.info({ serverId: this.serverId, scope }, 'Invalidating OAuth credentials');
|
|
166
|
-
if (scope === 'all') {
|
|
167
|
-
this.data = {};
|
|
168
|
-
}
|
|
169
|
-
else {
|
|
170
|
-
delete this.data.tokens;
|
|
171
|
-
}
|
|
172
|
-
await this.save();
|
|
173
|
-
}
|
|
174
|
-
redirectToAuthorization(url) {
|
|
175
|
-
const now = Date.now();
|
|
176
|
-
if (now - this.browserOpenedAt < 30_000) {
|
|
177
|
-
log.debug({ serverId: this.serverId }, 'Skipping duplicate browser open (debounced)');
|
|
178
|
-
return Promise.resolve();
|
|
179
|
-
}
|
|
180
|
-
this.browserOpenedAt = now;
|
|
181
|
-
// When using an HTTPS relay, wrap the state parameter with the local port
|
|
182
|
-
// so the relay can redirect back to localhost after the OAuth provider responds.
|
|
183
|
-
const originalState = url.searchParams.get('state') ?? '';
|
|
184
|
-
this.expectedState = originalState;
|
|
185
|
-
if (this.relayCallbackUrl) {
|
|
186
|
-
url.searchParams.set('state', `${this.callbackPort}.${originalState}`);
|
|
187
|
-
}
|
|
188
|
-
log.info({ serverId: this.serverId, url: url.toString() }, 'Opening browser for OAuth authorization');
|
|
189
|
-
const cmd = process.platform === 'darwin' ? 'open' : 'xdg-open';
|
|
190
|
-
execFile(cmd, [url.toString()]);
|
|
191
|
-
return Promise.resolve();
|
|
192
|
-
}
|
|
193
|
-
async saveCodeVerifier(verifier) {
|
|
194
|
-
this.data.codeVerifier = verifier;
|
|
195
|
-
await this.save();
|
|
196
|
-
}
|
|
197
|
-
async codeVerifier() {
|
|
198
|
-
await this.load();
|
|
199
|
-
return this.data.codeVerifier ?? '';
|
|
200
|
-
}
|
|
201
632
|
/**
|
|
202
|
-
*
|
|
203
|
-
*
|
|
633
|
+
* Resolves with the authorization code from the browser callback. In loopback mode this starts a
|
|
634
|
+
* temporary HTTP listener; in gateway mode it waits for the gateway's callback route to deliver
|
|
635
|
+
* the code for this provider's pending state.
|
|
204
636
|
*/
|
|
205
637
|
waitForAuthCode() {
|
|
638
|
+
if (this.stopped)
|
|
639
|
+
return Promise.reject(new Error('OAuth callback cancelled'));
|
|
640
|
+
if (this.callbackMode === 'gateway') {
|
|
641
|
+
if (!this.callbackRouter) {
|
|
642
|
+
return Promise.reject(new Error(`Provider "${this.serverId}" uses oauth_callback_mode: gateway, which needs the Airlock gateway's HTTP listener`));
|
|
643
|
+
}
|
|
644
|
+
return this.getGatewayWait().promise;
|
|
645
|
+
}
|
|
206
646
|
return new Promise((resolve, reject) => {
|
|
207
|
-
this.
|
|
647
|
+
this.rejectLoopbackCallback = reject;
|
|
208
648
|
this.callbackServer = createServer((req, res) => {
|
|
209
649
|
const url = new URL(req.url ?? '/', `http://127.0.0.1:${this.callbackPort}`);
|
|
210
650
|
if (url.pathname !== '/oauth/callback') {
|
|
@@ -218,22 +658,22 @@ export class FileOAuthProvider {
|
|
|
218
658
|
if (error) {
|
|
219
659
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
220
660
|
res.end('<html><body><h2>Authorization failed</h2><p>You can close this tab.</p></body></html>');
|
|
221
|
-
this.stopCallbackServer();
|
|
222
661
|
reject(new Error(`OAuth error: ${error}`));
|
|
662
|
+
this.stopCallbackServer();
|
|
223
663
|
return;
|
|
224
664
|
}
|
|
225
665
|
if (code) {
|
|
226
666
|
if (!this.isExpectedState(state)) {
|
|
227
667
|
res.writeHead(400, { 'Content-Type': 'text/html' });
|
|
228
668
|
res.end('<html><body><h2>Authorization failed</h2><p>OAuth state mismatch.</p></body></html>');
|
|
229
|
-
this.stopCallbackServer();
|
|
230
669
|
reject(new Error('OAuth state mismatch'));
|
|
670
|
+
this.stopCallbackServer();
|
|
231
671
|
return;
|
|
232
672
|
}
|
|
233
673
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
234
674
|
res.end('<html><body><h2>Authorized!</h2><p>You can close this tab.</p></body></html>');
|
|
675
|
+
resolve(code);
|
|
235
676
|
this.stopCallbackServer();
|
|
236
|
-
this.authCodeResolve?.(code);
|
|
237
677
|
return;
|
|
238
678
|
}
|
|
239
679
|
res.writeHead(400);
|
|
@@ -245,12 +685,65 @@ export class FileOAuthProvider {
|
|
|
245
685
|
this.callbackServer.on('error', (err) => {
|
|
246
686
|
log.error({ err }, 'OAuth callback server error');
|
|
247
687
|
reject(err);
|
|
688
|
+
this.stopCallbackServer();
|
|
248
689
|
});
|
|
249
690
|
});
|
|
250
691
|
}
|
|
251
692
|
stopCallbackServer() {
|
|
252
693
|
this.callbackServer?.close();
|
|
253
694
|
this.callbackServer = undefined;
|
|
695
|
+
this.withdrawGatewayCallback?.();
|
|
696
|
+
this.withdrawGatewayCallback = undefined;
|
|
697
|
+
const error = new Error('OAuth callback cancelled');
|
|
698
|
+
this.rejectLoopbackCallback?.(error);
|
|
699
|
+
this.rejectLoopbackCallback = undefined;
|
|
700
|
+
this.gatewayWait?.reject(error);
|
|
701
|
+
this.gatewayWait = undefined;
|
|
702
|
+
}
|
|
703
|
+
stop() {
|
|
704
|
+
// An SDK authorization already in progress may still call redirectToAuthorization.
|
|
705
|
+
this.stopped = true;
|
|
706
|
+
this.stopCallbackServer();
|
|
707
|
+
}
|
|
708
|
+
getGatewayWait() {
|
|
709
|
+
if (!this.gatewayWait) {
|
|
710
|
+
let resolve;
|
|
711
|
+
let reject;
|
|
712
|
+
const promise = new Promise((resolveCode, rejectCode) => {
|
|
713
|
+
resolve = resolveCode;
|
|
714
|
+
reject = rejectCode;
|
|
715
|
+
});
|
|
716
|
+
// A callback can fail before the client starts waiting. Preserve the rejection for it.
|
|
717
|
+
void promise.catch(() => { });
|
|
718
|
+
this.gatewayWait = { promise, resolve, reject, settled: false };
|
|
719
|
+
}
|
|
720
|
+
return this.gatewayWait;
|
|
721
|
+
}
|
|
722
|
+
armGatewayCallback(state) {
|
|
723
|
+
if (!this.callbackRouter)
|
|
724
|
+
return; // waitForAuthCode reports the missing gateway listener.
|
|
725
|
+
// An existing waiter can follow a replacement state while its attempt is still pending.
|
|
726
|
+
// Once a callback has settled, a later attempt needs a fresh promise.
|
|
727
|
+
this.withdrawGatewayCallback?.();
|
|
728
|
+
this.withdrawGatewayCallback = undefined;
|
|
729
|
+
if (this.gatewayWait?.settled)
|
|
730
|
+
this.gatewayWait = undefined;
|
|
731
|
+
const wait = this.getGatewayWait();
|
|
732
|
+
if (!state) {
|
|
733
|
+
wait.settled = true;
|
|
734
|
+
wait.reject(new Error('OAuth authorization request carried no state to route the callback'));
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
this.withdrawGatewayCallback = this.callbackRouter.register(state, this.serverId, (outcome) => {
|
|
738
|
+
this.withdrawGatewayCallback = undefined;
|
|
739
|
+
wait.settled = true;
|
|
740
|
+
if ('code' in outcome) {
|
|
741
|
+
wait.resolve(outcome.code);
|
|
742
|
+
}
|
|
743
|
+
else {
|
|
744
|
+
wait.reject(new Error(`OAuth error: ${outcome.error}`));
|
|
745
|
+
}
|
|
746
|
+
});
|
|
254
747
|
}
|
|
255
748
|
isExpectedState(state) {
|
|
256
749
|
if (this.expectedState === undefined)
|
|
@@ -267,43 +760,210 @@ export class FileOAuthProvider {
|
|
|
267
760
|
return (this.relayCallbackUrl !== undefined && state === `${this.callbackPort}.${this.expectedState}`);
|
|
268
761
|
}
|
|
269
762
|
async load() {
|
|
270
|
-
//
|
|
271
|
-
//
|
|
272
|
-
if (this.
|
|
763
|
+
// Memory with changes that are not on disk yet (a rotated token whose commit failed, staged
|
|
764
|
+
// state being written) is authoritative: never replace it with the older file.
|
|
765
|
+
if (this.mutationGen !== this.persistedGen)
|
|
273
766
|
return;
|
|
767
|
+
const generation = this.mutationGen;
|
|
768
|
+
let result;
|
|
274
769
|
try {
|
|
275
|
-
|
|
276
|
-
this.data = JSON.parse(data);
|
|
770
|
+
result = await this.store.read();
|
|
277
771
|
}
|
|
278
|
-
catch {
|
|
279
|
-
this.
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
772
|
+
catch (err) {
|
|
773
|
+
log.warn({ serverId: this.serverId, reason: err instanceof Error ? err.message : String(err) }, 'Could not read OAuth grant file; using the last state held in memory');
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
// A mutation landed while the read was in flight: the read is stale, discard it.
|
|
777
|
+
if (this.mutationGen !== generation || this.mutationGen !== this.persistedGen)
|
|
778
|
+
return;
|
|
779
|
+
if (result.corrupt) {
|
|
780
|
+
log.error({ serverId: this.serverId, path: this.store.path }, 'OAuth grant file is not valid JSON; leaving it untouched (it is backed up before any replacement)');
|
|
781
|
+
}
|
|
782
|
+
this.data = result.data;
|
|
783
|
+
this.diskRev = result.rev;
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* Load for a mutation: the change must be based on a successfully observed revision (a read that
|
|
787
|
+
* succeeded, or found nothing). A failed read never turns into "empty grant" + publish.
|
|
788
|
+
*/
|
|
789
|
+
async loadForMutation() {
|
|
790
|
+
await this.load();
|
|
791
|
+
if (this.diskRev === undefined) {
|
|
792
|
+
throw new Error('the stored OAuth grant could not be read; not changing it');
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
async save(reason, opts = {}) {
|
|
796
|
+
const generation = this.mutationGen;
|
|
797
|
+
if (this.diskRev === undefined) {
|
|
798
|
+
// Never publish without knowing which revision this change is based on.
|
|
799
|
+
throw new Error('the stored OAuth grant has not been read successfully; not writing');
|
|
800
|
+
}
|
|
801
|
+
const snapshot = JSON.parse(JSON.stringify(this.data));
|
|
802
|
+
let rev;
|
|
803
|
+
try {
|
|
804
|
+
({ rev } = await this.store.write(snapshot, reason, {
|
|
805
|
+
expectRev: this.diskRev,
|
|
806
|
+
// Replacing a grant with a different one: the previous one must be safely in history.
|
|
807
|
+
...(reason === 'authorization' ? { backup: 'before' } : {}),
|
|
808
|
+
}));
|
|
809
|
+
}
|
|
810
|
+
catch (err) {
|
|
811
|
+
if (err instanceof OAuthStoreConflictError && !opts.keepOnConflict) {
|
|
812
|
+
await this.resolveConflict(reason);
|
|
813
|
+
}
|
|
814
|
+
throw err;
|
|
815
|
+
}
|
|
816
|
+
if (generation >= this.persistedGen) {
|
|
817
|
+
this.persistedGen = generation;
|
|
818
|
+
this.diskRev = rev;
|
|
819
|
+
}
|
|
820
|
+
// Keep retrying while anything is still held only in memory (also unpreserved copies).
|
|
821
|
+
if (this.persistedGen === this.mutationGen && this.unpreserved.length === 0) {
|
|
822
|
+
this.persistFailing = false;
|
|
823
|
+
if (this.persistenceRetryTimer) {
|
|
824
|
+
clearTimeout(this.persistenceRetryTimer);
|
|
825
|
+
this.persistenceRetryTimer = undefined;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
/**
|
|
830
|
+
* The file changed under us (operator restore, another writer). Keep what we had in history —
|
|
831
|
+
* never drop it — then follow the file.
|
|
832
|
+
*/
|
|
833
|
+
async resolveConflict(reason) {
|
|
834
|
+
if (this.mutationGen !== this.persistedGen && (this.data.tokens || this.data.clientInfo)) {
|
|
835
|
+
const { pending: _pending, exchange: _exchange, ...unsaved } = this.data;
|
|
836
|
+
if (!(await this.preserve(unsaved, `unsaved-${reason}`))) {
|
|
837
|
+
// preserve() keeps it in memory and retries; following the file is now safe.
|
|
838
|
+
log.warn({ serverId: this.serverId }, 'Unsaved OAuth state held in memory until it can be archived');
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
log.warn({ serverId: this.serverId, reason }, 'OAuth grant file changed on disk (operator restore or another writer); following the file, unsaved in-memory state archived to history');
|
|
842
|
+
this.persistedGen = this.mutationGen;
|
|
843
|
+
if (this.unpreserved.length === 0) {
|
|
844
|
+
this.persistFailing = false;
|
|
291
845
|
if (this.persistenceRetryTimer) {
|
|
292
846
|
clearTimeout(this.persistenceRetryTimer);
|
|
293
847
|
this.persistenceRetryTimer = undefined;
|
|
294
848
|
}
|
|
295
849
|
}
|
|
850
|
+
this.diskRev = undefined;
|
|
851
|
+
await this.load();
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* Keep a grant that is not (or no longer) the stored one: durably in history, else as a parked
|
|
855
|
+
* successor, else in memory with retries (never dropped). True when it is on disk.
|
|
856
|
+
*/
|
|
857
|
+
async preserve(grant, reason) {
|
|
858
|
+
if (await this.store.archive(grant, reason))
|
|
859
|
+
return true;
|
|
860
|
+
try {
|
|
861
|
+
await this.store.writeOrphan(grant, {
|
|
862
|
+
presentedRefreshToken: '',
|
|
863
|
+
predecessorCredentials: 'never-adopt',
|
|
864
|
+
});
|
|
865
|
+
return true;
|
|
866
|
+
}
|
|
867
|
+
catch {
|
|
868
|
+
this.unpreserved.push({ grant, reason });
|
|
869
|
+
this.persistFailing = true;
|
|
870
|
+
this.schedulePersistenceRetry();
|
|
871
|
+
log.error({ serverId: this.serverId, reason }, 'Could not write an OAuth grant copy anywhere; holding it in memory and retrying');
|
|
872
|
+
return false;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
async flushUnpreserved() {
|
|
876
|
+
// An item leaves the list only once its copy is durable, so persistencePending stays true
|
|
877
|
+
// for as long as anything exists only in memory.
|
|
878
|
+
for (const item of [...this.unpreserved]) {
|
|
879
|
+
if (await this.store.archive(item.grant, item.reason)) {
|
|
880
|
+
this.unpreserved.splice(this.unpreserved.indexOf(item), 1);
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
return this.unpreserved.length === 0;
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* Commit a token change. On storage failure hold it in memory and retry only the disk write;
|
|
887
|
+
* when this process lost the writer lock during the exchange, park it for the lock holder.
|
|
888
|
+
*/
|
|
889
|
+
async persistGrant(reason, fields = {}, basis) {
|
|
890
|
+
try {
|
|
891
|
+
// A refresh commit that loses the race for publication must not be resolved by archiving
|
|
892
|
+
// and following the file: its successor goes to the orphan journal for the owner to adopt.
|
|
893
|
+
await this.save(reason, { keepOnConflict: Boolean(basis) });
|
|
894
|
+
// Record the refresh chain without logging token values or token-derived fingerprints.
|
|
895
|
+
log.info({
|
|
896
|
+
serverId: this.serverId,
|
|
897
|
+
reason,
|
|
898
|
+
refreshTokenPresent: Boolean(this.data.tokens?.refresh_token),
|
|
899
|
+
...fields,
|
|
900
|
+
persisted: true,
|
|
901
|
+
}, 'OAuth tokens saved');
|
|
902
|
+
return 'persisted';
|
|
903
|
+
}
|
|
904
|
+
catch (err) {
|
|
905
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
906
|
+
const lostOwnership = err instanceof OAuthStoreReadOnlyError || err instanceof OAuthStoreConflictError;
|
|
907
|
+
if (lostOwnership && basis) {
|
|
908
|
+
const { pending: _pending, exchange: _exchange, ...successor } = this.data;
|
|
909
|
+
try {
|
|
910
|
+
await this.store.writeOrphan(successor, {
|
|
911
|
+
presentedRefreshToken: basis.refreshToken,
|
|
912
|
+
predecessorCredentials: basis.credentials ?? '',
|
|
913
|
+
...(basis.revision !== undefined ? { predecessorRevision: basis.revision } : {}),
|
|
914
|
+
...(basis.exchangeId ? { exchangeId: basis.exchangeId } : {}),
|
|
915
|
+
});
|
|
916
|
+
// The holder (or the newer revision's writer) owns the grant now; follow the file.
|
|
917
|
+
this.persistedGen = this.mutationGen;
|
|
918
|
+
this.persistFailing = false;
|
|
919
|
+
this.diskRev = undefined;
|
|
920
|
+
log.warn({ serverId: this.serverId, reason, error }, 'Could not publish the refreshed OAuth grant (writer lock lost or a newer revision exists); parked the successor for adoption');
|
|
921
|
+
return 'handed_off';
|
|
922
|
+
}
|
|
923
|
+
catch (orphanErr) {
|
|
924
|
+
log.warn({
|
|
925
|
+
serverId: this.serverId,
|
|
926
|
+
error: orphanErr instanceof Error ? orphanErr.message : String(orphanErr),
|
|
927
|
+
}, 'Could not park an OAuth successor for adoption; holding it in memory');
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
else if (err instanceof OAuthStoreConflictError) {
|
|
931
|
+
return 'superseded';
|
|
932
|
+
}
|
|
933
|
+
// The fresh token still works in this process. Do not make anyone re-run the token request:
|
|
934
|
+
// with rotating refresh tokens that would spend the new token too. Retry only the disk write.
|
|
935
|
+
log.warn({ serverId: this.serverId, reason, ...fields, error }, 'Could not persist OAuth tokens; holding them in memory and retrying storage');
|
|
936
|
+
this.persistFailing = true;
|
|
937
|
+
this.schedulePersistenceRetry();
|
|
938
|
+
return 'pending';
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
/** Retry pending disk writes now (also used by tests and at shutdown). */
|
|
942
|
+
async retryPersistenceNow() {
|
|
943
|
+
const preserved = await this.flushUnpreserved();
|
|
944
|
+
if (this.mutationGen === this.persistedGen)
|
|
945
|
+
return preserved;
|
|
946
|
+
try {
|
|
947
|
+
await this.save('persistence-retry');
|
|
948
|
+
return preserved && this.mutationGen === this.persistedGen;
|
|
949
|
+
}
|
|
950
|
+
catch {
|
|
951
|
+
return false;
|
|
952
|
+
}
|
|
296
953
|
}
|
|
297
954
|
schedulePersistenceRetry() {
|
|
298
955
|
if (this.persistenceRetryTimer)
|
|
299
956
|
return;
|
|
300
957
|
this.persistenceRetryTimer = setTimeout(() => {
|
|
301
958
|
this.persistenceRetryTimer = undefined;
|
|
302
|
-
void this.
|
|
303
|
-
|
|
959
|
+
void this.retryPersistenceNow().then((done) => {
|
|
960
|
+
if (done)
|
|
961
|
+
return;
|
|
962
|
+
log.warn({ serverId: this.serverId }, 'OAuth token persistence still unavailable; retrying');
|
|
963
|
+
this.persistFailing = true;
|
|
304
964
|
this.schedulePersistenceRetry();
|
|
305
965
|
});
|
|
306
|
-
},
|
|
966
|
+
}, persistRetryMs);
|
|
307
967
|
this.persistenceRetryTimer.unref?.();
|
|
308
968
|
}
|
|
309
969
|
}
|