From 1a664e3f55723effca7f58f96fe2201a160e274b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Baumeyer?= Date: Tue, 26 Feb 2019 15:08:59 +0100 Subject: [PATCH] Add types for sns-validator --- types/sns-validator/index.d.ts | 22 ++++++++++++ types/sns-validator/sns-validator-tests.ts | 41 ++++++++++++++++++++++ types/sns-validator/tsconfig.json | 23 ++++++++++++ types/sns-validator/tslint.json | 1 + 4 files changed, 87 insertions(+) create mode 100644 types/sns-validator/index.d.ts create mode 100644 types/sns-validator/sns-validator-tests.ts create mode 100644 types/sns-validator/tsconfig.json create mode 100644 types/sns-validator/tslint.json diff --git a/types/sns-validator/index.d.ts b/types/sns-validator/index.d.ts new file mode 100644 index 0000000000..8a816117b0 --- /dev/null +++ b/types/sns-validator/index.d.ts @@ -0,0 +1,22 @@ +// Type definitions for sns-validator 0.3 +// Project: https://github.com/aws/aws-js-sns-message-validator#readme +// Definitions by: Kévin Baumeyer +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +declare class MessageValidator { + /** + * Create a new MessageValidator + * @param hostPattern - A pattern used to validate that a message's certificate originates from a trusted domain. Default: `/^sns\.[a-zA-Z0-9\-]{3,}\.amazonaws\.com(\.cn)?$/` + * @param encoding - The encoding of the messages being signed. Default is `utf8` + */ + constructor(hostPattern?: RegExp, encoding?: string); + + /** + * Validates a message's signature and passes it to the provided callback. + * @param hash Can be the raw or parsed message. + * @param cb The callback which is called with the message when it is valid or with an Error when it isn't. + */ + validate(hash: string | object, cb: (err: Error | null, message?: object) => void): void; +} +export = MessageValidator; diff --git a/types/sns-validator/sns-validator-tests.ts b/types/sns-validator/sns-validator-tests.ts new file mode 100644 index 0000000000..fc89b27776 --- /dev/null +++ b/types/sns-validator/sns-validator-tests.ts @@ -0,0 +1,41 @@ +import SNSValidator = require('sns-validator'); + +/* tslint:disable:max-line-length */ +const message = `{ + "Type" : "SubscriptionConfirmation", + "MessageId" : "165545c9-2a5c-472c-8df2-7ff2be2b3b1b", + "Token" : "2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736", + "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic", + "Message" : "You have chosen to subscribe to the topic arn:aws:sns:us-west-2:123456789012:MyTopic.\nTo confirm the subscription, visit the SubscribeURL included in this message.", + "SubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:123456789012:MyTopic&Token=2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736", + "Timestamp" : "2012-04-26T20:45:04.751Z", + "SignatureVersion" : "1", + "Signature" : "EXAMPLEpH+DcEwjAPg8O9mY8dReBSwksfg2S7WKQcikcNKWLQjwu6A4VbeS0QHVCkhRS7fUQvi2egU3N858fiTDN6bkkOxYDVrY0Ad8L10Hs3zH81mtnPk5uvvolIC1CXGu43obcgFxeL3khZl8IKvO61GWB6jI9b5+gLPoBc1Q=", + "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem" + }`; +/* tslint:enable:max-line-length */ +const messageObject = JSON.parse(message); + +// Test with a string +new SNSValidator().validate(message, (err, message) => { + const e = err; // $ExpectType Error | null + const m = message; // $ExpectType object | undefined +}); + +// Test with a JSON +new SNSValidator().validate(messageObject, (err, message) => { + const e = err; // $ExpectType Error | null + const m = message; // $ExpectType object | undefined +}); + +// Test with an invalid string +new SNSValidator().validate('invalid_message', (err, message) => { + const e = err; // $ExpectType Error | null + const m = message; // $ExpectType object | undefined +}); + +// Test with an invalid JSON +new SNSValidator().validate({...messageObject, Signature: "invalid_signature"}, (err, message) => { + const e = err; // $ExpectType Error | null + const m = message; // $ExpectType object | undefined +}); diff --git a/types/sns-validator/tsconfig.json b/types/sns-validator/tsconfig.json new file mode 100644 index 0000000000..a2f40ef3e3 --- /dev/null +++ b/types/sns-validator/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "sns-validator-tests.ts" + ] +} diff --git a/types/sns-validator/tslint.json b/types/sns-validator/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/sns-validator/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }