MyGit

auth0/go-jwt-middleware

Fork: 205 Star: 1083 (更新于 2024-11-08 11:41:28)

license: MIT

Language: Go .

A Middleware for Go Programming Language to check for JWTs on HTTP requests

最后发布版本: v2.2.2 ( 2024-08-24 03:19:54)

GitHub网址

Go JWT Middleware

GoDoc Go Report Card License Release Codecov Tests

📚 Documentation • 🚀 Getting Started • 💬 Feedback

Documentation

  • Godoc - explore the go-jwt-middleware documentation.
  • Docs site — explore our docs site and learn more about Auth0.
  • Quickstart - our guide for adding go-jwt-middleware to your app.

Getting started

Requirements

This library follows the same support policy as Go. The last two major Go releases are actively supported and compatibility issues will be fixed. While you may find that older versions of Go may work, we will not actively test and fix compatibility issues with these versions.

  • Go 1.22+

Installation

go get github.com/auth0/go-jwt-middleware/v2

Usage

package main

import (
	"context"
	"encoding/json"
	"log"
	"net/http"

	"github.com/auth0/go-jwt-middleware/v2"
	"github.com/auth0/go-jwt-middleware/v2/validator"
	jwtmiddleware "github.com/auth0/go-jwt-middleware/v2"
)

var handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	claims, ok := r.Context().Value(jwtmiddleware.ContextKey{}).(*validator.ValidatedClaims)
	if !ok {
		http.Error(w, "failed to get validated claims", http.StatusInternalServerError)
		return
	}
	
	payload, err := json.Marshal(claims)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(payload)
})

func main() {
	keyFunc := func(ctx context.Context) (interface{}, error) {
		// Our token must be signed using this data.
		return []byte("secret"), nil
	}

	// Set up the validator.
	jwtValidator, err := validator.New(
		keyFunc,
		validator.HS256,
		"https://<issuer-url>/",
		[]string{"<audience>"},
	)
	if err != nil {
		log.Fatalf("failed to set up the validator: %v", err)
	}

	// Set up the middleware.
	middleware := jwtmiddleware.New(jwtValidator.ValidateToken)

	http.ListenAndServe("0.0.0.0:3000", middleware.CheckJWT(handler))
}

After running that code (go run main.go) you can then curl the http server from another terminal:

$ curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJpc3MiOiJnby1qd3QtbWlkZGxld2FyZS1leGFtcGxlIiwiYXVkIjoiZ28tand0LW1pZGRsZXdhcmUtZXhhbXBsZSJ9.xcnkyPYu_b3qm2yeYuEgr5R5M5t4pN9s04U1ya53-KM" localhost:3000

That should give you the following response:

{
  "CustomClaims": null,
  "RegisteredClaims": {
    "iss": "go-jwt-middleware-example",
    "aud": "go-jwt-middleware-example",
    "sub": "1234567890",
    "iat": 1516239022
  }
}

The JWT included in the Authorization header above is signed with secret.

To test how the response would look like with an invalid token:

$ curl -v -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.yiDw9IDNCa1WXCoDfPR_g356vSsHBEerqh9IvnD49QE" localhost:3000

That should give you the following response:

...
< HTTP/1.1 401 Unauthorized
< Content-Type: application/json
{"message":"JWT is invalid."}
...

For more examples please check the examples folder.

Feedback

Contributing

We appreciate feedback and contribution to this repo! Before you get started, please see the following:

Raise an issue

To provide feedback or report a bug, please raise an issue on our issue tracker.

Vulnerability Reporting

Please do not report security vulnerabilities on the public Github issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.


Auth0 Logo

Auth0 is an easy to implement, adaptable authentication and authorization platform.
To learn more checkout Why Auth0?

This project is licensed under the MIT license. See the LICENSE file for more info.

最近版本更新:(数据更新于 2024-09-16 19:59:52)

2024-08-24 03:19:54 v2.2.2

2024-01-22 23:03:37 v2.2.1

2023-12-04 19:27:18 v2.2.0

2022-11-02 21:07:03 v2.1.0

2022-03-22 05:03:10 v2.0.1

2022-01-19 21:43:14 v2.0.0

2022-01-07 00:54:40 v2.0.0-beta.1

2021-12-08 16:38:37 v2.0.0-beta

2021-06-22 11:15:19 v1.0.1

2021-01-07 04:51:33 v1.0.0

主题(topics):

dx-sdk

auth0/go-jwt-middleware同语言 Go最近更新仓库

2024-11-24 20:18:09 SagerNet/sing-box

2024-11-23 10:28:20 ollama/ollama

2024-11-23 05:46:00 dolthub/dolt

2024-11-22 20:14:47 daytonaio/daytona

2024-11-22 08:53:37 AlistGo/alist

2024-11-22 02:54:58 projectdiscovery/nuclei