Skip to content

Middleware and service for authentication with JWT tokens for REST Api's written in Go

Notifications You must be signed in to change notification settings

martinreus/auth-middleware

Repository files navigation

Auth services and Middleware for Go applications

How to use it

package main

import (
    "github.com/gorilla/mux"
    "github.com/martinreus/auth-middleware"
)

func main() {
    r := mux.NewRouter()
    authConfig := auth.DefaultConfig()
    authMiddleware := auth.NewService(authConfig)

    api := r.PathPrefix("/api").Subrouter()
    api.HandleFunc("/user/{id}", GetUser).Methods("GET")
    api.Use(authMiddleware.IsAuthenticated)
    api.Use(authMiddleware.HasAnyRole("ADMIN", "SYSTEM"))

    http.ListenAndServe(":8000", r)
}

func GetUser(w http.ResponseWriter, r *http.Request) {...}

Each request will now need a valid JWT Token in order to access the get user route.

Generating JWT's from the command line

With this library it is possible to generate JWT tokens using the provided JWT structure defined here. Just

go run cmd/jwt_generator.go

and read the instructions.

TODO:

Improve

this.doc()

:)

About

Middleware and service for authentication with JWT tokens for REST Api's written in Go

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages