open-chess-server 0.1.4

A Lichess-compatible chess server: the Board, Bot, Challenge and OAuth APIs on Postgres, with post-game fair-play screening
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
//! Signing in to this server: its own accounts (a username and a
//! password), and, with `ESPORTS_LOGIN`, "Sign in with the host site"
//! (esports-lab). Both end in this server's session cookie, which the OAuth
//! consent page and the Account page (API tokens) use.
//!
//! **The host site.** It shares a secret with this server
//! (`ESPORTS_SECRET`) and uses it two ways:
//!
//! * Signing in: `GET /auth/esports` sends the browser to the site's
//!   `/chess/sso?return=…&nonce=…`; the site sends it back to
//!   `/auth/esports/back?a=…` with a signed statement of who is signed in
//!   there ([`sign`]: `base64url(json).base64url(HMAC-SHA256)`, the JSON
//!   `{sub, name, email, role, roles, nonce, exp}`).
//! * Its server asks for a token for one of its players:
//!   `POST /api/esports/token` with `Authorization: Bearer <secret>` and
//!   the same fields as a form (plus `scopes`, `days`, `label`).
//!
//! Either way the site's player is linked to a player here by `esports_id`
//! (made on first use, with a chess account named after them), so the
//! same person is the same chess player whichever way they come in.

use std::collections::HashMap;
use std::fmt::Write as _;

use argon2::Argon2;
use argon2::password_hash::phc::PasswordHash;
use argon2::password_hash::{PasswordHasher, PasswordVerifier};
use axum::Json;
use axum::extract::{Query, State};
use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
use axum::response::{IntoResponse, Redirect, Response};
use base64::Engine as _;
use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64;
use bytes::Bytes;
use chrono::{Duration, Utc};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};

use crate::app::Ctx;
use crate::auth::{SCOPES, SESSION_COOKIE, TOKEN_PREFIX, bearer, cookie, hash_key, random_token};
use crate::error::{ApiError, ApiResult};
use crate::http::oauth::page;
use crate::http::{form, html_escape};
use crate::model;

const SESSION_DAYS: i64 = 30;
const SSO_COOKIE: &str = "oc_sso";

// ─── HMAC and the site's signed statements ───

/// HMAC-SHA256 (RFC 2104).
pub fn hmac(key: &[u8], msg: &[u8]) -> [u8; 32] {
    let mut k = [0u8; 64];
    if key.len() > 64 {
        k[..32].copy_from_slice(&Sha256::digest(key));
    } else {
        k[..key.len()].copy_from_slice(key);
    }
    let pad = |b: u8| k.map(|x| x ^ b);
    let inner = Sha256::new().chain_update(pad(0x36)).chain_update(msg).finalize();
    Sha256::new().chain_update(pad(0x5c)).chain_update(inner).finalize().into()
}

fn same(a: &[u8], b: &[u8]) -> bool {
    a.len() == b.len() && a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0
}

/// A signed statement: `base64url(json).base64url(hmac)`.
pub fn sign(secret: &str, v: &Value) -> String {
    let body = B64.encode(v.to_string());
    let mac = B64.encode(hmac(secret.as_bytes(), body.as_bytes()));
    format!("{body}.{mac}")
}

/// The statement, if its signature is right and it hasn't expired.
pub fn verify(secret: &str, s: &str, now_secs: i64) -> Option<Value> {
    let (body, mac) = s.split_once('.')?;
    let want = hmac(secret.as_bytes(), body.as_bytes());
    if !same(&B64.decode(mac).ok()?, &want) {
        return None;
    }
    let v: Value = serde_json::from_slice(&B64.decode(body).ok()?).ok()?;
    (v.get("exp")?.as_i64()? > now_secs).then_some(v)
}

/// A player of the host site, as it describes them.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SitePlayer {
    pub sub: String,
    pub name: String,
    pub email: Option<String>,
    pub role: String,
    pub roles: Vec<String>,
}

impl SitePlayer {
    fn from_fields(get: impl Fn(&str) -> Option<String>) -> Option<SitePlayer> {
        let sub = get("sub").filter(|s| !s.is_empty() && s.len() <= 64)?;
        Some(SitePlayer {
            name: get("name").filter(|n| !n.trim().is_empty()).unwrap_or_else(|| format!("player{sub}")),
            email: get("email").filter(|e| !e.is_empty()),
            role: get("role").unwrap_or_else(|| "player".into()),
            roles: get("roles")
                .map(|r| r.split([',', ' ']).filter(|x| !x.is_empty()).map(str::to_owned).collect())
                .unwrap_or_default(),
            sub,
        })
    }

