puerto 0.22.0

Puerto — Rust full-stack DDD framework CLI. Scaffolds Clean Architecture projects with auto-wired DI, SQLx support, and zero manual wiring. Formerly puerto-framework.
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
//! `puerto nuevo --auth oidc` (ADR-0021): the `Usuario` entity every caller becomes, the provisioning
//! `Autenticador` in front of the provider, `GET /me`, and Keycloak for development. Every file
//! here is written once — the project's from then on.

use crate::generators::layout::{self, Layer};
use std::fs;
use std::path::Path;

use crate::generators::naming::write_file;
use crate::generators::project::{add_compose_service, add_workspace_dependency};
use crate::generators::render::render;

/// The Keycloak a project runs in development; `make test/auth` tests Puerto against the same tag.
pub const KEYCLOAK_IMAGE: &str = "quay.io/keycloak/keycloak:26.8.0";
/// Where Keycloak listens on the host: off 8080, the API's own port.
pub const KEYCLOAK_PORT: u16 = 8180;
/// The development user's password, in the imported realm and in `.env.example`.
pub const DEV_PASSWORD: &str = "admin-dev-only";
/// The audience Keycloak's mapper adds to the `web` client's tokens, and `AUTH_AUDIENCE`.
pub const AUDIENCE: &str = "api";
/// The OpenID Connect client of the frontend, pinned like every `web/package.json` dependency.
pub const OIDC_CLIENT_TS_VERSION: &str = "3.5.0";

/// A mode `puerto nuevo --auth` knows.
pub fn check_mode(mode: &str) -> Result<(), String> {
    crate::validation::validate_auth_mode(mode)
}

/// Whether `puerto nuevo --auth <mode>` can write a project with this database choice — asked
/// before anything is written. Local accounts live in Postgres.
pub fn check_new_project(mode: &str, db: bool) -> Result<(), String> {
    check_mode(mode)?;
    if mode == "local" && !db {
        return Err("--auth local keeps accounts in the database: add --db".into());
    }
    Ok(())
}

/// Who may call each use case of an entity: what the templates of an auth project branch on.
/// Outside an auth project nothing is protected.
#[derive(Debug, Clone, Default)]
pub struct Access {
    auth: bool,
    public: Vec<String>,
    roles: std::collections::BTreeMap<String, Vec<String>>,
}

impl Access {
    /// The entity `pascal`'s, as `puerto.toml` at `base` records it.
    pub fn of(base: &Path, pascal: &str) -> Self {
        crate::puerto_toml::read(base)
            .map(|config| Self::in_config(&config, pascal))
            .unwrap_or_default()
    }

    /// The entity `pascal`'s, as `config` records it.
    pub fn in_config(config: &crate::puerto_toml::PuertoConfig, pascal: &str) -> Self {
        let entity = config.entity.iter().find(|e| e.name == pascal);
        Self {
            auth: config.project.has_auth(),
            public: entity.map(|e| e.public.clone()).unwrap_or_default(),
            roles: entity.map(|e| e.roles.clone()).unwrap_or_default(),
        }
    }

    pub fn is_auth(&self) -> bool {
        self.auth
    }

    /// Its route takes `auth: BearerAuth`, its params an `actor`.
    pub fn is_protected(&self, use_case: &str) -> bool {
        self.auth && !self.public.iter().any(|p| p == use_case)
    }

    /// The roles its route requires (`#[requiere_rol]`), none when public.
    pub fn roles(&self, use_case: &str) -> Vec<String> {
        if !self.is_protected(use_case) {
            return vec![];
        }
        self.roles.get(use_case).cloned().unwrap_or_default()
    }

    /// Every role any of `use_cases` requires, once each — what the route tests' caller holds.
    pub fn all_roles(&self, use_cases: &[String]) -> Vec<String> {
        let mut all: Vec<String> = vec![];
        for role in use_cases.iter().flat_map(|uc| self.roles(uc)) {
            if !all.contains(&role) {
                all.push(role);
            }
        }
        all
    }
}

/// A host name has no `_`: the development user's address has to be one.
fn host(project: &str) -> String {
    project.replace('_', "-")
}

fn render_static(template: &str) -> Result<String, Box<dyn std::error::Error>> {
    Ok(render(template, &tera::Context::new())?)
}

/// A template that differs between the modes: `local` adds what registering and signing in need.
fn render_for(template: &str, local: bool) -> Result<String, Box<dyn std::error::Error>> {
    let mut ctx = tera::Context::new();
    ctx.insert("local", &local);
    Ok(render(template, &ctx)?)
}

