<commit_before>package api

import (
	"encoding/base64"
	"log"
	"net/http"

	"github.com/ant0ine/go-json-rest/rest"
	"github.com/joshheinrichs/geosource/server/transactions"
	"github.com/joshheinrichs/geosource/server/types"
	"github.com/markbates/goth/gothic"
	"github.com/pborman/uuid"
)

func BeginAuth(w rest.ResponseWriter, req *rest.Request) {
	setProvider(req)
	gothic.BeginAuthHandler(w.(http.ResponseWriter), req.Request)
}

func CallbackAuth(w rest.ResponseWriter, req *rest.Request) {
	setProvider(req)
	gothUser, err := gothic.CompleteUserAuth(w.(http.ResponseWriter), req.Request)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	user, err := transactions.GetUserByEmail(gothUser.Email)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if user == nil {
		user = &types.User{
			Name:   gothUser.Name,
			Avatar: gothUser.AvatarURL,
			Email:  gothUser.Email,
			Id:     base64.RawURLEncoding.EncodeToString(uuid.NewRandom()),
		}
		err = transactions.AddUser(user)
		if err != nil {
			rest.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}
	session, err := gothic.Store.Get(req.Request, gothic.SessionName)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	session.Values["userid"] = user.Id
	err = session.Save(req.Request, w.(http.ResponseWriter))
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	http.Redirect(w.(http.ResponseWriter), req.Request, url := fmt.Sprintf("https://%s%s", mainConfig.Website.Url, mainConfig.Website.HttpsPort), http.StatusTemporaryRedirect)
}

func Logout(w rest.ResponseWriter, req *rest.Request) {
	session, err := gothic.Store.Get(req.Request, gothic.SessionName)
	if err != nil {
		log.Printf("logout attempted by user that was not logged in.\n")
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	session.Values["userid"] = ""
	err = session.Save(req.Request, w.(http.ResponseWriter))
	if err != nil {
		log.Printf("error saving session.\n")
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	http.Redirect(w.(http.ResponseWriter), req.Request, url := fmt.Sprintf("https://%s%s", mainConfig.Website.Url, mainConfig.Website.HttpsPort), http.StatusTemporaryRedirect)
}

// Adds the provider path parameter from the given rest quest as a query value
// to the request URL. Gothic requires the provider as a query value, so this
// fixes that incompatibility error
func setProvider(req *rest.Request) {
	v := req.Request.URL.Query()
	v.Set("provider", req.PathParam("provider"))
	req.Request.URL.RawQuery = v.Encode()
}<commit_msg>Fix compile error from previous commit Oops...<commit_after>package api

import (
	"encoding/base64"
	"fmt"
	"log"
	"net/http"

	"github.com/ant0ine/go-json-rest/rest"
	"github.com/joshheinrichs/geosource/server/transactions"
	"github.com/joshheinrichs/geosource/server/types"
	"github.com/markbates/goth/gothic"
	"github.com/pborman/uuid"
)

func BeginAuth(w rest.ResponseWriter, req *rest.Request) {
	setProvider(req)
	gothic.BeginAuthHandler(w.(http.ResponseWriter), req.Request)
}

func CallbackAuth(w rest.ResponseWriter, req *rest.Request) {
	setProvider(req)
	gothUser, err := gothic.CompleteUserAuth(w.(http.ResponseWriter), req.Request)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	user, err := transactions.GetUserByEmail(gothUser.Email)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if user == nil {
		user = &types.User{
			Name:   gothUser.Name,
			Avatar: gothUser.AvatarURL,
			Email:  gothUser.Email,
			Id:     base64.RawURLEncoding.EncodeToString(uuid.NewRandom()),
		}
		err = transactions.AddUser(user)
		if err != nil {
			rest.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}
	session, err := gothic.Store.Get(req.Request, gothic.SessionName)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	session.Values["userid"] = user.Id
	err = session.Save(req.Request, w.(http.ResponseWriter))
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	http.Redirect(w.(http.ResponseWriter), req.Request, fmt.Sprintf("https://%s%s", apiConfig.Website.Url, apiConfig.Website.HttpsPort), http.StatusTemporaryRedirect)
}

func Logout(w rest.ResponseWriter, req *rest.Request) {
	session, err := gothic.Store.Get(req.Request, gothic.SessionName)
	if err != nil {
		log.Printf("logout attempted by user that was not logged in.\n")
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	session.Values["userid"] = ""
	err = session.Save(req.Request, w.(http.ResponseWriter))
	if err != nil {
		log.Printf("error saving session.\n")
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	http.Redirect(w.(http.ResponseWriter), req.Request, fmt.Sprintf("https://%s%s", apiConfig.Website.Url, apiConfig.Website.HttpsPort), http.StatusTemporaryRedirect)
}

// Adds the provider path parameter from the given rest quest as a query value
// to the request URL. Gothic requires the provider as a query value, so this
// fixes that incompatibility error
func setProvider(req *rest.Request) {
	v := req.Request.URL.Query()
	v.Set("provider", req.PathParam("provider"))
	req.Request.URL.RawQuery = v.Encode()
}
{'fraction_non_alphanumeric': 0.1442484380742374, 'fraction_numerical': 0.0018375597206909224, 'mean_word_length': 4.62293388429752, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '1732385', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1892, 'n_words': 375}