{"id":13718696,"url":"https://github.com/halvardssm/oak-middleware-jwt","last_synced_at":"2025-06-24T04:32:47.713Z","repository":{"id":43206773,"uuid":"266789111","full_name":"halvardssm/oak-middleware-jwt","owner":"halvardssm","description":"Oak middleware for JWT","archived":false,"fork":false,"pushed_at":"2023-09-25T08:28:40.000Z","size":64,"stargazers_count":35,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-06T05:39:09.746Z","etag":null,"topics":["deno","middleware","oak","oakland"],"latest_commit_sha":null,"homepage":"","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/halvardssm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","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},"funding":{"ko_fi":"halvardm","github":"halvardssm"}},"created_at":"2020-05-25T13:40:33.000Z","updated_at":"2024-07-29T13:05:29.000Z","dependencies_parsed_at":"2024-01-05T23:57:47.325Z","dependency_job_id":"854deed7-6b7d-456b-8223-076e9b0b1ee7","html_url":"https://github.com/halvardssm/oak-middleware-jwt","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/halvardssm/oak-middleware-jwt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halvardssm%2Foak-middleware-jwt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halvardssm%2Foak-middleware-jwt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halvardssm%2Foak-middleware-jwt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halvardssm%2Foak-middleware-jwt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/halvardssm","download_url":"https://codeload.github.com/halvardssm/oak-middleware-jwt/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halvardssm%2Foak-middleware-jwt/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261604234,"owners_count":23183611,"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":["deno","middleware","oak","oakland"],"created_at":"2024-08-03T01:00:36.074Z","updated_at":"2025-06-24T04:32:47.692Z","avatar_url":"https://github.com/halvardssm.png","language":"TypeScript","funding_links":["https://ko-fi.com/halvardm","https://github.com/sponsors/halvardssm"],"categories":[],"sub_categories":[],"readme":"# Oak Middleware JWT\n\n[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/halvardssm/oak-middleware-jwt?logo=deno\u0026style=flat-square)](https://github.com/halvardssm/oak-middleware-jwt)\n[![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/halvardssm/oak-middleware-jwt/CI/master?style=flat-square\u0026logo=github)](https://github.com/halvardssm/oak-middleware-jwt/actions?query=branch%3Amaster+workflow%3ACI)\n[![(Deno)](https://img.shields.io/badge/deno-v1.18.2-green.svg?style=flat-square\u0026logo=deno)](https://deno.land)\n[![(Deno)](https://img.shields.io/badge/oak-v10.2.0-orange.svg?style=flat-square\u0026logo=deno)](https://github.com/oakserver/oak)\n[![(Deno)](https://img.shields.io/badge/djwt-v2.4-orange.svg?style=flat-square\u0026logo=deno)](https://github.com/timonson/djwt)\n[![deno doc](https://img.shields.io/badge/deno-doc-blue.svg?style=flat-square\u0026logo=deno)](https://doc.deno.land/https/raw.githubusercontent.com/halvardssm/oak-middleware-jwt/master/mod.ts)\n[![nest badge](https://nest.land/badge-block.svg)](https://nest.land/package/oak-middleware-jwt)\n\nOak middleware for JWT using Djwt\n\n## Usage\n\n- As an application middleware\n\n  ```ts\n  import { jwtMiddleware } from \"https://raw.githubusercontent.com/halvardssm/oak-middleware-jwt/master/mod.ts\";\n  import { Middleware } from \"https://deno.land/x/oak/mod.ts\";\n\n  const app = new Application();\n\n  app.use(jwtMiddleware\u003cMiddleware\u003e({ key: \"foo\" }));\n\n  await app.listen(appOptions);\n  ```\n\n- As a router middleware\n\n  ```ts\n  import { jwtMiddleware, OnSuccessHandler } from \"https://raw.githubusercontent.com/halvardssm/oak-middleware-jwt/master/mod.ts\"\n  import { RouterMiddleware } from \"https://deno.land/x/oak/mod.ts\";\n\n  interface ApplicationState {\n    userId: string\n  }\n\n  const router = new Router();\n  const app = new Application\u003cApplicationState\u003e();\n\n  const onSuccess: OnSuccessHandler = (ctx, jwtPayload) =\u003e {\n    ctx.state.userId = jwtPayload.userId\n  }\n\n  router\n    .get(\"/bar\", jwtMiddleware\u003cRouterMiddleware\u003e({ key:\"foo\", onSuccess }), async (ctx) =\u003e {\n      const callerId = ctx.state.userId\n      ...\n    })\n\n  app.use(router.routes());\n\n  await app.listen(appOptions);\n  ```\n\n- With ignore patterns\n\n  ```ts\n  import {\n    IgnorePattern,\n    jwtMiddleware,\n    OnSuccessHandler,\n  } from \"https://raw.githubusercontent.com/halvardssm/oak-middleware-jwt/master/mod.ts\";\n  import { RouterMiddleware } from \"https://deno.land/x/oak/mod.ts\";\n\n  const app = new Application\u003cApplicationState\u003e();\n\n  const ignorePatterns: IgnorePattern[] = [\"/baz\", /buz/, {\n    path: \"/biz\",\n    methods: [\"GET\"],\n  }];\n\n  app.use(jwtMiddleware\u003cMiddleware\u003e({ key: \"foo\", ignorePatterns }));\n\n  await app.listen(appOptions);\n  ```\n\n## Options\n\n- key: string; // See the djwt module for Validation options\n- algorithm: AlgorithmInput ; // See the djwt module for Validation options\n- customMessages?: ErrorMessages; // Custom error messages\n- ignorePatterns?: Array\u003cIgnorePattern\u003e; // Pattern to ignore e.g.\n  `/authenticate`, can be a RegExp, Pattern object or string. When passing a\n  string, the string will be matched with the path `===`\n- onSuccess?: OnSuccessHandler; // Optional callback for successfull validation,\n  passes the Context and the Payload object from djwt module\n- onFailure?: OnFailureHandler; // Optional callback for unsuccessfull\n  validation, passes the Context and the Error encountered while validating the\n  jwt\n\n## Error Handling\n\nAll errors originating from this middleware is of class `JWTMiddlewareError`\nwhich is exported. To handle `JWTMiddlewareError`s you can do such:\n\n```ts\n...\n} catch(e){\n  if(e instanceof JWTMiddlewareError){\n    //do something\n  }\n}\n```\n\n## Migrating from v1.0.0\n\n- Change the previous `algorithm` parameter's type from `Algorithm` to\n  `AlgorithmInput`\n\n```ts\nimport { AlgorithmInput } from \"https://raw.githubusercontent.com/halvardssm/oak-middleware-jwt/master/mod.ts\";\n\nconst algorithm: AlgorithmInput = \"HS512\";\n\napp.use(jwtMiddleware\u003cMiddleware\u003e({ key: \"foo\", algorithm }));\n```\n\n- Change the onFailure and onSuccess callbacks.\n  - `onSuccess` gets an object of type `Payload` as a second argument (check\n    https://github.com/timonson/djwt#decode)\n  - `onFailure` gets an object of type `Error` as a second argument, should\n    return `true` if the error should be thrown instead of returning as a\n    response.\n\n```ts\nconst onFailure = (ctx, error: Error) =\u003e {\n  console.log(error.message);\n};\n\nconst onSuccess = (ctx, payload: Payload) =\u003e {\n  console.log(payload.userId);\n};\n```\n\n- The expired token bug was fixed. This module will now throw an error (and call\n  `onFailure` callback) if the token sent is expired. Can cause problems in\n  implementations that weren't expecting that\n\n## Contributing\n\nAll contributions are welcome, make sure to read the\n[contributing guidelines](./.github/CONTRIBUTING.md).\n\n## Uses\n\n- [Oak](https://deno.land/x/oak/)\n- [djwt](https://deno.land/x/djwt)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhalvardssm%2Foak-middleware-jwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhalvardssm%2Foak-middleware-jwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhalvardssm%2Foak-middleware-jwt/lists"}