/// One crate of the `usuario` context: its files, written once, then its `Cargo.toml`, `lib.rs`
/// and workspace entry (ADR-0023: it is not an `[[entidad]]`, so no scaffold or `cableado` entity
/// block touches it).
fn write_user_crate(
    base: &Path,
    layer: Layer,
    files: &[(&str, String)],
    db: bool,
    local: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    let c = layout::USER_CONTEXT;
    let dir = layout::crate_dir(c, layer);
    for (file, content) in files {
        write_file(&base.join(&dir).join("src").join(file), content)?;
    }
    let template = match layer {
        Layer::Domain => "contexto/usuario_dominio_cargo.tera",
        Layer::Application => "contexto/usuario_aplicacion_cargo.tera",
        Layer::Infrastructure => "contexto/usuario_infra_cargo.tera",
        Layer::Rest => "contexto/usuario_rest_cargo.tera",
    };
    let mut ctx = tera::Context::new();
    ctx.insert("db", &db);
    ctx.insert("local", &local);
    write_file(
        &base.join(layout::crate_manifest(c, layer)),
        &render(template, &ctx)?,
    )?;
    crate::generators::crates::write_lib(base, &dir, "")?;
    crate::generators::crates::add_internal_dependency(base, &layout::crate_name(c, layer), &dir)?;
    crate::generators::crates::sync_workspace_dependencies(base)?;
    Ok(())
}

/// The `Usuario` entity: model, errors and repository port.
fn write_user_domain(base: &Path, db: bool, local: bool) -> Result<(), Box<dyn std::error::Error>> {
    write_user_crate(
        base,
        Layer::Domain,
        &[
            ("modelo.rs", render_for("auth/user_model.tera", local)?),
            ("errores.rs", render_for("auth/user_errors.tera", local)?),
            (
                "repositorio.rs",
                render_static("auth/user_repository.tera")?,
            ),
        ],
        db,
        local,
    )
}

/// The user repository (in memory, or Postgres with its migration) and, for OIDC, the provisioning
/// authenticator.
fn write_user_infrastructure(
    base: &Path,
    db: bool,
    local: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    let repository = if db {
        "auth/user_repository_pg.tera"
    } else {
        "auth/user_repository_inmemory.tera"
    };
    let mut files = vec![("repositorio.rs", render_static(repository)?)];
    if !local {
        files.push((
            "aprovisionamiento.rs",
            render_static("auth/provisioning.tera")?,
        ));
    }
    write_user_crate(base, Layer::Infrastructure, &files, db, local)?;
    if db {
        crate::generators::migration::run_migration(
            "crear_tabla_usuarios",
            base,
            Some(&render_static("auth/users.sql")?),
        )?;
    }
    Ok(())
}

/// The local use cases: one file each, contract and implementation (ADR-0023 § 4).
const LOCAL_USE_CASES: [&str; 3] = ["login", "logout", "registrar_usuario"];

/// The `usuario` REST crate: `GET /me` and, with local accounts, `POST /auth/*`.
fn write_user_rest(base: &Path, db: bool, local: bool) -> Result<(), Box<dyn std::error::Error>> {
    let mut files = vec![
        ("me.rs", render_static("auth/me.tera")?),
        ("me/dto.rs", render_static("auth/me_dto.tera")?),
        ("me/rutas.rs", render_static("auth/me_routes.tera")?),
    ];
    if local {
        for (file, template) in [
            ("auth.rs", "auth_api"),
            ("auth/dto.rs", "auth_dto"),
            ("auth/errores.rs", "auth_error_mapper"),
            ("auth/respuestas.rs", "auth_responses"),
            ("auth/rutas.rs", "auth_routes"),
        ] {
            files.push((file, render_static(&format!("auth/local/{template}.tera"))?));
        }
    }
    write_user_crate(base, Layer::Rest, &files, db, local)
}

/// Local accounts (`PLAN.md` § 16.6): `registrar_usuario`, `login`, `logout`, the credentials and
/// sessions migration after `users`'.
fn write_local_accounts(base: &Path) -> Result<(), Box<dyn std::error::Error>> {
    let mut files = vec![];
    for uc in LOCAL_USE_CASES {
        files.push((
            uc,
            crate::generators::merge::merge_use_case(
                &render_static(&format!("auth/local/{uc}_trait.tera"))?,
                &render_static(&format!("auth/local/{uc}.tera"))?,
            ),
        ));
    }
    let files: Vec<(String, String)> = files
        .into_iter()
        .map(|(uc, content)| (format!("{uc}.rs"), content))
        .collect();
    let files: Vec<(&str, String)> = files.iter().map(|(f, c)| (f.as_str(), c.clone())).collect();
    write_user_crate(base, Layer::Application, &files, true, true)?;

    crate::generators::migration::run_migration(
        "crear_tablas_auth",
        base,
        Some(&render_static("auth/local/auth.sql")?),
    )?;
    Ok(())
}

