Every key needed an operator with a shell. This adds the login system that made that necessary, which is the thing I argued against building until it was asked for — so it is built as if it will be attacked, because a public login form is. Signing up produces an account that can do nothing. Approval is what makes it work and stays on the CLI: having a shell on the control plane is what makes someone an administrator, and that is a smaller thing to get right than roles and an admin UI that mints credentials. The details that matter: Passwords are argon2id, where a slow hash is correct — the input is chosen by a human and has a dictionary, which is the opposite of an API key. Both kinds of authentication now live in this codebase and they are in separate packages so neither set of assumptions leaks into the other. Length is the only rule: composition rules push people towards Password1! and cost more entropy than they buy. Signup never says whether an address already has an account, and sign-in answers the same way for a wrong password and a missing account — verifying against a dummy hash when there is no user, so a stopwatch cannot enumerate accounts. Account state is checked after the password, not before, so guessing an address reveals nothing about it. Guessing is bounded per address rather than per IP: an attacker has many addresses to come from and one to guess against, and limiting by IP locks out an office behind one NAT. Sessions are server-side, so signing out and suspending take effect immediately rather than whenever a token expires; suspension is checked in the same query that resolves the session, because a second round trip is a check somebody eventually forgets. Every state-changing form carries a CSRF token verified against a hash in the session — a cookie authenticates the browser, not the page that asked. Key ownership is enforced in the UPDATE rather than by reading first, so a guessed key id from another account changes nothing. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
8d35faf6f8
commit
216fc9bb03
22 files changed
+2263
-39
No files matched your search
@@ -0,0 +1,393 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base32"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/account"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
// SessionCookie is the name of the cookie a signed-in browser carries.
|
||||
const SessionCookie = "llmgw_session"
|
||||
|
||||
// Accounts is what the pages need from the account service.
|
||||
type Accounts interface {
|
||||
SignUp(ctx context.Context, email, name, password string) error
|
||||
SignIn(ctx context.Context, email, password string) (token, csrf string, err error)
|
||||
Resolve(ctx context.Context, token string) (store.Session, store.User, error)
|
||||
SignOut(ctx context.Context, token string) error
|
||||
CheckCSRF(sess store.Session, submitted string) bool
|
||||
SignupOpen() bool
|
||||
}
|
||||
|
||||
// KeyOwner is the store half of the profile page.
|
||||
type KeyOwner interface {
|
||||
KeysForUser(ctx context.Context, userID string) ([]store.Key, error)
|
||||
CreateUserKey(ctx context.Context, userID string, k store.Key) error
|
||||
RevokeUserKey(ctx context.Context, userID, keyID string, at time.Time) error
|
||||
Resolve(ctx context.Context, keyID string) (store.Effective, error)
|
||||
LimitsFor(ctx context.Context, scope store.Scope, id string) (store.Limits, error)
|
||||
}
|
||||
|
||||
// AccountPages serves signup, sign-in and the profile.
|
||||
type AccountPages struct {
|
||||
accounts Accounts
|
||||
keys KeyOwner
|
||||
version string
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// NewAccountPages builds the handlers.
|
||||
func NewAccountPages(a Accounts, k KeyOwner, version string, log *slog.Logger) *AccountPages {
|
||||
if log == nil {
|
||||
log = slog.Default()
|
||||
}
|
||||
return &AccountPages{accounts: a, keys: k, version: version, log: log}
|
||||
}
|
||||
|
||||
// Register mounts the pages. They are all browser surfaces and none of them
|
||||
// takes an API key, so the caller must exempt these paths from key
|
||||
// authentication.
|
||||
func (p *AccountPages) Register(mux *http.ServeMux) {
|
||||
if p.accounts.SignupOpen() {
|
||||
mux.HandleFunc("GET /signup", p.signupForm)
|
||||
mux.HandleFunc("POST /signup", p.signup)
|
||||
}
|
||||
mux.HandleFunc("GET /login", p.loginForm)
|
||||
mux.HandleFunc("POST /login", p.login)
|
||||
mux.HandleFunc("POST /logout", p.logout)
|
||||
mux.HandleFunc("GET /profile", p.profile)
|
||||
mux.HandleFunc("POST /profile/keys", p.createKey)
|
||||
mux.HandleFunc("POST /profile/keys/{id}/revoke", p.revokeKey)
|
||||
}
|
||||
|
||||
// --- signup ---
|
||||
|
||||
type signupData struct {
|
||||
Email, Name, Error string
|
||||
Done bool
|
||||
MinPassword int
|
||||
Version string
|
||||
}
|
||||
|
||||
func (p *AccountPages) signupForm(w http.ResponseWriter, _ *http.Request) {
|
||||
noStore(w)
|
||||
render(w, "signup", signupData{MinPassword: account.MinPasswordLen, Version: p.version})
|
||||
}
|
||||
|
||||
func (p *AccountPages) signup(w http.ResponseWriter, r *http.Request) {
|
||||
noStore(w)
|
||||
d := signupData{
|
||||
Email: strings.TrimSpace(r.FormValue("email")),
|
||||
Name: strings.TrimSpace(r.FormValue("name")),
|
||||
MinPassword: account.MinPasswordLen,
|
||||
Version: p.version,
|
||||
}
|
||||
|
||||
err := p.accounts.SignUp(r.Context(), d.Email, d.Name, r.FormValue("password"))
|
||||
switch {
|
||||
case err == nil:
|
||||
// The same page whether or not the address was already taken. Anything
|
||||
// else is an account enumerator that anyone can run.
|
||||
d.Done = true
|
||||
case errors.Is(err, account.ErrWeakPassword), errors.Is(err, account.ErrInvalidEmail):
|
||||
d.Error = err.Error()
|
||||
default:
|
||||
p.log.Error("signup failed", "err", err)
|
||||
d.Error = "something went wrong; try again"
|
||||
}
|
||||
render(w, "signup", d)
|
||||
}
|
||||
|
||||
// --- sign in and out ---
|
||||
|
||||
type loginData struct {
|
||||
Email, Error string
|
||||
SignupOpen bool
|
||||
Version string
|
||||
}
|
||||
|
||||
func (p *AccountPages) loginForm(w http.ResponseWriter, _ *http.Request) {
|
||||
noStore(w)
|
||||
render(w, "login", loginData{SignupOpen: p.accounts.SignupOpen(), Version: p.version})
|
||||
}
|
||||
|
||||
func (p *AccountPages) login(w http.ResponseWriter, r *http.Request) {
|
||||
noStore(w)
|
||||
email := strings.TrimSpace(r.FormValue("email"))
|
||||
|
||||
token, csrf, err := p.accounts.SignIn(r.Context(), email, r.FormValue("password"))
|
||||
if err != nil {
|
||||
d := loginData{Email: email, SignupOpen: p.accounts.SignupOpen(), Version: p.version}
|
||||
switch {
|
||||
case errors.Is(err, account.ErrBadCredentials),
|
||||
errors.Is(err, account.ErrNotApproved),
|
||||
errors.Is(err, account.ErrSuspended),
|
||||
errors.Is(err, account.ErrTooManyTries):
|
||||
d.Error = err.Error()
|
||||
default:
|
||||
p.log.Error("sign-in failed", "err", err)
|
||||
d.Error = "something went wrong; try again"
|
||||
}
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
render(w, "login", d)
|
||||
return
|
||||
}
|
||||
|
||||
setSessionCookie(w, r, token)
|
||||
setCSRFCookie(w, r, csrf)
|
||||
http.Redirect(w, r, "/profile", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (p *AccountPages) logout(w http.ResponseWriter, r *http.Request) {
|
||||
noStore(w)
|
||||
// Even signing out is a state change, so it needs its token: otherwise any
|
||||
// page can sign a visitor out of this one.
|
||||
sess, _, ok := p.session(r)
|
||||
if ok && !p.accounts.CheckCSRF(sess, r.FormValue("csrf")) {
|
||||
http.Error(w, "bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if c, err := r.Cookie(SessionCookie); err == nil {
|
||||
if err := p.accounts.SignOut(r.Context(), c.Value); err != nil {
|
||||
p.log.Warn("could not close a session", "err", err)
|
||||
}
|
||||
}
|
||||
clearSessionCookie(w, r)
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// --- profile ---
|
||||
|
||||
type limitRow struct{ Name, Value string }
|
||||
|
||||
type profileData struct {
|
||||
User store.User
|
||||
Keys []store.Key
|
||||
Limits []limitRow
|
||||
NewKey string
|
||||
Error string
|
||||
CSRF string
|
||||
BaseURL string
|
||||
Version string
|
||||
Now string
|
||||
}
|
||||
|
||||
func (p *AccountPages) profile(w http.ResponseWriter, r *http.Request) {
|
||||
noStore(w)
|
||||
sess, user, ok := p.session(r)
|
||||
if !ok {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
p.renderProfile(w, r, sess, user, "", "")
|
||||
}
|
||||
|
||||
func (p *AccountPages) createKey(w http.ResponseWriter, r *http.Request) {
|
||||
noStore(w)
|
||||
sess, user, ok := p.session(r)
|
||||
if !ok {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
if !p.accounts.CheckCSRF(sess, r.FormValue("csrf")) {
|
||||
http.Error(w, "bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
name := strings.TrimSpace(r.FormValue("name"))
|
||||
if name == "" {
|
||||
name = "untitled"
|
||||
}
|
||||
secret, k, err := store.NewKey(randomKeyID(), name, user.ProjectID)
|
||||
if err != nil {
|
||||
p.log.Error("could not mint a key", "err", err)
|
||||
p.renderProfile(w, r, sess, user, "", "something went wrong; try again")
|
||||
return
|
||||
}
|
||||
k.CreatedAt = time.Now()
|
||||
if err := p.keys.CreateUserKey(r.Context(), user.ID, k); err != nil {
|
||||
p.log.Error("could not store a key", "err", err)
|
||||
p.renderProfile(w, r, sess, user, "", "something went wrong; try again")
|
||||
return
|
||||
}
|
||||
p.log.Info("a user created a key", "user", user.ID, "key", k.ID)
|
||||
|
||||
// Shown on this response rather than after a redirect: a redirect would
|
||||
// have to carry the key in a URL or a flash, and both put it somewhere it
|
||||
// does not belong.
|
||||
p.renderProfile(w, r, sess, user, secret, "")
|
||||
}
|
||||
|
||||
func (p *AccountPages) revokeKey(w http.ResponseWriter, r *http.Request) {
|
||||
noStore(w)
|
||||
sess, user, ok := p.session(r)
|
||||
if !ok {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
if !p.accounts.CheckCSRF(sess, r.FormValue("csrf")) {
|
||||
http.Error(w, "bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Ownership is enforced in the statement, so a guessed key id from another
|
||||
// account changes nothing.
|
||||
err := p.keys.RevokeUserKey(r.Context(), user.ID, r.PathValue("id"), time.Now())
|
||||
if err != nil && !errors.Is(err, store.ErrNotFound) {
|
||||
p.log.Error("could not revoke a key", "err", err)
|
||||
}
|
||||
http.Redirect(w, r, "/profile", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (p *AccountPages) renderProfile(w http.ResponseWriter, r *http.Request,
|
||||
sess store.Session, user store.User, newKey, errMsg string,
|
||||
) {
|
||||
d := profileData{
|
||||
User: user, NewKey: newKey, Error: errMsg,
|
||||
BaseURL: baseURL(r),
|
||||
Version: p.version,
|
||||
Now: time.Now().UTC().Format(time.RFC3339),
|
||||
}
|
||||
// The form token comes from the cookie, never from whatever the request
|
||||
// happened to carry: echoing a submitted value back into the next form
|
||||
// would let an attacker choose both halves of the comparison.
|
||||
d.CSRF = csrfFor(r)
|
||||
_ = sess
|
||||
|
||||
keys, err := p.keys.KeysForUser(r.Context(), user.ID)
|
||||
if err != nil {
|
||||
p.log.Error("could not list keys", "err", err)
|
||||
}
|
||||
d.Keys = keys
|
||||
d.Limits = p.limitsFor(r.Context(), user)
|
||||
render(w, "profile", d)
|
||||
}
|
||||
|
||||
// limitsFor renders the limits a person's keys inherit from their project.
|
||||
//
|
||||
// From the project rather than from a key, because a person with no keys yet
|
||||
// still wants to know what they are getting.
|
||||
func (p *AccountPages) limitsFor(ctx context.Context, user store.User) []limitRow {
|
||||
l, err := p.keys.LimitsFor(ctx, store.ScopeProject, user.ProjectID)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var out []limitRow
|
||||
if l.RPM != nil {
|
||||
out = append(out, limitRow{"Requests per minute", itoa(*l.RPM)})
|
||||
}
|
||||
if l.TPM != nil {
|
||||
out = append(out, limitRow{"Tokens per minute", itoa(*l.TPM)})
|
||||
}
|
||||
if l.Budget != nil {
|
||||
out = append(out, limitRow{"Budget", units(*l.Budget) + " per " + string(l.BudgetPeriod)})
|
||||
}
|
||||
if l.AllowedModels != nil {
|
||||
if len(l.AllowedModels) == 0 {
|
||||
out = append(out, limitRow{"Models", "none"})
|
||||
} else {
|
||||
out = append(out, limitRow{"Models", strings.Join(l.AllowedModels, ", ")})
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// --- helpers ---
|
||||
|
||||
func (p *AccountPages) session(r *http.Request) (store.Session, store.User, bool) {
|
||||
c, err := r.Cookie(SessionCookie)
|
||||
if err != nil {
|
||||
return store.Session{}, store.User{}, false
|
||||
}
|
||||
sess, user, err := p.accounts.Resolve(r.Context(), c.Value)
|
||||
if err != nil {
|
||||
return store.Session{}, store.User{}, false
|
||||
}
|
||||
return sess, user, true
|
||||
}
|
||||
|
||||
// csrfFor returns the token a form in this session must carry.
|
||||
//
|
||||
// The session stores only a hash, so the value itself has to travel with the
|
||||
// browser. It rides the same cookie jar as the session under its own name,
|
||||
// which is the double-submit pattern: an attacker's page can make the browser
|
||||
// send our cookies but cannot read them to put one in a form field.
|
||||
func csrfFor(r *http.Request) string {
|
||||
if c, err := r.Cookie(csrfCookie); err == nil {
|
||||
return c.Value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func setCSRFCookie(w http.ResponseWriter, r *http.Request, csrf string) {
|
||||
// HttpOnly as well: the server renders it into forms, so no script needs to
|
||||
// read it, and the attack this defends against works by making the browser
|
||||
// send cookies it cannot read.
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: csrfCookie, Value: csrf, Path: "/",
|
||||
HttpOnly: true, Secure: isHTTPS(r), SameSite: http.SameSiteLaxMode,
|
||||
MaxAge: int(store.SessionTTL.Seconds()),
|
||||
})
|
||||
}
|
||||
|
||||
func randomKeyID() string {
|
||||
var buf [10]byte
|
||||
_, _ = rand.Read(buf[:])
|
||||
return "key-" + strings.ToLower(
|
||||
base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(buf[:]))
|
||||
}
|
||||
|
||||
func itoa(v int) string { return strconv.Itoa(v) }
|
||||
|
||||
func units(micros int64) string {
|
||||
return strconv.FormatFloat(float64(micros)/1e6, 'f', 2, 64)
|
||||
}
|
||||
|
||||
const csrfCookie = "llmgw_csrf"
|
||||
|
||||
func setSessionCookie(w http.ResponseWriter, r *http.Request, token string) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: SessionCookie, Value: token, Path: "/",
|
||||
HttpOnly: true, Secure: isHTTPS(r), SameSite: http.SameSiteLaxMode,
|
||||
MaxAge: int(store.SessionTTL.Seconds()),
|
||||
})
|
||||
}
|
||||
|
||||
func clearSessionCookie(w http.ResponseWriter, r *http.Request) {
|
||||
for _, name := range []string{SessionCookie, csrfCookie} {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: name, Value: "", Path: "/",
|
||||
HttpOnly: name == SessionCookie, Secure: isHTTPS(r),
|
||||
SameSite: http.SameSiteLaxMode, MaxAge: -1,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// isHTTPS decides whether to mark cookies Secure.
|
||||
//
|
||||
// Behind a reverse proxy this process sees plain HTTP, so the forwarded header
|
||||
// is the only thing that knows. Getting it wrong in the safe direction — marking
|
||||
// Secure when the connection is plain — would break sign-in over http://, which
|
||||
// is why it is not simply hardcoded.
|
||||
func isHTTPS(r *http.Request) bool {
|
||||
if r.TLS != nil {
|
||||
return true
|
||||
}
|
||||
return strings.EqualFold(firstValue(r.Header.Get("X-Forwarded-Proto")), "https")
|
||||
}
|
||||
|
||||
func noStore(w http.ResponseWriter) {
|
||||
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, private")
|
||||
w.Header().Set("Referrer-Policy", "no-referrer")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("X-Frame-Options", "DENY")
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
type LandingData struct {
|
||||
BaseURL string
|
||||
AuthEnabled bool
|
||||
SignupOpen bool
|
||||
Contact string
|
||||
ContactHref string
|
||||
Notice string
|
||||
@@ -27,7 +28,7 @@ type LandingData struct {
|
||||
//
|
||||
// It lists no models and no cluster state. Everything that says what is here
|
||||
// stays behind the key, including /v1/models.
|
||||
func Landing(version string, authEnabled bool, contact, notice string) http.HandlerFunc {
|
||||
func Landing(version string, authEnabled, signupOpen bool, contact, notice string) http.HandlerFunc {
|
||||
text, href := contactLink(contact)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Only the root: a catch-all would turn every mistyped path into this
|
||||
@@ -39,6 +40,7 @@ func Landing(version string, authEnabled bool, contact, notice string) http.Hand
|
||||
render(w, "landing", LandingData{
|
||||
BaseURL: baseURL(r),
|
||||
AuthEnabled: authEnabled,
|
||||
SignupOpen: signupOpen,
|
||||
Contact: text,
|
||||
ContactHref: href,
|
||||
Notice: notice,
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
{{define "signup"}}<!doctype html>
|
||||
<html lang="en"><head>
|
||||
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>Sign up · llmgw</title><link rel="stylesheet" href="/_static/base.css">
|
||||
</head><body><div class="wrap narrow">
|
||||
<header><h1>Create an account</h1>
|
||||
<p class="sub">An operator approves it before it can be used.</p></header>
|
||||
|
||||
{{if .Done}}
|
||||
<p>Thank you. Your account is waiting for approval.</p>
|
||||
<p class="note">You will not get an email from this gateway — it does not send any.
|
||||
Tell whoever you asked for access that you have signed up.</p>
|
||||
<p><a href="/login">Sign in</a> once you have been approved.</p>
|
||||
{{else}}
|
||||
{{if .Error}}<p class="error">{{.Error}}</p>{{end}}
|
||||
<form method="post" autocomplete="on">
|
||||
<label>Email<input type="email" name="email" value="{{.Email}}" required autocomplete="email"></label>
|
||||
<label>Name<input type="text" name="name" value="{{.Name}}" autocomplete="name"></label>
|
||||
<label>Password<input type="password" name="password" required minlength="{{.MinPassword}}"
|
||||
autocomplete="new-password"></label>
|
||||
<p class="note">At least {{.MinPassword}} characters. Length is the only rule —
|
||||
a long phrase beats a short one with punctuation in it.</p>
|
||||
<button type="submit">Sign up</button>
|
||||
</form>
|
||||
<p class="note">Already have an account? <a href="/login">Sign in</a>.</p>
|
||||
{{end}}
|
||||
<footer><span>llmgw {{.Version}}</span><span><a href="/">about this gateway</a></span></footer>
|
||||
</div></body></html>{{end}}
|
||||
|
||||
{{define "login"}}<!doctype html>
|
||||
<html lang="en"><head>
|
||||
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>Sign in · llmgw</title><link rel="stylesheet" href="/_static/base.css">
|
||||
</head><body><div class="wrap narrow">
|
||||
<header><h1>Sign in</h1></header>
|
||||
|
||||
{{if .Error}}<p class="error">{{.Error}}</p>{{end}}
|
||||
<form method="post" autocomplete="on">
|
||||
<label>Email<input type="email" name="email" value="{{.Email}}" required autocomplete="email"></label>
|
||||
<label>Password<input type="password" name="password" required autocomplete="current-password"></label>
|
||||
<button type="submit">Sign in</button>
|
||||
</form>
|
||||
{{if .SignupOpen}}<p class="note">No account? <a href="/signup">Sign up</a>.</p>{{end}}
|
||||
<footer><span>llmgw {{.Version}}</span><span><a href="/">about this gateway</a></span></footer>
|
||||
</div></body></html>{{end}}
|
||||
|
||||
{{define "profile"}}<!doctype html>
|
||||
<html lang="en"><head>
|
||||
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>Your keys · llmgw</title><link rel="stylesheet" href="/_static/base.css">
|
||||
</head><body><div class="wrap">
|
||||
<header>
|
||||
<h1>Your keys</h1>
|
||||
<p class="sub">{{.User.Name}} · {{.User.Email}} · project {{.User.ProjectID}}</p>
|
||||
<nav>
|
||||
<a href="/">about this gateway</a>
|
||||
<form method="post" action="/logout" class="inline">
|
||||
<input type="hidden" name="csrf" value="{{.CSRF}}">
|
||||
<button type="submit" class="link">Sign out</button>
|
||||
</form>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
{{if .Error}}<p class="error">{{.Error}}</p>{{end}}
|
||||
|
||||
{{if .NewKey}}
|
||||
<h2>Your new key</h2>
|
||||
<p>This is shown once. Nothing can produce it again — only its hash is kept.</p>
|
||||
<pre>{{.NewKey}}</pre>
|
||||
<p>Put it in a password manager now.</p>
|
||||
<pre>curl {{.BaseURL}}/v1/chat/completions \
|
||||
-H "Authorization: Bearer {{.NewKey}}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"model": "MODEL", "messages": [{"role":"user","content":"hello"}]}'</pre>
|
||||
{{end}}
|
||||
|
||||
<h2>Create a key</h2>
|
||||
<form method="post" action="/profile/keys">
|
||||
<input type="hidden" name="csrf" value="{{.CSRF}}">
|
||||
<label>What is it for?<input type="text" name="name" placeholder="laptop, ci, notebook" required></label>
|
||||
<button type="submit">Create</button>
|
||||
</form>
|
||||
|
||||
<h2>Keys</h2>
|
||||
{{if .Keys}}
|
||||
<table><tr><th>Name</th><th>Key</th><th>Created</th><th>Last used</th><th>State</th><th></th></tr>
|
||||
{{range .Keys}}<tr>
|
||||
<td>{{.Name}}</td>
|
||||
<td><code>{{.Prefix}}</code></td>
|
||||
<td>{{date .CreatedAt}}</td>
|
||||
<td>{{ago .LastUsedAt}}</td>
|
||||
<td class="state state-{{state .}}">{{state .}}</td>
|
||||
<td>{{if eq (state .) "active"}}
|
||||
<form method="post" action="/profile/keys/{{.ID}}/revoke">
|
||||
<input type="hidden" name="csrf" value="{{$.CSRF}}">
|
||||
<button type="submit" class="link danger">Revoke</button>
|
||||
</form>
|
||||
{{end}}</td>
|
||||
</tr>{{end}}</table>
|
||||
<p class="note">Revoking takes effect within the gateway's cache window, up to a
|
||||
minute. To rotate: create the new key, switch over, wait for "last used" on the
|
||||
old one to stop moving, then revoke it.</p>
|
||||
{{else}}<p class="empty">You have no keys yet.</p>{{end}}
|
||||
|
||||
<h2>Your limits</h2>
|
||||
{{if .Limits}}
|
||||
<table><tr><th>Limit</th><th class="num">Value</th></tr>
|
||||
{{range .Limits}}<tr><td>{{.Name}}</td><td class="num">{{.Value}}</td></tr>{{end}}</table>
|
||||
{{else}}<p class="empty">No limits are set for you.</p>{{end}}
|
||||
|
||||
<footer><span>llmgw {{.Version}}</span><span>{{.Now}}</span></footer>
|
||||
</div></body></html>{{end}}
|
||||
@@ -58,3 +58,17 @@ button:hover { filter: brightness(1.1); }
|
||||
ul { padding-left: 1.2rem; }
|
||||
li { margin: .25rem 0; }
|
||||
a { color: var(--accent); }
|
||||
.wrap.narrow { max-width: 30rem; }
|
||||
label { display: block; margin: .9rem 0; font-size: .9rem; font-weight: 600; }
|
||||
input[type=text], input[type=email], input[type=password] {
|
||||
display: block; width: 100%; margin-top: .3rem; font: inherit; font-weight: 400;
|
||||
padding: .5rem .6rem; border: 1px solid var(--line); border-radius: 6px;
|
||||
background: var(--card); color: var(--fg);
|
||||
}
|
||||
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
|
||||
.error { color: var(--bad); border: 1px solid var(--bad); border-radius: 6px;
|
||||
padding: .6rem .8rem; background: var(--card); }
|
||||
form.inline { display: inline; }
|
||||
button.link { background: none; color: var(--accent); padding: 0;
|
||||
text-decoration: underline; font-weight: 400; font-size: .9rem; }
|
||||
button.link.danger { color: var(--bad); }
|
||||
@@ -35,17 +35,21 @@ reach it. That is a deployment choice and only safe on a trusted network.</p>
|
||||
{{end}}
|
||||
|
||||
<h2>Getting a key</h2>
|
||||
{{if .Contact}}
|
||||
{{if .SignupOpen}}
|
||||
<p><a href="/signup">Create an account</a>. Someone will approve it, and then you
|
||||
can issue your own keys from your <a href="/profile">profile</a> — as many as you
|
||||
need, revoking them yourself when a laptop is retired or a script is done.</p>
|
||||
<p class="note">Already have an account? <a href="/login">Sign in</a>.</p>
|
||||
{{else if .Contact}}
|
||||
<p>Ask {{if .ContactHref}}<a href="{{.ContactHref}}">{{.Contact}}</a>{{else}}<strong>{{.Contact}}</strong>{{end}}
|
||||
for one.</p>
|
||||
for one. {{if .AuthEnabled}}If you already have an account,
|
||||
<a href="/login">sign in</a>.{{end}}</p>
|
||||
{{else}}
|
||||
<p>Keys are issued by whoever operates this gateway. If you do not know who that
|
||||
is, the address you reached this page at is the place to start asking.</p>
|
||||
{{end}}
|
||||
<p>You will be sent a one-time link. Opening it shows the key once and then the
|
||||
link stops working, so the key never has to travel through a chat window. Put it
|
||||
in a password manager straight away — nothing can produce it again, because only
|
||||
its hash is kept here.</p>
|
||||
<p>A key is shown once and never again — only its hash is kept here — so put it in
|
||||
a password manager straight away.</p>
|
||||
|
||||
{{if .Notice}}<p class="note">{{.Notice}}</p>{{end}}
|
||||
|
||||
|
||||
@@ -20,6 +20,8 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
//go:embed templates
|
||||
@@ -46,6 +48,25 @@ var funcs = template.FuncMap{
|
||||
"units": func(micros int64) string {
|
||||
return fmt.Sprintf("%.2f", float64(micros)/1e6)
|
||||
},
|
||||
"date": func(t time.Time) string {
|
||||
if t.IsZero() {
|
||||
return "—"
|
||||
}
|
||||
return t.Format("2006-01-02")
|
||||
},
|
||||
// A key's state as the profile page shows it, computed here rather than in
|
||||
// the template so "expired" cannot quietly become "active" when nobody
|
||||
// updates the condition.
|
||||
"state": func(k store.Key) string {
|
||||
switch {
|
||||
case !k.RevokedAt.IsZero():
|
||||
return "revoked"
|
||||
case !k.ExpiresAt.IsZero() && !time.Now().Before(k.ExpiresAt):
|
||||
return "expired"
|
||||
default:
|
||||
return "active"
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
var tmpl = template.Must(template.New("").Funcs(funcs).ParseFS(files, "templates/*.html"))
|
||||
|
||||
+26
-13
@@ -24,7 +24,7 @@ func get(t *testing.T, h http.Handler, target string, headers map[string]string)
|
||||
}
|
||||
|
||||
func TestLandingPageTellsYouHowToConnect(t *testing.T) {
|
||||
code, body := get(t, Landing("v1.2.3", true, "", ""), "http://llmgw.example/", nil)
|
||||
code, body := get(t, Landing("v1.2.3", true, false, "", ""), "http://llmgw.example/", nil)
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("status %d", code)
|
||||
}
|
||||
@@ -38,7 +38,7 @@ func TestLandingPageTellsYouHowToConnect(t *testing.T) {
|
||||
func TestLandingPageSaysNothingAboutWhatIsHere(t *testing.T) {
|
||||
// It is served without a key, to anyone who can reach the address. Anything
|
||||
// on it is public, so nothing about the cluster belongs on it.
|
||||
_, body := get(t, Landing("v1", true, "", ""), "http://llmgw.example/", nil)
|
||||
_, body := get(t, Landing("v1", true, false, "", ""), "http://llmgw.example/", nil)
|
||||
for _, leak := range []string{"llama", "qwen", "10.0.0.", "replica", "node01"} {
|
||||
if strings.Contains(strings.ToLower(body), leak) {
|
||||
t.Errorf("the landing page leaks %q", leak)
|
||||
@@ -49,11 +49,11 @@ func TestLandingPageSaysNothingAboutWhatIsHere(t *testing.T) {
|
||||
func TestLandingPageWarnsWhenTheGatewayIsOpen(t *testing.T) {
|
||||
// Someone who reaches this page on an unauthenticated gateway should learn
|
||||
// that from the page rather than by discovering they can use it.
|
||||
_, open := get(t, Landing("v1", false, "", ""), "http://llmgw.example/", nil)
|
||||
_, open := get(t, Landing("v1", false, false, "", ""), "http://llmgw.example/", nil)
|
||||
if !strings.Contains(open, "no API keys configured") {
|
||||
t.Error("an open gateway does not say so")
|
||||
}
|
||||
_, closed := get(t, Landing("v1", true, "", ""), "http://llmgw.example/", nil)
|
||||
_, closed := get(t, Landing("v1", true, false, "", ""), "http://llmgw.example/", nil)
|
||||
if strings.Contains(closed, "no API keys configured") {
|
||||
t.Error("a closed gateway claims to be open")
|
||||
}
|
||||
@@ -63,7 +63,7 @@ func TestTheExampleURLSurvivesAReverseProxy(t *testing.T) {
|
||||
// The process sees http and an internal host; the client used https and a
|
||||
// public name. An example naming the internal address is one nobody can
|
||||
// paste anywhere.
|
||||
_, body := get(t, Landing("v1", true, "", ""), "http://10.0.0.140:8080/", map[string]string{
|
||||
_, body := get(t, Landing("v1", true, false, "", ""), "http://10.0.0.140:8080/", map[string]string{
|
||||
"X-Forwarded-Proto": "https",
|
||||
"X-Forwarded-Host": "llmgw.mynodes.xyz",
|
||||
})
|
||||
@@ -77,7 +77,7 @@ func TestTheExampleURLSurvivesAReverseProxy(t *testing.T) {
|
||||
|
||||
func TestForwardedHeaderChainsTakeTheFirstEntry(t *testing.T) {
|
||||
// A chain of proxies appends rather than replaces.
|
||||
_, body := get(t, Landing("v1", true, "", ""), "http://internal/", map[string]string{
|
||||
_, body := get(t, Landing("v1", true, false, "", ""), "http://internal/", map[string]string{
|
||||
"X-Forwarded-Proto": "https, http",
|
||||
"X-Forwarded-Host": "llmgw.example, internal",
|
||||
})
|
||||
@@ -89,7 +89,7 @@ func TestForwardedHeaderChainsTakeTheFirstEntry(t *testing.T) {
|
||||
func TestOnlyTheRootIsTheLandingPage(t *testing.T) {
|
||||
// A catch-all would turn every mistyped path into this page, hiding real
|
||||
// 404s from anyone debugging a client.
|
||||
code, _ := get(t, Landing("v1", true, "", ""), "http://x/v1/chat/completionz", nil)
|
||||
code, _ := get(t, Landing("v1", true, false, "", ""), "http://x/v1/chat/completionz", nil)
|
||||
if code != http.StatusNotFound {
|
||||
t.Errorf("a mistyped path returned %d", code)
|
||||
}
|
||||
@@ -235,12 +235,14 @@ func TestStaticIsNotAFileServer(t *testing.T) {
|
||||
func TestTheLandingPageSaysWhereToGetAKey(t *testing.T) {
|
||||
// A page that explains the API and leaves the reader with no route to using
|
||||
// it is where onboarding stops.
|
||||
_, body := get(t, Landing("v1", true, "platform@example.com", ""), "http://x/", nil)
|
||||
_, body := get(t, Landing("v1", true, false, "platform@example.com", ""), "http://x/", nil)
|
||||
if !strings.Contains(body, `href="mailto:platform@example.com"`) {
|
||||
t.Error("an address was not turned into a mailto link")
|
||||
}
|
||||
if !strings.Contains(body, "one-time link") {
|
||||
t.Error("the page does not say how a key will arrive")
|
||||
// With signup closed, the route is a person; the page still has to say what
|
||||
// arriving looks like.
|
||||
if !strings.Contains(body, "shown once") {
|
||||
t.Error("the page does not say a key cannot be recovered")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -284,7 +286,7 @@ func TestADangerousContactSchemeIsNeverLinked(t *testing.T) {
|
||||
t.Errorf("contactLink(%q) dropped the text entirely", bad)
|
||||
}
|
||||
|
||||
_, body := get(t, Landing("v1", true, bad, ""), "http://x/", nil)
|
||||
_, body := get(t, Landing("v1", true, false, bad, ""), "http://x/", nil)
|
||||
if strings.Contains(body, "href=\"javascript") || strings.Contains(body, "href=\"data:") {
|
||||
t.Errorf("%q reached the page as a link", bad)
|
||||
}
|
||||
@@ -292,7 +294,7 @@ func TestADangerousContactSchemeIsNeverLinked(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTheNoticeIsEscaped(t *testing.T) {
|
||||
_, body := get(t, Landing("v1", true, "", "<script>alert(1)</script>"), "http://x/", nil)
|
||||
_, body := get(t, Landing("v1", true, false, "", "<script>alert(1)</script>"), "http://x/", nil)
|
||||
if strings.Contains(body, "<script>alert(1)</script>") {
|
||||
t.Error("the notice was rendered as markup")
|
||||
}
|
||||
@@ -302,8 +304,19 @@ func TestTheNoticeIsEscaped(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestWithoutAContactThePageStillSaysSomethingUseful(t *testing.T) {
|
||||
_, body := get(t, Landing("v1", true, "", ""), "http://x/", nil)
|
||||
_, body := get(t, Landing("v1", true, false, "", ""), "http://x/", nil)
|
||||
if !strings.Contains(body, "whoever operates this gateway") {
|
||||
t.Error("an unconfigured gateway leaves the reader with nothing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithSignupOpenTheLandingPagePointsAtIt(t *testing.T) {
|
||||
// The route from "I found this address" to "I have a key" has to be on the
|
||||
// page, or the reader is stuck whatever else it says.
|
||||
_, body := get(t, Landing("v1", true, true, "", ""), "http://x/", nil)
|
||||
for _, want := range []string{`href="/signup"`, `href="/login"`, `href="/profile"`} {
|
||||
if !strings.Contains(body, want) {
|
||||
t.Errorf("the page does not link to %s", want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user