Repository navigation
Expand file tree
/
Copy pathauth.ts
More file actions
254 lines (236 loc) · 7.99 KB
/
Copy pathauth.ts
File metadata and controls
254 lines (236 loc) · 7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { AuthSessionId, AuthStandardClientScopes } from "@t3tools/contracts";
import * as Console from "effect/Console";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as References from "effect/References";
import * as Schema from "effect/Schema";
import { Argument, Command, Flag, GlobalFlag } from "effect/cli";
import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
import {
formatIssuedPairingCredential,
formatIssuedSession,
formatPairingCredentialList,
formatSessionList,
} from "../cliAuthFormat.ts";
import * as ServerConfig from "../config.ts";
import { authScopesFlag } from "./authScopes.ts";
import {
authLocationFlags,
type CliAuthLocationFlags,
DurationFromString,
resolveCliAuthConfig,
} from "./config.ts";
const runWithEnvironmentAuth = Effect.fn("auth.runWithEnvironmentAuth")(function* <A, E>(
flags: CliAuthLocationFlags,
run: (environmentAuth: EnvironmentAuth.EnvironmentAuth["Service"]) => Effect.Effect<A, E>,
options?: {
readonly quietLogs?: boolean;
},
) {
const logLevel = yield* GlobalFlag.LogLevel;
const config = yield* resolveCliAuthConfig(flags, logLevel, options);
const minimumLogLevel = options?.quietLogs ? "Error" : config.logLevel;
return yield* Effect.gen(function* () {
const environmentAuth = yield* EnvironmentAuth.EnvironmentAuth;
return yield* run(environmentAuth);
}).pipe(
Effect.provide(
Layer.mergeAll(EnvironmentAuth.layerRuntime).pipe(
Layer.provide(ServerConfig.layer(config)),
Layer.provide(Layer.succeed(References.MinimumLogLevel, minimumLogLevel)),
),
),
);
});
const ttlFlag = Flag.String("ttl").pipe(
Flag.withSchema(DurationFromString),
Flag.withDescription("TTL, for example `5m`, `1h`, `30d`, or `15 minutes`."),
Flag.optional,
);
const jsonFlag = Flag.Boolean("json").pipe(
Flag.withDescription("Emit JSON instead of human-readable output."),
Flag.withDefault(false),
);
const labelFlag = Flag.String("label").pipe(
Flag.withDescription("Optional human-readable label."),
Flag.optional,
);
const subjectFlag = Flag.String("subject").pipe(
Flag.withDescription("Optional session subject."),
Flag.optional,
);
const baseUrlFlag = Flag.String("base-url").pipe(
Flag.withSchema(
Schema.URLFromString.pipe(
Schema.check(
Schema.makeFilter((url) => url.protocol === "http:" || url.protocol === "https:", {
expected: "an http:// or https:// URL",
}),
),
),
),
Flag.withDescription("Optional public base URL used to print a ready `/pair#token=...` link."),
Flag.optional,
);
const tokenOnlyFlag = Flag.Boolean("token-only").pipe(
Flag.withDescription("Print only the issued bearer token."),
Flag.withDefault(false),
);
const pairingCreateCommand = Command.make("create", {
...authLocationFlags,
scopes: authScopesFlag(AuthStandardClientScopes),
ttl: ttlFlag,
label: labelFlag,
baseUrl: baseUrlFlag,
json: jsonFlag,
}).pipe(
Command.withDescription("Issue a new client pairing token."),
Command.withHandler((flags) =>
runWithEnvironmentAuth(
flags,
(environmentAuth) =>
Effect.gen(function* () {
const issued = yield* environmentAuth.createPairingLink({
scopes: flags.scopes,
subject: "one-time-token",
...(Option.isSome(flags.ttl) ? { ttl: flags.ttl.value } : {}),
...(Option.isSome(flags.label) ? { label: flags.label.value } : {}),
});
const output = formatIssuedPairingCredential(issued, {
json: flags.json,
...(Option.isSome(flags.baseUrl) ? { baseUrl: flags.baseUrl.value.href } : {}),
});
yield* Console.log(output);
}),
{
quietLogs: flags.json,
},
),
),
);
const pairingListCommand = Command.make("list", {
...authLocationFlags,
json: jsonFlag,
}).pipe(
Command.withDescription("List active client pairing tokens without revealing their secrets."),
Command.withHandler((flags) =>
runWithEnvironmentAuth(
flags,
(environmentAuth) =>
Effect.gen(function* () {
const pairingLinks = yield* environmentAuth.listPairingLinks({
excludeSubjects: [EnvironmentAuth.INTERNAL_ADMINISTRATIVE_BOOTSTRAP_SUBJECT],
});
yield* Console.log(formatPairingCredentialList(pairingLinks, { json: flags.json }));
}),
{
quietLogs: flags.json,
},
),
),
);
const pairingRevokeCommand = Command.make("revoke", {
...authLocationFlags,
id: Argument.String("id").pipe(Argument.withDescription("Pairing credential id to revoke.")),
}).pipe(
Command.withDescription("Revoke an active client pairing token."),
Command.withHandler((flags) =>
runWithEnvironmentAuth(flags, (environmentAuth) =>
Effect.gen(function* () {
const revoked = yield* environmentAuth.revokePairingLink(flags.id);
yield* Console.log(
revoked
? `Revoked pairing credential ${flags.id}.\n`
: `No active pairing credential found for ${flags.id}.\n`,
);
}),
),
),
);
const pairingCommand = Command.make("pairing").pipe(
Command.withDescription("Manage one-time client pairing tokens."),
Command.withSubcommands([pairingCreateCommand, pairingListCommand, pairingRevokeCommand]),
);
const sessionIssueCommand = Command.make("issue", {
...authLocationFlags,
scopes: authScopesFlag(AuthStandardClientScopes),
ttl: ttlFlag,
label: labelFlag,
subject: subjectFlag,
tokenOnly: tokenOnlyFlag,
json: jsonFlag,
}).pipe(
Command.withDescription("Issue a scoped bearer access token for headless or remote clients."),
Command.withHandler((flags) =>
runWithEnvironmentAuth(
flags,
(environmentAuth) =>
Effect.gen(function* () {
const issued = yield* environmentAuth.issueSession({
scopes: flags.scopes,
...(Option.isSome(flags.ttl) ? { ttl: flags.ttl.value } : {}),
...(Option.isSome(flags.label) ? { label: flags.label.value } : {}),
...(Option.isSome(flags.subject) ? { subject: flags.subject.value } : {}),
});
yield* Console.log(
formatIssuedSession(issued, {
json: flags.json,
tokenOnly: flags.tokenOnly,
}),
);
}),
{
quietLogs: flags.json || flags.tokenOnly,
},
),
),
);
const sessionListCommand = Command.make("list", {
...authLocationFlags,
json: jsonFlag,
}).pipe(
Command.withDescription("List active sessions without revealing bearer tokens."),
Command.withHandler((flags) =>
runWithEnvironmentAuth(
flags,
(environmentAuth) =>
Effect.gen(function* () {
const sessions = yield* environmentAuth.listSessions();
yield* Console.log(formatSessionList(sessions, { json: flags.json }));
}),
{
quietLogs: flags.json,
},
),
),
);
const sessionRevokeCommand = Command.make("revoke", {
...authLocationFlags,
sessionId: Argument.String("session-id").pipe(
Argument.withDescription("Session id to revoke."),
Argument.withSchema(AuthSessionId),
),
}).pipe(
Command.withDescription("Revoke an active session."),
Command.withHandler((flags) =>
runWithEnvironmentAuth(flags, (environmentAuth) =>
Effect.gen(function* () {
const revoked = yield* environmentAuth.revokeSession(flags.sessionId);
yield* Console.log(
revoked
? `Revoked session ${flags.sessionId}.\n`
: `No active session found for ${flags.sessionId}.\n`,
);
}),
),
),
);
const sessionCommand = Command.make("session").pipe(
Command.withDescription("Manage bearer sessions."),
Command.withSubcommands([sessionIssueCommand, sessionListCommand, sessionRevokeCommand]),
);
export const authCommand = Command.make("auth").pipe(
Command.withDescription("Manage the local auth control plane for headless deployments."),
Command.withSubcommands([pairingCommand, sessionCommand]),
);