/// `puerto-auth` and the `usuario` crates for the `api` binary: `bootstrap.rs` builds the
/// authenticator, the user repository and, with local accounts, the use cases and the hasher.
fn add_dependencies(base: &Path, local: bool) -> Result<(), Box<dyn std::error::Error>> {
    use crate::generators::bootstrap::add_crate_dependency;
    add_workspace_dependency(base, "puerto-auth")?;
    let feature = if local { "local" } else { "oidc" };
    add_crate_dependency(
        base,
        layout::API_MANIFEST,
        "dependencies",
        "puerto-auth",
        &format!("{{ workspace = true, features = [\"{feature}\"] }}"),
    )?;
    add_crate_dependency(
        base,
        layout::API_MANIFEST,
        "dependencies",
        "puerto-nucleo",
        "{ workspace = true }",
    )?;
    let c = layout::USER_CONTEXT;
    let mut layers = vec![Layer::Domain, Layer::Infrastructure, Layer::Rest];
    if local {
        layers.push(Layer::Application);
    }
    for layer in layers {
        add_crate_dependency(
            base,
            layout::API_MANIFEST,
            "dependencies",
            &layout::crate_name(c, layer),
            "{ workspace = true }",
        )?;
    }
    if local {
        // The spec export test builds `AuthApi` from the use cases' doubles.
        add_crate_dependency(
            base,
            layout::API_MANIFEST,
            "dev-dependencies",
            &layout::crate_name(c, Layer::Application),
            "{ workspace = true, features = [\"pruebas\"] }",
        )?;
    }
    Ok(())
}

/// Inserts `line` after the first line of `manifest` that is exactly `after`.
fn insert_after_line(manifest: &str, after: &str, line: &str) -> Option<String> {
    let at = manifest.find(&format!("{after}\n"))? + after.len() + 1;
    Some(format!("{}{line}\n{}", &manifest[..at], &manifest[at..]))
}

fn keycloak_service(project: &str) -> String {
    format!(
        r#"  keycloak:
    image: {KEYCLOAK_IMAGE}
    container_name: {project}-keycloak
    # Importa keycloak/realm.json en el primer arranque: el cliente `web`, el
    # rol `admin` y un usuario que lo tiene (`make auth/ui`).
    command: start-dev --import-realm
    ports:
      - "{KEYCLOAK_PORT}:8080"
    environment:
      KC_BOOTSTRAP_ADMIN_USERNAME: admin
      KC_BOOTSTRAP_ADMIN_PASSWORD: admin
      KC_HEALTH_ENABLED: "true"
    volumes:
      - ./keycloak:/opt/keycloak/data/import:ro
    restart: unless-stopped
    healthcheck:
      # La imagen no trae curl: el /dev/tcp de bash pregunta al puerto de gestión.
      test: ["CMD-SHELL", "exec 3<>/dev/tcp/localhost/9000 && printf 'GET /health/ready HTTP/1.0\r\n\r\n' >&3 && grep -q UP <&3"]
      interval: 5s
      timeout: 5s
      retries: 30
"#
    )
}

fn write_keycloak(base: &Path, project: &str) -> Result<(), Box<dyn std::error::Error>> {
    let mut ctx = tera::Context::new();
    ctx.insert("realm", project);
    ctx.insert("project", project);
    ctx.insert("host", &host(project));
    ctx.insert("password", DEV_PASSWORD);
    write_file(
        &base.join(layout::KEYCLOAK_REALM),
        &render("auth/realm.json.tera", &ctx)?,
    )?;
    add_compose_service(base, "keycloak", &keycloak_service(project))
}

fn add_env(base: &Path, project: &str) -> Result<(), Box<dyn std::error::Error>> {
    let vars = format!(
        "\n# Auth — el proveedor OpenID Connect (ADR-0021). Keycloak (docker-compose.yml) en\n\
         # desarrollo: `make auth/ui`; inicia sesión como admin@{host}.localhost / {DEV_PASSWORD}.\n\
         # Auth0: AUTH_ISSUER=https://<tenant>.auth0.com/, AUTH_AUDIENCE=<el identificador de la API>,\n\
         # AUTH_ROLES_CLAIM=<el claim que añade una Action, p. ej. https://example.com/roles>\n\
         AUTH_ISSUER=http://localhost:{KEYCLOAK_PORT}/realms/{project}\n\
         AUTH_AUDIENCE={AUDIENCE}\nAUTH_ROLES_CLAIM=realm_access.roles\n",
        host = host(project)
    );
    for file in [".env", ".env.example"] {
        let path = base.join(file);
        let mut content = fs::read_to_string(&path).unwrap_or_default();
        if content.contains("AUTH_ISSUER") {
            continue;
        }
        if !content.is_empty() && !content.ends_with('\n') {
            content.push('\n');
        }
        content.push_str(&vars);
        fs::write(&path, content)?;
    }
    Ok(())
}

