Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh public keys on lookup failure. #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 2 additions & 23 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func initConfig() error {
return err
}
initDomains(*domains)
if err := initPublicKeys(*publicKeysPath); err != nil {
cfg.PublicKeyPath = *publicKeysPath
if err := cfg.RefreshPublicKeys(); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -116,25 +117,3 @@ func initDomains(domains string) {
}
}
}

func initPublicKeys(filePath string) error {
var err error
if len(filePath) != 0 {
cfg.PublicKeys, err = loadPublicKeysFromFile(filePath)
} else {
cfg.PublicKeys, err = jwt.FetchPublicKeys()
}
if err != nil {
return err
}
return cfg.Validate()
}

func loadPublicKeysFromFile(filePath string) (map[string]jwt.PublicKey, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
return jwt.DecodePublicKeys(f)
}
45 changes: 45 additions & 0 deletions jwt/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,53 @@ package jwt

import (
"errors"
"fmt"
"os"
"regexp"

jwt "github.com/dgrijalva/jwt-go"
)

// Config specifies the parameters for which to perform validation of JWT
// tokens in requests against.
type Config struct {
PublicKeyPath string
PublicKeys map[string]PublicKey
MatchAudiences *regexp.Regexp
MatchDomains map[string]bool
}

func (cfg *Config) RefreshPublicKeys() error {
var err error
if len(cfg.PublicKeyPath) != 0 {
cfg.PublicKeys, err = loadPublicKeysFromFile(cfg.PublicKeyPath)
} else {
cfg.PublicKeys, err = FetchPublicKeys()
}
return err
}

func (cfg *Config) GetPublicKey(keyID string) (interface{}, error) {
key, ok := cfg.PublicKeys[keyID]
// Refresh public keys on lookup failure. For details, see
// https://github.com/imkira/gcp-iap-auth/issues/10 and
// https://stackoverflow.com/questions/44828856/google-iap-public-keys-expiry.
if !ok {
if err := cfg.RefreshPublicKeys(); err != nil {
return nil, err
}
key, ok = cfg.PublicKeys[keyID]
if !ok {
return nil, fmt.Errorf("No public key for %q", keyID)
}
}
parsedKey, err := jwt.ParseECPublicKeyFromPEM(key)
if err != nil {
return nil, fmt.Errorf("Failed to parse key: %v", err)
}
return parsedKey, nil
}

// Validate validates the Configuration.
func (cfg *Config) Validate() error {
if cfg.MatchAudiences == nil {
Expand All @@ -31,3 +67,12 @@ func (cfg *Config) matchesAudience(aud *Audience) bool {
func (cfg *Config) matchesDomain(hd string) bool {
return len(cfg.MatchDomains) == 0 || cfg.MatchDomains[hd]
}

func loadPublicKeysFromFile(filePath string) (map[string]PublicKey, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
return DecodePublicKeys(f)
}
11 changes: 2 additions & 9 deletions jwt/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,8 @@ func tokenKey(token *jwt.Token) (interface{}, error) {
return nil, fmt.Errorf("Invalid algorithm: %v", token.Header[algorithmClaim])
}
keyID, _ := token.Header[keyIDClaim].(string)
key := token.Claims.(*Claims).cfg.PublicKeys[keyID]
if len(key) == 0 {
return nil, fmt.Errorf("No public key for %q", keyID)
}
parsedKey, err := jwt.ParseECPublicKeyFromPEM(key)
if err != nil {
return nil, fmt.Errorf("Failed to parse key: %v", err)
}
return parsedKey, nil
cfg := token.Claims.(*Claims).cfg
return cfg.GetPublicKey(keyID)
}

func tokenMethod(token *jwt.Token) (jwt.SigningMethod, bool) {
Expand Down