2016-09-01 03:09:34 +02:00
|
|
|
// Package jwt implements a Hook that fails an Announce if the client's request
|
|
|
|
// is missing a valid JSON Web Token.
|
|
|
|
//
|
|
|
|
// JWTs are validated against the standard claims in RFC7519 along with an
|
|
|
|
// extra "infohash" claim that verifies the client has access to the Swarm.
|
|
|
|
// RS256 keys are asychronously rotated from a provided JWK Set HTTP endpoint.
|
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
jc "github.com/SermoDigital/jose/crypto"
|
|
|
|
"github.com/SermoDigital/jose/jws"
|
|
|
|
"github.com/SermoDigital/jose/jwt"
|
2016-09-05 18:10:42 +02:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2016-09-01 03:09:34 +02:00
|
|
|
"github.com/mendsley/gojwk"
|
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/bittorrent"
|
|
|
|
"github.com/chihaya/chihaya/middleware"
|
2016-09-24 19:38:05 +02:00
|
|
|
"github.com/chihaya/chihaya/stopper"
|
2016-09-01 03:09:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrMissingJWT is returned when a JWT is missing from a request.
|
|
|
|
ErrMissingJWT = bittorrent.ClientError("unapproved request: missing jwt")
|
|
|
|
|
|
|
|
// ErrInvalidJWT is returned when a JWT fails to verify.
|
|
|
|
ErrInvalidJWT = bittorrent.ClientError("unapproved request: invalid jwt")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config represents all the values required by this middleware to fetch JWKs
|
|
|
|
// and verify JWTs.
|
|
|
|
type Config struct {
|
|
|
|
Issuer string `yaml:"issuer"`
|
|
|
|
Audience string `yaml:"audience"`
|
|
|
|
JWKSetURL string `yaml:"jwk_set_url"`
|
|
|
|
JWKUpdateInterval time.Duration `yaml:"jwk_set_update_interval"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type hook struct {
|
|
|
|
cfg Config
|
|
|
|
publicKeys map[string]crypto.PublicKey
|
|
|
|
closing chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHook returns an instance of the JWT middleware.
|
2016-09-26 18:00:28 +02:00
|
|
|
func NewHook(cfg Config) (middleware.Hook, error) {
|
2016-10-26 19:58:28 +02:00
|
|
|
log.Debugf("creating new JWT middleware with config: %#v", cfg)
|
2016-09-01 03:09:34 +02:00
|
|
|
h := &hook{
|
|
|
|
cfg: cfg,
|
|
|
|
publicKeys: map[string]crypto.PublicKey{},
|
|
|
|
closing: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
2016-10-26 19:58:28 +02:00
|
|
|
log.Debug("performing initial fetch of JWKs")
|
2016-09-26 18:00:28 +02:00
|
|
|
err := h.updateKeys()
|
|
|
|
if err != nil {
|
2016-10-26 19:58:28 +02:00
|
|
|
return nil, errors.New("failed to fetch initial JWK Set: " + err.Error())
|
2016-09-26 18:00:28 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 03:09:34 +02:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-h.closing:
|
|
|
|
return
|
|
|
|
case <-time.After(cfg.JWKUpdateInterval):
|
2016-10-26 19:58:28 +02:00
|
|
|
log.Debug("performing fetch of JWKs")
|
2016-09-25 21:51:58 +02:00
|
|
|
h.updateKeys()
|
2016-09-01 03:09:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-09-26 18:00:28 +02:00
|
|
|
return h, nil
|
2016-09-01 03:09:34 +02:00
|
|
|
}
|
|
|
|
|
2016-09-26 18:00:28 +02:00
|
|
|
func (h *hook) updateKeys() error {
|
2016-09-25 21:51:58 +02:00
|
|
|
resp, err := http.Get(h.cfg.JWKSetURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("failed to fetch JWK Set: " + err.Error())
|
2016-09-26 18:00:28 +02:00
|
|
|
return err
|
2016-09-25 21:51:58 +02:00
|
|
|
}
|
|
|
|
|
2016-10-26 19:58:28 +02:00
|
|
|
var parsedJWKs gojwk.Key
|
2016-09-25 21:51:58 +02:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(&parsedJWKs)
|
|
|
|
if err != nil {
|
|
|
|
resp.Body.Close()
|
|
|
|
log.Errorln("failed to decode JWK JSON: " + err.Error())
|
2016-09-26 18:00:28 +02:00
|
|
|
return err
|
2016-09-25 21:51:58 +02:00
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
keys := map[string]crypto.PublicKey{}
|
2016-10-26 19:58:28 +02:00
|
|
|
for _, parsedJWK := range parsedJWKs.Keys {
|
2016-09-25 21:51:58 +02:00
|
|
|
publicKey, err := parsedJWK.DecodePublicKey()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("failed to decode JWK into public key: " + err.Error())
|
2016-09-26 18:00:28 +02:00
|
|
|
return err
|
2016-09-25 21:51:58 +02:00
|
|
|
}
|
2016-10-26 19:58:28 +02:00
|
|
|
keys[parsedJWK.Kid] = publicKey
|
2016-09-25 21:51:58 +02:00
|
|
|
}
|
|
|
|
h.publicKeys = keys
|
2016-09-26 18:00:28 +02:00
|
|
|
|
2016-10-26 19:58:28 +02:00
|
|
|
log.Debug("successfully fetched JWK Set")
|
2016-09-26 18:00:28 +02:00
|
|
|
return nil
|
2016-09-25 21:51:58 +02:00
|
|
|
}
|
|
|
|
|
2016-09-24 19:38:05 +02:00
|
|
|
func (h *hook) Stop() <-chan error {
|
2016-10-26 19:58:28 +02:00
|
|
|
log.Debug("attempting to shutdown JWT middleware")
|
2016-09-24 19:38:05 +02:00
|
|
|
select {
|
|
|
|
case <-h.closing:
|
|
|
|
return stopper.AlreadyStopped
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
c := make(chan error)
|
|
|
|
go func() {
|
|
|
|
close(h.closing)
|
|
|
|
close(c)
|
|
|
|
}()
|
|
|
|
return c
|
2016-09-01 03:09:34 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:23:10 +02:00
|
|
|
func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) (context.Context, error) {
|
2016-09-01 03:09:34 +02:00
|
|
|
if req.Params == nil {
|
2016-09-06 00:23:10 +02:00
|
|
|
return ctx, ErrMissingJWT
|
2016-09-01 03:09:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jwtParam, ok := req.Params.String("jwt")
|
|
|
|
if !ok {
|
2016-09-06 00:23:10 +02:00
|
|
|
return ctx, ErrMissingJWT
|
2016-09-01 03:09:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateJWT(req.InfoHash, []byte(jwtParam), h.cfg.Issuer, h.cfg.Audience, h.publicKeys); err != nil {
|
2016-09-06 00:23:10 +02:00
|
|
|
return ctx, ErrInvalidJWT
|
2016-09-01 03:09:34 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:23:10 +02:00
|
|
|
return ctx, nil
|
2016-09-01 03:09:34 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:23:10 +02:00
|
|
|
func (h *hook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) (context.Context, error) {
|
2016-09-01 03:09:34 +02:00
|
|
|
// Scrapes don't require any protection.
|
2016-09-06 00:23:10 +02:00
|
|
|
return ctx, nil
|
2016-09-01 03:09:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func validateJWT(ih bittorrent.InfoHash, jwtBytes []byte, cfgIss, cfgAud string, publicKeys map[string]crypto.PublicKey) error {
|
|
|
|
parsedJWT, err := jws.ParseJWT(jwtBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
claims := parsedJWT.Claims()
|
|
|
|
if iss, ok := claims.Issuer(); !ok || iss != cfgIss {
|
|
|
|
return jwt.ErrInvalidISSClaim
|
|
|
|
}
|
|
|
|
|
|
|
|
if aud, ok := claims.Audience(); !ok || !validAudience(aud, cfgAud) {
|
|
|
|
return jwt.ErrInvalidAUDClaim
|
|
|
|
}
|
|
|
|
|
|
|
|
if ihClaim, ok := claims.Get("infohash").(string); !ok || !validInfoHash(ihClaim, ih) {
|
|
|
|
return errors.New("claim \"infohash\" is invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedJWS := parsedJWT.(jws.JWS)
|
|
|
|
kid, ok := parsedJWS.Protected().Get("kid").(string)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("invalid kid")
|
|
|
|
}
|
|
|
|
publicKey, ok := publicKeys[kid]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("signed by unknown kid")
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedJWS.Verify(publicKey, jc.SigningMethodRS256)
|
|
|
|
}
|
|
|
|
|
|
|
|
func validAudience(aud []string, cfgAud string) bool {
|
|
|
|
for _, a := range aud {
|
|
|
|
if a == cfgAud {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func validInfoHash(claim string, ih bittorrent.InfoHash) bool {
|
|
|
|
if len(claim) == 20 && bittorrent.InfoHashFromString(claim) == ih {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
unescapedClaim, err := url.QueryUnescape(claim)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(unescapedClaim) == 20 && bittorrent.InfoHashFromString(unescapedClaim) == ih {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|