@abskmj/jwt-utility 中文文档教程

发布于 7年前 浏览 22 项目主页 更新于 3年前

JWT Utility for Node.js

此实用程序可用于生成和解析 JWT(JSON Web 令牌)

Supported Algorithms

  • HS256
  • HS384
  • HS512

Utility Methods

MethodArgumentsDescription
getFactoryStringSet algorithm to be used to generate the JWT and returns an instance of Factory
getParserReturns an instance of Parser which can be used to parse and verify a JWT

Generating a Token

const JWTUtility = require('@abskmj/jwt-utility');

let jwt = JWTUtility.getFactory('HS256')
    .setIssuer('AuthServer')
    .setSubject('Login')
    .setExpiry(10)
    .setClaims({
        user: 'testUser',
        name: 'Test User'
    })
    .sign('secret key');

Factory Instance Methods

MethodArgumentsDescription
setIssuerStringSet iss claim value.
setSubjectStringSet sub claim value.
setAudienceStringSet aud claim value.
setExpiryNumberSet exp claim value, value will be current epoch time in seconds + seconds passed as argument.
setClaimsJSONSet the custom data that will be part of the JWT.
signStringGenerate the JWT using the secret key passed

Parsing a Token

const JWTUtility = require('@abskmj/jwt-utility');

let data = JWTUtility.getParser()
    .validateIssuer('AuthServer')
    .validateSubject('Login')
    .parse(jwt, 'secret key');

/*
data:
{ 
    headers: {
        alg: 'HS256',
        typ: 'JWT'
    },
    claims: {
        user: 'testUser',
        name: 'Test User',
        iat: 1512555172,
        iss: 'AuthServer',
        sub: 'Login',
        exp: 1512555182
    }
}

*/    

Parser Instance Methods

MethodArgumentsDescription
validateIssuerStringValidate iss claim value.
validateSubjectStringValidate sub claim value.
validateAudienceStringValidate aud claim value.
validateExpiryNumberValidate exp claim value, value validated against current epoch time in seconds + seconds passed as argument.
parseString, StringSet JWT token and key used to verify the token

Further Development

尚未计划支持 RS256、RS384、RS512。 如果您希望请求任何新功能或错误修复,请联系开发人员

JWT Utility for Node.js

This utility can be used to generate and parse JWTs (JSON Web Tokens).

Supported Algorithms

  • HS256
  • HS384
  • HS512

Utility Methods

MethodArgumentsDescription
getFactoryStringSet algorithm to be used to generate the JWT and returns an instance of Factory
getParserReturns an instance of Parser which can be used to parse and verify a JWT

Generating a Token

const JWTUtility = require('@abskmj/jwt-utility');

let jwt = JWTUtility.getFactory('HS256')
    .setIssuer('AuthServer')
    .setSubject('Login')
    .setExpiry(10)
    .setClaims({
        user: 'testUser',
        name: 'Test User'
    })
    .sign('secret key');

Factory Instance Methods

MethodArgumentsDescription
setIssuerStringSet iss claim value.
setSubjectStringSet sub claim value.
setAudienceStringSet aud claim value.
setExpiryNumberSet exp claim value, value will be current epoch time in seconds + seconds passed as argument.
setClaimsJSONSet the custom data that will be part of the JWT.
signStringGenerate the JWT using the secret key passed

Parsing a Token

const JWTUtility = require('@abskmj/jwt-utility');

let data = JWTUtility.getParser()
    .validateIssuer('AuthServer')
    .validateSubject('Login')
    .parse(jwt, 'secret key');

/*
data:
{ 
    headers: {
        alg: 'HS256',
        typ: 'JWT'
    },
    claims: {
        user: 'testUser',
        name: 'Test User',
        iat: 1512555172,
        iss: 'AuthServer',
        sub: 'Login',
        exp: 1512555182
    }
}

*/    

Parser Instance Methods

MethodArgumentsDescription
validateIssuerStringValidate iss claim value.
validateSubjectStringValidate sub claim value.
validateAudienceStringValidate aud claim value.
validateExpiryNumberValidate exp claim value, value validated against current epoch time in seconds + seconds passed as argument.
parseString, StringSet JWT token and key used to verify the token

Further Development

Support for RS256, RS384, RS512 is not planned yet. Please get in touch with the developer, if you wish to request any new functionality or bug fix.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文