-
Notifications
You must be signed in to change notification settings - Fork 26
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
Add TypeScript declarations #20
Add TypeScript declarations #20
Conversation
Just tested the types (where testing means playing around in an empty project). Code: import * as jsontokens from 'jsontokens';
// Signing Tokens
const rawPrivateKey: string = '278a5de700e29faae8e40e366ec5012b5ec63d36ec77e8a2417154cc1d25383f';
const tokenPayload = {"iat": 1440713414.85};
const tokenSigner: jsontokens.TokenSigner = new jsontokens.TokenSigner('ES256K', rawPrivateKey);
const signedTokenAsString: string|jsontokens.JWT = tokenSigner.sign(tokenPayload);
const signedTokenAsObject: string|jsontokens.JWT = tokenSigner.sign(tokenPayload, true);
// Creating Unsecured Tokens
const unsignedToken: string = jsontokens.createUnsecuredToken(tokenPayload);
// Decoding Tokens
const tokenDataFromString: jsontokens.JWT = jsontokens.decodeToken(signedTokenAsString);
const tokenDataFromObject: jsontokens.JWT = jsontokens.decodeToken(signedTokenAsObject);
// Verifying Tokens
const rawPublicKey: string = '03fdd57adec3d438ea237fe46b33ee1e016eda6b585c3e27ea66686c2ea5358479';
const tokenVerifier: jsontokens.TokenVerifier = new jsontokens.TokenVerifier('ES256K', rawPublicKey);
const isValidFromString: boolean = tokenVerifier.verify(signedTokenAsString);
const isValidFromObject: boolean = tokenVerifier.verify(signedTokenAsObject);
// Other non-readme things
const header: jsontokens.JWTHeader = tokenSigner.header();
// Print everything
console.log('signedTokenAsString: ' + signedTokenAsString);
console.log('signedTokenAsObject: ' + JSON.stringify(signedTokenAsObject));
console.log();
console.log('unsignedToken: ' + unsignedToken);
console.log();
console.log('tokenDataFromString: ' + JSON.stringify(tokenDataFromString));
console.log('tokenDataFromObject: ' + JSON.stringify(tokenDataFromObject));
console.log();
console.log('isValidFromString: ' + isValidFromString);
console.log('isValidFromObject: ' + isValidFromObject);
console.log();
console.log('header: ' + JSON.stringify(header)); Output:
Seems to work pretty well. Note: I didn't test the |
Just discovered while porting that I forgot to make some parameters optional (fixed now). I think it would be good to keep this open until I finished porting (in case there's more to fix). |
Guess that's not needed anymore :) (#38) |
This PR adds type declarations for better support for this library in TypeScript.
Fixes #12.
There are some
any
s in there, but it's not really possible to define them more precisely. Happy to adjust them on request or better ideas. This also adds documentation that's showing up on code completion when in use with TypeScript (also not sure whether it's completely correct, please check :)).To test this, you can clone my branch and link it into an empty TypeScript project.
Please note: These types aren't extensively tested. I'll probably do this later, but I thought that I'll go ahead and raise this PR first (for review and discussion).