    fn from_json(v: &Value) -> Option<SitePlayer> {
        SitePlayer::from_fields(|k| match v.get(k)? {
            Value::String(s) => Some(s.clone()),
            Value::Number(n) => Some(n.to_string()),
            Value::Array(a) => Some(a.iter().filter_map(Value::as_str).collect::<Vec<_>>().join(",")),
            _ => None,
        })
    }
}

/// The player here linked to the site's, made on first use (and kept up
/// to date: email and roles). Returns the player's id and chess username.
pub async fn link(ctx: &Ctx, p: &SitePlayer) -> ApiResult<(i64, String)> {
    let c = ctx.db.conn().await?;
    let id: i64 = c
        .query_one(
            &ctx.db.sql(
                "INSERT INTO chess.players (name, email, role, roles, esports_id, key_hash)
                 VALUES ($1, $2, $3, $4, $5, $6)
                 ON CONFLICT (esports_id) DO UPDATE
                    SET email = EXCLUDED.email, role = EXCLUDED.role, roles = EXCLUDED.roles
                 RETURNING id",
            ),
            &[&p.name, &p.email, &p.role, &p.roles, &p.sub, &hash_key(&random_token(28))],
        )
        .await?
        .get(0);
    let a = model::ensure_account(&ctx.db, &*c, id).await?;
    Ok((id, a.username))
}

// ─── sessions ───

fn secure(ctx: &Ctx) -> &'static str {
    if ctx.cfg.public_url().starts_with("https://") { "; Secure" } else { "" }
}

fn cookie_path(ctx: &Ctx) -> String {
    let base = ctx.cfg.base_path().unwrap_or_default();
    if base.is_empty() { "/".into() } else { base }
}

/// Start a session: the cookie to set.
async fn start_session(ctx: &Ctx, player: i64) -> ApiResult<HeaderValue> {
    let token = random_token(40);
    let c = ctx.db.conn().await?;
    c.execute(
        &ctx.db.sql(
            "INSERT INTO chess.sessions (token_hash, player_id, expires_at) VALUES ($1, $2, now() + make_interval(days => $3))",
        ),
        &[&hash_key(&token), &player, &i32::try_from(SESSION_DAYS).unwrap_or(30)],
    )
    .await?;
    HeaderValue::from_str(&format!(
        "{SESSION_COOKIE}={token}; Path={}; HttpOnly; SameSite=Lax; Max-Age={}{}",
        cookie_path(ctx),
        SESSION_DAYS * 86_400,
        secure(ctx)
    ))
    .map_err(|e| ApiError::from(anyhow::anyhow!("{e}")))
}

/// Where to go after signing in: a path on this server, or the Account page.
fn back_to(ctx: &Ctx, want: Option<&str>) -> String {
    let base = ctx.cfg.base_path().unwrap_or_default();
    match want {
        Some(p) if p.starts_with('/') && !p.starts_with("//") && !p.contains('\\') => p.to_owned(),
        _ => format!("{base}/account"),
    }
}

fn redirect_with_cookies(to: &str, cookies: Vec<HeaderValue>) -> Response {
    let mut r = Redirect::to(to).into_response();
    for c in cookies {
        r.headers_mut().append(header::SET_COOKIE, c);
    }
    r
}

/// A form tied to the session that saw it.
fn csrf(session: &str, what: &str) -> String {
    hash_key(&format!("{session}|{what}"))
}

// ─── pages ───

fn hidden(k: &str, v: &str) -> String {
    format!("<input type=hidden name=\"{k}\" value=\"{}\">", html_escape(v))
}

fn sign_in_form(ctx: &Ctx, ret: &str, error: Option<&str>) -> String {
    let base = ctx.cfg.base_path().unwrap_or_default();
    let mut body = String::new();
    if let Some(e) = error {
        let _ = write!(body, "<p class=err>{}</p>", html_escape(e));
    }
    if ctx.cfg.esports().is_some() {
        let _ = write!(
            body,
            "<p><a class=btn href=\"{base}/auth/esports?return={r}\">Sign in with {site}</a></p><p class=muted>or with an account here:</p>",
            r = urlencoding::encode(ret),
            site = html_escape(&ctx.cfg.site_name)
        );
    }
    let _ = write!(
        body,
        "<form method=post action=\"{base}/login\">{h}\
         <label>Username or email<input name=name autocomplete=username required></label>\
         <label>Password<input name=password type=password autocomplete=current-password required></label>\
         <button class=go>Sign in</button></form>",
        h = hidden("return", ret)
    );
    if ctx.cfg.signup {
        let _ = write!(
            body,
            "<p class=muted>No account? <a href=\"{base}/signup?return={}\">Make one</a>.</p>",
            urlencoding::encode(ret)
        );
    }
    body
}

