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

Add TypeScript declarations #20

Closed

Conversation

vsund
Copy link
Contributor

@vsund vsund commented Mar 14, 2018

This PR adds type declarations for better support for this library in TypeScript.
Fixes #12.

There are some anys 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).

@vsund
Copy link
Contributor Author

vsund commented Mar 15, 2018

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:

signedTokenAsString: eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJpYXQiOjE0NDA3MTM0MTQuODV9.cnCOBNFSfzpn35pwo2aT74xoH7JzCwmCEAWvKRxwqGV4lUiKLmWeA6V4fnU9-NX8sYNrsrUzY-5g5oEhzQ3mqQ
signedTokenAsObject: {"header":["eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ"],"payload":"{\"iat\":1440713414.85}","signature":["cnCOBNFSfzpn35pwo2aT74xoH7JzCwmCEAWvKRxwqGV4lUiKLmWeA6V4fnU9-NX8sYNrsrUzY-5g5oEhzQ3mqQ"]}

unsignedToken: eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJpYXQiOjE0NDA3MTM0MTQuODV9.

tokenDataFromString: {"header":{"typ":"JWT","alg":"ES256K"},"payload":{"iat":1440713414.85},"signature":"cnCOBNFSfzpn35pwo2aT74xoH7JzCwmCEAWvKRxwqGV4lUiKLmWeA6V4fnU9-NX8sYNrsrUzY-5g5oEhzQ3mqQ"}
tokenDataFromObject: {"header":[{"typ":"JWT","alg":"ES256K"}],"payload":{"iat":1440713414.85},"signature":["cnCOBNFSfzpn35pwo2aT74xoH7JzCwmCEAWvKRxwqGV4lUiKLmWeA6V4fnU9-NX8sYNrsrUzY-5g5oEhzQ3mqQ"]}

isValidFromString: true
isValidFromObject: true

header: {"typ":"JWT","alg":"ES256K"}

Seems to work pretty well. Note: I didn't test the MissingParametersError because you can't pass too less parameters in TypeScript (probably can remove this from the types), the InvalidTokenError doesn't get thrown anywhere, so didn't test it either.

@vsund
Copy link
Contributor Author

vsund commented Mar 16, 2018

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).

@vsund
Copy link
Contributor Author

vsund commented Jun 5, 2019

Guess that's not needed anymore :) (#38)

@vsund vsund closed this Jun 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant