├── .eslintignore ├── .eslintrc ├── test ├── fixtures │ └── apps │ │ └── captcha-test │ │ ├── config │ │ ├── config.default.js │ │ └── plugin.js │ │ ├── package.json │ │ └── app │ │ ├── router.js │ │ ├── controller │ │ ├── home.js │ │ └── captcha.js │ │ └── service │ │ └── captcha.js └── captcha.test.js ├── .gitignore ├── .travis.yml ├── app.js ├── appveyor.yml ├── app └── extend │ └── captcha.js ├── .autod.conf.js ├── config └── config.default.js ├── .github ├── workflows │ └── nodejs.yml └── PULL_REQUEST_TEMPLATE.md ├── LICENSE ├── package.json ├── README.zh_CN.md └── README.md /.eslintignore: -------------------------------------------------------------------------------- 1 | coverage 2 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint-config-egg" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/apps/captcha-test/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | -------------------------------------------------------------------------------- /test/fixtures/apps/captcha-test/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "captcha-test", 3 | "version": "0.0.1" 4 | } 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | logs/ 2 | npm-debug.log 3 | node_modules/ 4 | coverage/ 5 | .idea/ 6 | run/ 7 | .DS_Store 8 | *.swp 9 | 10 | -------------------------------------------------------------------------------- /test/fixtures/apps/captcha-test/app/router.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | const { router, controller } = app; 5 | 6 | router.get('/', controller.home.index); 7 | router.get('/captcha', controller.captcha.index); 8 | }; 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '8' 5 | - '9' 6 | before_install: 7 | - npm i npminstall -g 8 | install: 9 | - npminstall 10 | script: 11 | - npm run ci 12 | after_script: 13 | - npminstall codecov && codecov 14 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const Captcha = require('./app/extend/captcha'); 3 | module.exports = app => { 4 | const captcha = new Captcha({}); 5 | app.captcha = { 6 | generate() { 7 | return captcha.generate(); 8 | }, 9 | }; 10 | }; 11 | -------------------------------------------------------------------------------- /test/fixtures/apps/captcha-test/app/controller/home.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Controller = require('egg').Controller; 4 | 5 | class HomeController extends Controller { 6 | async index() { 7 | this.ctx.body = 'hi, ' + this.app.plugins.captcha.name; 8 | } 9 | } 10 | 11 | module.exports = HomeController; 12 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - nodejs_version: '8' 4 | - nodejs_version: '9' 5 | 6 | install: 7 | - ps: Install-Product node $env:nodejs_version 8 | - npm i npminstall && node_modules\.bin\npminstall 9 | 10 | test_script: 11 | - node --version 12 | - npm --version 13 | - npm run test 14 | 15 | build: off 16 | -------------------------------------------------------------------------------- /test/fixtures/apps/captcha-test/app/service/captcha.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const Service = require('egg').Service; 3 | 4 | class CaptchaService extends Service { 5 | async captcha() { 6 | const { app } = this; 7 | const ret = app.captcha.generate(); 8 | return { captcha: ret && ret[1], txt: ret && ret[0] }; 9 | } 10 | } 11 | 12 | module.exports = CaptchaService; 13 | -------------------------------------------------------------------------------- /app/extend/captcha.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const { captcha } = require('../../config/config.default'); 3 | const ccap = require('ccap'); 4 | 5 | class Captcha { 6 | constructor(option) { 7 | this.option = Object.assign(captcha, option); 8 | this.ccap = ccap(this.option); 9 | } 10 | 11 | generate() { 12 | return this.ccap.get(); 13 | } 14 | } 15 | 16 | module.exports = Captcha; 17 | -------------------------------------------------------------------------------- /.autod.conf.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | write: true, 5 | prefix: '^', 6 | plugin: 'autod-egg', 7 | test: [ 8 | 'test', 9 | 'benchmark', 10 | ], 11 | devdep: [ 12 | 'egg', 13 | 'egg-ci', 14 | 'egg-bin', 15 | 'autod', 16 | 'autod-egg', 17 | 'eslint', 18 | 'eslint-config-egg', 19 | ], 20 | exclude: [ 21 | './test/fixtures', 22 | './docs', 23 | './coverage', 24 | ], 25 | }; 26 | -------------------------------------------------------------------------------- /test/fixtures/apps/captcha-test/config/plugin.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const path = require('path'); 3 | /** @type Egg.EggPlugin */ 4 | exports.captcha = { 5 | enable: true, 6 | // enable bellow row when your clone egg-captcha and run the example server. 7 | // !!! don't use it on prod env. !!! 8 | path: path.join(__dirname, '../../../../../'), 9 | 10 | // enable bellow row on prod env. must install this pkg via `npm i egg-captcha` 11 | // package: 'egg-captcha', 12 | }; 13 | -------------------------------------------------------------------------------- /config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * egg-captcha default config 5 | * @member Config#captcha 6 | * @property {String} SOME_KEY - some description 7 | */ 8 | exports.captcha = { 9 | width: 256, // set width,default is 256 10 | 11 | height: 60, // set height,default is 60 12 | 13 | offset: 40, // set text spacing,default is 40 14 | 15 | quality: 100, // set pic quality,default is 50 16 | 17 | fontsize: 57, // set font size,default is 57 18 | }; 19 | -------------------------------------------------------------------------------- /test/fixtures/apps/captcha-test/app/controller/captcha.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Controller = require('egg').Controller; 4 | 5 | class CaptchaController extends Controller { 6 | async index() { 7 | const { ctx } = this; 8 | const { captcha, txt } = await ctx.service.captcha.captcha(); 9 | ctx.body = captcha; 10 | ctx.type = 'image/png'; 11 | // need open the egg-session plugin 12 | ctx.session.captcha = txt; 13 | } 14 | } 15 | 16 | module.exports = CaptchaController; 17 | -------------------------------------------------------------------------------- /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | name: Node CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | strategy: 11 | matrix: 12 | node-version: [8.x, 10.x] 13 | 14 | steps: 15 | - uses: actions/checkout@v1 16 | - name: Use Node.js ${{ matrix.node-version }} 17 | uses: actions/setup-node@v1 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - name: npm install, build, and test 21 | run: | 22 | npm install 23 | npm run ci 24 | env: 25 | CI: true 26 | -------------------------------------------------------------------------------- /test/captcha.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const mock = require('egg-mock'); 4 | 5 | describe('test/captcha.test.js', () => { 6 | let app; 7 | before(() => { 8 | app = mock.app({ 9 | baseDir: 'apps/captcha-test', 10 | }); 11 | return app.ready(); 12 | }); 13 | 14 | after(() => app.close()); 15 | afterEach(mock.restore); 16 | 17 | it('should GET /', () => { 18 | return app.httpRequest() 19 | .get('/') 20 | .expect('hi, captcha') 21 | .expect(200); 22 | }); 23 | it('should GET /captcha', () => { 24 | return app.httpRequest() 25 | .get('/captcha') 26 | .expectHeader('content-type') 27 | .expect(200); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 10 | 11 | ##### Checklist 12 | 13 | 14 | - [ ] `npm test` passes 15 | - [ ] tests and/or benchmarks are included 16 | - [ ] documentation is changed or added 17 | - [ ] commit message follows commit guidelines 18 | 19 | ##### Affected core subsystem(s) 20 | 21 | 22 | 23 | ##### Description of change 24 | 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Alibaba Group Holding Limited and other contributors. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "egg-captcha", 3 | "version": "1.2.2", 4 | "description": "captcha plugin for egg, based on c++ lib ccap", 5 | "eggPlugin": { 6 | "name": "captcha" 7 | }, 8 | "keywords": [ 9 | "egg", 10 | "eggPlugin", 11 | "egg-plugin", 12 | "ccap", 13 | "captcha" 14 | ], 15 | "dependencies": { 16 | "ccap": "^0.6.10" 17 | }, 18 | "devDependencies": { 19 | "@types/egg-mock": "^3.0.4", 20 | "autod": "^3.1.0", 21 | "autod-egg": "^1.1.0", 22 | "egg": "^2.23.0", 23 | "egg-bin": "^4.13.2", 24 | "egg-ci": "^1.13.0", 25 | "egg-mock": "^3.24.1", 26 | "eslint": "^6.5.0", 27 | "eslint-config-egg": "^7.5.1", 28 | "webstorm-disable-index": "^1.2.0" 29 | }, 30 | "engines": { 31 | "node": ">=8.0.0" 32 | }, 33 | "scripts": { 34 | "test": "npm run lint -- --fix && egg-bin pkgfiles && npm run test-local", 35 | "test-local": "egg-bin test", 36 | "cov": "egg-bin cov", 37 | "lint": "eslint .", 38 | "lint:fix": "eslint . --fix", 39 | "ci": "egg-bin pkgfiles --check && npm run lint && npm run cov", 40 | "pkgfiles": "egg-bin pkgfiles", 41 | "autod": "autod" 42 | }, 43 | "files": [ 44 | "app", 45 | "config", 46 | "app.js" 47 | ], 48 | "ci": { 49 | "version": "8, 9" 50 | }, 51 | "repository": { 52 | "type": "git", 53 | "url": "git+https://github.com/Raoul1996/egg-captcha.git" 54 | }, 55 | "bugs": { 56 | "url": "https://github.com/Raoul1996/egg-captcha/issues" 57 | }, 58 | "homepage": "https://github.com/Raoul1996/egg-captcha#readme", 59 | "author": "Raoul1996 ", 60 | "license": "MIT" 61 | } 62 | -------------------------------------------------------------------------------- /README.zh_CN.md: -------------------------------------------------------------------------------- 1 | # egg-captcha 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | [![build status][travis-image]][travis-url] 5 | [![Test coverage][codecov-image]][codecov-url] 6 | [![David deps][david-image]][david-url] 7 | [![Known Vulnerabilities][snyk-image]][snyk-url] 8 | [![npm download][download-image]][download-url] 9 | 10 | [npm-image]: https://img.shields.io/npm/v/egg-captcha.svg?style=flat-square 11 | [npm-url]: https://npmjs.org/package/egg-captcha 12 | [travis-image]: https://img.shields.io/travis/eggjs/egg-captcha.svg?style=flat-square 13 | [travis-url]: https://travis-ci.org/eggjs/egg-captcha 14 | [codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-captcha.svg?style=flat-square 15 | [codecov-url]: https://codecov.io/github/eggjs/egg-captcha?branch=master 16 | [david-image]: https://img.shields.io/david/eggjs/egg-captcha.svg?style=flat-square 17 | [david-url]: https://david-dm.org/eggjs/egg-captcha 18 | [snyk-image]: https://snyk.io/test/npm/egg-captcha/badge.svg?style=flat-square 19 | [snyk-url]: https://snyk.io/test/npm/egg-captcha 20 | [download-image]: https://img.shields.io/npm/dm/egg-captcha.svg?style=flat-square 21 | [download-url]: https://npmjs.org/package/egg-captcha 22 | 23 | 26 | 27 | ## 依赖说明 28 | 29 | ### 依赖的 egg 版本 30 | 31 | egg-captcha 版本 | egg 1.x 32 | --- | --- 33 | 1.x | 😁 34 | 0.x | ❌ 35 | 36 | ### 依赖的插件 37 | 45 | 46 | ## 开启插件 47 | 48 | ```js 49 | // config/plugin.js 50 | exports.captcha = { 51 | enable: true, 52 | package: 'egg-captcha', 53 | }; 54 | ``` 55 | 56 | ## 使用场景 57 | 58 | - Why and What: 描述为什么会有这个插件,它主要在完成一件什么事情。 59 | 尽可能描述详细。 60 | - How: 描述这个插件是怎样使用的,具体的示例代码,甚至提供一个完整的示例,并给出链接。 61 | 62 | ## 详细配置 63 | 64 | 请到 [config/config.default.js](config/config.default.js) 查看详细配置项说明。 65 | 66 | ## 单元测试 67 | 68 | 69 | 70 | ## 提问交流 71 | 72 | 请到 [egg issues](https://github.com/eggjs/egg/issues) 异步交流。 73 | 74 | ## License 75 | 76 | [MIT](LICENSE) 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # egg-captcha 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | [![build status][travis-image]][travis-url] 5 | [![Test coverage][codecov-image]][codecov-url] 6 | [![David deps][david-image]][david-url] 7 | [![Known Vulnerabilities][snyk-image]][snyk-url] 8 | [![npm download][download-image]][download-url] 9 | 10 | [npm-image]: https://img.shields.io/npm/v/egg-captcha.svg?style=flat-square 11 | [npm-url]: https://npmjs.org/package/egg-captcha 12 | [travis-image]: https://img.shields.io/travis/raoul1996/egg-captcha.svg?style=flat-square 13 | [travis-url]: https://travis-ci.org/raoul1996/egg-captcha 14 | [codecov-image]: https://img.shields.io/codecov/c/github/raoul1996/egg-captcha.svg?style=flat-square 15 | [codecov-url]: https://codecov.io/github/raoul1996/egg-captcha?branch=master 16 | [david-image]: https://img.shields.io/david/raoul1996/egg-captcha.svg?style=flat-square 17 | [david-url]: https://david-dm.org/raoul1996/egg-captcha 18 | [snyk-image]: https://snyk.io/test/npm/egg-captcha/badge.svg?style=flat-square 19 | [snyk-url]: https://snyk.io/test/npm/egg-captcha 20 | [download-image]: https://img.shields.io/npm/dm/egg-captcha.svg?style=flat-square 21 | [download-url]: https://npmjs.org/package/egg-captcha 22 | 23 | > captcha plugin for egg framework, based on [ccap](https://www.npmjs.com/package/ccap) 24 | 27 | 28 | ## Install 29 | 30 | ```bash 31 | $ npm i egg-captcha --save 32 | ``` 33 | 34 | ## Usage 35 | 36 | ```js 37 | // {app_root}/config/plugin.js 38 | 39 | exports.captcha = { 40 | enable: true, 41 | package: 'egg-captcha', 42 | }; 43 | ``` 44 | ## Configuration 45 | 46 | ```js 47 | // {app_root}/config/config.default.js 48 | 49 | exports.captcha = { 50 | width: 256, // set width,default is 256 51 | height: 60, // set height,default is 60 52 | offset: 40, // set text spacing,default is 40 53 | quality: 100, // set pic quality,default is 50 54 | fontsize: 57 // set font size,default is 57 55 | } 56 | ``` 57 | see [config/config.default.js](config/config.default.js) for more detail. 58 | 59 | ## Example 60 | 61 | ```js 62 | // {app_root}/controller/user.js 63 | 64 | async captcha() { 65 | const {ctx, service} = this 66 | const {captcha, txt} = await service.user.captcha() 67 | ctx.body = captcha 68 | ctx.type = 'image/png' 69 | // need open the egg-session plugin 70 | ctx.session.captcha = txt 71 | } 72 | ``` 73 | ```js 74 | // {app_root}/service/user.js 75 | 76 | async captcha() { 77 | const {app} = this 78 | const ary = app.captcha.generate() 79 | return {captcha: ary[1], txt: ary[0]} 80 | } 81 | ``` 82 | 83 | ## Questions & Suggestions 84 | 85 | Please open an issue [here](https://github.com/eggjs/egg/issues). 86 | 87 | ## License 88 | 89 | [MIT](LICENSE) 90 | --------------------------------------------------------------------------------