/// `GET /login`.
pub async fn login_page(State(ctx): State<Ctx>, Query(q): Query<HashMap<String, String>>) -> Response {
    let ret = back_to(&ctx, q.get("return").map(String::as_str));
    page("Sign in", &sign_in_form(&ctx, &ret, q.get("error").map(String::as_str)))
}

/// `POST /login`: a username (or email) and password.
pub async fn login(State(ctx): State<Ctx>, body: Bytes) -> ApiResult<Response> {
    let f = form(&body);
    let ret = back_to(&ctx, f.get("return").map(String::as_str));
    let name = f.get("name").map_or("", |s| s.trim());
    let password = f.get("password").map_or("", String::as_str);
    let c = ctx.db.conn().await?;
    let row = c
        .query_opt(
            &ctx.db.sql(
                "SELECT p.id, p.password_hash FROM chess.players p JOIN chess.accounts a ON a.player_id = p.id
                  WHERE (lower(a.username) = lower($1) OR lower(p.email) = lower($1))
                    AND p.disabled_at IS NULL AND p.password_hash IS NOT NULL
                  LIMIT 1",
            ),
            &[&name],
        )
        .await?;
    drop(c);
    let ok = row.as_ref().is_some_and(|r| {
        let stored: String = r.get(1);
        PasswordHash::new(&stored)
            .is_ok_and(|h| Argon2::default().verify_password(password.as_bytes(), &h).is_ok())
    });
    let Some(row) = row.filter(|_| ok) else {
        // The same pace either way, and a little slower.
        tokio::time::sleep(std::time::Duration::from_millis(400)).await;
        return Ok((
            StatusCode::UNAUTHORIZED,
            page(
                "Sign in",
                &sign_in_form(&ctx, &ret, Some("That name and password don't match an account here.")),
            ),
        )
            .into_response());
    };
    let cookie = start_session(&ctx, row.get(0)).await?;
    Ok(redirect_with_cookies(&ret, vec![cookie]))
}

fn signup_form(ctx: &Ctx, ret: &str, error: Option<&str>, name: &str, email: &str) -> String {
    let base = ctx.cfg.base_path().unwrap_or_default();
    let mut body = String::new();
    if let Some(e) = error {
        let _ = write!(body, "<p class=err>{}</p>", html_escape(e));
    }
    let _ = write!(
        body,
        "<form method=post action=\"{base}/signup\">{h}\
         <label>Username (letters, digits, _ and -)<input name=name value=\"{n}\" autocomplete=username required minlength=2 maxlength=20></label>\
         <label>Email (optional, to sign in with)<input name=email type=email value=\"{e}\" autocomplete=email></label>\
         <label>Password (10 or more characters)<input name=password type=password autocomplete=new-password required minlength=10></label>\
         <button class=go>Make the account</button></form>\
         <p class=muted>Already have one? <a href=\"{base}/login?return={r}\">Sign in</a>.</p>",
        h = hidden("return", ret),
        n = html_escape(name),
        e = html_escape(email),
        r = urlencoding::encode(ret),
    );
    body
}

/// `GET /signup`.
pub async fn signup_page(State(ctx): State<Ctx>, Query(q): Query<HashMap<String, String>>) -> Response {
    if !ctx.cfg.signup {
        return (
            StatusCode::FORBIDDEN,
            page("No sign-ups here", "<p>This server doesn't take new accounts.</p>"),
        )
            .into_response();
    }
    let ret = back_to(&ctx, q.get("return").map(String::as_str));
    page("Make an account", &signup_form(&ctx, &ret, None, "", ""))
}

