diff --git a/rpc/js/src/dial.ts b/rpc/js/src/dial.ts index 5dbfd167..5e525bce 100644 --- a/rpc/js/src/dial.ts +++ b/rpc/js/src/dial.ts @@ -132,87 +132,69 @@ export const dialDirect = async ( return createTransport(transportOpts); } - const authFact = await makeAuthenticatedTransportFactory( + return makeAuthenticatedTransport( address, createTransport, - opts + opts, + transportOpts, ); - return authFact(transportOpts); }; const addressCleanupRegex = /^.*:\/\//u; -const makeAuthenticatedTransportFactory = async ( +const makeAuthenticatedTransport = async ( address: string, defaultFactory: TransportFactory, - opts: DialOptions -): Promise => { - let accessToken = ''; - const getExtraHeaders = async (): Promise => { - const headers = new Headers(); - // TODO(GOUT-10): handle expiration - if (accessToken === '') { - let thisAccessToken = ''; + opts: DialOptions, + transportOpts: TransportInitOptions, +): Promise => { + const authHeaders = new Headers(); - if (!opts.accessToken || opts.accessToken === '') { - const request = new AuthenticateRequest({ - entity: opts.authEntity ?? address.replace(addressCleanupRegex, ''), - }); - if (opts.credentials) { - request.credentials = new PBCredentials({ - type: opts.credentials.type, - payload: opts.credentials.payload, - }); - } + let accessToken; + if (!opts.accessToken || opts.accessToken === '') { + const request = new AuthenticateRequest({ + entity: opts.authEntity ?? address.replace(addressCleanupRegex, ''), + }); + if (opts.credentials) { + request.credentials = new PBCredentials({ + type: opts.credentials.type, + payload: opts.credentials.payload, + }); + } - const resolvedAddress = opts.externalAuthAddress ?? address; - const transport = defaultFactory({ baseUrl: resolvedAddress }); - const authClient = createPromiseClient(AuthService, transport); - const resp = await authClient.authenticate(request); - thisAccessToken = resp.accessToken; - } else { - thisAccessToken = opts.accessToken; - } + const resolvedAddress = opts.externalAuthAddress ?? address; + const transport = defaultFactory({ baseUrl: resolvedAddress }); + const authClient = createPromiseClient(AuthService, transport); + const resp = await authClient.authenticate(request); + accessToken = resp.accessToken; + } else { + accessToken = opts.accessToken; + } - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- await race - if (accessToken === '') { - accessToken = thisAccessToken; - - if (opts.externalAuthAddress && opts.externalAuthToEntity) { - const authHeaders = new Headers(); - authHeaders.set('authorization', `Bearer ${accessToken}`); - - thisAccessToken = ''; - - const request = new AuthenticateRequest({ - entity: opts.externalAuthToEntity, - }); - const transport = defaultFactory({ - baseUrl: opts.externalAuthAddress, - }); - const externalAuthClient = createPromiseClient( - ExternalAuthService, - transport - ); - const resp = await externalAuthClient.authenticateTo(request, { - headers: authHeaders, - }); - thisAccessToken = resp.accessToken; - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- await race - if (accessToken === '') { - accessToken = thisAccessToken; - } - } - } - } - headers.set('authorization', `Bearer ${accessToken}`); - return headers; - }; - const extraMd = await getExtraHeaders(); - return (transportOpts: TransportInitOptions): Transport => { - return new AuthenticatedTransport(transportOpts, defaultFactory, extraMd); - }; -}; + if (opts.externalAuthAddress && opts.externalAuthToEntity) { + const extAuthHeaders = new Headers(); + extAuthHeaders.set('authorization', `Bearer ${accessToken}`); + + accessToken = ''; + + const request = new AuthenticateRequest({ + entity: opts.externalAuthToEntity, + }); + const transport = defaultFactory({ + baseUrl: opts.externalAuthAddress, + }); + const externalAuthClient = createPromiseClient( + ExternalAuthService, + transport + ); + const resp = await externalAuthClient.authenticateTo(request, { + headers: extAuthHeaders, + }); + accessToken = resp.accessToken; + } + authHeaders.set('authorization', `Bearer ${accessToken}`); + return new AuthenticatedTransport(transportOpts, defaultFactory, authHeaders); +} class AuthenticatedTransport implements Transport { protected readonly transport: Transport;