Skip to content

Commit f8673df

Browse files
authored
Merge pull request #25 from betwixt-labs/auth-context-fixes
Refactor 'AuthContext' and 'ServerContext'
2 parents 661bdd0 + 2b41593 commit f8673df

File tree

21 files changed

+541
-339
lines changed

21 files changed

+541
-339
lines changed

typescript/build.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ echo "export const TempoVersion = '$VERSION';" > $COMMON_VERSION_FILE
8888

8989
echo "All package versions and '@tempojs' dependencies have been updated to $VERSION."
9090

91-
# Test
92-
yarn vitest run
91+
# Install
92+
yarn install
9393

9494
# Build and pack the 'common' package.
9595
build ./packages/common
@@ -109,5 +109,5 @@ build ./packages/cf-router
109109
# Build and pack the 'node-http-router' package.
110110
build ./packages/node-http
111111

112-
# Install package dependencies
113-
yarn install
112+
# Test
113+
yarn vitest run

typescript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@
2929
"@tempojs/client": "^0.0.5",
3030
"@tempojs/common": "^0.0.5",
3131
"@tempojs/server": "^0.0.5",
32-
"bebop": "^2.8.2"
32+
"bebop": "^2.8.3"
3333
}
3434
}

typescript/packages/cf-router/src/router.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
TempoError,
55
TempoStatusCode,
66
TempoUtil,
7-
stringifyCredentials,
7+
stringifyCredential,
88
Deadline,
99
MethodType,
1010
tempoStream,
@@ -114,15 +114,15 @@ export class TempoRouter<TEnv> extends BaseRouter<Request, TEnv, Response> {
114114
}
115115
headers.set(
116116
'access-control-expose-headers',
117-
'content-encoding, content-length, content-type, tempo-status, tempo-message, custom-metadata, tempo-credentials',
117+
'content-encoding, content-length, content-type, tempo-status, tempo-message, custom-metadata, tempo-credential',
118118
);
119119
}
120120

121121
private async setAuthContext(request: Request, context: ServerContext): Promise<void> {
122122
const authHeader = request.headers.get('authorization');
123123
if (authHeader !== null && this.authInterceptor !== undefined) {
124124
const authContext = await this.authInterceptor.intercept(context, authHeader);
125-
context.setAuthContext(authContext);
125+
context.authContext = authContext;
126126
}
127127
}
128128