/// `AUTH_SESSION_TTL`, read by `puerto_sqlx::auth::ttl_sesion_from_env`.
fn add_local_env(base: &Path) -> Result<(), Box<dyn std::error::Error>> {
    let vars = "\n# Auth — las cuentas, en esta base de datos (ADR-0021). Lo que dura una sesión: 30d, 12h, 90m.\n\
                AUTH_SESSION_TTL=30d\n";
    for file in [".env", ".env.example"] {
        let path = base.join(file);
        let mut content = fs::read_to_string(&path).unwrap_or_default();
        if content.contains("AUTH_SESSION_TTL") {
            continue;
        }
        if !content.is_empty() && !content.ends_with('\n') {
            content.push('\n');
        }
        content.push_str(vars);
        fs::write(&path, content)?;
    }
    Ok(())
}

/// The compose targets a project without a database lacks (a `--db` one has them already).
const COMPOSE_MAKEFILE_TARGETS: &str = r#"
.PHONY: docker-compose/up docker-compose/down

DOCKER_COMPOSE := docker compose

docker-compose/up: ## Arranca los contenedores
	@echo "${CYAN}Arrancando los contenedores...${NC}"
	$(DOCKER_COMPOSE) up -d --wait

docker-compose/down: ## Para los contenedores
	@echo "${CYAN}Parando los contenedores...${NC}"
	$(DOCKER_COMPOSE) down

# El `run` de la plantilla conserva su receta; aquí gana el proveedor que descubre `bootstrap.rs`.
run: docker-compose/up
"#;

fn auth_makefile_targets(project: &str) -> String {
    format!(
        r#"
.PHONY: auth/ui auth/token

auth/ui: docker-compose/up ## La consola de Keycloak: usuarios, roles, sesiones
	@echo "${{GREEN}}Keycloak: http://localhost:{KEYCLOAK_PORT}/admin/master/console/#/{project} (admin / admin)${{NC}}"
	@echo "${{GREEN}}Inicia sesión en la app como admin@{host}.localhost / {DEV_PASSWORD} (rol admin)${{NC}}"

# El cliente `dev-cli` del realm de desarrollo (password grant): curl -H "Authorization: Bearer
# $(make -s auth/token)" … Un EMAIL del entorno de la shell no se toma por uno.
AUTH_ISSUER ?= $(shell sed -n 's/^AUTH_ISSUER=//p' .env 2>/dev/null)
TOKEN_EMAIL = $(if $(filter command line,$(origin EMAIL)),$(EMAIL),admin@{host}.localhost)

auth/token: ## Un token de acceso de desarrollo: make -s auth/token [EMAIL=user@{host}.localhost]
	@curl -sf "$(AUTH_ISSUER)/protocol/openid-connect/token" -d grant_type=password -d client_id=dev-cli \
		--data-urlencode "username=$(TOKEN_EMAIL)" --data-urlencode "password={DEV_PASSWORD}" \
		| sed -n 's/.*"access_token":"\([^"]*\)".*/\1/p' | grep . \
		|| {{ echo "${{RED}}sin token para $(TOKEN_EMAIL) de $(AUTH_ISSUER): ¿está arrancado Keycloak? make docker-compose/up${{NC}}" >&2; exit 1; }}
"#,
        host = host(project)
    )
}

/// `make auth/conceder`: a role is a row's `roles` entry in a local project, applied from the next
/// request (no cache in between).
fn local_makefile_targets(project: &str) -> String {
    let db_name = project.replace('-', "_");
    format!(
        r#"
.PHONY: auth/conceder

auth/conceder: docker-compose/up ## Concede un rol a una cuenta: make auth/conceder EMAIL=ada@example.com ROL=admin
	@test -n "$(EMAIL)" -a -n "$(ROL)" || {{ echo "${{RED}}uso: make auth/conceder EMAIL=… ROL=…${{NC}}"; exit 1; }}
	$(DOCKER_COMPOSE) exec -T postgres psql -U devuser -d {db_name} -v ON_ERROR_STOP=1 -c \
		"UPDATE usuarios SET roles = array_append(roles, '$(ROL)'), actualizado_en = now() WHERE emisor = 'local' AND sujeto = lower(trim('$(EMAIL)')) AND NOT '$(ROL)' = ANY(roles)"
"#
    )
}

fn add_makefile_targets(
    base: &Path,
    project: &str,
    local: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    let path = base.join("Makefile");
    if !path.exists() {
        return Ok(());
    }
    let mut src = fs::read_to_string(&path)?;
    if src.contains("auth/ui:") || src.contains("auth/conceder:") {
        return Ok(());
    }
    if !src.ends_with('\n') {
        src.push('\n');
    }
    if !src.contains("docker-compose/up:") {
        src.push_str(COMPOSE_MAKEFILE_TARGETS);
    }
    src.push_str(&if local {
        local_makefile_targets(project)
    } else {
        auth_makefile_targets(project)
    });
    fs::write(&path, src)?;
    Ok(())
}

