1import * as Schema from "effect/Schema";
2import { AuthClientPresentationMetadata, AuthGrantScopes } from "./auth.ts";
3import { EnvironmentId } from "./baseSchemas.ts";
4
5const ProofChallenge = Schema.String.check(Schema.isPattern(/^[A-Za-z0-9_-]{43}$/u));
6const ProofVerifier = Schema.String.check(Schema.isPattern(/^[A-Za-z0-9._~-]{43,128}$/u));
7
8export const HostedAuthDeviceStart = Schema.Struct({
9 codeChallenge: ProofChallenge,
10 state: ProofChallenge,
11 client: AuthClientPresentationMetadata,
12});
13export type HostedAuthDeviceStart = typeof HostedAuthDeviceStart.Type;
14
15export const HostedAuthDeviceStartResult = Schema.Struct({ authorizationUrl: Schema.String });
16export type HostedAuthDeviceStartResult = typeof HostedAuthDeviceStartResult.Type;
17
18export const HostedAuthDeviceExchange = Schema.Struct({
19 code: ProofChallenge,
20 codeVerifier: ProofVerifier,
21});
22export type HostedAuthDeviceExchange = typeof HostedAuthDeviceExchange.Type;
23
24export const HostedAuthDeviceRefresh = Schema.Struct({ refreshToken: Schema.NonEmptyString });
25export type HostedAuthDeviceRefresh = typeof HostedAuthDeviceRefresh.Type;
26
27export const HostedAuthGrant = Schema.Struct({
28 deviceId: Schema.String,
29 accessToken: Schema.String,
30 refreshToken: Schema.String,
31 expiresAt: Schema.DateTimeUtc,
32});
33export type HostedAuthGrant = typeof HostedAuthGrant.Type;
34
35export const HostedAuthDevice = Schema.Struct({
36 deviceId: Schema.String,
37 client: AuthClientPresentationMetadata,
38 createdAt: Schema.DateTimeUtc,
39 expiresAt: Schema.DateTimeUtc,
40 revokedAt: Schema.NullOr(Schema.DateTimeUtc),
41 current: Schema.Boolean,
42 environmentId: Schema.optionalKey(EnvironmentId),
43});
44export type HostedAuthDevice = typeof HostedAuthDevice.Type;
45
46export const HostedAuthAccount = Schema.Struct({
47 accountId: Schema.String,
48 deviceId: Schema.String,
49 csrfToken: Schema.String,
50 expiresAt: Schema.DateTimeUtc,
51});
52export type HostedAuthAccount = typeof HostedAuthAccount.Type;
53
54export const HostedAuthTokenRequest = Schema.Struct({ scopes: AuthGrantScopes });
55export type HostedAuthTokenRequest = typeof HostedAuthTokenRequest.Type;
56
57export class HostedAuthError extends Schema.TaggedError<HostedAuthError>()("HostedAuthError", {
58 reason: Schema.Literals([
59 "disabled",
60 "configuration",
61 "identity",
62 "transaction",
63 "credential",
64 "forbidden",
65 "reauthentication",
66 "upstream",
67 "storage",
68 ]),
69 cause: Schema.optionalKey(Schema.Redacted(Schema.Defect())),
70}) {}