<commit_before>package authentication

import (
	log "github.com/Sirupsen/logrus"

	"fmt"
	"github.com/SpectoLabs/hoverfly/authentication/backends"
	jwt "github.com/dgrijalva/jwt-go"
	"net/http"
)

type AuthMiddleware struct {
	AB backends.AuthBackend
}

func GetNewAuthenticationMiddleware(authBackend backends.AuthBackend) *AuthMiddleware {
	return &AuthMiddleware{AB: authBackend}
}

func (a *AuthMiddleware) RequireTokenAuthentication(rw http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	authBackend := InitJWTAuthenticationBackend(a.AB)

	token, err := jwt.ParseFromRequest(req, func(token *jwt.Token) (interface{}, error) {
		if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
			return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
		} else {
			return authBackend.SecretKey, nil
		}
	})
	log.WithFields(log.Fields{
		"valid":  token.Valid,
		"string": token.Raw,
	}).Warn("token information")

	if err == nil && token.Valid && !authBackend.IsInBlacklist(req.Header.Get("Authorization")) {
		next(rw, req)
	} else {<commit_msg>passing secret key to jwt auth middleware init<commit_after>package authentication

import (
	log "github.com/Sirupsen/logrus"

	"fmt"
	"github.com/SpectoLabs/hoverfly/authentication/backends"
	jwt "github.com/dgrijalva/jwt-go"
	"net/http"
)

type AuthMiddleware struct {
	AB        backends.AuthBackend
	SecretKey []byte
}

func GetNewAuthenticationMiddleware(authBackend backends.AuthBackend, secretKey []byte) *AuthMiddleware {
	return &AuthMiddleware{AB: authBackend, SecretKey: secretKey}
}

func (a *AuthMiddleware) RequireTokenAuthentication(rw http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	authBackend := InitJWTAuthenticationBackend(a.AB, a.SecretKey)

	token, err := jwt.ParseFromRequest(req, func(token *jwt.Token) (interface{}, error) {
		if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
			return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
		} else {
			return authBackend.SecretKey, nil
		}
	})
	log.WithFields(log.Fields{
		"valid":  token.Valid,
		"string": token.Raw,
	}).Warn("token information")

	if err == nil && token.Valid && !authBackend.IsInBlacklist(req.Header.Get("Authorization")) {
		next(rw, req)
	} else {
{'fraction_non_alphanumeric': 0.13694407749889917, 'fraction_numerical': 0.0, 'mean_word_length': 5.311111111111111, 'pattern_counts': {'":': 4, '<': 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': '6248092', 'n_tokens_mistral': 780, 'n_tokens_neox': 775, 'n_words': 154}