/// The frontend's auth files of both modes, written once under `web/` (their templates mirror the
/// paths). The last three replace the template's own: the app runs inside `AuthProvider`, the
/// layout shows the user, and `renderScreen` signs a caller in.
const WEB_FILES: [&str; 11] = [
    "src/domain/auth/session.ts",
    "src/infrastructure/auth/bearer-middleware.ts",
    "src/infrastructure/auth/bearer-middleware.test.ts",
    "src/presentation/core/auth.tsx",
    "src/presentation/core/auth.test.tsx",
    "src/presentation/shared/ui/user-menu.tsx",
    "src/presentation/shared/ui/user-menu.test.tsx",
    "src/test-support/auth.ts",
    "src/main.tsx",
    "src/presentation/shared/ui/layout.tsx",
    "src/test-support/render.tsx",
];

/// An OIDC project's: the provider's client and the page it sends the user back to.
const OIDC_WEB_FILES: [&str; 4] = [
    "src/infrastructure/auth/oidc-auth-gateway.ts",
    "src/infrastructure/auth/oidc-auth-gateway.test.ts",
    "src/presentation/pages/auth-callback.tsx",
    "src/presentation/pages/auth-callback.test.tsx",
];

/// A local project's: the `Accounts` port, the gateway over `/auth/*`, the sign-in and sign-up pages.
const LOCAL_WEB_FILES: [&str; 8] = [
    "src/domain/auth/accounts.ts",
    "src/domain/auth/messages.ts",
    "src/infrastructure/auth/local-auth-gateway.ts",
    "src/infrastructure/auth/local-auth-gateway.test.ts",
    "src/presentation/pages/login.tsx",
    "src/presentation/pages/login.test.tsx",
    "src/presentation/pages/register.tsx",
    "src/presentation/pages/register.test.tsx",
];

/// The dependency-cruiser rule that keeps the provider's client behind `AuthGateway`.
const CRUISER_RULE: &str = r#"    {
      name: "only-the-auth-adapter-imports-oidc-client-ts",
      comment:
        "The provider's client stays behind AuthGateway: screens see a CurrentUser, never a token.",
      severity: "error",
      from: { pathNot: "^src/infrastructure/auth/" },
      to: { path: "oidc-client-ts" },
    },
"#;

/// `oidc-client-ts` in `web/package.json`, before `openapi-fetch` (dependencies stay sorted).
fn add_oidc_client(package: &str) -> Option<String> {
    if package.contains("\"oidc-client-ts\"") {
        return Some(package.to_string());
    }
    let at = package.find("    \"openapi-fetch\": ")?;
    Some(format!(
        "{}    \"oidc-client-ts\": \"{OIDC_CLIENT_TS_VERSION}\",\n{}",
        &package[..at],
        &package[at..]
    ))
}

/// The provider and client `oidc-client-ts` signs in with — read by `oidcSettingsFromEnv()`.
fn web_env(project: &str) -> String {
    format!(
        "# Auth — el proveedor OpenID Connect con el que inicia sesión el navegador (ADR-0021).\n\
         # Keycloak en desarrollo; Auth0: VITE_AUTH_ISSUER=https://<tenant>.auth0.com/,\n\
         # VITE_AUTH_CLIENT_ID=<el client id de la SPA>, VITE_AUTH_AUDIENCE=<el identificador de la\n\
         # API, el AUTH_AUDIENCE de la API>.\n\
         VITE_AUTH_ISSUER=http://localhost:{KEYCLOAK_PORT}/realms/{project}\n\
         VITE_AUTH_CLIENT_ID=web\n\
         VITE_AUTH_AUDIENCE=\n"
    )
}

/// The frontend half (`PLAN.md` § 16.5, § 16.7): sign in — with the provider, or with a password
/// — send the token, guard screens.
fn write_web(base: &Path, project: &str, local: bool) -> Result<(), Box<dyn std::error::Error>> {
    let web = base.join("web");
    let mode_files: &[&str] = if local {
        &LOCAL_WEB_FILES
    } else {
        &OIDC_WEB_FILES
    };
    for file in WEB_FILES.iter().chain(mode_files) {
        write_file(
            &web.join(file),
            &render_for(&format!("auth/web/{file}.tera"), local)?,
        )?;
    }
    if local {
        return Ok(());
    }

    let path = web.join("package.json");
    let package = fs::read_to_string(&path)?;
    let package = add_oidc_client(&package)
        .ok_or_else(|| format!("{}: no `openapi-fetch` dependency", path.display()))?;
    fs::write(&path, package)?;

    let path = web.join(".dependency-cruiser.cjs");
    let cruiser = fs::read_to_string(&path)?;
    if !cruiser.contains("oidc-client-ts") {
        let cruiser = insert_after_line(&cruiser, "  forbidden: [", CRUISER_RULE.trim_end())
            .ok_or_else(|| format!("{}: no `forbidden: [`", path.display()))?;
        fs::write(&path, cruiser)?;
    }

    for file in [".env", ".env.example"] {
        let path = web.join(file);
        let mut content = fs::read_to_string(&path).unwrap_or_default();
        if content.contains("VITE_AUTH_ISSUER") {
            continue;
        }
        if !content.is_empty() && !content.ends_with('\n') {
            content.push('\n');
        }
        content.push_str(&web_env(project));
        fs::write(&path, content)?;
    }
    Ok(())
}