/// `POST /signup`: a username, a password and (optionally) an email.
pub async fn signup(State(ctx): State<Ctx>, body: Bytes) -> ApiResult<Response> {
    if !ctx.cfg.signup {
        return Err(ApiError::forbidden("this server doesn't take new accounts"));
    }
    let f = form(&body);
    let ret = back_to(&ctx, f.get("return").map(String::as_str));
    let name = f.get("name").map_or("", |s| s.trim()).to_owned();
    let email = f.get("email").map_or("", |s| s.trim()).to_owned();
    let password = f.get("password").cloned().unwrap_or_default();
    let refuse = |why: &str| {
        Ok((
            StatusCode::BAD_REQUEST,
            page("Make an account", &signup_form(&ctx, &ret, Some(why), &name, &email)),
        )
            .into_response())
    };
    let valid = name.len() >= 2
        && name.len() <= 20
        && name.chars().next().is_some_and(|c| c.is_ascii_alphanumeric())
        && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-');
    if !valid {
        return refuse("A username is 2 to 20 letters, digits, _ and -, starting with a letter or digit.");
    }
    if name.to_lowercase().starts_with("ai-level-") {
        return refuse("That name is the computer's.");
    }
    if password.chars().count() < 10 {
        return refuse("The password needs 10 or more characters.");
    }
    if !email.is_empty() && !email.contains('@') {
        return refuse("That email doesn't look right.");
    }
    let hash = Argon2::default()
        .hash_password(password.as_bytes())
        .map_err(|e| ApiError::from(anyhow::anyhow!("hashing the password: {e}")))?
        .to_string();
    let mut c = ctx.db.conn().await?;
    let tx = c.transaction().await?;
    if model::account_by_name(&ctx.db, &tx, &name).await?.is_some() {
        return refuse("That username is taken.");
    }
    if !email.is_empty()
        && tx
            .query_opt(&ctx.db.sql("SELECT 1 FROM chess.players WHERE lower(email) = lower($1)"), &[&email])
            .await?
            .is_some()
    {
        return refuse("An account here already has that email.");
    }
    let id: i64 = tx
        .query_one(
            &ctx.db.sql(
                "INSERT INTO chess.players (name, email, password_hash) VALUES ($1, $2, $3) RETURNING id",
            ),
            &[&name, &(!email.is_empty()).then_some(&email), &hash],
        )
        .await?
        .get(0);
    tx.execute(
        &ctx.db.sql("INSERT INTO chess.accounts (player_id, username) VALUES ($1, $2)"),
        &[&id, &name],
    )
    .await?;
    tx.commit().await?;
    let cookie = start_session(&ctx, id).await?;
    Ok(redirect_with_cookies(&ret, vec![cookie]))
}

/// `POST /logout`.
pub async fn logout(State(ctx): State<Ctx>, h: HeaderMap) -> ApiResult<Response> {
    if let Some(t) = cookie(&h, SESSION_COOKIE) {
        let c = ctx.db.conn().await?;
        c.execute(
            &ctx.db.sql("UPDATE chess.sessions SET revoked_at = now() WHERE token_hash = $1"),
            &[&hash_key(&t)],
        )
        .await?;
    }
    let base = ctx.cfg.base_path().unwrap_or_default();
    let gone = HeaderValue::from_str(&format!(
        "{SESSION_COOKIE}=; Path={}; Max-Age=0; HttpOnly; SameSite=Lax",
        cookie_path(&ctx)
    ))
    .map_err(|e| ApiError::from(anyhow::anyhow!("{e}")))?;
    Ok(redirect_with_cookies(&format!("{base}/login"), vec![gone]))
}

/// The scopes a player may give a token (staff-only ones aside).
pub async fn allowed(ctx: &Ctx, player: i64, wanted: &[String]) -> ApiResult<Vec<String>> {
    let c = ctx.db.conn().await?;
    let r =
        c.query_one(&ctx.db.sql("SELECT role, roles FROM chess.players WHERE id = $1"), &[&player]).await?;
    let role: String = r.get(0);
    let roles: Vec<String> = r.get(1);
    let staff = role == "admin" || roles.iter().any(|x| x == "manager");
    Ok(wanted
        .iter()
        .filter(|s| SCOPES.iter().any(|(k, _)| k == s))
        .filter(|s| staff || !matches!(s.as_str(), "chess:fairplay" | "bot:play"))
        .cloned()
        .collect())
}

/// Make a token; returns it (shown once) and when it expires.
pub async fn issue(
    ctx: &Ctx,
    player: i64,
    label: &str,
    scopes: &[String],
    days: i64,
) -> ApiResult<(String, chrono::DateTime<Utc>)> {
    issue_for(ctx, player, label, scopes, Duration::days(days.clamp(1, 365))).await
}

