Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: await credentials when subscribing #351

Merged
merged 6 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/AsyncUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export async function* map<T, U>(

export async function* filter<T>(
iterable: AsyncIterable<T>,
predicate: (value: T) => boolean,
predicate: (value: T) => Promise<boolean> | boolean,
): AsyncGenerator<T, void, void> {
for await (const value of iterable) {
if (predicate(value)) yield value;
if (await predicate(value)) yield value;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/AuthorizedSocketConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ export default class AuthorizedSocketConnection<TContext, TCredentials> {
this.socket.emit('app_error', error);
}

isAuthorized(
async isAuthorized(
data: any,
hasPermission: (data: any, credentials: TCredentials) => boolean,
) {
const credentials = this.config.credentialsManager.getCredentials();
const credentials = await this.config.credentialsManager.getCredentials();
const isAuthorized = !!credentials && hasPermission(data, credentials);
if (!isAuthorized) {
this.log('info', 'unauthorized', {
Expand Down Expand Up @@ -239,7 +239,7 @@ export default class AuthorizedSocketConnection<TContext, TCredentials> {
const stream: AsyncIterable<unknown> = resultOrStream as any;

for await (const payload of stream) {
const credentials = this.config.credentialsManager.getCredentials();
const credentials = await this.config.credentialsManager.getCredentials();

let response;
try {
Expand Down
2 changes: 1 addition & 1 deletion src/CredentialsManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface CredentialsManager<TCredentials> {
getCredentials(): TCredentials | null | undefined;
getCredentials(): Promise<TCredentials | null | undefined>;
authenticate(authorization: string): void | Promise<void>;
unauthenticate(): void | Promise<void>; // allow for redis etc down the line
}
23 changes: 13 additions & 10 deletions src/JwtCredentialsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ export default abstract class JwtCredentialsManager<

token: string | null | undefined;

credentials: TCredentials | null | undefined;
credentialsPromise: Promise<TCredentials | null | undefined> | null;

renewHandle: NodeJS.Timeout | null | undefined;

constructor(config: JwtCredentialsManagerConfig) {
this.config = config;

this.token = null;
this.credentials = null;
this.credentialsPromise = null;
}

getCredentials(): TCredentials | null | undefined {
const { credentials } = this;
async getCredentials(): Promise<TCredentials | null | undefined> {
if (!this.credentialsPromise) return null;

patricktemple marked this conversation as resolved.
Show resolved Hide resolved
const credentials = await this.credentialsPromise;
if (credentials && Date.now() >= credentials.exp * SECONDS_TO_MS) {
return null;
}
Expand All @@ -56,7 +58,7 @@ export default abstract class JwtCredentialsManager<
}

this.token = null;
this.credentials = null;
this.credentialsPromise = null;
}

async updateCredentials() {
Expand All @@ -65,21 +67,22 @@ export default abstract class JwtCredentialsManager<
throw new Error('JwtCredentialManager: Unauthenticated');
}

const credentials = await this.getCredentialsFromAuthorization(token);
this.credentialsPromise = Promise.resolve(
this.getCredentialsFromAuthorization(token),
);
await this.credentialsPromise;

// Avoid race conditions with multiple updates.
if (this.token !== token) {
return;
}
patricktemple marked this conversation as resolved.
Show resolved Hide resolved

this.credentials = credentials;

// TODO: Don't schedule renewal if the new credentials are expired or
// almost expired.
this.scheduleRenewCredentials();
}

scheduleRenewCredentials() {
async scheduleRenewCredentials() {
if (this.renewHandle) {
clearTimeout(this.renewHandle);
}
Expand All @@ -89,7 +92,7 @@ export default abstract class JwtCredentialsManager<
return;
}

const { credentials } = this;
const credentials = await this.credentialsPromise;
if (!credentials) {
return;
}
Expand Down