0.6.0 • Published 24 days ago

fib-session v0.6.0

Weekly downloads
3
License
MIT
Repository
github
Last release
24 days ago

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

optionsdefaultobject/MapLruCacheLevelDBRedisMongoDBSQLite/MySQL
table_name"kvs"xxx
key_name"k"xxxx
value_name"v"xxxx
key_size32xxxxx
value_size256xxxxx
cleanup_interval(ms)60000xxxxx
timeout(ms)0xx
prefix""
cachefalse
cache_size65536
cache_timeout(ms)60000

session options

optionsdefault
session_cache_size65536max number of session in cache
session_cache_timeout(ms)900000clear session objects which is not operated for a period of time from buffer, default 15 minutes
session_cache_delay100time 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

optionsdefault
session_jwt_algonullsee jws.ALGORITHMS in fib-jws
session_jwt_keynullsign 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)
0.6.0

24 days ago

0.5.3

1 year ago

0.5.0

1 year ago

0.5.2

1 year ago

0.5.1

1 year ago

0.4.2

4 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.2

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

7 years ago

0.1.0

7 years ago