/// Make a token lasting `life`.
pub async fn issue_for(
    ctx: &Ctx,
    player: i64,
    label: &str,
    scopes: &[String],
    life: Duration,
) -> ApiResult<(String, chrono::DateTime<Utc>)> {
    let token = format!("{TOKEN_PREFIX}{}", random_token(36));
    let prefix: String = token.chars().take(TOKEN_PREFIX.len() + 6).collect();
    let expires = Utc::now() + life;
    let label: String = label.trim().chars().take(80).collect();
    let c = ctx.db.conn().await?;
    c.execute(
        &ctx.db.sql(
            "INSERT INTO chess.api_tokens (player_id, name, token_hash, prefix, scopes, expires_at)
             VALUES ($1, $2, $3, $4, $5, $6)",
        ),
        &[
            &player,
            &if label.is_empty() { "token".to_owned() } else { label },
            &hash_key(&token),
            &prefix,
            &scopes,
            &expires,
        ],
    )
    .await?;
    Ok((token, expires))
}

async fn account_page(ctx: &Ctx, player: i64, session: &str, fresh: Option<&str>) -> ApiResult<Response> {
    let base = ctx.cfg.base_path().unwrap_or_default();
    let c = ctx.db.conn().await?;
    let a = model::ensure_account(&ctx.db, &*c, player).await?;
    let linked: Option<String> = c
        .query_one(&ctx.db.sql("SELECT esports_id FROM chess.players WHERE id = $1"), &[&player])
        .await?
        .get(0);
    let tokens = c
        .query(
            &ctx.db.sql(
                "SELECT id, name, prefix, scopes, to_char(expires_at, 'YYYY-MM-DD'),
                        coalesce(to_char(last_used_at, 'YYYY-MM-DD'), 'never')
                   FROM chess.api_tokens
                  WHERE player_id = $1 AND revoked_at IS NULL AND expires_at > now()
                  ORDER BY created_at DESC",
            ),
            &[&player],
        )
        .await?;
    drop(c);
    let token_csrf = csrf(session, "tokens");
    let mut body = format!(
        "<p>Signed in as <b>{}</b>{}.</p>",
        html_escape(&a.username),
        if linked.is_some() {
            format!(" (linked to your {} account)", html_escape(&ctx.cfg.site_name))
        } else {
            String::new()
        }
    );
    if let Some(t) = fresh {
        let _ = write!(
            body,
            "<p class=ok>Your new token, shown once: copy it now.</p><p><code class=token>{}</code></p>",
            html_escape(t)
        );
    }
    body.push_str("<h2>API tokens</h2>");
    if tokens.is_empty() {
        body.push_str("<p class=muted>None yet.</p>");
    } else {
        body.push_str("<table><tr><th>Name</th><th>Token</th><th>Scopes</th><th>Expires</th><th>Last used</th><th></th></tr>");
        for t in &tokens {
            let id: i64 = t.get(0);
            let scopes: Vec<String> = t.get(3);
            let _ = write!(
                body,
                "<tr><td>{}</td><td><code>{}…</code></td><td>{}</td><td>{}</td><td>{}</td>\
                 <td><form method=post action=\"{base}/account/tokens/revoke\">{}{}<button>Revoke</button></form></td></tr>",
                html_escape(&t.get::<_, String>(1)),
                html_escape(&t.get::<_, String>(2)),
                html_escape(&scopes.join(" ")),
                t.get::<_, String>(4),
                t.get::<_, String>(5),
                hidden("csrf", &token_csrf),
                hidden("id", &id.to_string()),
            );
        }
        body.push_str("</table>");
    }
    let mut boxes = String::new();
    for (k, d) in SCOPES {
        let checked =
            if matches!(*k, "board:play" | "challenge:read" | "challenge:write") { " checked" } else { "" };
        let _ = write!(
            boxes,
            "<label class=check><input type=checkbox name=\"scope:{k}\"{checked}> {} <code>{k}</code></label>",
            html_escape(d)
        );
    }
    let _ = write!(
        body,
        "<h2>New token</h2><form method=post action=\"{base}/account/tokens\">{c}\
         <label>Name<input name=label placeholder=\"e.g. my board\" maxlength=80></label>{boxes}\
         <label>Days it lasts<input name=days type=number min=1 max=365 value=365></label>\
         <button class=go>Make the token</button></form>\
         <form method=post action=\"{base}/logout\"><button>Sign out</button></form>",
        c = hidden("csrf", &token_csrf)
    );
    Ok(page("Your chess account", &body))
}

async fn signed_in(ctx: &Ctx, h: &HeaderMap) -> ApiResult<Option<(i64, String)>> {
    let session = cookie(h, SESSION_COOKIE).unwrap_or_default();
    Ok(ctx.auth.session(h).await?.map(|p| (p, session)))
}

fn to_login(ctx: &Ctx, ret: &str) -> Response {
    let base = ctx.cfg.base_path().unwrap_or_default();
    Redirect::to(&format!("{base}/login?return={}", urlencoding::encode(ret))).into_response()
}

