<commit_before>package net

import (
	"fmt"
	"log"

	"github.com/ghthor/filu/auth"
)

type AuthenticatedUser struct {
	Username string
}

func AuthenticateFrom(conn Conn, stream auth.Stream) (AuthenticatedUser, error) {
readNextType:
	eType, err := conn.NextType()
	if err != nil {
		return AuthenticatedUser{}, err
	}

	switch eType {
	default:
		// TODO: Log error to universal error log
		log.Println("unexpected EncodeType:", eType)

		err := conn.Encode(ProtocolError(
			fmt.Sprintf("expected EncodeType(%v) got EncodeType(%v)", ET_USER_LOGIN_REQUEST, eType),
		))
		if err != nil {
			return AuthenticatedUser{}, err
		}

		goto readNextType

	case ET_USER_LOGIN_REQUEST:
	}

	var r UserLoginRequest
	err = conn.Decode(&r)
	if err != nil {
		return AuthenticatedUser{}, err
	}

	authReq := auth.NewRequest(r.Name, r.Password)
	stream.RequestAuthentication() <- authReq

	select {
	case user := <-authReq.CreatedUser:
		err = conn.Encode(UserCreateSuccess{user.Username})
		if err != nil {
			return AuthenticatedUser{}, err
		}

		return AuthenticatedUser{user.Username}, nil

	case user := <-authReq.AuthenticatedUser:
		err = conn.Encode(UserLoginSuccess{user.Username})
		if err != nil {
			return AuthenticatedUser{}, err
		}

		return AuthenticatedUser{user.Username}, nil

	case invalid := <-authReq.InvalidPassword:
		err := conn.Encode(UserLoginFailure{invalid.Username})
		if err != nil {
			return AuthenticatedUser{}, err
		}
	}

	goto readNextType
}<commit_msg>[filu/net] Remove internal looping during an authenticate request<commit_after>package net

import (
	"errors"
	"fmt"
	"log"

	"github.com/ghthor/filu/auth"
)

type AuthenticatedUser struct {
	Username string
}

var ErrInvalidLoginCredentials = errors.New("client provided invalid login credentials")

func AuthenticateFrom(conn Conn, stream auth.Stream) (AuthenticatedUser, error) {
	eType, err := conn.NextType()
	if err != nil {
		return AuthenticatedUser{}, err
	}

	switch eType {
	default:
		// TODO: Log error to universal error log
		log.Println("unexpected EncodeType:", eType)

		protoError := ProtocolError(
			fmt.Sprintf("expected EncodeType(%v) got EncodeType(%v)", ET_USER_LOGIN_REQUEST, eType),
		)

		err := conn.Encode(protoError)
		if err != nil {
			return AuthenticatedUser{}, err
		}

		return AuthenticatedUser{}, protoError

	case ET_USER_LOGIN_REQUEST:
	}

	var r UserLoginRequest
	err = conn.Decode(&r)
	if err != nil {
		return AuthenticatedUser{}, err
	}

	authReq := auth.NewRequest(r.Name, r.Password)
	stream.RequestAuthentication() <- authReq

	select {
	case user := <-authReq.CreatedUser:
		err = conn.Encode(UserCreateSuccess{user.Username})
		if err != nil {
			return AuthenticatedUser{}, err
		}

		return AuthenticatedUser{user.Username}, nil

	case user := <-authReq.AuthenticatedUser:
		err = conn.Encode(UserLoginSuccess{user.Username})
		if err != nil {
			return AuthenticatedUser{}, err
		}

		return AuthenticatedUser{user.Username}, nil

	case invalid := <-authReq.InvalidPassword:
		err := conn.Encode(UserLoginFailure{invalid.Username})
		if err != nil {
			return AuthenticatedUser{}, err
		}

		return AuthenticatedUser{}, ErrInvalidLoginCredentials
	}
}
{'fraction_non_alphanumeric': 0.117425431711146, 'fraction_numerical': 0.0, 'mean_word_length': 4.372681281618887, 'pattern_counts': {'":': 0, '<': 11, '<?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': '166998', 'n_tokens_mistral': 1129, 'n_tokens_neox': 1028, 'n_words': 252}