/// The root `AGENTS.md` section an auth project gets (ADR-0010: what it names is tested).
pub const AGENTS_AUTH_SECTION: &str = r#"## Auth (`[proyecto] auth`)

La API es un servidor de recursos OAuth2 (ADR-0021 de Puerto): verifica el token bearer de cada
petición protegida contra el proveedor de `AUTH_ISSUER`: Keycloak en desarrollo
(`make docker-compose/up`, `make auth/ui`), Auth0 o cualquier proveedor OpenID Connect en
producción.

- Un handler que recibe `auth: BearerAuth` está protegido: sin token o con uno malo se responde
  `401 auth.no_autenticado` antes de ejecutarlo. Un `#[requiere_rol("admin")]` encima añade la
  comprobación `403 auth.prohibido`. Las dos cosas vienen de `puerto_web`.
- `puerto generar scaffold <Nombre>` protege todas las rutas CRUD. `--publico
  listar_productos,obtener_producto` deja rutas abiertas; `--rol borrar_producto=admin` añade
  `#[requiere_rol]` (dos roles en una ruta: quien llama necesita los dos). Las dos opciones quedan
  en `publicos` / `roles` de la entidad en `puerto.toml`, y `puerto validar` comprueba que
  `rutas.rs` sigue diciendo lo mismo: cambia los dos a la vez.
- Los params de un caso de uso protegido llevan `actor: Principal` (el handler pasa `auth.0`); los
  tests construyen uno con `puerto_nucleo::auth::dobles::MadrePrincipal`. `generar caso-de-uso`
  también lo añade.
- **Los roles salen solo del token** (`AUTH_ROLES_CLAIM`); la columna `usuarios.roles` no se lee.
- Para llamar a la API a mano: `curl -H "Authorization: Bearer $(make -s auth/token)" …`;
  `admin@…` tiene `admin`, y `make -s auth/token EMAIL=user@…` ningún rol. Lo pide al cliente
  `dev-cli` del realm de desarrollo (password grant): no crees nunca un cliente así en un proveedor
  real.
- Cada quien llama es un `Usuario` (`contextos/usuario/dominio/`), que crea en su primera petición
  `contextos/usuario/infraestructura/src/aprovisionamiento.rs`. Señala a una persona con un
  `IdUsuario`, nunca con el sujeto del proveedor. Estos ficheros son tuyos.
- `GET /me` responde quién llama, tal como lo resolvió la API. El frontend lo lee a él, nunca el
  token.
- Una regla que depende de los datos («solo edita el propietario») va en el caso de uso, no en un
  atributo.

"#;

/// The root `AGENTS.md` section of a project that keeps its own accounts (`--auth local`).
pub const AGENTS_AUTH_LOCAL_SECTION: &str = r#"## Auth (`[proyecto] auth`)

La API guarda sus propias cuentas (ADR-0021 de Puerto): `POST /auth/registro` crea una,
`POST /auth/login` responde un token de sesión opaco y `POST /auth/logout` lo revoca. Un cliente
envía el token como `Authorization: Bearer <token>`; la base de datos solo guarda su SHA-256.

- Un handler que recibe `auth: BearerAuth` está protegido: sin sesión, o con una desconocida o
  caducada, se responde `401 auth.no_autenticado` antes de ejecutarlo. Un `#[requiere_rol("admin")]`
  encima añade la comprobación `403 auth.prohibido`. Las dos cosas vienen de `puerto_web`.
- `puerto generar scaffold <Nombre>` protege todas las rutas CRUD. `--publico
  listar_productos,obtener_producto` deja rutas abiertas; `--rol borrar_producto=admin` añade
  `#[requiere_rol]` (dos roles en una ruta: quien llama necesita los dos). Las dos opciones quedan
  en `publicos` / `roles` de la entidad en `puerto.toml`, y `puerto validar` comprueba que
  `rutas.rs` sigue diciendo lo mismo: cambia los dos a la vez.
