Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yuri2peter/easy-jwt
easy-jwt
https://github.com/yuri2peter/easy-jwt
Last synced: 6 days ago
JSON representation
easy-jwt
- Host: GitHub
- URL: https://github.com/yuri2peter/easy-jwt
- Owner: yuri2peter
- License: apache-2.0
- Created: 2020-06-11T03:22:01.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-03-26T21:30:19.000Z (almost 3 years ago)
- Last Synced: 2024-12-17T23:09:10.032Z (about 1 month ago)
- Language: JavaScript
- Size: 85.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# easy-jwt
Generate and parse the URL friendly JWT token.
> easy 系列新作。
## Usage
```javascript
const jwt = require('easy-jwt');function testSimpleJwt() {
console.log('\n ------------------- \n Begin simple jwt tests.');
// Defines a secret
const secret = 'I am the secret!';
const payload = 'Hello, world!';
console.log('payload:', payload);
// Make a jwt token.
const token = jwt.sign(payload, secret, 3);
console.log('token:', token);
// print: eyJwIjoiSGVsbG8sIHdvcmxkISIsImUiOjE1OTE4NDk3Mzc4OTgsInMiOiI2NDliMjdiMTNlZTAxOGU0YWEyODcxNDY5NTQwMTc5YiJ9// Decodes without secret
const decodedPayload = jwt.decode(token);
console.log('Decoded Payload:', decodedPayload); // print: Hello, world!
const decodedBody = jwt.decode(token, true);
console.log('Decoded Body:\n', decodedBody);
// print: { p:'Hello, world!', e:1591849737898 ,s:'649b27b13ee018e4aa2871469540179b' }// Verifies
console.log(
'Verifies with wrong secret:',
jwt.verify(token, 'I am the wrong secret!'),
);
// print: Verify with wrong secret: falseconsole.log('Verifies immediately:', jwt.verify(token, secret));
// print: Verify immediately: truesetTimeout(() => {
console.log('Verifies after 4s:', jwt.verify(token, secret));
// print: Verify after 4s: false
}, 4000);
}function testRsaJwt() {
console.log('\n ------------------- \n Begin RSA jwt tests.');
// Defines a secret
const privateKey = `-----BEG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I5wXh4Mq4Vtu7uEje
ogAOgXACaIqiFyrk3wIDAQAB
-----END PUBLIC KEY-----`;const payload = 'Hello, world!';
console.log('payload:', payload);
// Make a jwt token.
const token = jwt.signRSA(payload, privateKey, 3);
console.log('token:', token);
// print: eyJwIjoiSGVsbG8sIHdvcmxkISIsImUiOjE1OTIyNzYyODMyMDUsInMiOiJ3SnVieG11VnVGZDZCZ2oyNFpncVpMWXBsOUktMG1TUkZ5U1B2ZWJackc3UDlCU3p1bmlwTTB1c01TY1J3b1ZKSTR6UUprTWtGdGNrSENLa1kzM2k1dkVwWXo3ZlNZLVZhcW51MWhob1dEUEJ4TXMtV0lNYVFwRl9FOGcyczBUS1hXbGl5S2FDSmVRRGZ4X0x1NFRKWk9VY3c5ZTZJMG9BTUxjZmdWTnljWDgifQ// Decodes without secret
const decodedPayload = jwt.decode(token);
console.log('Decoded Payload:', decodedPayload); // print: Hello, world!
const decodedBody = jwt.decode(token, true);
console.log('Decoded Body:\n', decodedBody);
// print: { p:'Hello, world!', e:1591849737898 ,s:'wJubxmuVuFd6Bgj24ZgqZLYpl9I-0mSRFySPvebZrG7P9BSzunipM0usMScRwoVJI4zQJkMkFtckHCKkY33i5vEpYz7fSY-Vaqnu1hhoWDPBxMs-WIMaQpF_E8g2s0TKXWliyKaCJeQDfx_Lu4TJZOUcw9e6I0oAMLcfgVNycX8' }// Verifies
console.log(
'Verifies with wrong secret:',
jwt.verifyRSA(token, 'I am the wrong secret!'),
);
// print: Verify with wrong secret: falseconsole.log('Verifies immediately:', jwt.verifyRSA(token, publicKey));
// print: Verify immediately: truesetTimeout(() => {
console.log('Verifies after 4s:', jwt.verifyRSA(token, publicKey));
// print: Verify after 4s: false
}, 4000);
}function main() {
setTimeout(() => {
testSimpleJwt();
}, 0);
setTimeout(() => {
testRsaJwt();
}, 5000);
}main();
```## API
```javascript
// md5函数
function md5(text)/**
* 生成简单加密jwt
* @param {*} payload 数据
* @param {string} secret 密钥,推荐长度不小于16
* @param {number} expiresIn 有效期,单位秒。如3600表示该token在一小时后过期
* @returns {string} token
*/
function sign(payload, secret, expiresIn = 600)/**
* 验证简单加密jwtToken是否合法
* @param {string} token jwtToken
* @param {string} secret 密钥
* @returns {boolean}
*/
function verify(token, secret)/**
* 生成RSA加密jwt
* @param {*} payload 数据
* @param {string} privateKey 私钥
* @param {number} expiresIn 有效期,单位秒。如3600表示该token在一小时后过期
* @returns {string} token
*/
function signRSA(payload, privateKey, expiresIn = 600)/**
* 验证RSA jwtToken是否合法
* @param {string} token jwtToken
* @param {string} publicKey 公钥
* @returns {boolean}
*/
function verifyRSA(token, publicKey)/**
* 解码jwtToken(无须密钥)
* @param {string} token jwtToken
* @param {boolean} complete 是否返回完整的jwt构造对象,可以查看过期时间。默认false。
* @returns {any | object<{ payload, expires, signature }>}
*/
function decode(token, complete = false)```