Skip to content

Commit

Permalink
remove legacy secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
d-fischer committed Apr 20, 2023
1 parent d7a38c5 commit f4cdf98
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 100 deletions.
2 changes: 0 additions & 2 deletions packages/eventsub-base/src/EventSubBase.ts
Expand Up @@ -130,8 +130,6 @@ export abstract class EventSubBase extends EventEmitter {
/** @private */ @Enumerable(false) readonly _apiClient: ApiClient;
/** @private */ readonly _logger: Logger;

/** @private */ _legacySecrets: boolean | 'migrate' = false;

/**
* Fires when a subscription is revoked.
*
Expand Down
50 changes: 0 additions & 50 deletions packages/eventsub-base/src/subscriptions/EventSubSubscription.ts
Expand Up @@ -92,43 +92,6 @@ export abstract class EventSubSubscription</** @private */ T = unknown> {
this._client._dropSubscription(this.id);
}

/**
* Migrates the subscription from legacy secrets to modern secrets.
*/
async migrate(): Promise<void> {
if (this._client._legacySecrets !== 'migrate') {
throw new Error(
"The `.migrate()` method is not available unless the legacySecrets options is set to 'migrate'"
);
}
if (!this._startedFromExistingTwitchSub) {
this._client._logger.warn(`Tried to migrate subscription ${this.id} but it was already migrated`);
return;
}
await this._unsubscribe().then(
async () => {
this._verified = false;
this._twitchSubscriptionData = undefined;
this._startedFromExistingTwitchSub = false;

await this._subscribe().then(
data => {
this._twitchSubscriptionData = data;
this._client._registerTwitchSubscription(this as EventSubSubscription, data);
},
e => {
this._client._logger.error(
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
`Subscription ${this.id} failed to subscribe: ${(e as Error).message ?? e}`
);
this._client._notifySubscriptionCreateError(this as EventSubSubscription, e);
}
);
},
e => this._client._notifySubscriptionDeleteError(this as EventSubSubscription, e)
);
}

/**
* Outputs the base command to execute for testing the subscription using the Twitch CLI.
*
Expand All @@ -138,19 +101,6 @@ export abstract class EventSubSubscription</** @private */ T = unknown> {
return await this._client._getCliTestCommandForSubscription(this as EventSubSubscription);
}

/**
* Whether the subscription uses a legacy secret.
*
* You can use this property to check whether any subscription still has to be migrated from legacy secrets.
*/
get usesLegacySecret(): boolean {
if (this._client._legacySecrets === 'migrate') {
return this._startedFromExistingTwitchSub;
}

return this._client._legacySecrets;
}

/**
* The user ID the subscription is supposed to be authenticated as.
*
Expand Down
55 changes: 7 additions & 48 deletions packages/eventsub-http/src/EventSubHttpBase.ts
Expand Up @@ -48,18 +48,6 @@ export interface EventSubHttpBaseConfig extends EventSubBaseConfig {
* Enabled by default. Set this to `false` to disable it.
*/
helperRoutes?: boolean;

/**
* Whether to use the legacy way of augmenting your EventSub secret in subscriptions.
*
* This setting is only provided for compatibility/migration purposes.
* You should switch it off at your earliest convenience.
*
* You can set this to the string 'migrate' to migrate your subscription to the new secrets.
* This will treat all existing subscriptions as legacy and all new subscriptions as non-legacy,
* then you may migrate the existing subscriptions using `.migrate()`.
*/
legacySecrets?: boolean | 'migrate';
}

/**
Expand Down Expand Up @@ -98,21 +86,6 @@ export abstract class EventSubHttpBase extends EventSubBase {
this._secret = config.secret;
this._strictHostCheck = config.strictHostCheck ?? true;
this._helperRoutes = config.helperRoutes ?? true;
if (config.legacySecrets === undefined) {
this._logger.warn(`In version 6.0, the automatic augmentation of EventSub secrets was disabled by default.
If you have been using a lower version before, your subscriptions will fail to verify now.
A new option named \`legacySecrets\` was introduced in order to enable you to migrate your subscriptions.
You should still migrate this as soon as possible, as in the next major version this switch will go away, and then you will have to remove all your subscriptions and subscribe to them again.
To make Twurple migrate the subscriptions smoothly, please add \`legacySecrets: 'migrate'\` to your EventSub configuration.
This will treat all pre-existing subscriptions as legacy and all new subscriptions as modern.
You can then call \`.migrate()\` on your pre-existing subscriptions to make them use modern secrets.
After restarting all these subscriptions, before you restart again, set it to \`false\`.
To silence this warning (if you're done migrating or if you're a new user), please add \`legacySecrets: false\` to your EventSub configuration.
To use your legacy subscriptions without having to clean them up and resubscribing, please add \`legacySecrets: true\` to your EventSub configuration.`);
}
this._legacySecrets = config.legacySecrets ?? false;
}

/** @private */
Expand All @@ -132,15 +105,15 @@ To use your legacy subscriptions without having to clean them up and resubscribi
return {
method: 'webhook',
callback: await this._buildHookUrl(subscription.id),
secret: this._createSecretForSubscription(subscription)
secret: this._secret
};
}

/** @private */
async _getCliTestCommandForSubscription(subscription: EventSubSubscription): Promise<string> {
return `twitch event trigger ${subscription._cliName} -F ${await this._buildHookUrl(
subscription.id
)} -s ${this._createSecretForSubscription(subscription)}`;
return `twitch event trigger ${subscription._cliName} -F ${await this._buildHookUrl(subscription.id)} -s ${
this._secret
}`;
}

/** @private */
Expand Down Expand Up @@ -220,7 +193,7 @@ To use your legacy subscriptions without having to clean them up and resubscribi
return;
}

const verified = this._verifyData(subscription, messageId, timestamp, body, algoAndSignature);
const verified = this._verifyData(messageId, timestamp, body, algoAndSignature);
const data = JSON.parse(body) as EventSubHttpPayload;
if (!verified) {
this._logger.warn(`Could not verify action ${type} of event: ${id}`);
Expand Down Expand Up @@ -365,28 +338,14 @@ To use your legacy subscriptions without having to clean them up and resubscribi
subscription._handleData(payload);
}

private _verifyData(
subscription: EventSubSubscription,
messageId: string,
timestamp: string,
body: string,
algoAndSignature: string
): boolean {
private _verifyData(messageId: string, timestamp: string, body: string, algoAndSignature: string): boolean {
const [algorithm, signature] = algoAndSignature.split('=', 2);

const hash = crypto
.createHmac(algorithm, this._createSecretForSubscription(subscription))
.createHmac(algorithm, this._secret)
.update(messageId + timestamp + body)
.digest('hex');

return hash === signature;
}

private _createSecretForSubscription(subscription: EventSubSubscription) {
if (subscription.usesLegacySecret) {
return `${subscription.id}.${this._secret}`.slice(-100);
}

return this._secret;
}
}

0 comments on commit f4cdf98

Please sign in to comment.