- Los params de un caso de uso protegido llevan `actor: Principal` (el handler pasa `auth.0`); los
  tests construyen uno con `puerto_nucleo::auth::dobles::MadrePrincipal`. `generar caso-de-uso`
  también lo añade.
- **Los roles son la columna `usuarios.roles`**: `make auth/conceder EMAIL=… ROL=…`. Se aplican
  desde la petición siguiente.
- Las contraseñas son hashes argon2id en `credenciales`; `Usuario` no lleva ningún secreto. Un login
  con un email desconocido y otro con una contraseña equivocada reciben el mismo
  `401 auth.credenciales_invalidas`, en el mismo tiempo. Las sesiones duran `AUTH_SESSION_TTL`
  (`.env`); las filas caducadas se rechazan, y podarlas te toca a ti.
- `registrar_usuario`, `login`, `logout` (`contextos/usuario/aplicacion/`) y
  `contextos/usuario/rest/src/auth/` son tuyos. El registro está abierto: ciérralo, o añade reglas,
  ahí.
- `GET /me` responde quién llama, tal como lo resolvió la API.
- Una regla que depende de los datos («solo edita el propietario») va en el caso de uso, no en un
  atributo.

"#;

/// What follows [`AGENTS_AUTH_SECTION`] in a project with `web/`.
pub const AGENTS_AUTH_WEB_SECTION: &str = r#"### Auth en `web/`

- `useAuth()` (`presentation/core/auth.tsx`) da `{ user, can(role), login, logout }`; `user` es
  `GET /me`, nunca el token. Las pantallas generadas esconden lo que `can` niega, pero decide la
  API: su `403` llega a la alerta como `auth.prohibido`.
- `core/generated/routes.tsx` envuelve cada pantalla no pública en `<RequireAuth>`, que inicia
  sesión y vuelve. Envuelve igual tus propias rutas en `routes.tsx`.
- El token se queda en memoria y `oidc-client-ts` se queda en `infrastructure/auth/`
  (`make web/lint` lo comprueba): al recargar se vuelve a iniciar sesión con la sesión del
  proveedor. `web/.env` guarda `VITE_AUTH_*`.
- Los tests de pantalla inician sesión con alguien que tiene todos los roles (`renderScreen`); pasa
  `{ user }` para otro (`CurrentUserMother`), o `{ auth: fakeAuthGateway(…) }` para ver los inicios
  de sesión que lanza.

"#;

/// What follows [`AGENTS_AUTH_LOCAL_SECTION`] in a project with `web/`.
pub const AGENTS_AUTH_LOCAL_WEB_SECTION: &str = r#"### Auth en `web/`

- `useAuth()` (`presentation/core/auth.tsx`) da `{ user, can(role), login, logout }`; `user` es
  `GET /me`. Las pantallas generadas esconden lo que `can` niega, pero decide la API: su `403` llega
  a la alerta como `auth.prohibido`.
- `core/generated/routes.tsx` envuelve cada pantalla no pública en `<RequireAuth>`, que manda a
  quien no tiene sesión iniciada a `/login?next=…`, igual que un `401` de cualquier llamada.
  Envuelve igual tus propias rutas en `routes.tsx`.
- `/login` y `/register` (`presentation/pages/`) inician sesión a través de `Accounts`
  (`domain/auth/accounts.ts`); `domain/auth/messages.ts` convierte los códigos de la API en lo que
  dicen los formularios, bajo el campo al que se refieren. Estos ficheros son tuyos.
- El token de sesión está en `localStorage` (`infrastructure/auth/local-auth-gateway.ts`): al
  recargar se sigue con la sesión iniciada; «Sign out» la revoca en la API.
- Los tests de pantalla inician sesión con alguien que tiene todos los roles (`renderScreen`); pasa
  `{ user }` para otro (`CurrentUserMother`), `{ auth: fakeAuthGateway(…) }` o
  `{ accounts: fakeAccounts() }` para ver lo que pidió una pantalla.

"#;

fn add_agents_section(
    base: &Path,
    web: bool,
    local: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    let path = base.join("AGENTS.md");
    let Ok(agents) = fs::read_to_string(&path) else {
        return Ok(());
    };
    if agents.contains("## Auth (`[proyecto] auth`)") {
        return Ok(());
    }
    let section = if local && web {
        format!("{AGENTS_AUTH_LOCAL_SECTION}{AGENTS_AUTH_LOCAL_WEB_SECTION}")
    } else if local {
        AGENTS_AUTH_LOCAL_SECTION.to_string()
    } else if web {
        format!("{AGENTS_AUTH_SECTION}{AGENTS_AUTH_WEB_SECTION}")
    } else {
        AGENTS_AUTH_SECTION.to_string()
    };
    // Before the conventions: next to the rules about what is generated.
    let out = match agents.find("## Convenciones") {
        Some(at) => format!("{}{section}{}", &agents[..at], &agents[at..]),
        None => format!("{agents}\n{section}"),
    };
    fs::write(&path, out)?;
    Ok(())
}

