{"id":20809577,"url":"https://github.com/codetrial/egg-token","last_synced_at":"2025-07-03T09:32:51.639Z","repository":{"id":57220813,"uuid":"162150018","full_name":"codetrial/egg-token","owner":"codetrial","description":"Egg.js middleware that uses encrypted token to authenticate.","archived":false,"fork":false,"pushed_at":"2018-12-23T16:53:10.000Z","size":69,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-05-07T08:12:01.659Z","etag":null,"topics":["authentication","egg","egg-plugin","koa","middleware","token-based-authentication"],"latest_commit_sha":null,"homepage":"https://codetrial.github.io/egg-token","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codetrial.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-17T15:19:21.000Z","updated_at":"2024-07-02T04:28:26.000Z","dependencies_parsed_at":"2022-08-28T23:23:54.599Z","dependency_job_id":null,"html_url":"https://github.com/codetrial/egg-token","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/codetrial/egg-token","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codetrial%2Fegg-token","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codetrial%2Fegg-token/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codetrial%2Fegg-token/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codetrial%2Fegg-token/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codetrial","download_url":"https://codeload.github.com/codetrial/egg-token/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codetrial%2Fegg-token/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263300653,"owners_count":23445273,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authentication","egg","egg-plugin","koa","middleware","token-based-authentication"],"created_at":"2024-11-17T20:14:34.587Z","updated_at":"2025-07-03T09:32:51.593Z","avatar_url":"https://github.com/codetrial.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# egg-token\n\nEgg.js middleware that uses encrypted token to authenticate.\n\n[![License](https://img.shields.io/github/license/codetrial/egg-token.svg)](https://github.com/codetrial/egg-token)\n[![Version](https://img.shields.io/npm/v/egg-token.svg)](https://www.npmjs.com/package/egg-token)\n[![codecov](https://codecov.io/gh/codetrial/egg-token/branch/develop/graph/badge.svg)](https://codecov.io/gh/codetrial/egg-token)\n[![TravisCI](https://travis-ci.org/codetrial/egg-token.svg?branch=develop)](https://travis-ci.org/codetrial/egg-token)\n\n## Install\n\n```bash\nnpm i egg-token\n# or\nyarn add egg-token\n```\n\n## Usage\n\n```js\n// {app_root}/config/plugin.js\nexports.token = {\n  enable: true,\n  package: 'egg-token'\n};\n```\n\n## Configuration\n\n```js\n// {app_root}/config/config.default.js\nexports.middleware = ['token'];\n\nexports.token = {\n  type: 'md5',\n\n  apps: {\n    felixpy: {\n      secret: 'XnMib79vzwP01gtr',\n      expires: 30000\n    },\n    codetrial: {\n      secret: 'mi9yNGT6zwrqMv8z',\n      expires: 30000\n    }\n  }\n};\n```\n\n`type` is the algorithm that can be used to generate hash digests.\n\nSee [crypto.createHash](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options) for more detail.\n\nEach key of `apps` is the application's code, `secret` is used to generate token and `expires` is the validity period of the token.\n\nThe way to generate tokens is as follows:\n\n```js\nconst ts = Date.now();\nconst md5Value = md5(`${APP_CODE}:${ts}:${APP_SECRET}`);\nconst token = base64Encode(`${APP_CODE}:${ts}:${md5Value}`);\n```\n\n## Example\n\nThis is an example of using axios to request an api:\n\n```js\nconst crypto = require('crypto');\nconst axios = require('axios');\n\nconst hash = crypto.createHash('md5');\n\nconst APP_CODE = 'felixpy';\nconst APP_SECRET = 'XnMib79vzwP01gtr';\n\nconst ts = Date.now();\nconst md5Value = hash.update(`${APP_CODE}:${ts}:${APP_SECRET}`).digest('hex');\n\nconst token = Buffer.from(`${APP_CODE}:${ts}:${md5Value}`).toString('base64');\n\naxios.get('/url/to/your/egg/service', {\n  headers: {\n    'egg-api-token': token\n  }\n});\n```\n\n## License\n\n[MIT](http://opensource.org/licenses/MIT)\n\nCopyright (c) 2018 - present, Felix Yang\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodetrial%2Fegg-token","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodetrial%2Fegg-token","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodetrial%2Fegg-token/lists"}