Repository navigation
Expand file tree
/
Copy pathcliAuthFormat.ts
More file actions
192 lines (175 loc) · 5.42 KB
/
Copy pathcliAuthFormat.ts
File metadata and controls
192 lines (175 loc) · 5.42 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
import type { AuthClientMetadata, AuthClientSession, AuthPairingLink } from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import type { IssuedBearerSession, IssuedPairingLink } from "./auth/EnvironmentAuth.ts";
const newline = "\n";
function serializeOptionalFields(values: ReadonlyArray<string | null | undefined>) {
return values.filter((value): value is string => typeof value === "string" && value.length > 0);
}
function formatClientMetadata(metadata: AuthClientMetadata): string {
const details = serializeOptionalFields([
metadata.label,
metadata.deviceType !== "unknown" ? metadata.deviceType : undefined,
metadata.os,
metadata.browser,
metadata.ipAddress,
]);
return details.length > 0 ? details.join(" | ") : "unlabeled client";
}
function toIsoString(value: DateTime.DateTime | DateTime.Utc): string {
return DateTime.formatIso(DateTime.toUtc(value));
}
export function formatIssuedPairingCredential(
credential: IssuedPairingLink,
options?: {
readonly json?: boolean;
readonly baseUrl?: string;
},
): string {
const pairUrl =
options?.baseUrl != null && options.baseUrl.length > 0
? (() => {
const url = new URL("/pair", options.baseUrl);
url.searchParams.delete("token");
url.hash = new URLSearchParams([["token", credential.credential]]).toString();
return url.toString();
})()
: undefined;
if (options?.json) {
return `${JSON.stringify(
{
id: credential.id,
credential: credential.credential,
...(credential.label ? { label: credential.label } : {}),
scopes: credential.scopes,
expiresAt: toIsoString(credential.expiresAt),
...(pairUrl ? { pairUrl } : {}),
},
null,
2,
)}${newline}`;
}
return (
[
`Issued client pairing token ${credential.id}.`,
`Token: ${credential.credential}`,
...(pairUrl ? [`Pair URL: ${pairUrl}`] : []),
`Expires at: ${toIsoString(credential.expiresAt)}`,
].join(newline) + newline
);
}
export function formatPairingCredentialList(
credentials: ReadonlyArray<AuthPairingLink>,
options?: {
readonly json?: boolean;
},
): string {
if (options?.json) {
return `${JSON.stringify(
credentials.map((credential) => ({
id: credential.id,
...(credential.label ? { label: credential.label } : {}),
scopes: credential.permissions ?? credential.scopes,
createdAt: toIsoString(credential.createdAt),
expiresAt: toIsoString(credential.expiresAt),
})),
null,
2,
)}${newline}`;
}
if (credentials.length === 0) {
return `No active pairing credentials.${newline}`;
}
return (
credentials
.map((credential) =>
[
`${credential.id}${credential.label ? ` (${credential.label})` : ""}`,
` scopes: ${(credential.permissions ?? credential.scopes).join(" ")}`,
` created: ${toIsoString(credential.createdAt)}`,
` expires: ${toIsoString(credential.expiresAt)}`,
].join(newline),
)
.join(`${newline}${newline}`) + newline
);
}
export function formatIssuedSession(
session: IssuedBearerSession,
options?: {
readonly json?: boolean;
readonly tokenOnly?: boolean;
},
): string {
if (options?.tokenOnly) {
return `${session.token}${newline}`;
}
if (options?.json) {
return `${JSON.stringify(
{
sessionId: session.sessionId,
token: session.token,
method: session.method,
scopes: session.scopes,
subject: session.subject,
client: session.client,
expiresAt: toIsoString(session.expiresAt),
},
null,
2,
)}${newline}`;
}
return (
[
`Issued bearer access token ${session.sessionId}.`,
`Scopes: ${session.scopes.join(" ")}`,
`Token: ${session.token}`,
`Subject: ${session.subject}`,
`Client: ${formatClientMetadata(session.client)}`,
`Expires at: ${toIsoString(session.expiresAt)}`,
].join(newline) + newline
);
}
export function formatSessionList(
sessions: ReadonlyArray<AuthClientSession>,
options?: {
readonly json?: boolean;
},
): string {
if (options?.json) {
return `${JSON.stringify(
sessions.map((session) => ({
sessionId: session.sessionId,
method: session.method,
scopes: session.scopes,
subject: session.subject,
client: session.client,
connected: session.connected,
issuedAt: toIsoString(session.issuedAt),
expiresAt: toIsoString(session.expiresAt),
lastConnectedAt: session.lastConnectedAt ? toIsoString(session.lastConnectedAt) : null,
})),
null,
2,
)}${newline}`;
}
if (sessions.length === 0) {
return `No active sessions.${newline}`;
}
return (
sessions
.map((session) =>
[
`${session.sessionId}${session.connected ? " connected" : ""}`,
` scopes: ${session.scopes.join(" ")}`,
` method: ${session.method}`,
` subject: ${session.subject}`,
` client: ${formatClientMetadata(session.client)}`,
` issued: ${toIsoString(session.issuedAt)}`,
` last connected: ${
session.lastConnectedAt ? toIsoString(session.lastConnectedAt) : "never"
}`,
` expires: ${toIsoString(session.expiresAt)}`,
].join(newline),
)
.join(`${newline}${newline}`) + newline
);
}