/// `GET /account`.
pub async fn account(State(ctx): State<Ctx>, h: HeaderMap) -> ApiResult<Response> {
    let base = ctx.cfg.base_path().unwrap_or_default();
    let Some((player, session)) = signed_in(&ctx, &h).await? else {
        return Ok(to_login(&ctx, &format!("{base}/account")));
    };
    account_page(&ctx, player, &session, None).await
}

/// `POST /account/tokens`.
pub async fn new_token(State(ctx): State<Ctx>, h: HeaderMap, body: Bytes) -> ApiResult<Response> {
    let (player, session) = signed_in(&ctx, &h).await?.ok_or_else(|| ApiError::forbidden("sign in first"))?;
    let f = form(&body);
    if f.get("csrf").map(String::as_str) != Some(csrf(&session, "tokens").as_str()) {
        return Err(ApiError::forbidden("this form has expired: reload the page"));
    }
    let wanted: Vec<String> = f.keys().filter_map(|k| k.strip_prefix("scope:")).map(str::to_owned).collect();
    let scopes = allowed(&ctx, player, &wanted).await?;
    let days = f.get("days").and_then(|d| d.parse().ok()).unwrap_or(365);
    let (token, _) = issue(&ctx, player, f.get("label").map_or("", String::as_str), &scopes, days).await?;
    account_page(&ctx, player, &session, Some(&token)).await
}

/// `POST /account/tokens/revoke`.
pub async fn revoke_token(State(ctx): State<Ctx>, h: HeaderMap, body: Bytes) -> ApiResult<Response> {
    let (player, session) = signed_in(&ctx, &h).await?.ok_or_else(|| ApiError::forbidden("sign in first"))?;
    let f = form(&body);
    if f.get("csrf").map(String::as_str) != Some(csrf(&session, "tokens").as_str()) {
        return Err(ApiError::forbidden("this form has expired: reload the page"));
    }
    let id: i64 = f.get("id").and_then(|i| i.parse().ok()).ok_or_else(|| ApiError::bad("which token?"))?;
    let c = ctx.db.conn().await?;
    c.execute(
        &ctx.db.sql(
            "UPDATE chess.api_tokens SET revoked_at = now(), revoked_by = 'chess: account page'
              WHERE id = $1 AND player_id = $2 AND revoked_at IS NULL",
        ),
        &[&id, &player],
    )
    .await?;
    let base = ctx.cfg.base_path().unwrap_or_default();
    Ok(Redirect::to(&format!("{base}/account")).into_response())
}

// ─── the host site ───

/// `GET /auth/esports`: off to the site to sign in.
pub async fn esports_start(
    State(ctx): State<Ctx>,
    Query(q): Query<HashMap<String, String>>,
) -> ApiResult<Response> {
    if ctx.cfg.esports().is_none() {
        return Err(ApiError::not_found("signing in with the site"));
    }
    let ret = back_to(&ctx, q.get("return").map(String::as_str));
    let nonce = random_token(24);
    let back = format!("{}/auth/esports/back", ctx.cfg.public_url());
    let to = format!(
        "{}/chess/sso?return={}&nonce={nonce}",
        ctx.cfg.site_url.trim_end_matches('/'),
        urlencoding::encode(&back)
    );
    let c = HeaderValue::from_str(&format!(
        "{SSO_COOKIE}={nonce}.{}; Path={}; Max-Age=600; HttpOnly; SameSite=Lax{}",
        B64.encode(&ret),
        cookie_path(&ctx),
        secure(&ctx)
    ))
    .map_err(|e| ApiError::from(anyhow::anyhow!("{e}")))?;
    Ok(redirect_with_cookies(&to, vec![c]))
}

/// `GET /auth/esports/back?a=…`: back from the site, signed in there.
pub async fn esports_back(
    State(ctx): State<Ctx>,
    h: HeaderMap,
    Query(q): Query<HashMap<String, String>>,
) -> ApiResult<Response> {
    let secret = ctx.cfg.esports().ok_or_else(|| ApiError::not_found("signing in with the site"))?.to_owned();
    let (nonce, ret) = cookie(&h, SSO_COOKIE)
        .and_then(|c| {
            let (n, r) = c.split_once('.')?;
            Some((n.to_owned(), String::from_utf8(B64.decode(r).ok()?).ok()?))
        })
        .ok_or_else(|| ApiError::forbidden("this sign-in has expired: start again"))?;
    let v = q
        .get("a")
        .and_then(|a| verify(&secret, a, Utc::now().timestamp()))
        .ok_or_else(|| ApiError::forbidden("the site's answer isn't valid or has expired: start again"))?;
    if v.get("nonce").and_then(Value::as_str) != Some(nonce.as_str()) {
        return Err(ApiError::forbidden("this sign-in was for another page: start again"));
    }
    let p = SitePlayer::from_json(&v).ok_or_else(|| ApiError::bad("the site didn't say who you are"))?;
    let (player, _) = link(&ctx, &p).await?;
    let session = start_session(&ctx, player).await?;
    let gone = HeaderValue::from_str(&format!("{SSO_COOKIE}=; Path={}; Max-Age=0", cookie_path(&ctx)))
        .map_err(|e| ApiError::from(anyhow::anyhow!("{e}")))?;
    Ok(redirect_with_cookies(&back_to(&ctx, Some(&ret)), vec![session, gone]))
}

