-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement webhook token * add test for webhook token * add token validator * fix lint error * allow null for token in CreateWebhookRequestDto * feat: add webhook token in web (#534) * feat: add webhook token in web * fix: remove console lkog * fix: ci * add test for token-validator * implement retry logic * fix: i18n --------- Co-authored-by: Miller <[email protected]>
- Loading branch information
Showing
23 changed files
with
480 additions
and
104 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
apps/api/src/common/validators/token-validator.decorator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright 2023 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { IsNotEmpty, validate } from 'class-validator'; | ||
|
||
import { TokenValidator } from './token-validator'; | ||
|
||
class TokenDto { | ||
@IsNotEmpty() | ||
@TokenValidator({ message: 'Invalid token format' }) | ||
token: string; | ||
} | ||
|
||
describe('TokenValidator', () => { | ||
it('should validate a correct token', async () => { | ||
const dto = new TokenDto(); | ||
dto.token = 'validToken123456'; | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBe(0); | ||
}); | ||
|
||
it('should invalidate a token with invalid characters', async () => { | ||
const dto = new TokenDto(); | ||
dto.token = 'invalidToken$123'; | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBeGreaterThan(0); | ||
expect(errors[0].constraints).toHaveProperty('TokenValidatorConstraint'); | ||
}); | ||
|
||
it('should invalidate a token that is too short', async () => { | ||
const dto = new TokenDto(); | ||
dto.token = 'short'; | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBeGreaterThan(0); | ||
expect(errors[0].constraints).toHaveProperty('TokenValidatorConstraint'); | ||
}); | ||
|
||
it('should invalidate an empty token', async () => { | ||
const dto = new TokenDto(); | ||
dto.token = ''; | ||
|
||
const errors = await validate(dto); | ||
expect(errors.length).toBeGreaterThan(0); | ||
expect(errors[0].constraints).toHaveProperty('isNotEmpty'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright 2023 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { | ||
registerDecorator, | ||
ValidationOptions, | ||
ValidatorConstraint, | ||
ValidatorConstraintInterface, | ||
} from 'class-validator'; | ||
|
||
@ValidatorConstraint({ async: false }) | ||
export class TokenValidatorConstraint implements ValidatorConstraintInterface { | ||
validate(token: string | null) { | ||
const regex = /^[a-zA-Z0-9._-]+$/; | ||
return ( | ||
!token || | ||
(typeof token === 'string' && regex.test(token) && token.length >= 16) | ||
); | ||
} | ||
|
||
defaultMessage() { | ||
return 'Token must be at least 16 characters long and contain only alphanumeric characters, dots, hyphens, and underscores.'; | ||
} | ||
} | ||
|
||
export function TokenValidator(validationOptions?: ValidationOptions) { | ||
return function (object: object, propertyName: string) { | ||
registerDecorator({ | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
options: validationOptions, | ||
constraints: [], | ||
validator: TokenValidatorConstraint, | ||
}); | ||
}; | ||
} |
30 changes: 30 additions & 0 deletions
30
apps/api/src/configs/modules/typeorm-config/migrations/1720760282371-add-token-on-webhook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright 2023 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import type { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddTokenOnWebhook1720760282371 implements MigrationInterface { | ||
name = 'AddTokenOnWebhook1720760282371'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE \`webhooks\` ADD \`token\` varchar(255) NULL DEFAULT NULL AFTER \`url\``, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE \`webhooks\` DROP COLUMN \`token\``); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.