auth0 jwt validator with jwks caching

JWT Validator for Auth0 (https://auth0.com/) that caches public JWKS (since there is a limit on calls to public JWKS URL)

Example securing a GraphQL server using chi-router:

package main

import (
	"log"
	"net/http"
	"os"

	"github.com/99designs/gqlgen/graphql/handler"
	"github.com/99designs/gqlgen/graphql/playground"
	"github.com/dragonlobster/auth0-jwt-cache/auth"
	"github.com/dragonlobster/example-repo/example"
	"github.com/dragonlobster/example-db-connection/common"
	"github.com/go-chi/chi/v5"
)

const defaultPort = "8080"

func main() {

	pool, err := common.ConnectToDB()

	if err == nil {
		log.Printf("Connected to DB")
	}

	port := os.Getenv("PORT")
	if port == "" {
		port = defaultPort
	}

	config := generated.Config{Resolvers: &graph.Resolver{
		ExampleRepo: example.ExampleRepo{Pool: pool},
	}}

	srv := handler.NewDefaultServer(generated.NewExecutableSchema(config))
	playground := playground.Handler("GraphQL playground", "/query")

	cert := auth.CachedCert{
		JWKSUrl: PUBLIC_JWKS_URL,
		Aud:     AUDIENCE,
		Iss:     ISSUER,
	}

	auth0_validator, _ := cert.ValidateToken()

	router := chi.NewRouter()

	router.Handle("/", auth0_validator.Handler(playground))
	router.Handle("/query", auth0_validator.Handler(srv))

	log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
	log.Fatal(http.ListenAndServe(":"+port, router))
}

The above code secures / and /query endpoint with jwt middleware that validates a JWT (signed with private key) using auth0 public JWKS url, its audience, and issuer - sending a request to these endpoints will require a valid JWT to be in Authorization header as bearer: Authorization: Bearer abcd123456

TODO:

  • Tests
  • Add scope validation
Owner
Backend Engineer @ Crypto.com
null
Similar Resources

This is an implementation of JWT in golang!

jwt This is a minimal implementation of JWT designed with simplicity in mind. What is JWT? Jwt is a signed JSON object used for claims based authentic

Oct 25, 2022

This package provides json web token (jwt) middleware for goLang http servers

jwt-auth jwt auth middleware in goLang. If you're interested in using sessions, checkout my sessions library! README Contents: Quickstart Performance

Dec 5, 2022

Golang implementation of JSON Web Tokens (JWT)

jwt-go A go (or 'golang' for search engine friendliness) implementation of JSON Web Tokens NEW VERSION COMING: There have been a lot of improvements s

Jan 6, 2023

JWT login microservice with plugable backends such as OAuth2, Google, Github, htpasswd, osiam, ..

JWT login microservice with plugable backends such as OAuth2, Google, Github, htpasswd, osiam, ..

loginsrv loginsrv is a standalone minimalistic login server providing a JWT login for multiple login backends. ** Attention: Update to v1.3.0 for Goog

Dec 24, 2022

Simple JWT Golang

sjwt Simple JSON Web Token - Uses HMAC SHA-256 Example // Set Claims claims := New() claims.Set("username", "billymister") claims.Set("account_id", 86

Dec 8, 2022

simple-jwt-provider - Simple and lightweight provider which exhibits JWTs, supports login, password-reset (via mail) and user management.

Simple and lightweight JWT-Provider written in go (golang). It exhibits JWT for the in postgres persisted user, which can be managed via api. Also, a password-reset flow via mail verification is available. User specific custom-claims also available for jwt-generation and mail rendering.

Dec 18, 2022

The easiest JWT library to GO

JWT Go The easiest JWT Library that could be a starting point for your project. Installation go get github.com/supanadit/jwt-go Quick Start package ma

Sep 23, 2022

YSHOP-GO基于当前流行技术组合的前后端RBAC管理系统:Go1.15.x+Beego2.x+Jwt+Redis+Mysql8+Vue 的前后端分离系统,权限控制采用 RBAC,支持数据字典与数据权限管理,支持动态路由等

YSHOP-GO 后台管理系统 项目简介 YSHOP-GO基于当前流行技术组合的前后端RBAC管理系统:Go1.15.x+Beego2.x+Jwt+Redis+Mysql8+Vue 的前后端分离系统,权限控制采用 RBAC,支持数据字典与数据权限管理,支持动态路由等 体验地址: https://go

Dec 30, 2022

Small Lambda function which performs a Aws:Sts:AssumeRole based on the presented JWT-Token

About This implements a AWS Lambda handler which takes a JWT-Token, validates it and then performs a Aws:Sts:AssumeRole based on preconfigured rules.

Aug 8, 2022
Related tags
404 Page

404

Sorry! Page not found.

Unfortunately the page you are looking for has been moved or deleted.