package agent

import (
	"net/http"
	"strings"
)

type keyChecker struct {}

func (a *keyChecker) check(c *client, r *http.Request) bool {
	if c.authenticated {
		return true
	}
	var apikey string
	if keys, ok := r.Header["X-Api-Key"]; ok {
		apikey = keys[0]
	} else {
		i := strings.Index(r.RequestURI, "api_key=")
		if i > 0 {
			apikey = r.RequestURI[i+len("api_key="):]
		}
	}
	if len(apikey) > 0 && strings.HasPrefix(apikey, "simpletest2025_") {
		c.setApiKey(apikey)
		return true
	}
	return false
}