/// `POST /api/esports/token`: the site's server asks for a token for one
/// of its players (who gets linked, and a chess account, on first use).
pub async fn esports_token(State(ctx): State<Ctx>, h: HeaderMap, body: Bytes) -> ApiResult<Response> {
    let secret = ctx.cfg.esports().ok_or_else(|| ApiError::not_found("the site's token endpoint"))?;
    if !bearer(&h).is_some_and(|b| same(b.as_bytes(), secret.as_bytes())) {
        return Err(ApiError::unauthorized());
    }
    let f = form(&body);
    let p = SitePlayer::from_fields(|k| f.get(k).cloned())
        .ok_or_else(|| ApiError::bad("sub (the site's player id) is required"))?;
    let (player, username) = link(&ctx, &p).await?;
    let wanted: Vec<String> = f.get("scopes").map_or_else(
        || ["board:play", "challenge:read", "challenge:write"].map(str::to_owned).to_vec(),
        |s| s.split([' ', ',']).filter(|x| !x.is_empty()).map(str::to_owned).collect(),
    );
    let scopes = allowed(&ctx, player, &wanted).await?;
    let label = f.get("label").map_or_else(|| format!("{} (site)", ctx.cfg.site_name), Clone::clone);
    let (token, expires) = match f.get("hours").and_then(|h| h.parse::<i64>().ok()) {
        Some(hours) => {
            issue_for(&ctx, player, &label, &scopes, Duration::hours(hours.clamp(1, 24 * 365))).await?
        }
        None => {
            issue(&ctx, player, &label, &scopes, f.get("days").and_then(|d| d.parse().ok()).unwrap_or(30))
                .await?
        }
    };
    Ok(Json(json!({
        "token": token,
        "username": username,
        "scopes": scopes,
        "expires": expires.timestamp_millis(),
    }))
    .into_response())
}

/// `GET /api/esports/players` (bearer the secret): the site's players
/// linked here, `{"players": [{"sub", "username"}]}`, so the site can tell
/// whose games the feed shows.
pub async fn esports_players(State(ctx): State<Ctx>, h: HeaderMap) -> ApiResult<Response> {
    let secret = ctx.cfg.esports().ok_or_else(|| ApiError::not_found("the site's token endpoint"))?;
    if !bearer(&h).is_some_and(|b| same(b.as_bytes(), secret.as_bytes())) {
        return Err(ApiError::unauthorized());
    }
    let c = ctx.db.conn().await?;
    let rows = c
        .query(
            &ctx.db.sql(
                "SELECT p.esports_id, a.username FROM chess.players p JOIN chess.accounts a ON a.player_id = p.id
                  WHERE p.esports_id IS NOT NULL ORDER BY p.id",
            ),
            &[],
        )
        .await?;
    let players: Vec<Value> = rows
        .iter()
        .map(|r| json!({"sub": r.get::<_, String>(0), "username": r.get::<_, String>(1)}))
        .collect();
    Ok(Json(json!({ "players": players })).into_response())
}

/// `POST /api/esports/revoke` (bearer the secret; form `label`, and
/// optionally `sub`): revoke the tokens of that name the site asked for
/// (all its players', or one's). Answers how many.
pub async fn esports_revoke(State(ctx): State<Ctx>, h: HeaderMap, body: Bytes) -> ApiResult<Response> {
    let secret = ctx.cfg.esports().ok_or_else(|| ApiError::not_found("the site's token endpoint"))?;
    if !bearer(&h).is_some_and(|b| same(b.as_bytes(), secret.as_bytes())) {
        return Err(ApiError::unauthorized());
    }
    let f = form(&body);
    let label = f.get("label").filter(|l| !l.is_empty()).ok_or_else(|| ApiError::bad("label is required"))?;
    let sub = f.get("sub").filter(|s| !s.is_empty());
    let c = ctx.db.conn().await?;
    let n = c
        .execute(
            &ctx.db.sql(
                "UPDATE chess.api_tokens t SET revoked_at = now(), revoked_by = 'site: ' || $1
                   FROM chess.players p
                  WHERE p.id = t.player_id AND p.esports_id IS NOT NULL AND ($2::text IS NULL OR p.esports_id = $2)
                    AND t.name = $1 AND t.revoked_at IS NULL",
            ),
            &[label, &sub],
        )
        .await?;
    Ok(Json(json!({ "revoked": n })).into_response())
}

