<commit_before>package main

import (
	"bytes"
	"crypto/md5"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"errors"
	"log"
	"net/http"
	"strings"
	"time"

	"github.com/gorilla/securecookie"

	"github.com/couchbaselabs/consolio/types"
)

const (
	BROWSERID_ENDPOINT = "https://verifier.login.persona.org/verify"
	AUTH_COOKIE        = "consolio"
)

var NotAUser = errors.New("not a user")

func getUser(email string) (consolio.User, error) {
	rv := consolio.User{}
	k := "u-" + email
	err := db.Get(k, &rv)
	if err == nil && rv.Type != "consolio.User" {
		return consolio.User{}, NotAUser
	}
	return rv, err
}

type browserIdData struct {
	Status   string
	Reason   string
	Email    string
	Audience string
	Expires  uint64
	Issuer   string
}

var secureCookie *securecookie.SecureCookie

func initSecureCookie(hashKey []byte) {
	secureCookie = securecookie.New(hashKey, nil)
}

func userFromCookie(cookie string) (consolio.User, error) {
	val := browserIdData{}
	err := secureCookie.Decode("consolio.User", cookie, &val)
	if err == nil {
		u, err := getUser(val.Email)
		if err != nil {
			u.Id = val.Email
		}
		return u, nil
	}
	return consolio.User{}, err
}

func whoami(r *http.Request) consolio.User {
	if cookie, err := r.Cookie(AUTH_COOKIE); err == nil {
		u, err := userFromCookie(cookie.Value)
		if err == nil {
			return u
		}
	}
	if ahdr := r.Header.Get("Authorization"); ahdr != "" {
		parts := strings.Split(ahdr, " ")
		if len(parts) < 2 {
			return consolio.User{}
		}
		decoded, err := base64.StdEncoding.DecodeString(parts[1])
		if err != nil {
			return consolio.User{}
		}
		userpass := strings.SplitN(string(decoded), ":", 2)

		user, err := getUser(userpass[0])
		if err != nil {
			return consolio.User{}
		}

		if user.AuthToken == userpass[1] {
			u, err := getUser(userpass[0])
			if err != nil {
				u.Id = userpass[0]
				u.Type = "consolio.User"
			}
			return u
		}
	}<commit_msg>Change embedded type name from consolio.User to User<commit_after>package main

import (
	"bytes"
	"crypto/md5"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"errors"
	"log"
	"net/http"
	"strings"
	"time"

	"github.com/gorilla/securecookie"

	"github.com/couchbaselabs/consolio/types"
)

const (
	BROWSERID_ENDPOINT = "https://verifier.login.persona.org/verify"
	AUTH_COOKIE        = "consolio"
)

var NotAUser = errors.New("not a user")

func getUser(email string) (consolio.User, error) {
	rv := consolio.User{}
	k := "u-" + email
	err := db.Get(k, &rv)
	if err == nil && rv.Type != "User" {
		return consolio.User{}, NotAUser
	}
	return rv, err
}

type browserIdData struct {
	Status   string
	Reason   string
	Email    string
	Audience string
	Expires  uint64
	Issuer   string
}

var secureCookie *securecookie.SecureCookie

func initSecureCookie(hashKey []byte) {
	secureCookie = securecookie.New(hashKey, nil)
}

func userFromCookie(cookie string) (consolio.User, error) {
	val := browserIdData{}
	err := secureCookie.Decode("consolio.User", cookie, &val)
	if err == nil {
		u, err := getUser(val.Email)
		if err != nil {
			u.Id = val.Email
		}
		return u, nil
	}
	return consolio.User{}, err
}

func whoami(r *http.Request) consolio.User {
	if cookie, err := r.Cookie(AUTH_COOKIE); err == nil {
		u, err := userFromCookie(cookie.Value)
		if err == nil {
			return u
		}
	}
	if ahdr := r.Header.Get("Authorization"); ahdr != "" {
		parts := strings.Split(ahdr, " ")
		if len(parts) < 2 {
			return consolio.User{}
		}
		decoded, err := base64.StdEncoding.DecodeString(parts[1])
		if err != nil {
			return consolio.User{}
		}
		userpass := strings.SplitN(string(decoded), ":", 2)

		user, err := getUser(userpass[0])
		if err != nil {
			return consolio.User{}
		}

		if user.AuthToken == userpass[1] {
			u, err := getUser(userpass[0])
			if err != nil {
				u.Id = userpass[0]
				u.Type = "User"
			}
			return u
		}
	}
{'fraction_non_alphanumeric': 0.1513892495455726, 'fraction_numerical': 0.007270838743183588, 'mean_word_length': 3.442906574394464, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3235718', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1492, 'n_words': 335}