@@ -20,6 +20,7 @@ import * as HttpClientResponse from "effect/unstable/http/HttpClientResponse";
2020import * as HttpRouter from "effect/unstable/http/HttpRouter" ;
2121import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest" ;
2222import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse" ;
23+ import { getUrlDiagnostics } from "@t3tools/shared/urlDiagnostics" ;
2324
2425import * as ServerSecretStore from "../auth/ServerSecretStore.ts" ;
2526import { cloudCliOAuthConfig , type CloudCliOAuthConfig } from "./publicConfig.ts" ;
@@ -63,6 +64,7 @@ const CloudCliCredentialRefreshStage = Schema.Literals([
6364 "encode-credential" ,
6465 "persist-credential" ,
6566] ) ;
67+ type CloudCliCredentialRefreshStage = typeof CloudCliCredentialRefreshStage . Type ;
6668
6769const CloudCliAuthorizationStage = Schema . Literals ( [
6870 "load-oauth-config" ,
@@ -72,6 +74,27 @@ const CloudCliAuthorizationStage = Schema.Literals([
7274 "encode-credential" ,
7375 "persist-credential" ,
7476] ) ;
77+ type CloudCliAuthorizationStage = typeof CloudCliAuthorizationStage . Type ;
78+
79+ function tokenEndpointDiagnosticFields ( tokenEndpoint : string | undefined ) {
80+ if ( tokenEndpoint === undefined ) return { } ;
81+ const diagnostics = getUrlDiagnostics ( tokenEndpoint ) ;
82+ return {
83+ tokenEndpointInputLength : diagnostics . inputLength ,
84+ ...( diagnostics . protocol === undefined ? { } : { tokenEndpointProtocol : diagnostics . protocol } ) ,
85+ ...( diagnostics . hostname === undefined ? { } : { tokenEndpointHostname : diagnostics . hostname } ) ,
86+ } ;
87+ }
88+
89+ function redirectUriDiagnosticFields ( redirectUri : string | undefined ) {
90+ if ( redirectUri === undefined ) return { } ;
91+ const diagnostics = getUrlDiagnostics ( redirectUri ) ;
92+ return {
93+ redirectUriInputLength : diagnostics . inputLength ,
94+ ...( diagnostics . protocol === undefined ? { } : { redirectUriProtocol : diagnostics . protocol } ) ,
95+ ...( diagnostics . hostname === undefined ? { } : { redirectUriHostname : diagnostics . hostname } ) ,
96+ } ;
97+ }
7598
7699export class CloudCliCredentialRemovalError extends Schema . TaggedErrorClass < CloudCliCredentialRemovalError > ( ) (
77100 "CloudCliCredentialRemovalError" ,
@@ -90,10 +113,25 @@ export class CloudCliCredentialRefreshError extends Schema.TaggedErrorClass<Clou
90113 {
91114 stage : CloudCliCredentialRefreshStage ,
92115 secretName : Schema . Literal ( CLOUD_CLI_OAUTH_TOKEN_SECRET ) ,
93- tokenEndpoint : Schema . optional ( Schema . String ) ,
116+ tokenEndpointInputLength : Schema . optionalKey ( Schema . Number ) ,
117+ tokenEndpointProtocol : Schema . optionalKey ( Schema . String ) ,
118+ tokenEndpointHostname : Schema . optionalKey ( Schema . String ) ,
94119 cause : Schema . Defect ( ) ,
95120 } ,
96121) {
122+ static fromStage ( input : {
123+ readonly stage : CloudCliCredentialRefreshStage ;
124+ readonly cause : unknown ;
125+ readonly tokenEndpoint ?: string ;
126+ } ) : CloudCliCredentialRefreshError {
127+ return new CloudCliCredentialRefreshError ( {
128+ stage : input . stage ,
129+ secretName : CLOUD_CLI_OAUTH_TOKEN_SECRET ,
130+ ...tokenEndpointDiagnosticFields ( input . tokenEndpoint ) ,
131+ cause : input . cause ,
132+ } ) ;
133+ }
134+
97135 static fromCredentialRead ( cause : CredentialReadFailure ) : CloudCliCredentialRefreshError {
98136 return new CloudCliCredentialRefreshError ( {
99137 stage : cause . _tag === "SecretStoreReadError" ? "read-credential" : "decode-credential" ,
@@ -111,7 +149,10 @@ export class CloudCliCredentialRefreshError extends Schema.TaggedErrorClass<Clou
111149 }
112150
113151 override get message ( ) : string {
114- const tokenEndpoint = this . tokenEndpoint ? ` using ${ this . tokenEndpoint } ` : "" ;
152+ const tokenEndpoint =
153+ this . tokenEndpointInputLength === undefined
154+ ? ""
155+ : ` using the token endpoint${ this . tokenEndpointHostname ? ` at ${ this . tokenEndpointHostname } ` : "" } (input length ${ this . tokenEndpointInputLength } )` ;
115156 return `Could not refresh the T3 Connect CLI credential ${ this . secretName } during ${ this . stage } ${ tokenEndpoint } .` ;
116157 }
117158}
@@ -142,13 +183,36 @@ export class CloudCliAuthorizationError extends Schema.TaggedErrorClass<CloudCli
142183 {
143184 stage : CloudCliAuthorizationStage ,
144185 secretName : Schema . Literal ( CLOUD_CLI_OAUTH_TOKEN_SECRET ) ,
145- tokenEndpoint : Schema . optional ( Schema . String ) ,
146- redirectUri : Schema . optional ( Schema . String ) ,
186+ tokenEndpointInputLength : Schema . optionalKey ( Schema . Number ) ,
187+ tokenEndpointProtocol : Schema . optionalKey ( Schema . String ) ,
188+ tokenEndpointHostname : Schema . optionalKey ( Schema . String ) ,
189+ redirectUriInputLength : Schema . optionalKey ( Schema . Number ) ,
190+ redirectUriProtocol : Schema . optionalKey ( Schema . String ) ,
191+ redirectUriHostname : Schema . optionalKey ( Schema . String ) ,
147192 callbackHost : Schema . optional ( Schema . String ) ,
148193 callbackPort : Schema . optional ( Schema . Number ) ,
149194 cause : Schema . Defect ( ) ,
150195 } ,
151196) {
197+ static fromStage ( input : {
198+ readonly stage : CloudCliAuthorizationStage ;
199+ readonly cause : unknown ;
200+ readonly tokenEndpoint ?: string ;
201+ readonly redirectUri ?: string ;
202+ readonly callbackHost ?: string ;
203+ readonly callbackPort ?: number ;
204+ } ) : CloudCliAuthorizationError {
205+ return new CloudCliAuthorizationError ( {
206+ stage : input . stage ,
207+ secretName : CLOUD_CLI_OAUTH_TOKEN_SECRET ,
208+ ...tokenEndpointDiagnosticFields ( input . tokenEndpoint ) ,
209+ ...redirectUriDiagnosticFields ( input . redirectUri ) ,
210+ ...( input . callbackHost === undefined ? { } : { callbackHost : input . callbackHost } ) ,
211+ ...( input . callbackPort === undefined ? { } : { callbackPort : input . callbackPort } ) ,
212+ cause : input . cause ,
213+ } ) ;
214+ }
215+
152216 static fromCredentialPersist ( cause : CredentialPersistFailure ) : CloudCliAuthorizationError {
153217 return new CloudCliAuthorizationError ( {
154218 stage : cause . _tag === "SchemaError" ? "encode-credential" : "persist-credential" ,
@@ -158,8 +222,14 @@ export class CloudCliAuthorizationError extends Schema.TaggedErrorClass<CloudCli
158222 }
159223
160224 override get message ( ) : string {
161- const tokenEndpoint = this . tokenEndpoint ? ` using ${ this . tokenEndpoint } ` : "" ;
162- const redirectUri = this . redirectUri ? ` with callback ${ this . redirectUri } ` : "" ;
225+ const tokenEndpoint =
226+ this . tokenEndpointInputLength === undefined
227+ ? ""
228+ : ` using the token endpoint${ this . tokenEndpointHostname ? ` at ${ this . tokenEndpointHostname } ` : "" } (input length ${ this . tokenEndpointInputLength } )` ;
229+ const redirectUri =
230+ this . redirectUriInputLength === undefined
231+ ? ""
232+ : ` with a callback URI input of length ${ this . redirectUriInputLength } ` ;
163233 const callbackAddress =
164234 this . callbackHost && this . callbackPort !== undefined
165235 ? ` on ${ this . callbackHost } :${ this . callbackPort } `
@@ -171,13 +241,31 @@ export class CloudCliAuthorizationError extends Schema.TaggedErrorClass<CloudCli
171241export class CloudCliAuthorizationTimeoutError extends Schema . TaggedErrorClass < CloudCliAuthorizationTimeoutError > ( ) (
172242 "CloudCliAuthorizationTimeoutError" ,
173243 {
174- redirectUri : Schema . String ,
244+ redirectUriInputLength : Schema . Number ,
245+ redirectUriProtocol : Schema . optionalKey ( Schema . String ) ,
246+ redirectUriHostname : Schema . optionalKey ( Schema . String ) ,
175247 timeoutMillis : Schema . Number ,
176248 cause : Schema . Defect ( ) ,
177249 } ,
178250) {
251+ static fromRedirectUri ( input : {
252+ readonly redirectUri : string ;
253+ readonly timeoutMillis : number ;
254+ readonly cause : unknown ;
255+ } ) : CloudCliAuthorizationTimeoutError {
256+ const diagnostics = getUrlDiagnostics ( input . redirectUri ) ;
257+ return new CloudCliAuthorizationTimeoutError ( {
258+ redirectUriInputLength : diagnostics . inputLength ,
259+ ...( diagnostics . protocol === undefined ? { } : { redirectUriProtocol : diagnostics . protocol } ) ,
260+ ...( diagnostics . hostname === undefined ? { } : { redirectUriHostname : diagnostics . hostname } ) ,
261+ timeoutMillis : input . timeoutMillis ,
262+ cause : input . cause ,
263+ } ) ;
264+ }
265+
179266 override get message ( ) : string {
180- return `Timed out after ${ this . timeoutMillis } ms waiting for T3 Connect authorization at ${ this . redirectUri } .` ;
267+ const callback = this . redirectUriHostname ? ` for ${ this . redirectUriHostname } ` : "" ;
268+ return `Timed out after ${ this . timeoutMillis } ms waiting for T3 Connect authorization${ callback } (callback URI input length ${ this . redirectUriInputLength } ).` ;
181269 }
182270}
183271
@@ -262,41 +350,35 @@ export const make = Effect.gen(function* () {
262350
263351 const refresh = Effect . fn ( "cloud.cli_token.refresh" ) ( function * ( token : PersistedToken ) {
264352 const metadata = yield * cloudCliOAuthConfig . pipe (
265- Effect . mapError (
266- ( cause ) =>
267- new CloudCliCredentialRefreshError ( {
268- stage : "load-oauth-config" ,
269- secretName : CLOUD_CLI_OAUTH_TOKEN_SECRET ,
270- cause,
271- } ) ,
353+ Effect . mapError ( ( cause ) =>
354+ CloudCliCredentialRefreshError . fromStage ( {
355+ stage : "load-oauth-config" ,
356+ cause,
357+ } ) ,
272358 ) ,
273359 ) ;
274360 return yield * exchangeToken ( metadata , {
275361 grant_type : "refresh_token" ,
276362 refresh_token : token . refreshToken ,
277363 client_id : metadata . clientId ,
278364 } ) . pipe (
279- Effect . mapError (
280- ( cause ) =>
281- new CloudCliCredentialRefreshError ( {
282- stage : "exchange-token" ,
283- secretName : CLOUD_CLI_OAUTH_TOKEN_SECRET ,
284- tokenEndpoint : metadata . tokenEndpoint ,
285- cause,
286- } ) ,
365+ Effect . mapError ( ( cause ) =>
366+ CloudCliCredentialRefreshError . fromStage ( {
367+ stage : "exchange-token" ,
368+ tokenEndpoint : metadata . tokenEndpoint ,
369+ cause,
370+ } ) ,
287371 ) ,
288372 ) ;
289373 } ) ;
290374
291375 const login = Effect . fn ( "cloud.cli_token.login" ) ( function * ( ) {
292376 const metadata = yield * cloudCliOAuthConfig . pipe (
293- Effect . mapError (
294- ( cause ) =>
295- new CloudCliAuthorizationError ( {
296- stage : "load-oauth-config" ,
297- secretName : CLOUD_CLI_OAUTH_TOKEN_SECRET ,
298- cause,
299- } ) ,
377+ Effect . mapError ( ( cause ) =>
378+ CloudCliAuthorizationError . fromStage ( {
379+ stage : "load-oauth-config" ,
380+ cause,
381+ } ) ,
300382 ) ,
301383 ) ;
302384 const { challenge, state, verifier } = yield * Effect . gen ( function * ( ) {
@@ -307,14 +389,12 @@ export const make = Effect.gen(function* () {
307389 const state = yield * crypto . randomUUIDv4 ;
308390 return { challenge, state, verifier } ;
309391 } ) . pipe (
310- Effect . mapError (
311- ( cause ) =>
312- new CloudCliAuthorizationError ( {
313- stage : "prepare-pkce" ,
314- secretName : CLOUD_CLI_OAUTH_TOKEN_SECRET ,
315- redirectUri : metadata . redirectUri ,
316- cause,
317- } ) ,
392+ Effect . mapError ( ( cause ) =>
393+ CloudCliAuthorizationError . fromStage ( {
394+ stage : "prepare-pkce" ,
395+ redirectUri : metadata . redirectUri ,
396+ cause,
397+ } ) ,
318398 ) ,
319399 ) ;
320400 const callback = yield * Deferred . make < string > ( ) ;
@@ -353,16 +433,14 @@ export const make = Effect.gen(function* () {
353433 } ) ,
354434 ) ,
355435 Layer . build ,
356- Effect . mapError (
357- ( cause ) =>
358- new CloudCliAuthorizationError ( {
359- stage : "start-callback-server" ,
360- secretName : CLOUD_CLI_OAUTH_TOKEN_SECRET ,
361- redirectUri : metadata . redirectUri ,
362- callbackHost : CLOUD_CLI_OAUTH_CALLBACK_HOST ,
363- callbackPort : CLOUD_CLI_OAUTH_CALLBACK_PORT ,
364- cause,
365- } ) ,
436+ Effect . mapError ( ( cause ) =>
437+ CloudCliAuthorizationError . fromStage ( {
438+ stage : "start-callback-server" ,
439+ redirectUri : metadata . redirectUri ,
440+ callbackHost : CLOUD_CLI_OAUTH_CALLBACK_HOST ,
441+ callbackPort : CLOUD_CLI_OAUTH_CALLBACK_PORT ,
442+ cause,
443+ } ) ,
366444 ) ,
367445 ) ;
368446 const authorizationUrl = new URL ( metadata . authorizationEndpoint ) ;
@@ -379,7 +457,7 @@ export const make = Effect.gen(function* () {
379457 Effect . catchTags ( {
380458 TimeoutError : ( cause ) =>
381459 Effect . fail (
382- new CloudCliAuthorizationTimeoutError ( {
460+ CloudCliAuthorizationTimeoutError . fromRedirectUri ( {
383461 redirectUri : metadata . redirectUri ,
384462 timeoutMillis : Duration . toMillis ( CLOUD_CLI_OAUTH_CALLBACK_TIMEOUT ) ,
385463 cause,
@@ -394,15 +472,13 @@ export const make = Effect.gen(function* () {
394472 client_id : metadata . clientId ,
395473 code_verifier : verifier ,
396474 } ) . pipe (
397- Effect . mapError (
398- ( cause ) =>
399- new CloudCliAuthorizationError ( {
400- stage : "exchange-token" ,
401- secretName : CLOUD_CLI_OAUTH_TOKEN_SECRET ,
402- tokenEndpoint : metadata . tokenEndpoint ,
403- redirectUri : metadata . redirectUri ,
404- cause,
405- } ) ,
475+ Effect . mapError ( ( cause ) =>
476+ CloudCliAuthorizationError . fromStage ( {
477+ stage : "exchange-token" ,
478+ tokenEndpoint : metadata . tokenEndpoint ,
479+ redirectUri : metadata . redirectUri ,
480+ cause,
481+ } ) ,
406482 ) ,
407483 ) ;
408484 } ) ;
0 commit comments