Skip to content
プロジェクト

概要

Create a jwt.Keyfunc for JWT parsing with a JWK Set or given cryptographic keys (like HMAC) in Golang.

セキュリティ・プライバシー

この数値の測り方

できること

  • An HTTP client that automatically updates one or more remote JWK Set resources.
  • An automatic refresh of remote HTTP resources when an unknown key ID (kid) is encountered.
  • X.509 URIs or embedded certificate chains, when a JWK contains them.
  • Support for private asymmetric keys.

計測したスター増加

24時間
0
7日間
+1
15日間
-1
1か月
履歴を収集中

期間ごとの純増数です。期間は重複しており、日別の値ではありません。未計測の値は空欄です。スター数は注目度の指標であり、品質の保証ではありません。

Go Reference

keyfunc

The purpose of this package is to provide a jwt.Keyfunc for the github.com/golang-jwt/jwt/v5 package using a JSON Web Key Set (JWK Set) for parsing and verifying JSON Web Tokens (JWTs).

It's common for an identity providers, particularly those using OAuth 2.0 or OpenID Connect, such as Keycloak or Amazon Cognito (AWS) to expose a JWK Set via an HTTPS endpoint. This package has the ability to consume that JWK Set and produce a jwt.Keyfunc. It is important that a JWK Set endpoint is using HTTPS to ensure the keys are from the correct trusted source.

Basic usage

For complete examples, please see the examples directory.

import "github.com/MicahParks/keyfunc/v3"

Step 1: Create the keyfunc.Keyfunc

The below example is for a remote HTTP resource. See examples/json/main.go for a JSON example.

// Create the keyfunc.Keyfunc.
k, err := keyfunc.NewDefaultCtx(ctx, []string{server.URL}) // Context is used to end the refresh goroutine.
if err != nil {
	log.Fatalf("Failed to create a keyfunc.Keyfunc from the server's URL.\nError: %s", err)
}

When using the keyfunc.NewDefault function, the JWK Set will be automatically refreshed using jwkset.NewDefaultHTTPClient. This does launch a " refresh goroutine". If you want the ability to end this goroutine, use the keyfunc.NewDefaultCtx function.

It is also possible to create a keyfunc.Keyfunc from given keys like HMAC shared secrets. See examples/hmac/main.go.

Step 2: Use the keyfunc.Keyfunc to parse and verify JWTs

// Parse the JWT.
parsed, err := jwt.Parse(signed, k.Keyfunc)
if err != nil {
	log.Fatalf("Failed to parse the JWT.\nError: %s", err)
}

Additional features

This project's primary purpose is to provide a jwt.Keyfunc implementation for JWK Sets.

Since version 3.X.X, this project has become a thin wrapper around github.com/MicahParks/jwkset. Newer versions contain a superset of features available in versions 2.X.X and earlier, but some of the deep customization has been moved to the jwkset project. The intention behind this is to make keyfunc easier to use for most use cases.

Access the jwkset.Storage from a keyfunc.Keyfunc via the .Storage() method. Using the github.com/MicahParks/jwkset package provides the below features, and more:

  • An HTTP client that automatically updates one or more remote JWK Set resources.
  • An automatic refresh of remote HTTP resources when an unknown key ID (kid) is encountered.
  • X.509 URIs or embedded certificate chains, when a JWK contains them.
  • Support for private asymmetric keys.
  • Specified key operations and usage.

Related projects

github.com/MicahParks/jwkset:

A JWK Set implementation. The keyfunc project is a wrapper around this project.

関連プロジェクトを探す

読み込み中…