1 Star 0 Fork 0

fibjs / fib-session

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

fib-session

NPM version Build Status Build status

Session middleware for fibjs

Install

npm install fib-session [--save]

Test

npm run ci

Creating a cookie-based session middleware

var Session = require('fib-session')
var session = new Session(conn, opts);

var srv = new http.Server(8000, [
    session.cookie_filter, // use session ID via cookie
    {
        // routers
        '^/foo$': (r) => {
            var v = r.session.v;
        },
        ...
    }
]);

Creating a api session middleware

var Session = require('fib-session')
var session = new Session(conn, opts);

var srv = new http.Server(8000, [
    session.api_filter, // use api session filter
    {
        // routers
        '^/foo$': (r) => {
            var v = r.session.v;
        },
        '^/get-token$': session.api_token
    }
]);

Both kv options and sesion options are in the same object.

kv-store options

options default object/Map LruCache LevelDB Redis MongoDB SQLite/MySQL
table_name "kvs" x x x
key_name "k" x x x x
value_name "v" x x x x
key_size 32 x x x x x
value_size 256 x x x x x
cleanup_interval(ms) 60000 x x x x x
timeout(ms) 0 x x
prefix ""
cache false
cache_size 65536
cache_timeout(ms) 60000

session options

options default
session_cache_size 65536 max number of session in cache
session_cache_timeout(ms) 900000 clear session objects which is not operated for a period of time from buffer, default 15 minutes
session_cache_delay 100 time delay for write session to persistent storage
session_id_name "sessionID"
  • session cache is used to keep session consistency among http requires.

  • the timeout of session cache must be larger than its delay

  • The client-side has only the session ID. The session is operated on the server-side.

Methods

session.setup()

setup the backend database.

v = session.cookie_filter

returns a cookie-based session filter.

session.api_filter

returns a header-based session filter.

session.api_token

returns an api handler that gets a new session ID.

JSON Web Token(JWT)

options

options default
session_jwt_algo null see jws.ALGORITHMS in fib-jws
session_jwt_key null sign key. see in fib-jws
// session_jwt_algo is encryption algorithms in fib-jws
// session_jwt_key is to verify the signature. 
var session = new Session(conn, { session_jwt_algo: 'HS256',  session_jwt_key: verify_key })

Methods

session.setTokenCookie

  • set the JSON Web Token in cookie
  • sign_key for signature, may be different from session_jwt_key, depending on the algorithm.
  • according to JSON Web Token specification, one user session can only be called setTokenCookie once for set user session info. The second will throw an exception: new Error('Can't modify the JSON Web Token')
  • default value r.session={}
// sign user info: { id: 12345, name: "Frank" }, and set the cookie
// r is request
session.setTokenCookie(r, { id: 12345, name: "Frank" }, sign_key)

session.getToken

  • get token for api filter mode, see session.api_filter
session.getToken ({ id: 12345, name: "Frank" }, sign_key)
MIT License Copyright (c) 2017 响马 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Session middleware for fibjs 展开 收起
JavaScript 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/fibjs/fib-session.git
git@gitee.com:fibjs/fib-session.git
fibjs
fib-session
fib-session
master

搜索帮助