<commit_before>package auth

import (
	"flag"
	"fmt"
	"net/http"
	"strings"

	"github.com/ViBiOh/httputils"
)

const authorizationHeader = `Authorization`
const forwardedForHeader = `X-Forwarded-For`

// User of the app
type User struct {
	Username string
	profiles string
}

// HasProfile check if given User has given profile
func (user *User) HasProfile(profile string) bool {
	return strings.Contains(user.profiles, profile)
}

// NewUser creates new user with username and profiles
func NewUser(username string, profiles string) *User {
	return &User{username, profiles}
}

var users map[string]*User

var (
	authURL       = flag.String(`authUrl`, ``, `URL of auth service`)
	usersProfiles = flag.String(`users`, ``, `List of allowed users and profiles (e.g. user:profile1,profile2|user2:profile3`)
)

// Init auth
func Init() error {
	LoadUsersProfiles(*usersProfiles)

	return nil
}

// LoadUsersProfiles parses users ands profiles from given string
func LoadUsersProfiles(usersAndProfiles string) {
	users = make(map[string]*User, 0)

	if usersAndProfiles == `` {
		return
	}

	usersList := strings.Split(usersAndProfiles, `|`)
	for _, user := range usersList {
		username := user
		profiles := ``

		sepIndex := strings.Index(user, `:`)
		if sepIndex != -1 {
			username = user[:sepIndex]
			profiles = user[sepIndex+1:]
		}

		users[strings.ToLower(username)] = NewUser(username, profiles)
	}
}

// IsAuthenticated check if request has correct headers for authentification
func IsAuthenticated(r *http.Request) (*User, error) {
	return IsAuthenticatedByAuth(r.Header.Get(authorizationHeader), r.Header.Get(forwardedForHeader))
}

// IsAuthenticatedByAuth check if authorization is correct
func IsAuthenticatedByAuth(authContent, remoteIP string) (*User, error) {
	headers := map[string]string{
		authorizationHeader: authContent,
		forwardedForHeader:  remoteIP,
	}

	username, err := httputils.GetBody(*authURL+`/user`, headers, true)
	if err != nil {
		return nil, fmt.Errorf(`Error while getting username: %v`, err)
	}

	if user, ok := users[strings.ToLower(string(username))]; ok {
		return user, nil
	}

	return nil, fmt.Errorf(`[%s] Not allowed to use app`, username)
}<commit_msg>Adding lof for auth<commit_after>package auth

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"strings"

	"github.com/ViBiOh/httputils"
)

const authorizationHeader = `Authorization`
const forwardedForHeader = `X-Forwarded-For`

// User of the app
type User struct {
	Username string
	profiles string
}

// HasProfile check if given User has given profile
func (user *User) HasProfile(profile string) bool {
	return strings.Contains(user.profiles, profile)
}

// NewUser creates new user with username and profiles
func NewUser(username string, profiles string) *User {
	return &User{username, profiles}
}

var users map[string]*User

var (
	authURL       = flag.String(`authUrl`, ``, `URL of auth service`)
	usersProfiles = flag.String(`users`, ``, `List of allowed users and profiles (e.g. user:profile1,profile2|user2:profile3`)
)

// Init auth
func Init() error {
	LoadUsersProfiles(*usersProfiles)

	return nil
}

// LoadUsersProfiles parses users ands profiles from given string
func LoadUsersProfiles(usersAndProfiles string) {
	users = make(map[string]*User, 0)

	if usersAndProfiles == `` {
		return
	}

	usersList := strings.Split(usersAndProfiles, `|`)
	for _, user := range usersList {
		username := user
		profiles := ``

		sepIndex := strings.Index(user, `:`)
		if sepIndex != -1 {
			username = user[:sepIndex]
			profiles = user[sepIndex+1:]
		}

		users[strings.ToLower(username)] = NewUser(username, profiles)
	}
}

// IsAuthenticated check if request has correct headers for authentification
func IsAuthenticated(r *http.Request) (*User, error) {
	log.Printf(`IsAuthenticated %v, %v`, r.Header.Get(authorizationHeader), r.Header.Get(forwardedForHeader))
	return IsAuthenticatedByAuth(r.Header.Get(authorizationHeader), r.Header.Get(forwardedForHeader))
}

// IsAuthenticatedByAuth check if authorization is correct
func IsAuthenticatedByAuth(authContent, remoteIP string) (*User, error) {
	headers := map[string]string{
		authorizationHeader: authContent,
		forwardedForHeader:  remoteIP,
	}

	log.Printf(`IsAuthenticatedByAuth %v, %v, %v`, authContent, remoteIP, headers)

	username, err := httputils.GetBody(*authURL+`/user`, headers, true)
	if err != nil {
		return nil, fmt.Errorf(`Error while getting username: %v`, err)
	}

	if user, ok := users[strings.ToLower(string(username))]; ok {
		return user, nil
	}

	return nil, fmt.Errorf(`[%s] Not allowed to use app`, username)
}
{'fraction_non_alphanumeric': 0.1233949945593036, 'fraction_numerical': 0.003046789989118607, 'mean_word_length': 4.884763124199744, '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': '4571460', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1474, 'n_words': 402}