| 1 | import * as Schema from "effect/Schema"; |
| 2 | import { AuthClientPresentationMetadata, AuthGrantScopes } from "./auth.ts"; |
| 3 | import { EnvironmentId } from "./baseSchemas.ts"; |
| 4 | |
| 5 | const ProofChallenge = Schema.String.check(Schema.isPattern(/^[A-Za-z0-9_-]{43}$/u)); |
| 6 | const ProofVerifier = Schema.String.check(Schema.isPattern(/^[A-Za-z0-9._~-]{43,128}$/u)); |
| 7 | |
| 8 | export const HostedAuthDeviceStart = Schema.Struct({ |
| 9 | codeChallenge: ProofChallenge, |
| 10 | state: ProofChallenge, |
| 11 | client: AuthClientPresentationMetadata, |
| 12 | }); |
| 13 | export type HostedAuthDeviceStart = typeof HostedAuthDeviceStart.Type; |
| 14 | |
| 15 | export const HostedAuthDeviceStartResult = Schema.Struct({ authorizationUrl: Schema.String }); |
| 16 | export type HostedAuthDeviceStartResult = typeof HostedAuthDeviceStartResult.Type; |
| 17 | |
| 18 | export const HostedAuthDeviceExchange = Schema.Struct({ |
| 19 | code: ProofChallenge, |
| 20 | codeVerifier: ProofVerifier, |
| 21 | }); |
| 22 | export type HostedAuthDeviceExchange = typeof HostedAuthDeviceExchange.Type; |
| 23 | |
| 24 | export const HostedAuthDeviceRefresh = Schema.Struct({ refreshToken: Schema.NonEmptyString }); |
| 25 | export type HostedAuthDeviceRefresh = typeof HostedAuthDeviceRefresh.Type; |
| 26 | |
| 27 | export const HostedAuthGrant = Schema.Struct({ |
| 28 | deviceId: Schema.String, |
| 29 | accessToken: Schema.String, |
| 30 | refreshToken: Schema.String, |
| 31 | expiresAt: Schema.DateTimeUtc, |
| 32 | }); |
| 33 | export type HostedAuthGrant = typeof HostedAuthGrant.Type; |
| 34 | |
| 35 | export 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 | }); |
| 44 | export type HostedAuthDevice = typeof HostedAuthDevice.Type; |
| 45 | |
| 46 | export const HostedAuthAccount = Schema.Struct({ |
| 47 | accountId: Schema.String, |
| 48 | deviceId: Schema.String, |
| 49 | csrfToken: Schema.String, |
| 50 | expiresAt: Schema.DateTimeUtc, |
| 51 | }); |
| 52 | export type HostedAuthAccount = typeof HostedAuthAccount.Type; |
| 53 | |
| 54 | export const HostedAuthTokenRequest = Schema.Struct({ scopes: AuthGrantScopes }); |
| 55 | export type HostedAuthTokenRequest = typeof HostedAuthTokenRequest.Type; |
| 56 | |
| 57 | export 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 | }) {} |