/// `POST /api/esports/link` (bearer the secret; form `sub`, `username`):
/// a manager on the site says the account `username` here is their player
/// `sub`'s, made before they signed in with the site. The site player's
/// link moves to it (an account it was linked to before stays, unlinked,
/// with its games). Refused (409) when that account is another site
/// player's. An empty `username` unlinks. Answers `{"username"}`.
pub async fn esports_link(State(ctx): State<Ctx>, h: HeaderMap, body: Bytes) -> ApiResult<Response> {
    let secret = ctx.cfg.esports().ok_or_else(|| ApiError::not_found("the site's token endpoint"))?;
    if !bearer(&h).is_some_and(|b| same(b.as_bytes(), secret.as_bytes())) {
        return Err(ApiError::unauthorized());
    }
    let f = form(&body);
    let sub = f.get("sub").filter(|s| !s.is_empty()).ok_or_else(|| ApiError::bad("sub is required"))?;
    let username = f.get("username").map(|u| u.trim()).unwrap_or_default();
    let mut c = ctx.db.conn().await?;
    let tx = c.transaction().await?;
    if username.is_empty() {
        tx.execute(&ctx.db.sql("UPDATE chess.players SET esports_id = NULL WHERE esports_id = $1"), &[sub])
            .await?;
        tx.commit().await?;
        return Ok(Json(json!({ "username": null })).into_response());
    }
    let row = tx
        .query_opt(
            &ctx.db.sql(
                "SELECT p.id, p.esports_id, a.username FROM chess.accounts a JOIN chess.players p ON p.id = a.player_id
                  WHERE lower(a.username) = lower($1)",
            ),
            &[&username],
        )
        .await?
        .ok_or_else(|| ApiError::not_found("no account of that name here"))?;
    let (player, linked, name): (i64, Option<String>, String) = (row.get(0), row.get(1), row.get(2));
    if linked.as_deref().is_some_and(|l| l != sub) {
        return Err(ApiError::new(
            axum::http::StatusCode::CONFLICT,
            format!("{name} is another of the site's players' account"),
        ));
    }
    tx.execute(
        &ctx.db.sql("UPDATE chess.players SET esports_id = NULL WHERE esports_id = $1 AND id <> $2"),
        &[sub, &player],
    )
    .await?;
    tx.execute(&ctx.db.sql("UPDATE chess.players SET esports_id = $1 WHERE id = $2"), &[sub, &player])
        .await?;
    tx.commit().await?;
    Ok(Json(json!({ "username": name })).into_response())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn hmac_matches_rfc_4231() {
        // Test case 2.
        assert_eq!(
            hex::encode(hmac(b"Jefe", b"what do ya want for nothing?")),
            "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"
        );
        // Test case 6: a key longer than the block.
        assert_eq!(
            hex::encode(hmac(&[0xaa; 131], b"Test Using Larger Than Block-Size Key - Hash Key First")),
            "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54"
        );
    }

    #[test]
    fn signed_statements() {
        let secret = "s".repeat(32);
        let v = json!({"sub": "42", "name": "Mark", "roles": ["manager"], "nonce": "n", "exp": 2000});
        let s = sign(&secret, &v);
        assert_eq!(verify(&secret, &s, 1000), Some(v.clone()));
        assert_eq!(verify(&secret, &s, 3000), None, "expired");
        assert_eq!(verify(&"t".repeat(32), &s, 1000), None, "another secret");
        let (body, mac) = s.split_once('.').unwrap();
        let forged = format!("{}x.{mac}", &body[..body.len() - 1]);
        assert_eq!(verify(&secret, &forged, 1000), None, "changed");
        let p = SitePlayer::from_json(&v).unwrap();
        assert_eq!(
            (p.sub.as_str(), p.name.as_str(), p.roles.as_slice()),
            ("42", "Mark", &["manager".to_owned()][..])
        );
    }
}