jwtmiddleware

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2023 License: MIT Imports: 7 Imported by: 0

README

lambda-jwt-middleware Test

A middleware function used for verifying the jwt token from cookies which is compitable with aws-lambda-go

The basic structure of the function is based on this article.

Since it uses Generics feature, Go version 1.18 or greater is required.

Usage

It uses env variable to read the cookie secret if you've encrypted your cookie.

.env

ACCESS_TOKEN_SECRET=yoursecret

Define your own payloadHandler, as the example below, I extract the userId and search the entity in MongoDB, then store the entity into the context.

func payloadHandler(ctx context.Context, payload interface{}) (context.Context, error) {
    userId, ok := payload.(string)
    userObjId, _ := primitive.ObjectIDFromHex(userId)
    if !ok {
        log.Printf("wrong payload format: %v", payload)
        return nil, fmt.Errorf("wrong payload format")
    }

    userRepo := UserRepository.New()
    defer userRepo.Disconnect()()
    var user UserRepository.User

    err := userRepo.FindOne(context.TODO(), bson.M{"_id": userObjId}).Decode(&user)
    if err != nil {
        log.Println(err)
        return nil, err
    }

    ctx = context.WithValue(ctx, helper.ContextKeyUser, user)

    return ctx, nil
}

Then you use the middleware in the lambda.Start() function with the handler and payloadHandler as the arguments.

func main() {
    lambda.Start(jwtmiddleware.Handle(Handler, payloadHandler))
}

Or you can combine the middleware with the payloadHandler to form another function which makes it shorter.

import (
    "github.com/noobj/jwtmiddleware"
    "github.com/noobj/jwtmiddleware/types"
)

func Auth[T types.ApiRequest, R types.ApiResponse](f types.HandlerFunc[T, R]) types.HandlerFunc[T, R] {
    return jwtmiddleware.Handle(f, payloadHandler)
}

func main() {
    lambda.Start(Auth(Handler))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handle

func Handle[T types.ApiRequest, R types.ApiResponse](f types.HandlerFunc[T, R], payloadHandler func(context.Context, interface{}) (context.Context, error)) types.HandlerFunc[T, R]

Handle will extract the payload from cookie named access_token. It then pass the payload to the payloadHandler you assign. Finally, it calls and returns the result of the handler you define.

Types

This section is empty.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL