Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
use ES6
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jun 22, 2017
1 parent 182d1f4 commit fb83de6
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 83 deletions.
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
sudo: false
language: node_js
node_js:
- "0.12"
- "4.5.0"
- "4"
- "5"
- "6"
sudo: false
- "7"
- "8"
cache:
directories:
- node_modules
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015-2016 Toajs
Copyright (c) 2015-2017 Toajs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
toa-token
====
# toa-token

Json web token (JWT) module for toa.

It sign and verify token through a rotating credential system, in which new server keys can be added and old ones removed regularly, without invalidating client credentials.
Expand All @@ -19,18 +19,18 @@ often issued using OpenID Connect.
## Demo

```js
var Toa = require('toa')
var toaToken = require('toa-token')
var Router = require('toa-router')
var toaBody = require('toa-body')
const Toa = require('toa')
const toaToken = require('toa-token')
const Router = require('toa-router')
const toaBody = require('toa-body')

var router = new Router()
const router = new Router()

router
.get('/auth', function * () {
var user = yield this.parseBody()
let user = yield this.parseBody()
// verify with user.name and user.passwd, get user._id
var token = this.signToken({
let token = this.signToken({
name: user.name,
_id: user._id
})
Expand All @@ -42,7 +42,7 @@ router
// ....
})

var app = Toa(function * () {
const app = Toa(function * () {
yield router.route(this)
})

Expand All @@ -63,7 +63,7 @@ npm install toa-token
## API

```js
var toaToken = require('toa-token')
const toaToken = require('toa-token')
```

### toaToken(app, secretOrPrivateKeys, [options]))
Expand Down Expand Up @@ -120,14 +120,17 @@ It is a reference of `jsonwebtoken` module.
It is a wrap of `jsonwebtoken` module.

```js
var jwt = new toaToken.JWT(secretOrPrivateKeys)
const jwt = new toaToken.JWT(secretOrPrivateKeys)
```

#### jwt.signToken(payload, options)

#### jwt.decodeToken(token, options)

#### jwt.verifyToken(token, options)

## Licences

(The MIT License)

[npm-url]: https://npmjs.org/package/toa-token
Expand Down
19 changes: 9 additions & 10 deletions examples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
// **Github:** https://github.com/toajs/toa-token
//
// **License:** MIT
var Toa = require('toa')
var toaToken = require('toa-token')
var Router = require('toa-router')
var toaBody = require('toa-body')
const Toa = require('toa')
const toaToken = require('toa-token')
const Router = require('toa-router')
const toaBody = require('toa-body')

var router = new Router()
const router = new Router()

router
.get('/auth', function * () {
var user = yield this.parseBody()
let user = yield this.parseBody()
// verify with user.name and user.passwd, get user._id
var token = this.signToken({
let token = this.signToken({
name: user.name,
_id: user._id
})
Expand All @@ -25,9 +25,8 @@ router
// ....
})

var app = Toa(function * () {
yield router.route(this)
})
const app = new Toa()
app.use(router)

toaBody(app)
toaToken(app, 'secretKeyxxx', {
Expand Down
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
//
// **License:** MIT

var jsonwebtoken = require('jsonwebtoken')
var JWT_OPTIONS = ['algorithm', 'expiresIn', 'notBefore', 'audience', 'issuer',
const jsonwebtoken = require('jsonwebtoken')
const JWT_OPTIONS = ['algorithm', 'expiresIn', 'notBefore', 'audience', 'issuer',
'jwtid', 'subject', 'noTimestamp', 'header']

module.exports = toaToken
module.exports.jwt = jsonwebtoken
module.exports.JWT = JWT
toaToken.jwt = jsonwebtoken
toaToken.JWT = JWT

function toaToken (app, secretOrPrivateKeys, options) {
options = options || {}

var jwt = new JWT(secretOrPrivateKeys)
var useProperty = options.useProperty || 'token'
var authScheme = options.authScheme || 'Bearer'
var getToken = typeof options.getToken === 'function' ? options.getToken : null
var authReg = new RegExp('^' + authScheme.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
const jwt = new JWT(secretOrPrivateKeys)
const useProperty = options.useProperty || 'token'
const authScheme = options.authScheme || 'Bearer'
const getToken = typeof options.getToken === 'function' ? options.getToken : null
const authReg = new RegExp('^' + authScheme.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))

if (options.expiresInMinutes && !options.expiresIn) {
options.expiresIn = options.expiresInMinutes * 60
}
var jwtOptions = {}
const jwtOptions = {}
JWT_OPTIONS.forEach(function (key) {
if (options[key] != null) jwtOptions[key] = options[key]
})
Expand All @@ -47,8 +47,8 @@ function toaToken (app, secretOrPrivateKeys, options) {
get: function () {
if (this._toaJsonWebToken) return this._toaJsonWebToken

var token
var authorization = this.get('authorization')
let token
let authorization = this.get('authorization')

if (getToken) token = getToken.call(this)
if (!token && authorization) {
Expand Down Expand Up @@ -83,10 +83,10 @@ JWT.prototype.decodeToken = function (token, options) {
}

JWT.prototype.verifyToken = function (token, options) {
var error = null
var secretOrPrivateKeys = this.secretOrPrivateKeys
let error = null
let secretOrPrivateKeys = this.secretOrPrivateKeys

for (var i = 0, len = secretOrPrivateKeys.length - 1; i <= len; i++) {
for (let i = 0, len = secretOrPrivateKeys.length - 1; i <= len; i++) {
try {
return jsonwebtoken.verify(token, secretOrPrivateKeys[i], options)
} catch (err) {
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"authors": [
"Yan Qing <[email protected]>"
],
"version": "2.1.1",
"version": "2.2.0",
"main": "index.js",
"repository": {
"type": "git",
"url": "[email protected]:toajs/toa-token.git"
},
"engines": {
"node": ">=0.10.0"
"node": ">= 4.5.0"
},
"homepage": "https://github.com/toajs/toa-token",
"keywords": [
Expand All @@ -29,13 +29,13 @@
"oauth"
],
"dependencies": {
"jsonwebtoken": "^7.1.9"
"jsonwebtoken": "^7.4.1"
},
"devDependencies": {
"standard": "^8.2.0",
"supertest": "^2.0.0",
"tman": "^1.6.0",
"toa": "^1.8.13"
"standard": "^10.0.2",
"supertest": "^3.0.0",
"tman": "^1.6.9",
"toa": "^2.6.8"
},
"scripts": {
"test": "standard && tman"
Expand Down
Loading

0 comments on commit fb83de6

Please sign in to comment.