<commit_before>package main

import (
	"fmt"
	"net/http"
	"github.com/dgrijalva/jwt-go"
	"time"
	"github.com/gorilla/mux"
	"encoding/json"
	"strings"
	"io/ioutil"
	"crypto/rsa"
)

var (
	verifyKey  *rsa.PublicKey
	signKey    *rsa.PrivateKey
)

func loadKeys(subConfig *SubConfig) {

	signBytes, err := ioutil.ReadFile(subConfig.PrivateKey)
	fatal(err)

	signKey, err = jwt.ParseRSAPrivateKeyFromPEM(signBytes)
	fatal(err)

	verifyBytes, err := ioutil.ReadFile(subConfig.PublicKey)
	fatal(err)

	verifyKey, err = jwt.ParseRSAPublicKeyFromPEM(verifyBytes)
	fatal(err)
}

func registerAuth(r *mux.Router, subConfig *SubConfig) {
	loadKeys(subConfig)

	r.Handle("/authenticate", GetTokenHandler).Methods("POST")
}

/* Set up a global string for our secret */
const (
	ValidUser = "foo"
	ValidPass = "<PASSWORD>"
)

type User struct{
	Id      string
	Password string
}

func createToken (user User) (string, error) {
	/* Create the token */
	token := jwt.New(jwt.GetSigningMethod("RS256"))

	/* Set token claims */
	claims := make(jwt.MapClaims)
	claims["admin"] = true
	claims["username"] = user.Id
	claims["exp"] = time.Now().Add(time.Hour * 24).Unix()

	/* Sign the token with our secret */
	return token.SignedString(signKey)
}

/* Handlers */
var GetTokenHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
	var user User
	if r.Body == nil {
		http.Error(w, "Please send a request body", 400)
		return
	}
	err := json.NewDecoder(r.Body).Decode(&user)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	if user.Id == ValidUser && user.Password == <PASSWORD> {

		tokenString, err := createToken(user)
		/* Finally, write the token to the browser window */
		if err != nil {
			fmt.Println("User or Password is not valid:", tokenString)
			w.WriteHeader(http.StatusUnauthorized)
			w.Write([]byte("Unauthorized"))
		} else {

			w.Write([]byte(tokenString))
		}
	}  else {
		fmt.Println(err)
		fmt.Println("User or Password is not valid:")
		w.WriteHeader(http.StatusUnauthorized)
		w.Write([]byte("Unauthorized"))
	}
	w.Header().Set("Access-Control-Allow-Origin", "*")
})


func authMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		reqToken := r.Header.Get("Authorization")
		splitToken := strings.Split(reqToken, "Bearer")
		reqToken = splitToken[1]

		token, err := jwt.Parse(reqToken, func(token *jwt.Token) (interface{}, error) {
			// Don't forget to validate the alg is what you expect:
			return verifyKey, nil
		})

		if err == nil && token.Valid {
			next.ServeHTTP(w, r)
		} else {
			fmt.Println(err)
			fmt.Println("Token is not valid:")
			w.WriteHeader(http.StatusUnauthorized)
			w.Write([]byte("Unauthorized"))
		}
	})
}

<commit_msg>add other headers<commit_after>package main

import (
	"fmt"
	"net/http"
	"github.com/dgrijalva/jwt-go"
	"time"
	"github.com/gorilla/mux"
	"encoding/json"
	"strings"
	"io/ioutil"
	"crypto/rsa"
)

var (
	verifyKey  *rsa.PublicKey
	signKey    *rsa.PrivateKey
)

func loadKeys(subConfig *SubConfig) {

	signBytes, err := ioutil.ReadFile(subConfig.PrivateKey)
	fatal(err)

	signKey, err = jwt.ParseRSAPrivateKeyFromPEM(signBytes)
	fatal(err)

	verifyBytes, err := ioutil.ReadFile(subConfig.PublicKey)
	fatal(err)

	verifyKey, err = jwt.ParseRSAPublicKeyFromPEM(verifyBytes)
	fatal(err)
}

func registerAuth(r *mux.Router, subConfig *SubConfig) {
	loadKeys(subConfig)

	r.Handle("/authenticate", GetTokenHandler).Methods("POST")
}

/* Set up a global string for our secret */
const (
	ValidUser = "foo"
	ValidPass = "<PASSWORD>"
)

type User struct{
	Id      string
	Password string
}

func createToken (user User) (string, error) {
	/* Create the token */
	token := jwt.New(jwt.GetSigningMethod("RS256"))

	/* Set token claims */
	claims := make(jwt.MapClaims)
	claims["admin"] = true
	claims["username"] = user.Id
	claims["exp"] = time.Now().Add(time.Hour * 24).Unix()

	/* Sign the token with our secret */
	return token.SignedString(signKey)
}

/* Handlers */
var GetTokenHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
	var user User
	if r.Body == nil {
		http.Error(w, "Please send a request body", 400)
		return
	}
	err := json.NewDecoder(r.Body).Decode(&user)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	if user.Id == ValidUser && user.Password == <PASSWORD> {

		tokenString, err := createToken(user)
		/* Finally, write the token to the browser window */
		if err != nil {
			fmt.Println("User or Password is not valid:", tokenString)
			w.WriteHeader(http.StatusUnauthorized)
			w.Write([]byte("Unauthorized"))
		} else {

			w.Write([]byte(tokenString))
		}
	}  else {
		fmt.Println(err)
		fmt.Println("User or Password is not valid:")
		w.WriteHeader(http.StatusUnauthorized)
		w.Write([]byte("Unauthorized"))
	}
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
	w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token")
	w.Header().Set("Access-Control-Allow-Credentials", "true")
})


func authMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		reqToken := r.Header.Get("Authorization")
		splitToken := strings.Split(reqToken, "Bearer")
		reqToken = splitToken[1]

		token, err := jwt.Parse(reqToken, func(token *jwt.Token) (interface{}, error) {
			// Don't forget to validate the alg is what you expect:
			return verifyKey, nil
		})

		if err == nil && token.Valid {
			next.ServeHTTP(w, r)
		} else {
			fmt.Println(err)
			fmt.Println("Token is not valid:")
			w.WriteHeader(http.StatusUnauthorized)
			w.Write([]byte("Unauthorized"))
		}
	})
}


{'fraction_non_alphanumeric': 0.1539528432732316, 'fraction_numerical': 0.004160887656033287, 'mean_word_length': 4.173991031390135, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4051612', 'n_tokens_mistral': 2213, 'n_tokens_neox': 2049, 'n_words': 447}