Skip to content

Commit

Permalink
Using keys bundled in the package
Browse files Browse the repository at this point in the history
Type: Feat

- Makes generation much faster
  • Loading branch information
benfdking committed Dec 27, 2020
1 parent 55c51d3 commit c8d4017
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.PHONY: keys_file
keys_file:
go-bindata -o pkg/keys/data.go keys/...
go-bindata -o pkg/keys/data.go -pkg keys -prefix keys/ keys/...
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var rootCmd = &cobra.Command{
}
} else {
var err error
_, privateKey, err = url.ReturnJWKSAndPrivateKey(key)
_, privateKey, err = url.ReturnJWKSAndPrivateKeyFromData(key)
if err != nil {
log.Fatal(err)
}
Expand Down
124 changes: 61 additions & 63 deletions pkg/keys/data.go

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

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

Expand All @@ -28,19 +27,21 @@ func handler(letter string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
jwks, _, err := url.ReturnJWKSAndPrivateKey(letter)
jwks, _, err := url.ReturnJWKSAndPrivateKeyFromData(letter)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
jsonbuf, err := json.MarshalIndent(jwks, "", " ")
if err != nil {
log.Fatalf("failed to generate JSON: %s", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(jsonbuf)
return
default:
w.WriteHeader(http.StatusNotFound)
return
}
}
Expand Down
43 changes: 40 additions & 3 deletions pkg/url/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,51 @@ import (
"context"
"crypto"
"encoding/json"
"github.com/benfdking/jen/pkg/rsapem"
"github.com/lestrrat-go/jwx/jwk"
"golang.org/x/sync/errgroup"
"fmt"
"io/ioutil"
"net/http"
"time"

"github.com/benfdking/jen/pkg/keys"
"github.com/benfdking/jen/pkg/rsapem"
"github.com/lestrrat-go/jwx/jwk"
"golang.org/x/sync/errgroup"
)

func ReturnJWKSAndPrivateKeyFromData(version string) (jwk.Set, crypto.PrivateKey, error) {
if !IsVersion(version) {
return jwk.Set{}, nil, fmt.Errorf("version %s is invalid", version)
}

g, _ := errgroup.WithContext(context.Background())
var set jwk.Set
g.Go(func() error {
data, err := keys.Asset(version + "/jwks.json")
if err != nil {
return err
}
return json.Unmarshal(data, &set)
})

var private crypto.PrivateKey
g.Go(func() error {
data, err := keys.Asset(version + "/private.pem")
if err != nil {
return err
}
private, err = rsapem.RSAPrivateFromPen(data)
if err != nil {
return err
}
return nil
})

if err := g.Wait(); err != nil {
return jwk.Set{}, nil, err
}
return set, private, nil
}

func ReturnJWKSAndPrivateKey(version string) (jwk.Set, crypto.PrivateKey, error) {
jwksURL, cryptoURL, err := ReturnJWKSAndPrivatePEMURL(version)
if err != nil {
Expand Down

0 comments on commit c8d4017

Please sign in to comment.