<commit_before>package auth

import (
	"encoding/json"
	"log"
	"net/http"
	"os"

	"github.com/boltdb/bolt"
	"github.com/cswank/quimby/controllers"
	"github.com/cswank/quimby/models"
	"github.com/gorilla/mux"
	"github.com/gorilla/securecookie"
)

type controller func(args *controllers.Args) error

var (
	DB           *bolt.DB
	hashKey      = []byte(os.Getenv("QUIMBY_HASH_KEY"))
	blockKey     = []byte(os.Getenv("QUIMBY_BLOCK_KEY"))
	SecureCookie = securecookie.New(hashKey, blockKey)
)

func CheckAuth(w http.ResponseWriter, r *http.Request, ctrl controller, acl ACL) {
	user, err := getUserFromCookie(r)
	if err != nil {
		http.Error(w, "Not Authorized", http.StatusUnauthorized)
		return
	}
	vars := mux.Vars(r)
	args := &controllers.Args{
		W:    w,
		R:    r,
		User: user,
		Vars: vars,
		DB:   DB,
	}
	if !acl(args) {
		http.Error(w, "Not Authorized", http.StatusUnauthorized)
		return
	}

	g := &models.Gadget{
		DB:   DB,
		Name: vars["name"],
	}

	args.Gadget = g
	err = ctrl(args)
	if err != nil {
		log.Println(args.R.URL.Path, err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func getUserFromCookie(r *http.Request) (*models.User, error) {
	user := &models.User{
		DB: DB,
	}
	cookie, err := r.Cookie("quimby")
	if err != nil {
		return nil, err
	}
	var m map[string]string
	err = SecureCookie.Decode("quimby", cookie.Value, &m)
	if err != nil {
		return nil, err
	}
	user.Username = m["user"]
	return user, user.Fetch()
}

func Logout(w http.ResponseWriter, r *http.Request) {
	cookie := &http.Cookie{
		Name:   "quimby",
		Value:  "",
		Path:   "/",
		MaxAge: -1,
	}
	http.SetCookie(w, cookie)
}

func Login(w http.ResponseWriter, r *http.Request) {
	user := &models.User{
		DB: DB,<commit_msg>get rid of hashed pw<commit_after>package auth

import (
	"encoding/json"
	"log"
	"net/http"
	"os"

	"github.com/boltdb/bolt"
	"github.com/cswank/quimby/controllers"
	"github.com/cswank/quimby/models"
	"github.com/gorilla/mux"
	"github.com/gorilla/securecookie"
)

type controller func(args *controllers.Args) error

var (
	DB           *bolt.DB
	hashKey      = []byte(os.Getenv("QUIMBY_HASH_KEY"))
	blockKey     = []byte(os.Getenv("QUIMBY_BLOCK_KEY"))
	SecureCookie = securecookie.New(hashKey, blockKey)
)

func CheckAuth(w http.ResponseWriter, r *http.Request, ctrl controller, acl ACL) {
	user, err := getUserFromCookie(r)
	if err != nil {
		http.Error(w, "Not Authorized", http.StatusUnauthorized)
		return
	}
	vars := mux.Vars(r)
	args := &controllers.Args{
		W:    w,
		R:    r,
		User: user,
		Vars: vars,
		DB:   DB,
	}
	if !acl(args) {
		http.Error(w, "Not Authorized", http.StatusUnauthorized)
		return
	}

	g := &models.Gadget{
		DB:   DB,
		Name: vars["name"],
	}

	args.Gadget = g
	err = ctrl(args)
	if err != nil {
		log.Println(args.R.URL.Path, err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func getUserFromCookie(r *http.Request) (*models.User, error) {
	user := &models.User{
		DB: DB,
	}
	cookie, err := r.Cookie("quimby")
	if err != nil {
		return nil, err
	}
	var m map[string]string
	err = SecureCookie.Decode("quimby", cookie.Value, &m)
	if err != nil {
		return nil, err
	}
	user.Username = m["user"]

	err = user.Fetch()
	user.HashedPassword = []byte{}
	return user, err
}

func Logout(w http.ResponseWriter, r *http.Request) {
	cookie := &http.Cookie{
		Name:   "quimby",
		Value:  "",
		Path:   "/",
		MaxAge: -1,
	}
	http.SetCookie(w, cookie)
}

func Login(w http.ResponseWriter, r *http.Request) {
	user := &models.User{
		DB: DB,
{'fraction_non_alphanumeric': 0.16235427921524026, 'fraction_numerical': 0.0005686664771111743, 'mean_word_length': 3.5747724317295186, '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': '2298565', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1434, 'n_words': 299}