Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4bcfd87
commit fea009a
Showing
18 changed files
with
605 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Alibaba Group Holding Limited and other contributors. | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# egg-weapp-sdk | ||
|
||
[![NPM version][npm-image]][npm-url] | ||
[![build status][travis-image]][travis-url] | ||
[![Test coverage][codecov-image]][codecov-url] | ||
[![David deps][david-image]][david-url] | ||
[![Known Vulnerabilities][snyk-image]][snyk-url] | ||
[![npm download][download-image]][download-url] | ||
|
||
[npm-image]: https://img.shields.io/npm/v/egg-weapp-sdk.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/egg-weapp-sdk | ||
[travis-image]: https://img.shields.io/travis/eggjs/egg-weapp-sdk.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/eggjs/egg-weapp-sdk | ||
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-weapp-sdk.svg?style=flat-square | ||
[codecov-url]: https://codecov.io/github/eggjs/egg-weapp-sdk?branch=master | ||
[david-image]: https://img.shields.io/david/eggjs/egg-weapp-sdk.svg?style=flat-square | ||
[david-url]: https://david-dm.org/eggjs/egg-weapp-sdk | ||
[snyk-image]: https://snyk.io/test/npm/egg-weapp-sdk/badge.svg?style=flat-square | ||
[snyk-url]: https://snyk.io/test/npm/egg-weapp-sdk | ||
[download-image]: https://img.shields.io/npm/dm/egg-weapp-sdk.svg?style=flat-square | ||
[download-url]: https://npmjs.org/package/egg-weapp-sdk | ||
|
||
<!-- | ||
Description here. | ||
--> | ||
|
||
## 依赖说明 | ||
|
||
### 依赖的 egg 版本 | ||
|
||
egg-weapp-sdk 版本 | egg 1.x | ||
--- | --- | ||
1.x | 😁 | ||
0.x | ❌ | ||
|
||
### 依赖的插件 | ||
<!-- | ||
如果有依赖其它插件,请在这里特别说明。如 | ||
- security | ||
- multipart | ||
--> | ||
|
||
## 开启插件 | ||
|
||
```js | ||
// config/plugin.js | ||
exports.weappSDK = { | ||
enable: true, | ||
package: 'egg-weapp-sdk', | ||
}; | ||
``` | ||
|
||
|
||
## 使用场景 | ||
|
||
- Why and What: 独立管理微信小程序用户会话,校验身份。 | ||
|
||
- How: 具体的示例代码: | ||
|
||
```js | ||
// app/controller/weapp.js | ||
module.exports = app => { | ||
class WeappController extends app.Controller { | ||
* login() { | ||
const { ctx, app } = this; | ||
const loginService = app.weapp.LoginService.create(ctx.request, ctx.response); | ||
yield loginService.login() | ||
.then(data => { | ||
ctx.body = data; | ||
}); | ||
} | ||
|
||
* user() { | ||
const { ctx, app } = this; | ||
const loginService = app.weapp.LoginService.create(ctx.request, ctx.response); | ||
yield loginService.check() | ||
.then(data => { | ||
ctx.body = { | ||
code: 0, | ||
message: 'ok', | ||
data: { | ||
userInfo: data.userInfo, | ||
}, | ||
}; | ||
}); | ||
} | ||
} | ||
return WeappController; | ||
}; | ||
``` | ||
|
||
## 详细配置 | ||
|
||
请到 [config/config.default.js](config/config.default.js) 查看详细配置项说明。 | ||
```js | ||
// {app_root}/config/config.default.js | ||
exports.weappSDK = { | ||
appId: '', | ||
appSecret: '' | ||
}; | ||
``` | ||
|
||
## 单元测试 | ||
|
||
<!-- 描述如何在单元测试中使用此插件,例如 schedule 如何触发。无则省略。--> | ||
|
||
## 提问交流 | ||
|
||
请到 [egg issues](https://github.com/eggjs/egg/issues) 异步交流。 | ||
|
||
## License | ||
|
||
[MIT](LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = agent => { | ||
console.log('agent.config.env =', agent.config.env); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
const weapp = require('./lib/index'); | ||
|
||
module.exports = app => { | ||
const config = app.config.weappSDK; | ||
|
||
weapp.config({ | ||
AppId: config.appId, | ||
AppSecret: config.appSecret, | ||
Redis: app.sessionStore, | ||
}); | ||
|
||
app.weapp = weapp; | ||
|
||
app.coreLogger.info('[当前 SDK 使用配置] =>', config); | ||
app.coreLogger.info('read data ok'); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
environment: | ||
matrix: | ||
- nodejs_version: '6' | ||
- nodejs_version: '7' | ||
|
||
install: | ||
- ps: Install-Product node $env:nodejs_version | ||
- npm i npminstall && node_modules\.bin\npminstall | ||
|
||
test_script: | ||
- node --version | ||
- npm --version | ||
- npm run ci | ||
|
||
build: off |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
/** | ||
* weapp-sdk default config | ||
* @member Config#weapp-sdk | ||
* @property {String} SOME_KEY - some description | ||
*/ | ||
exports.weappSDK = { | ||
appId: '', | ||
appSecret: '', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
class AuthAPIError extends Error { | ||
constructor(code, message) { | ||
super(message); | ||
|
||
this.code = code; | ||
this.message = message; | ||
} | ||
} | ||
|
||
module.exports = AuthAPIError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
const co = require('co'); | ||
const config = require('../config'); | ||
const constants = require('./constants'); | ||
const AuthAPIError = require('./auth-api-error'); | ||
const jscode2session = require('../helper/jscode2session'); | ||
const uuid = require('../helper/uuid'); | ||
|
||
const ONE_MONTH = 1000 * 60 * 60 * 24 * 30; | ||
|
||
module.exports = { | ||
login: co.wrap(function* (code, encrypt_data, iv) { | ||
const session = yield jscode2session.getSessionKey(code); | ||
const data = yield jscode2session.decrypt(session.sessionKey, encrypt_data, iv); | ||
const redis = config.getRedis(); | ||
const token = uuid(); | ||
yield redis.set(token, data, ONE_MONTH); | ||
return { | ||
id: token, | ||
skey: 'bravo', | ||
user_info: data, | ||
}; | ||
}), | ||
|
||
checkLogin: co.wrap(function* (id, skey) { | ||
if (skey !== 'bravo') { | ||
const error = new AuthAPIError(constants.RETURN_CODE_WX_SESSION_FAILED, constants.ERR_LOGIN_FAILED); | ||
throw error; | ||
|
||
} | ||
const redis = config.getRedis(); | ||
const user_info = yield redis.get(id); | ||
return { user_info }; | ||
}), | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
WX_HEADER_CODE: 'X-WX-Code', | ||
WX_HEADER_ENCRYPTED_DATA: 'X-WX-Encrypted-Data', | ||
WX_HEADER_IV: 'X-WX-IV', | ||
WX_HEADER_ID: 'X-WX-Id', | ||
WX_HEADER_SKEY: 'X-WX-Skey', | ||
|
||
WX_SESSION_MAGIC_ID: 'F2C224D4-2BCE-4C64-AF9F-A6D872000D1A', | ||
|
||
ERR_LOGIN_FAILED: 'ERR_LOGIN_FAILED', | ||
ERR_INVALID_SESSION: 'ERR_INVALID_SESSION', | ||
ERR_CHECK_LOGIN_FAILED: 'ERR_CHECK_LOGIN_FAILED', | ||
|
||
RETURN_CODE_SUCCESS: 0, | ||
RETURN_CODE_SKEY_EXPIRED: 60011, | ||
RETURN_CODE_WX_SESSION_FAILED: 60012, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
class LoginServiceError extends Error { | ||
constructor(type, message) { | ||
super(message); | ||
|
||
this.type = type; | ||
this.message = message; | ||
} | ||
} | ||
|
||
module.exports = LoginServiceError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
'use strict'; | ||
|
||
const co = require('co'); | ||
const ServiceBase = require('../service-base'); | ||
const constants = require('./constants'); | ||
const authApi = require('./auth-api'); | ||
const AuthAPIError = require('./auth-api-error'); | ||
const LoginServiceError = require('./login-service-error'); | ||
|
||
class LoginService extends ServiceBase { | ||
login(callback) { | ||
const promise = co.wrap(function* () { | ||
try { | ||
const code = this._getHeader(constants.WX_HEADER_CODE); | ||
const encryptedData = this._getHeader(constants.WX_HEADER_ENCRYPTED_DATA); | ||
const iv = this._getHeader(constants.WX_HEADER_IV); | ||
|
||
const result = yield authApi.login(code, encryptedData, iv); | ||
|
||
return { | ||
[constants.WX_SESSION_MAGIC_ID]: 1, | ||
session: { | ||
id: result.id, | ||
skey: result.skey, | ||
}, | ||
userInfo: result.user_info, | ||
}; | ||
} catch (err) { | ||
const error = new LoginServiceError(constants.ERR_LOGIN_FAILED, err.message); | ||
this._writeError(error); | ||
throw error; | ||
} | ||
}).call(this); | ||
|
||
return this._promiseOrCallback(promise, callback); | ||
} | ||
|
||
check(callback) { | ||
const promise = co.wrap(function* () { | ||
try { | ||
const id = this._getHeader(constants.WX_HEADER_ID); | ||
const skey = this._getHeader(constants.WX_HEADER_SKEY); | ||
|
||
const result = yield authApi.checkLogin(id, skey); | ||
|
||
return { userInfo: result.user_info }; | ||
} catch (err) { | ||
let error; | ||
|
||
if (err instanceof AuthAPIError) { | ||
switch (err.code) { | ||
case constants.RETURN_CODE_SKEY_EXPIRED: | ||
case constants.RETURN_CODE_WX_SESSION_FAILED: | ||
error = new LoginServiceError(constants.ERR_INVALID_SESSION, err.message); | ||
break; | ||
|
||
default: | ||
error = new LoginServiceError(constants.ERR_CHECK_LOGIN_FAILED, err.message); | ||
break; | ||
} | ||
} else { | ||
error = new LoginServiceError(constants.ERR_CHECK_LOGIN_FAILED, err.message); | ||
} | ||
|
||
this._writeError(error); | ||
throw error; | ||
} | ||
}).call(this); | ||
|
||
return this._promiseOrCallback(promise, callback); | ||
} | ||
|
||
_writeError(err) { | ||
if (this.res.headersSent) { | ||
return; | ||
} | ||
|
||
this.writeJsonResult({ | ||
[constants.WX_SESSION_MAGIC_ID]: 1, | ||
error: err.type, | ||
message: err.message, | ||
}); | ||
} | ||
|
||
_promiseOrCallback(promise, callback) { | ||
if (typeof callback !== 'function') { | ||
return promise; | ||
} | ||
|
||
promise.then( | ||
result => setTimeout(() => callback(null, result), 0), | ||
error => setTimeout(() => callback(error), 0) | ||
); | ||
} | ||
|
||
_getHeader(headerKey) { | ||
const headerValue = super.getHeader(headerKey); | ||
|
||
if (!headerValue) { | ||
throw new Error(`请求头未包含 ${headerKey},请配合客户端 SDK 登录后再进行请求`); | ||
} | ||
|
||
return headerValue; | ||
} | ||
} | ||
|
||
module.exports = LoginService; |
Oops, something went wrong.