@@ -174,7 +174,7 @@ export class TempoRouter<TEnv> extends BaseRouter<Request, TEnv, Response> {
174174
}
175175
return record;
176176
},
177-
context.clientDeadline(),
177+
context.clientDeadline,
178178
);
179179
};
180180
return await method.invoke(generator, context);
@@ -231,7 +231,7 @@ export class TempoRouter<TEnv> extends BaseRouter<Request, TEnv, Response> {
231231
}
232232
return record;
233233
},
234-
context.clientDeadline(),
234+
context.clientDeadline,
235235
);
236236
};
237237
if (!TempoUtil.isAsyncGeneratorFunction(method.invoke)) {
@@ -355,9 +355,9 @@ export class TempoRouter<TEnv> extends BaseRouter<Request, TEnv, Response> {
355355
}
356356
responseHeaders.set('content-type', contentType.raw);
357357

358-
const outgoingCredentials = context.getOutgoingCredentials();
359-
if (outgoingCredentials) {
360-
responseHeaders.set('tempo-credentials', stringifyCredentials(outgoingCredentials));
358+
const outgoingCredential = context.outgoingCredential;
359+
if (outgoingCredential) {
360+
responseHeaders.set('tempo-credential', stringifyCredential(outgoingCredential));
361361
}
362362
responseHeaders.set('tempo-status', '0');
363363
responseHeaders.set('tempo-message', 'OK');
@@ -384,7 +384,7 @@ export class TempoRouter<TEnv> extends BaseRouter<Request, TEnv, Response> {
384384
}
385385
return data;
386386
},
387-
context.clientDeadline(),
387+
context.clientDeadline,
388388
);
389389
} else {
390390
if (record === undefined) {
Lines changed: 80 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,102 @@
1-
import { Credentials, parseCredentials, stringifyCredentials } from '@tempojs/common';
1+
import { Credential, parseCredential, stringifyCredential } from '@tempojs/common';
22

33
/**
44
* A type representing an HTTP header with a name and value.
55
*/
66
type Header = { name: string; value: string };
77

88
/**
9-
* An interface defining the methods required for a credentials storage strategy.
9+
* An interface defining the methods required for a credential storage strategy.
1010
*/
11-
export interface CredentialsStorage {
11+
export interface CredentialStorage {
1212
/**
13-
* Retrieves stored credentials for a given key.
14-
* @param key - The key associated with the stored credentials.
15-
* @returns A Promise that resolves with the credentials, or undefined if not found.
13+
* Retrieves stored credential for a given key.
14+
* @param key - The key associated with the stored credential.
15+
* @returns A Promise that resolves with the credential, or undefined if not found.
1616
*/
17-
getCredentials(key: string): Promise<Credentials | undefined>;
17+
getCredential(key: string): Promise<Credential | undefined>;
1818

1919
/**
20-
* Stores credentials for a given key.
21-
* @param key - The key associated with the credentials to store.
22-
* @param credentials - The credentials to store.
23-
* @returns A Promise that resolves when the credentials have been stored.
20+
* Stores credential for a given key.
21+
* @param key - The key associated with the credential to store.
22+
* @param credential - The credential to store.
23+
* @returns A Promise that resolves when the credential have been stored.
2424
*/
25-
storeCredentials(key: string, credentials: Credentials): Promise<void>;
25+
storeCredential(key: string, credential: Credential): Promise<void>;
2626

2727
/**
28-
* Removes the credentials stored for a given key.
29-
* @param key - The key associated with the credentials to store.
28+
* Removes the credential stored for a given key.
29+
* @param key - The key associated with the credential to store.
3030
*/
31-
removeCredentials(key: string): Promise<void>;
31+
removeCredential(key: string): Promise<void>;
3232
}
3333

3434
/**
35-
* An implementation of CredentialsStorage using the browser's localStorage.
35+
* An implementation of CredentialStorage using the browser's localStorage.
3636
*/
37-
export class LocalStorageStrategy implements CredentialsStorage {
38-
async getCredentials(key: string): Promise<Credentials | undefined> {
37+
export class LocalStorageStrategy implements CredentialStorage {
38+
async getCredential(key: string): Promise<Credential | undefined> {
3939
const storedValue = localStorage.getItem(key);
4040
if (!storedValue) return undefined;
41-
return parseCredentials(storedValue);
41+
return parseCredential(storedValue);
4242
}
4343

44-
async storeCredentials(key: string, credentials: Credentials): Promise<void> {
45-
localStorage.setItem(key, stringifyCredentials(credentials));
44+
async storeCredential(key: string, credential: Credential): Promise<void> {
45+
localStorage.setItem(key, stringifyCredential(credential));
4646
}
4747

48-
async removeCredentials(key: string): Promise<void> {
48+
async removeCredential(key: string): Promise<void> {
4949
localStorage.removeItem(key);
5050
}
5151
}
5252

5353
/**
54-
* An implementation of CredentialsStorage using the browser's sessionStorage.
54+
* An implementation of CredentialStorage using the browser's sessionStorage.
5555
*/
56-
export class SessionStorageStrategy implements CredentialsStorage {
57-
async getCredentials(key: string): Promise<Credentials | undefined> {
56+
export class SessionStorageStrategy implements CredentialStorage {
57+
async getCredential(key: string): Promise<Credential | undefined> {
5858
const storedValue = sessionStorage.getItem(key);
5959
if (!storedValue) return undefined;
60-
return parseCredentials(storedValue);
60+
return parseCredential(storedValue);
6161
}
6262

63-
async storeCredentials(key: string, credentials: Credentials): Promise<void> {
64-
sessionStorage.setItem(key, stringifyCredentials(credentials));
63+
async storeCredential(key: string, credential: Credential): Promise<void> {
64+
sessionStorage.setItem(key, stringifyCredential(credential));
6565
}
6666

67-
async removeCredentials(key: string): Promise<void> {
67+
async removeCredential(key: string): Promise<void> {
6868
sessionStorage.removeItem(key);
6969
}
7070
}
7171

7272
/**
73-
* A no-operation implementation of CredentialsStorage that does not store or retrieve credentials.
73+
* A no-operation implementation of CredentialStorage that does not store or retrieve credential.
7474
*/
75-
export class NoStorageStrategy implements CredentialsStorage {
76-
async getCredentials(_key: string): Promise<Credentials | undefined> {
75+
export class NoStorageStrategy implements CredentialStorage {
76+
async getCredential(_key: string): Promise<Credential | undefined> {
7777
return undefined;
7878
}
7979

80-
async storeCredentials(_key: string, _credentials: Credentials): Promise<void> {
80+
async storeCredential(_key: string, _credential: Credential): Promise<void> {
8181
// No storage implementation
8282
}
8383

84-
async removeCredentials(_key: string): Promise<void> {
84+
async removeCredential(_key: string): Promise<void> {
8585
// No storage implementation
8686
}
8787
}
8888

8989
/**
90-
* An abstract class representing the base for all CallCredentials implementations.
90+
* An abstract class representing the base for all CallCredential implementations.
9191
*/
92-
export abstract class CallCredentials {
93-
protected constructor(protected storage: CredentialsStorage, protected key: string) {}
92+
export abstract class CallCredential {
93+
protected constructor(protected storage: CredentialStorage, protected key: string) {}
9494

9595
/**
96-
* Retrieves stored credentials.
97-
* @returns A Promise that resolves with the credentials, or undefined if not found.
96+
* Retrieves stored credential.
97+
* @returns A Promise that resolves with the credential, or undefined if not found.
9898
*/
99-
public abstract getCredentials(): Promise<Credentials | undefined>;
99+
public abstract getCredential(): Promise<Credential | undefined>;
100100

101101
/**
102102
* Retrieves the HTTP header to be used for authentication.
@@ -105,31 +105,30 @@ export abstract class CallCredentials {
105105
public abstract getHeader(): Promise<Header | undefined>;
106106

107107
/**
108-
* Stores credentials.
109-
* @param credentials - The credentials to store.
110-
* @returns A Promise that resolves when the credentials have been stored.
108+
* Stores credential.
109+
* @param credential - The credential to store.
110+
* @returns A Promise that resolves when the credential have been stored.
111111
*/
112-
public abstract storeCredentials(credentials: Credentials): Promise<void>;
112+
public abstract storeCredential(credential: Credential): Promise<void>;
113113

114114
/**
115-
* Remove credentials.
116-
* @param credentials - The credentials to remove.
117-
* @returns A Promise that resolves when the credentials have been removed.
115+
* Remove credential.
116+
* @returns A Promise that resolves when the credential have been removed.
118117
*/
119-
public async removeCredentials(): Promise<void> {
120-
await this.storage.removeCredentials(this.key);
118+
public async removeCredential(): Promise<void> {
119+
await this.storage.removeCredential(this.key);
121120
}
122121
}
123122

124123
/**
125-
* An implementation of CallCredentials for insecure (unauthenticated) channels.
124+
* An implementation of CallCredential for insecure (unauthenticated) channels.
126125
*/
127-
export class InsecureChannelCredentials extends CallCredentials {
126+
export class InsecureChannelCredential extends CallCredential {
128127
/**
129-
* Retrieves stored credentials. Always returns undefined for insecure channels.
128+
* Retrieves stored credential. Always returns undefined for insecure channels.
130129
* @returns A Promise that resolves with undefined.
131130
*/
132-
public override getCredentials(): Promise<Credentials | undefined> {
131+
public override getCredential(): Promise<Credential | undefined> {
133132
return Promise.resolve(undefined);
134133
}
135134

@@ -142,69 +141,69 @@ export class InsecureChannelCredentials extends CallCredentials {
142141
}
143142

144143
/**
145-
* Stores credentials. Does nothing for insecure channels.
146-
* @param credentials - The credentials to store.
144+
* Stores credential. Does nothing for insecure channels.
145+
* @param credential - The credential to store.
147146
* @returns A Promise that resolves immediately.
148147
*/
149-
public override storeCredentials(_credentials: Credentials): Promise<void> {
148+
public override storeCredential(_credential: Credential): Promise<void> {
150149
return Promise.resolve();
151150
}
152151

153152
/**
154-
* Creates a new instance of InsecureChannelCredentials.
155-
* @returns A new instance of InsecureChannelCredentials.
153+
* Creates a new instance of InsecureChannelCredential.
154+
* @returns A new instance of InsecureChannelCredential.
156155
*/
157-
public static create(): InsecureChannelCredentials {
158-
return new InsecureChannelCredentials(new NoStorageStrategy(), '');
156+
public static create(): InsecureChannelCredential {
157+
return new InsecureChannelCredential(new NoStorageStrategy(), '');
159158
}
160159
}
161160

162161
/**
163-
* An implementation of CallCredentials for Bearer token authentication.
162+
* An implementation of CallCredential for Bearer token authentication.
164163
*/
165-
export class BearerCredentials extends CallCredentials {
166-
constructor(storage: CredentialsStorage, key: string) {
164+
export class BearerCredential extends CallCredential {
165+
constructor(storage: CredentialStorage, key: string) {
167166
super(storage, key);
168167
}
169168

170169
/**
171-
* Retrieves stored credentials from the storage strategy.
172-
* @returns A Promise that resolves with the credentials, or undefined if not found.
170+
* Retrieves stored credential from the storage strategy.
171+
* @returns A Promise that resolves with the credential, or undefined if not found.
173172
*/
174-
public async getCredentials(): Promise<Credentials | undefined> {
175-
return await this.storage.getCredentials(this.key);
173+
public async getCredential(): Promise<Credential | undefined> {
174+
return await this.storage.getCredential(this.key);
176175
}
177176

178177
/**
179178
* Retrieves the HTTP header for Bearer token authentication.
180179
* @returns A Promise that resolves with the header, or undefined if the token is not available.
181180
*/
182181
public override async getHeader(): Promise<Header | undefined> {
183-
const credentials = await this.getCredentials();
184-
if (!credentials) return undefined;
182+
const credential = await this.getCredential();
183+
if (!credential) return undefined;
185184

186-
const token = credentials['access_token'] || credentials['token'] || credentials['accessToken'];
185+
const token = credential['access_token'] || credential['token'] || credential['accessToken'];
187186
if (!token) return undefined;
188187

189188
return { name: 'Authorization', value: `Bearer ${token}` };
190189
}
191190

192191
/**
193-
* Stores credentials using the storage strategy.
194-
* @param credentials - The credentials to store.
195-
* @returns A Promise that resolves when the credentials have been stored.
192+
* Stores credential using the storage strategy.
193+
* @param credential - The credential to store.
194+
* @returns A Promise that resolves when the credential have been stored.
196195
*/
197-
public async storeCredentials(credentials: Credentials): Promise<void> {
198-
await this.storage.storeCredentials(this.key, credentials);
196+
public async storeCredential(credential: Credential): Promise<void> {
197+
await this.storage.storeCredential(this.key, credential);
199198
}
200199

201200
/**
202-
* Creates a new instance of BearerCredentials with the specified storage strategy and key.
203-
* @param storage - The storage strategy to use for storing and retrieving credentials.
204-
* @param key - The key associated with the credentials.
205-
* @returns A new instance of BearerCredentials.
201+
* Creates a new instance of BearerCredential with the specified storage strategy and key.
202+
* @param storage - The storage strategy to use for storing and retrieving credential.
203+
* @param key - The key associated with the credential.
204+
* @returns A new instance of BearerCredential.
206205
*/
207-
public static create(storage: CredentialsStorage, key: string): BearerCredentials {
208-
return new BearerCredentials(storage, key);
206+
public static create(storage: CredentialStorage, key: string): BearerCredential {
207+
return new BearerCredential(storage, key);
209208
}
210209
}

0 commit comments

Comments
 (0)