<commit_before>package gitserver

import (
	"errors"
	"time"

	"net/http"

	"golang.org/x/net/context"
)

var ErrUserNotFound = errors.New("User not found")

type Session struct {
	Value   string
	Expires time.Time
}

//Perhaps this should be an interface instead?
type User struct {
	ID    string
	Email string
	//There are some concerns here about thread safety
	Sessions []Session
}

type UserStore interface {
	//TODO might not be needing Sessions
	UpdateUser(ctx context.Context, user *User) error
	LookupUser(ctx context.Context, id string) (*User, error)
	Sessions(ctx context.Context, id string) []Session
}

func AuthorizedUser(req *http.Request) (*User, error) {

	if loginHandler.logger == nil {
		panic("You must call gitserver.Setup(...) before gitserver.AuthorizedUser(...)")
	}

	ctx := loginHandler.contextFromRequest(req)

	loginHandler.logger.Debugf(ctx, "Serving logged in")

	/* Check if user was already logged in */
	sc, err := sessionCookieFromRequest(req)
	if err != nil && err != ErrSessionCookieNotFound {
		return nil, ErrUserNotFound
	}

	if err != ErrSessionCookieNotFound {
		user, _ := loginHandler.userStore.LookupUser(ctx, sc.UserID)
		if user != nil {
			for _, s := range user.Sessions {
				if s.Value == sc.SessionID && !isExpired(s) {
					return user, nil
				}
			}<commit_msg>Treats all errors the same when trying to get user from a request and returns ErrUserNotFound on all of them<commit_after>package gitserver

import (
	"errors"
	"time"

	"net/http"

	"golang.org/x/net/context"
)

var ErrUserNotFound = errors.New("User not found")

type Session struct {
	Value   string
	Expires time.Time
}

//Perhaps this should be an interface instead?
type User struct {
	ID    string
	Email string
	//There are some concerns here about thread safety
	Sessions []Session
}

type UserStore interface {
	//TODO might not be needing Sessions
	UpdateUser(ctx context.Context, user *User) error
	LookupUser(ctx context.Context, id string) (*User, error)
	Sessions(ctx context.Context, id string) []Session
}

func AuthorizedUser(req *http.Request) (*User, error) {

	if loginHandler.logger == nil {
		panic("You must call gitserver.Setup(...) before gitserver.AuthorizedUser(...)")
	}

	ctx := loginHandler.contextFromRequest(req)

	loginHandler.logger.Debugf(ctx, "Serving logged in")

	/* Check if user was already logged in */
	sc, err := sessionCookieFromRequest(req)
	if err != nil {
		return nil, ErrUserNotFound
	}

	if err != ErrSessionCookieNotFound {
		user, _ := loginHandler.userStore.LookupUser(ctx, sc.UserID)
		if user != nil {
			for _, s := range user.Sessions {
				if s.Value == sc.SessionID && !isExpired(s) {
					return user, nil
				}
			}
{'fraction_non_alphanumeric': 0.111358574610245, 'fraction_numerical': 0.0, 'mean_word_length': 3.917883211678832, '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': '26971', 'n_tokens_mistral': 897, 'n_tokens_neox': 842, 'n_words': 269}