Giter VIP home page Giter VIP logo

go-jwks's Introduction

Build Status codecov

go-jwks

A Go library to retrieve RSA public keys from a JWKS (JSON Web Key Set) endpoint.

Installation

Using Go modules

go get github.com/s12v/[email protected]

Dependencies

  • github.com/square/go-jose - JWT library
  • github.com/patrickmn/go-cache - default in-memory cache

Example

GetEncryptionKey returns *jose.JSONWebKey for a given key id:

package main

import (
	"log"
	"time"

	"github.com/s12v/go-jwks"
	"github.com/square/go-jose"
)

func main() {
	jwksSource := jwks.NewWebSource("https://www.googleapis.com/oauth2/v3/certs")
	jwksClient := jwks.NewDefaultClient(
		jwksSource,
		time.Hour,    // Refresh keys every 1 hour
		12*time.Hour, // Expire keys after 12 hours
	)

	var jwk *jose.JSONWebKey
	jwk, err := jwksClient.GetEncryptionKey("c6af7caa0895fd01e778dceaa7a7988347d8f25c")
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("key: %v, alg: %v, use: %v", jwk.KeyID, jwk.Algorithm, jwk.Use)
}

Log:

2018/07/30 01:22:35 Fetchng JWKS from https://www.googleapis.com/oauth2/v3/certs
2018/07/30 01:22:36 key: c6af7caa0895fd01e778dceaa7a7988347d8f25c, alg: RS256, use: sig

Caching

Key refresh and TTL

There are two caching parameters:

  • refresh - the key will be fetched from the source after this interval
  • ttl - if not used, the key will be deleted from cache

On the first request, the key is synchronously fetched from the key server and stored in the cache. On the next request after refresh interval, the key will be refreshed in the background (not affect response time). Only 1 key refresh is executed at the same time.

If the key is not requested during ttl interval, it will be removed from cache.

Cache implementations

Default cache is github.com/patrickmn/go-cache in-memory cache. You can provide your own cache implementation, see cache.go:

type Cache interface {
	// Get an item from the cache and itsexpiration time.
	// Returns the item or nil, and a bool indicating whether the key was found
	GetWithExpiration(k string) (interface{}, time.Time, bool)
	// Add an item to the cache, replacing any existing item.
	Set(k string, x interface{})
}

and pass it to func NewClient(...)

Source

Default source is WebSource. You can provide your own implementation, see source.go:

type JWKSSource interface {
	JSONWebKeySet() (*jose.JSONWebKeySet, error)
}

go-jwks's People

Contributors

bigkraig avatar dependabot[bot] avatar imle avatar s12v avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-jwks's Issues

Remove noisy log message

Remove logger.Printf("Fetching JWKS from %s", s.jwksUri) in source.go

This log is being printed as an error log in datadog. While this is not an error.

Question about some suspect dependencies of "go-jose"

I'm quite new to Go and I have some issues with module dependencies in my project. While trying to figure out what's the problem I found out that in the go.mod of the go-jwks repository there are two (to me) suspect lines:

require(
  // ...
  github.com/square/go-jose v2.6.0+incompatible
  // ...  
  gopkg.in/square/go-jose.v2 v2.3.1 // indirect
)

They are targeting the same module, but with different versions and on different package registries (github.com and gopkg.in). Furthermore (as far as I can see) the gopkg.in/square/go-jose.v2 is never user inside the code of the repository.

Is this intentional? Cause I think that this could be the problem that is breaking my code.

refreshKey in background routine should have Background context?

By the time sem is released the context may have been done, if is ok to use context.Background() I'll be happy to open a PR.

go-jwks/client.go

Lines 63 to 68 in f5cc55a

go func() {
defer c.sem.Release(1)
if _, err := c.refreshKey(ctx, keyId, use); err != nil {
logger.Printf("unable to refresh key: %v", err)
}
}()

seeing this message in production prompted me to come here and create this issue:

unable to refresh key: Get "<the jwks source url>": context canceled

Disable logger or allow specifying custom one

Instead of hard coding log.Printf("unable to refresh key: %v", err) it would be nice if you either A) allowed disabling this log output or B) allowed specifying a custom logger using an interface.

Timeout for default client

What is the timeout for the default client? Is the default one for net/http?
Is it possible to change it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.