Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

providerSetup.ts

180 lines5.8 KB

1import * as Schema from "effect/Schema";
2​
3import {
4 ForwardCompatibleArray,
5 ForwardCompatibleOptional,
6 IsoDateTime,
7 TrimmedNonEmptyString,
8} from "./baseSchemas.ts";
9import { ProviderDriverKind, ProviderInstanceId } from "./providerInstance.ts";
10​
11export const ProviderSetupInput = Schema.Struct({
12 instanceId: ProviderInstanceId,
13});
14export type ProviderSetupInput = typeof ProviderSetupInput.Type;
15​
16const SetupOperationId = TrimmedNonEmptyString.check(Schema.isMaxLength(128));
17​
18export const ProviderAuthMethod = Schema.Struct({
19 id: SetupOperationId,
20 name: TrimmedNonEmptyString,
21 description: Schema.NullOr(Schema.String),
22 type: Schema.Literals(["agent", "terminal", "credentials"]),
23});
24export type ProviderAuthMethod = typeof ProviderAuthMethod.Type;
25​
26// These describe client interactions, not OAuth grant types. The provider
27// adapter remains responsible for credentials, callbacks, and refresh.
28export const ProviderAuthInteraction = Schema.Union([
29 Schema.Struct({
30 type: Schema.Literal("browser"),
31 id: SetupOperationId,
32 url: TrimmedNonEmptyString.check(Schema.isMaxLength(16_384)),
33 requiresConsent: Schema.Boolean,
34 acceptsCallback: Schema.optionalKey(Schema.Boolean),
35 }),
36 Schema.Struct({
37 type: Schema.Literal("deviceCode"),
38 id: SetupOperationId,
39 url: TrimmedNonEmptyString.check(Schema.isMaxLength(16_384)),
40 userCode: TrimmedNonEmptyString.check(Schema.isMaxLength(256)),
41 }),
42 Schema.Struct({
43 type: Schema.Literal("terminal"),
44 id: SetupOperationId,
45 output: Schema.String.check(Schema.isMaxLength(16_384)),
46 outputOffset: Schema.optionalKey(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))),
47 }),
48 Schema.Struct({
49 type: Schema.Literal("credentials"),
50 id: SetupOperationId,
51 fields: Schema.Array(
52 Schema.Struct({
53 name: SetupOperationId,
54 label: TrimmedNonEmptyString,
55 secret: Schema.Boolean,
56 }),
57 ).check(Schema.isMaxLength(16)),
58 }),
59]);
60export type ProviderAuthInteraction = typeof ProviderAuthInteraction.Type;
61​
62export const ProviderAuthResponse = Schema.Union([
63 Schema.Struct({
64 type: Schema.Literal("browser"),
65 action: Schema.Literals(["accept", "decline"]),
66 }),
67 Schema.Struct({
68 type: Schema.Literal("terminal"),
69 data: Schema.String.check(Schema.isMaxLength(4_096)),
70 size: Schema.optionalKey(
71 Schema.Struct({
72 cols: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 500 })),
73 rows: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 200 })),
74 }),
75 ),
76 }),
77 Schema.Struct({
78 type: Schema.Literal("credentials"),
79 values: Schema.Record(SetupOperationId, Schema.String.check(Schema.isMaxLength(16_384))).check(
80 Schema.isMaxProperties(16),
81 ),
82 }),
83]);
84export type ProviderAuthResponse = typeof ProviderAuthResponse.Type;
85​
86export const ProviderAuthStartInput = Schema.Struct({
87 instanceId: ProviderInstanceId,
88 methodId: Schema.optionalKey(SetupOperationId),
89});
90export type ProviderAuthStartInput = typeof ProviderAuthStartInput.Type;
91​
92export const ProviderAuthRespondInput = Schema.Struct({
93 instanceId: ProviderInstanceId,
94 flowId: SetupOperationId,
95 interactionId: SetupOperationId,
96 response: ProviderAuthResponse,
97});
98export type ProviderAuthRespondInput = typeof ProviderAuthRespondInput.Type;
99​
100export const ProviderAuthState = Schema.Struct({
101 instanceId: ProviderInstanceId,
102 phase: Schema.Literals([
103 "idle",
104 "starting",
105 "waiting",
106 "verifying",
107 "succeeded",
108 "failed",
109 "cancelled",
110 ]),
111 flowId: Schema.NullOr(SetupOperationId),
112 authorizationUrl: Schema.NullOr(Schema.String),
113 expiresAt: Schema.NullOr(IsoDateTime),
114 message: Schema.NullOr(Schema.String),
115 // Newer servers may add method types, interactions, or owners; older
116 // clients drop what they cannot decode instead of rejecting the state.
117 methods: Schema.optionalKey(
118 ForwardCompatibleArray(ProviderAuthMethod).check(Schema.isMaxLength(32)),
119 ),
120 interaction: ForwardCompatibleOptional(Schema.NullOr(ProviderAuthInteraction)),
121 credentialOwner: ForwardCompatibleOptional(Schema.Literals(["provider", "t3"])),
122});
123export type ProviderAuthState = typeof ProviderAuthState.Type;
124​
125export const ProviderAuthCompleteInput = Schema.Struct({
126 instanceId: ProviderInstanceId,
127 flowId: SetupOperationId,
128 callbackUrl: TrimmedNonEmptyString.check(Schema.isMaxLength(16_384)),
129});
130export type ProviderAuthCompleteInput = typeof ProviderAuthCompleteInput.Type;
131​
132export const ProviderAuthCancelInput = Schema.Struct({
133 instanceId: ProviderInstanceId,
134 flowId: SetupOperationId,
135});
136export type ProviderAuthCancelInput = typeof ProviderAuthCancelInput.Type;
137​
138const ByteCount = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
139​
140export const ProviderInstallState = Schema.Struct({
141 driver: ProviderDriverKind,
142 operationId: Schema.NullOr(SetupOperationId),
143 phase: Schema.Literals([
144 "idle",
145 "downloading",
146 "extracting",
147 "verifying",
148 "succeeded",
149 "failed",
150 "cancelled",
151 ]),
152 downloadedBytes: ByteCount,
153 totalBytes: Schema.NullOr(ByteCount),
154 version: Schema.NullOr(TrimmedNonEmptyString),
155 installedVersion: Schema.NullOr(TrimmedNonEmptyString),
156 canRemove: Schema.Boolean,
157 message: Schema.NullOr(Schema.String),
158});
159export type ProviderInstallState = typeof ProviderInstallState.Type;
160​
161export const ProviderInstallCancelInput = Schema.Struct({
162 instanceId: ProviderInstanceId,
163 operationId: SetupOperationId,
164});
165export type ProviderInstallCancelInput = typeof ProviderInstallCancelInput.Type;
166​
167/** Safe setup failure text. Never include OAuth codes, URLs, or native token data. */
168export class ProviderSetupError extends Schema.TaggedError<ProviderSetupError>()(
169 "ProviderSetupError",
170 {
171 instanceId: ProviderInstanceId,
172 operation: Schema.String,
173 detail: Schema.String,
174 cause: Schema.optional(Schema.Defect()),
175 },
176) {
177 override get message(): string {
178 return this.detail;
179 }