<commit_before>package jwt

import (
	"crypto/rsa"
	"errors"
	"fmt"
	"net/http"
	"os"
	"strconv"
	"strings"

	"bytes"

	"time"

	"io/ioutil"
	"path"

	"github.com/dgrijalva/jwt-go"
	"github.com/mholt/caddy/caddyhttp/httpserver"
)

// global declaration of the backend to enable caching secret key material across requests
// when keys are specified in the config file
var b = backend{
	cache: make(map[string]keycache),
}

// AuthBackend represents a backend interface that retrieves secret key material
// to validate tokens
type AuthBackend interface {
	GetHMACSecret() (b []byte)
	GetRSAPublicKey() (r *rsa.PublicKey)
	IsConfigValid() (v bool)
}

func (h Auth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
	// if the request path is any of the configured paths, validate JWT
	for _, p := range h.Rules {
		if !httpserver.Path(r.URL.Path).Matches(p.Path) {
			continue
		}

		// Check excepted paths for this rule and allow access without validating any token
		var isExceptedPath bool
		for _, e := range p.ExceptedPaths {
			if httpserver.Path(r.URL.Path).Matches(e) {
				isExceptedPath = true
			}
		}
		if isExceptedPath {
			continue
		}
		if r.URL.Path == "/" && p.AllowRoot {
			// special case for protecting children of the root path, only allow access to base directory with directive `allowbase`
			continue
		}

		// Path matches, look for unvalidated token
		uToken, err := ExtractToken(r)
		if err != nil {
			if p.Passthrough {
				return h.Next.ServeHTTP(w, r)
			}
			return handleUnauthorized(w, r, p, h.Realm), nil
		}

		// Initialize a new caching layer if this is the first request to a protected path.
		// Cache only operates when key material is stored on disk.  When using environment variables
		// this has no effect.
		_, ok := b.cache[p.KeyFile]
		if !ok {
			b.cache[p.KeyFile] = keycache{
				KeyFile:     p.KeyFile,
				KeyFileType: p.KeyFileType,
			}
		}
		b.current = b.cache[p.KeyFile]
<commit_msg>use continue instead of skipping straight to serve next<commit_after>package jwt

import (
	"crypto/rsa"
	"errors"
	"fmt"
	"net/http"
	"os"
	"strconv"
	"strings"

	"bytes"

	"time"

	"io/ioutil"
	"path"

	"github.com/dgrijalva/jwt-go"
	"github.com/mholt/caddy/caddyhttp/httpserver"
)

// global declaration of the backend to enable caching secret key material across requests
// when keys are specified in the config file
var b = backend{
	cache: make(map[string]keycache),
}

// AuthBackend represents a backend interface that retrieves secret key material
// to validate tokens
type AuthBackend interface {
	GetHMACSecret() (b []byte)
	GetRSAPublicKey() (r *rsa.PublicKey)
	IsConfigValid() (v bool)
}

func (h Auth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
	// if the request path is any of the configured paths, validate JWT
	for _, p := range h.Rules {
		if !httpserver.Path(r.URL.Path).Matches(p.Path) {
			continue
		}

		// Check excepted paths for this rule and allow access without validating any token
		var isExceptedPath bool
		for _, e := range p.ExceptedPaths {
			if httpserver.Path(r.URL.Path).Matches(e) {
				isExceptedPath = true
			}
		}
		if isExceptedPath {
			continue
		}
		if r.URL.Path == "/" && p.AllowRoot {
			// special case for protecting children of the root path, only allow access to base directory with directive `allowbase`
			continue
		}

		// Path matches, look for unvalidated token
		uToken, err := ExtractToken(r)
		if err != nil {
			if p.Passthrough {
				continue
			}
			return handleUnauthorized(w, r, p, h.Realm), nil
		}

		// Initialize a new caching layer if this is the first request to a protected path.
		// Cache only operates when key material is stored on disk.  When using environment variables
		// this has no effect.
		_, ok := b.cache[p.KeyFile]
		if !ok {
			b.cache[p.KeyFile] = keycache{
				KeyFile:     p.KeyFile,
				KeyFileType: p.KeyFileType,
			}
		}
		b.current = b.cache[p.KeyFile]

{'fraction_non_alphanumeric': 0.10693421386842773, 'fraction_numerical': 0.0, 'mean_word_length': 3.2944383860414397, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3116989', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1316, 'n_words': 430}