All Projects → halvardssm → oak-middleware-jwt

halvardssm / oak-middleware-jwt

Licence: MIT License
Oak middleware for JWT

Programming Languages

typescript
32286 projects
Makefile
30231 projects

Projects that are alternatives of or similar to oak-middleware-jwt

destjs
Make beautiful APIs with the NestJS inspired framework for Deno
Stars: ✭ 31 (+29.17%)
Mutual labels:  oak, deno
superdeno
Super-agent driven library for testing Deno HTTP servers.
Stars: ✭ 119 (+395.83%)
Mutual labels:  oak, deno
Oak
A middleware framework for handling HTTP with Deno 🐿️ 🦕
Stars: ✭ 3,799 (+15729.17%)
Mutual labels:  oak, deno
deno-pokemon
a simple api to create and explore pokemons - made with oak deno framework
Stars: ✭ 20 (-16.67%)
Mutual labels:  oak, deno
doa
A middleware framework for Deno's http serve🦕. Transplanted from Koa with ❤️
Stars: ✭ 20 (-16.67%)
Mutual labels:  middleware, deno
deno rest
A Boilerplate for deno RESTful apis
Stars: ✭ 74 (+208.33%)
Mutual labels:  oak, deno
drink-if-exists
🍷 The NPM drinking game recreated and cli-ified with Deno
Stars: ✭ 18 (-25%)
Mutual labels:  deno
sherlock-deno
🕵️ Find usernames across 350+ websites & social networks - written in TypeScript and run via Deno
Stars: ✭ 19 (-20.83%)
Mutual labels:  deno
cli badges
Generate Badges for your CLI written in 🦕 Deno and TS
Stars: ✭ 20 (-16.67%)
Mutual labels:  deno
deno desktop
Windowing support for Deno WebGPU.
Stars: ✭ 49 (+104.17%)
Mutual labels:  deno
discord-api-types
Up to date Discord API Typings, versioned by the API version
Stars: ✭ 270 (+1025%)
Mutual labels:  deno
make-deno-edition
Automatically makes package.json projects (such as npm packages and node.js modules) compatible with Deno.
Stars: ✭ 39 (+62.5%)
Mutual labels:  deno
deno-auth
User authentication implemented in Deno in accordance with MVC architecture
Stars: ✭ 24 (+0%)
Mutual labels:  deno
jackrabbit-filevault
Apache Jackrabbit FileVault
Stars: ✭ 37 (+54.17%)
Mutual labels:  oak
nhttp
An Simple http framework for Deno, Deno Deploy and Cloudflare Workers. so hot 🚀
Stars: ✭ 26 (+8.33%)
Mutual labels:  deno
deno2node
Compile your Deno project to run on Node.js.
Stars: ✭ 66 (+175%)
Mutual labels:  deno
dictator
Dictates what your users see. Plug-based authorization.
Stars: ✭ 77 (+220.83%)
Mutual labels:  middleware
deno-canvas
Canvas API for Deno, ported from canvaskit-wasm (Skia).
Stars: ✭ 124 (+416.67%)
Mutual labels:  deno
Uwuifier-node
This repository contains the Uwuifier package! It's written in Deno with TypeScript and compiled into JavaScript for NPM, makes use of Jest for testing the code and is deployed on NPM and https://deno.land.
Stars: ✭ 48 (+100%)
Mutual labels:  deno
request-context
Simple connect middleware for accessing data in a request context.
Stars: ✭ 55 (+129.17%)
Mutual labels:  middleware

Oak Middleware JWT

GitHub release (latest SemVer) GitHub Workflow Status (branch) (Deno) (Deno) (Deno) deno doc nest badge

Oak middleware for JWT using Djwt

Usage

  • As an application middleware

    import { jwtMiddleware } from "https://raw.githubusercontent.com/halvardssm/oak-middleware-jwt/master/mod.ts";
    import { Middleware } from "https://deno.land/x/oak/mod.ts";
    
    const app = new Application();
    
    app.use(jwtMiddleware<Middleware>({ key: "foo" }));
    
    await app.listen(appOptions);
  • As a router middleware

    import { jwtMiddleware, OnSuccessHandler } from "https://raw.githubusercontent.com/halvardssm/oak-middleware-jwt/master/mod.ts"
    import { RouterMiddleware } from "https://deno.land/x/oak/mod.ts";
    
    interface ApplicationState {
      userId: string
    }
    
    const router = new Router();
    const app = new Application<ApplicationState>();
    
    const onSuccess: OnSuccessHandler = (ctx, jwtPayload) => {
      ctx.state.userId = jwtPayload.userId
    }
    
    router
      .get("/bar", jwtMiddleware<RouterMiddleware>({ key:"foo", onSuccess }), async (ctx) => {
        const callerId = ctx.state.userId
        ...
      })
    
    app.use(router.routes());
    
    await app.listen(appOptions);
  • With ignore patterns

    import {
      IgnorePattern,
      jwtMiddleware,
      OnSuccessHandler,
    } from "https://raw.githubusercontent.com/halvardssm/oak-middleware-jwt/master/mod.ts";
    import { RouterMiddleware } from "https://deno.land/x/oak/mod.ts";
    
    const app = new Application<ApplicationState>();
    
    const ignorePatterns: IgnorePattern[] = ["/baz", /buz/, {
      path: "/biz",
      methods: ["GET"],
    }];
    
    app.use(jwtMiddleware<Middleware>({ key: "foo", ignorePatterns }));
    
    await app.listen(appOptions);

Options

  • key: string; // See the djwt module for Validation options
  • algorithm: AlgorithmInput ; // See the djwt module for Validation options
  • customMessages?: ErrorMessages; // Custom error messages
  • ignorePatterns?: Array; // Pattern to ignore e.g. /authenticate, can be a RegExp, Pattern object or string. When passing a string, the string will be matched with the path ===
  • onSuccess?: OnSuccessHandler; // Optional callback for successfull validation, passes the Context and the Payload object from djwt module
  • onFailure?: OnFailureHandler; // Optional callback for unsuccessfull validation, passes the Context and the Error encountered while validating the jwt

Error Handling

All errors originating from this middleware is of class JWTMiddlewareError which is exported. To handle JWTMiddlewareErrors you can do such:

...
} catch(e){
  if(e instanceof JWTMiddlewareError){
    //do something
  }
}

Migrating from v1.0.0

  • Change the previous algorithm parameter's type from Algorithm to AlgorithmInput
import { AlgorithmInput } from "https://raw.githubusercontent.com/halvardssm/oak-middleware-jwt/master/mod.ts";

const algorithm: AlgorithmInput = "HS512";

app.use(jwtMiddleware<Middleware>({ key: "foo", algorithm }));
  • Change the onFailure and onSuccess callbacks.
    • onSuccess gets an object of type Payload as a second argument (check https://github.com/timonson/djwt#decode)
    • onFailure gets an object of type Error as a second argument, should return true if the error should be thrown instead of returning as a response.
const onFailure = (ctx, error: Error) => {
  console.log(error.message);
};

const onSuccess = (ctx, payload: Payload) => {
  console.log(payload.userId);
};
  • The expired token bug was fixed. This module will now throw an error (and call onFailure callback) if the token sent is expired. Can cause problems in implementations that weren't expecting that

Contributing

All contributions are welcome, make sure to read the contributing guidelines.

Uses

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].