/// Turns a fresh project into an auth one (`puerto nuevo --auth oidc|local`). The Saludo demo
/// stays open: it is listed `public`.
pub fn apply_auth_to_new_project(
    base: &Path,
    mode: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    check_mode(mode)?;
    let mut config = crate::puerto_toml::read(base)?;
    config.project.auth = Some(mode.to_string());
    for entity in &mut config.entity {
        if entity.name == "Saludo" && entity.public.is_empty() {
            entity.public = entity.use_cases.clone();
        }
    }
    crate::puerto_toml::write(base, &config)?;
    let project = config.project.name.clone();

    let local = mode == "local";

    write_user_domain(base, config.project.db, local)?;
    write_user_infrastructure(base, config.project.db, local)?;
    write_user_rest(base, config.project.db, local)?;
    add_dependencies(base, local)?;
    if local {
        write_local_accounts(base)?;
        add_local_env(base)?;
    } else {
        write_keycloak(base, &project)?;
        add_env(base, &project)?;
    }
    add_makefile_targets(base, &project, local)?;
    if config.project.web {
        write_web(base, &project, local)?;
    }
    add_agents_section(base, config.project.web, local)?;

    crate::generators::bootstrap::regenerate_bootstrap(base)?;
    Ok(())
}

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

    #[test]
    fn should_write_local_accounts_only_with_a_database() {
        assert!(check_new_project("oidc", false).is_ok());
        assert!(check_new_project("local", true).is_ok());
        assert!(
            check_new_project("local", false)
                .unwrap_err()
                .contains("--db")
        );
        assert!(
            check_new_project("auth0", true)
                .unwrap_err()
                .contains("unknown auth mode")
        );
    }

    #[test]
    fn should_insert_after_the_exact_line_when_the_manifest_has_it() {
        let manifest = "[dependencies]\npuerto-nucleo = { workspace = true }\npoem = \"3\"\n";
        assert_eq!(
            insert_after_line(manifest, "puerto-nucleo = { workspace = true }", "x = 1"),
            Some(
                "[dependencies]\npuerto-nucleo = { workspace = true }\nx = 1\npoem = \"3\"\n"
                    .into()
            )
        );
        assert_eq!(
            insert_after_line(manifest, "[dev-dependencies]", "x = 1"),
            None
        );
    }

    fn access(auth: bool, public: &[&str], roles: &[(&str, &[&str])]) -> Access {
        Access {
            auth,
            public: public.iter().map(|s| s.to_string()).collect(),
            roles: roles
                .iter()
                .map(|(uc, r)| (uc.to_string(), r.iter().map(|s| s.to_string()).collect()))
                .collect(),
        }
    }

    #[test]
    fn should_protect_every_use_case_but_the_public_ones_when_the_project_has_auth() {
        let access = access(true, &["listar_products"], &[]);
        assert!(access.is_protected("crear_product"));
        assert!(!access.is_protected("listar_products"));
    }

    #[test]
    fn should_protect_nothing_when_the_project_has_no_auth() {
        let access = access(false, &[], &[("crear_product", &["admin"])]);
        assert!(!access.is_protected("crear_product"));
        assert_eq!(access.roles("crear_product"), Vec::<String>::new());
    }

    #[test]
    fn should_collect_each_role_once_when_use_cases_share_it() {
        let access = access(
            true,
            &[],
            &[
                ("crear_product", &["admin"]),
                ("borrar_product", &["admin", "owner"]),
            ],
        );
        let use_cases = ["crear_product".to_string(), "borrar_product".to_string()];
        assert_eq!(access.roles("borrar_product"), vec!["admin", "owner"]);
        assert_eq!(access.all_roles(&use_cases), vec!["admin", "owner"]);
    }

    #[test]
    fn should_add_the_oidc_client_before_openapi_fetch_once() {
        let package =
            "  \"dependencies\": {\n    \"openapi-fetch\": \"0.17.0\",\n    \"react\": \"19\"\n";
        let added = add_oidc_client(package).unwrap();
        assert_eq!(
            added,
            format!(
                "  \"dependencies\": {{\n    \"oidc-client-ts\": \"{OIDC_CLIENT_TS_VERSION}\",\n    \"openapi-fetch\": \"0.17.0\",\n    \"react\": \"19\"\n"
            )
        );
        assert_eq!(add_oidc_client(&added).unwrap(), added);
        assert_eq!(add_oidc_client("{}"), None);
    }

    #[test]
    fn should_make_a_host_name_when_the_project_name_has_underscores() {
        assert_eq!(host("my_shop"), "my-shop");
    }
}