{"id":15046568,"url":"https://github.com/michielvdvelde/koa-easy-auth","last_synced_at":"2026-03-08T14:31:23.127Z","repository":{"id":85316969,"uuid":"244741939","full_name":"MichielvdVelde/koa-easy-auth","owner":"MichielvdVelde","description":"Easy and simple authentication for Koa","archived":false,"fork":false,"pushed_at":"2020-03-11T21:20:27.000Z","size":32,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-12T01:05:40.198Z","etag":null,"topics":["authentication","authorization","koa","koajs"],"latest_commit_sha":null,"homepage":"https://github.com/MichielvdVelde/koa-easy-auth","language":"TypeScript","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/MichielvdVelde.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-03T21:06:58.000Z","updated_at":"2020-03-11T21:19:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"56a6a07e-4c10-4862-aec0-f8ed6318152a","html_url":"https://github.com/MichielvdVelde/koa-easy-auth","commit_stats":{"total_commits":30,"total_committers":1,"mean_commits":30.0,"dds":0.0,"last_synced_commit":"b40796777af3ddd2e46b1377cf9ef32d34bf7efd"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MichielvdVelde%2Fkoa-easy-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MichielvdVelde%2Fkoa-easy-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MichielvdVelde%2Fkoa-easy-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MichielvdVelde%2Fkoa-easy-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MichielvdVelde","download_url":"https://codeload.github.com/MichielvdVelde/koa-easy-auth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241256092,"owners_count":19934981,"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","authorization","koa","koajs"],"created_at":"2024-09-24T20:53:14.799Z","updated_at":"2026-03-08T14:31:23.087Z","avatar_url":"https://github.com/MichielvdVelde.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# koa-easy-auth\n\nEasy and simple authentication for [Koa](https://github.com/koajs/koa).\n\n* Uses the `Authorization` header\n* Supports multiple authorization type strategies\n\nExtracted from a personal project in which I required simple, no-nonsense\nauthentication with support for multiple authorization types.\n\n[Koa](https://koajs.com/) is marked as a peer dependency; you'll need to install Koa yourself.\n\n## Install\n\nVersions `0.x.x` are considered unstable and may have breaking changes (i.e. does\nnot follow semver). When ready, version 1 will be the first stable release and\nwill follow semver from then on.\n\n```\nnpm i koa-easy-auth\n```\n\n## Example\n\nSee the source code for more specifics.\n\n```ts\nimport Koa from 'koa'\nimport Authentication from 'koa-simple-auth'\nimport createError from 'http-errors'\n\nimport basicAuth from 'basic-auth'\n\n// optional state interface/type\ninterface State {\n  user: string\n}\n\nconst app = new Koa()\nconst auth = new Authentication\u003cState\u003e()\n\n// Add a strategy for the `Basic` authorization type\n// NOTE: strategies are NOT middleware, a new strategy for the same type\n// will REPLACE the old strategy!\nauth.use('Basic', async ctx =\u003e {\n  const credentials = basicAuth(ctx.req)\n  \n  // usually we'd do some checking here to see\n  // if the supplied user name and password are correct\n  if (credentials.pass !== 'secret') {\n    throw createError(401, 'Invalid password')\n  }\n  \n  // now we can update the state\n  ctx.state.user = credentials.user\n  \n  // optional strategy parameters, are exposed in the WWW-Authenticate header\n}, { realm: 'my-realm', charset: 'UTF-8' })\n\n// Add a second strategy\nauth.use('Token', async ctx =\u003e {\n  const token = ctx.req.headers.authorization.split(' ')[1]\n  \n  if (token !== 'token') {\n    throw createError(401, 'Invalid token')\n  }\n  \n  // get user name somehow\n  ctx.state.user = 'me'\n})\n\n// add the middleware for the `Basic` strategy only\napp.use(auth.middleware('Basic'))\n\napp.use(async ctx =\u003e {\n  const { state } = ctx\n  const { user } = state\n  \n  ctx.body = `Hello, you are ${user}!`\n})\n\napp.listen(3000)\n```\n\n## License\n\nCopyright 2020 Michiel van der Velde.\n\nThis software is licensed under [the MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichielvdvelde%2Fkoa-easy-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichielvdvelde%2Fkoa-easy-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichielvdvelde%2Fkoa-easy-auth/lists"}