npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

titbit-token

v2.1.3

Published

the extension of titbit for user login with token

Downloads

54

Readme

titbit-token

用于进行token会话验证的处理。通过make生成token,verify验证token是否有效。

安装

npm i titbit-token

如何使用:


const titbit = require('titbit')

const titok = require('titbit-token')

let tk = new titok({
    //验证失败时,返回的状态码,默认是403
    failedCode: 400
})

const app = new titbit({
  debug: true
})

app.addService('token', tk)

//必须是32位密码
tk.setKey('1234567890abcdefghijklmnopqrstuv')

app.addService('tok', tk)

app.use(tk, {group: 'token'})

app.post('/login', async c => {
    //进行登录验证
    
    //...

    let userinfo = {
        //....
    }

    let token = c.service.token.make(userinfo)
    //最后返回登录成功后的token
    c.send(token)

})

//其他路由都属于需要验证的token分组。
app.get('/user', async c => {
    //...
}, {group: 'token'})

app.run(1234)

这个示例是放在一个程序文件中的完整使用过程。你应该把验证过程放在独立的中间件模块中,让其自动进行token验证。

初始化以及token的生成和验证


const titok = require('titbit-token')

let tok = new titok({
    //表示有效期的秒数
    expires: 3600,

    //加解密的密钥,长度32。
    key : 'qazxswedcfvrgthynujmkiolpr5tf765',

    //初始向量值,用于和最开始的数据块进行运算,长度16。
    iv : 'qawsedrftgyhujie',
})

let data = {
    id : '1001',
    name : 'world'
}

let tokenstr = tok.make(data)

let dedata = tok.verify(tokenstr)

console.log(tokenstr, dedata)

make一定要对键值对形式的数据进行处理并使用JSON.stringify进行序列化,不能传递其他类型的数据。

verify的返回值是一个对象,属性ok表示是否成功,如果ok为false,则errcode是一个单词描述的状态码信息,并且是大写的。

如果ok为true,则data是解密后的数据,同时存在now属性是获取的当前时间戳,后续处理不必再次调用Date.now()获取。主要目的是用于根据时间判断是否自动更新token的情况。

添加自定义的ID以及Key、IV

make支持第二个参数是token id。使用token id可以进行比较精确的控制,多个应用签发的token可以有不同的token id。要启用此功能,则需要先调用addTokenId添加要识别的应用Id。

要让不同的id具有不同的key和iv,则可以使用setIdKeyIv。


const titok = require('titbit-token')

let tok = new titok()

//支持参数是stirng或array,最终都是转换成array,然后循环添加。
tok.addTokenId(['test', 'app1', 'app2'])

//参数:id、key、iv
//一旦设置了此值,make生成token的时候,如果
tok.setIdKeyIv('app1', 'qazxswedcfvrgthynujmkiolp0987654', 'qawsedrftgyhujik')

自定key、iv、id作为参数


const titok = require('titbit-token')

let tok = new titok()

let ikv = {
    key : 'qazxswedcfvrgthynujmkiolp0987654',
    iv : 'qawsedrftgyhujik',
    id : 'app1'
}

let data = {
    id : '1001',
    name : 'unixx'
}

let token = tok.makeikv(data, ikv)

let dedata = tok.verifyikv(token, ikv)

console.log(token, dedata)

随机生成iv并生成token

const titok = require('titbit-token')

let tok = new titok()

let data = {
    id : '1001',
    name : 'unixx'
}

let t = tok.randIvToken(data)

//返回值t包括token、iv、id、key。
let dedata = tok.verifyikv(t.token, t)

console.log( t, dedata )

生成和验证带标签和IV的token

生成带标签和iv的access_token

//返回值: 'titbit-token [IV].[TOKEN]'
TitbitToken.prototype.makeAccessToken(info, id=null, key=null)

验证

//返回值和verify一致
TitbitToken.prototype.verifyAccessToken(info)