auths-cli 0.0.1-rc.8

Command-line interface for Auths decentralized identity system
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
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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
use anyhow::{Context, Result, anyhow};
use auths_core::crypto::signer::decrypt_keypair;
use auths_id::attestation::create::create_signed_attestation;
use auths_id::attestation::revoke::create_signed_revocation;
use auths_id::identity::initialize::initialize_registry_identity;
use auths_id::identity::resolve::DidResolver;
use chrono::{DateTime, Utc};
use clap::{ArgAction, Parser, Subcommand};
use serde_json;
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;

use auths_core::signing::{PassphraseProvider, StorageSigner};
use auths_core::storage::keychain::{KeyAlias, get_platform_keychain};
use auths_id::{
    attestation::{export::AttestationSink, group::AttestationGroup, verify::verify_with_resolver},
    identity::resolve::DefaultDidResolver,
    storage::git_refs::AttestationMetadata,
    storage::{
        attestation::AttestationSource,
        identity::IdentityStorage,
        layout::{self, StorageLayoutConfig},
    },
};

use auths_sdk::workflows::org::{Role, member_role_order};
use auths_storage::git::{
    GitRegistryBackend, RegistryAttestationStorage, RegistryConfig, RegistryIdentityStorage,
};
use auths_verifier::types::DeviceDID;
use auths_verifier::{Capability, Ed25519PublicKey, Prefix};

use clap::ValueEnum;

/// CLI-level role wrapper that derives `ValueEnum` for argument parsing.
///
/// Converts to `auths_sdk::workflows::org::Role` at the CLI boundary.
#[derive(Debug, Clone, Copy, ValueEnum, PartialEq, Eq)]
pub enum CliRole {
    Admin,
    Member,
    Readonly,
}

impl From<CliRole> for Role {
    fn from(r: CliRole) -> Self {
        match r {
            CliRole::Admin => Role::Admin,
            CliRole::Member => Role::Member,
            CliRole::Readonly => Role::Readonly,
        }
    }
}

/// The `org` subcommand, handling member authorizations.
#[derive(Parser, Debug, Clone)]
pub struct OrgCommand {
    #[clap(subcommand)]
    pub subcommand: OrgSubcommand,

    #[command(flatten)]
    pub overrides: crate::commands::registry_overrides::RegistryOverrides,
}

/// Subcommands for managing authorizations issued by this identity.
#[derive(Subcommand, Debug, Clone)]
pub enum OrgSubcommand {
    /// Create a new organization identity
    #[command(visible_alias = "init")]
    Create {
        /// Organization name
        #[arg(long)]
        name: String,

        /// Alias for the local signing key (auto-generated if not provided)
        #[arg(long)]
        local_key_alias: Option<String>,

        /// Optional metadata file (if provided, merged with org metadata)
        #[arg(long)]
        metadata_file: Option<PathBuf>,
    },
    Attest {
        #[arg(long = "subject-did", visible_alias = "subject")]
        subject_did: String,
        #[arg(long)]
        payload_file: PathBuf,
        #[arg(long)]
        note: Option<String>,
        #[arg(long)]
        expires_at: Option<String>,
        #[arg(long)]
        signer_alias: Option<String>,
    },
    Revoke {
        #[arg(long = "subject-did", visible_alias = "subject")]
        subject_did: String,
        #[arg(long)]
        note: Option<String>,
        #[arg(long)]
        signer_alias: Option<String>,
    },
    Show {
        #[arg(long = "subject-did", visible_alias = "subject")]
        subject_did: String,
        #[arg(long, action = ArgAction::SetTrue)]
        include_revoked: bool,
    },
    List {
        #[arg(long, action = ArgAction::SetTrue)]
        include_revoked: bool,
    },
    /// Add a member to an organization
    AddMember {
        /// Organization identity ID
        #[arg(long)]
        org: String,

        /// Member identity ID to add
        #[arg(long = "member-did", visible_alias = "member")]
        member_did: String,

        /// Role to assign (admin, member, readonly)
        #[arg(long, value_enum)]
        role: CliRole,

        /// Override default capabilities (comma-separated)
        #[arg(long, value_delimiter = ',')]
        capabilities: Option<Vec<String>>,

        /// Alias of the signing key in keychain
        #[arg(long)]
        signer_alias: Option<String>,

        /// Optional note for the authorization
        #[arg(long)]
        note: Option<String>,
    },

    /// Revoke a member from an organization
    RevokeMember {
        /// Organization identity ID
        #[arg(long)]
        org: String,

        /// Member identity ID to revoke
        #[arg(long = "member-did", visible_alias = "member")]
        member_did: String,

        /// Reason for revocation
        #[arg(long)]
        note: Option<String>,

        /// Alias of the signing key in keychain
        #[arg(long)]
        signer_alias: Option<String>,

        /// Preview actions without making changes.
        #[arg(long)]
        dry_run: bool,
    },

    /// List members of an organization
    ListMembers {
        /// Organization identity ID
        #[arg(long)]
        org: String,

        /// Include revoked members
        #[arg(long, action = ArgAction::SetTrue)]
        include_revoked: bool,
    },
}

/// Handles `org` commands for issuing or revoking member authorizations.
pub fn handle_org(
    cmd: OrgCommand,
    repo_opt: Option<PathBuf>,
    identity_ref_override: Option<String>,
    identity_blob_name_override: Option<String>,
    attestation_prefix_override: Option<String>,
    attestation_blob_name_override: Option<String>,
    passphrase_provider: Arc<dyn PassphraseProvider + Send + Sync>,
) -> Result<()> {
    let repo_path = layout::resolve_repo_path(repo_opt)?;

    let mut config = StorageLayoutConfig::default();
    if let Some(r) = identity_ref_override {
        config.identity_ref = r.into();
    }
    if let Some(b) = identity_blob_name_override {
        config.identity_blob_name = b.into();
    }
    if let Some(p) = attestation_prefix_override {
        config.device_attestation_prefix = p.into();
    }
    if let Some(b) = attestation_blob_name_override {
        config.attestation_blob_name = b.into();
    }

    let _attestation_storage = RegistryAttestationStorage::new(repo_path.clone());
    let resolver: DefaultDidResolver = DefaultDidResolver::with_repo(&repo_path);

    match cmd.subcommand {
        OrgSubcommand::Create {
            name,
            local_key_alias,
            metadata_file,
        } => {
            // Generate a key alias if not provided
            let key_alias = local_key_alias.unwrap_or_else(|| {
                format!(
                    "org-{}",
                    name.chars()
                        .filter(|c| c.is_alphanumeric())
                        .take(20)
                        .collect::<String>()
                        .to_lowercase()
                )
            });

            println!("🏛️  Initializing new organization identity...");
            println!("   Organization Name: {}", name);
            println!("   Repository path:   {:?}", repo_path);
            println!("   Local Key Alias:   {}", key_alias);
            println!("   Using Identity Ref: '{}'", config.identity_ref);

            // --- Ensure Git repo exists ---
            use crate::factories::storage::{ensure_git_repo, open_git_repo};

            let identity_storage_check = RegistryIdentityStorage::new(repo_path.clone());
            if repo_path.exists() {
                match open_git_repo(&repo_path) {
                    Ok(_) => {
                        println!("   Git repository found.");
                        if identity_storage_check.load_identity().is_ok() {
                            return Err(anyhow!(
                                "An identity already exists at {:?}. Aborting.",
                                repo_path
                            ));
                        }
                    }
                    Err(_) => {
                        println!("   Path exists but is not a Git repo. Initializing...");
                        ensure_git_repo(&repo_path)
                            .context("Failed to initialize Git repository")?;
                    }
                }
            } else {
                println!("   Creating Git repo directory...");
                ensure_git_repo(&repo_path)
                    .context("Failed to create and initialize Git repository")?;
            }

            // --- Build org metadata ---
            let mut metadata_json = serde_json::json!({
                "type": "org",
                "name": name,
                "created_at": Utc::now().to_rfc3339()
            });

            // Merge with additional metadata file if provided
            if let Some(ref mf) = metadata_file
                && mf.exists()
            {
                let metadata_content = fs::read_to_string(mf)
                    .with_context(|| format!("Failed to read metadata file: {:?}", mf))?;
                let additional: serde_json::Value = serde_json::from_str(&metadata_content)
                    .with_context(|| format!("Invalid JSON in metadata file: {:?}", mf))?;

                // Merge additional metadata (preserving type and name)
                if let (Some(base), Some(add)) =
                    (metadata_json.as_object_mut(), additional.as_object())
                {
                    for (k, v) in add {
                        if k != "type" && k != "name" {
                            base.insert(k.clone(), v.clone());
                        }
                    }
                }
                println!("   Merged additional metadata from {:?}", mf);
            }

            println!(
                "   Org metadata: {}",
                serde_json::to_string(&metadata_json)?
            );

            // --- Generate KERI Identity ---
            println!("   Creating KERI-based organization identity (did:keri)...");

            let backend = std::sync::Arc::new(GitRegistryBackend::from_config_unchecked(
                RegistryConfig::single_tenant(&repo_path),
            ));
            let key_alias = KeyAlias::new_unchecked(key_alias);
            let (controller_did, alias) = initialize_registry_identity(
                backend,
                &key_alias,
                passphrase_provider.as_ref(),
                &get_platform_keychain()?,
                None,
            )
            .context("Failed to initialize org identity")?;

            // --- Create admin self-attestation ---
            println!("   Creating admin attestation for organization creator...");

            let identity_storage = RegistryIdentityStorage::new(repo_path.clone());
            let managed_identity = identity_storage
                .load_identity()
                .context("Failed to load newly created org identity")?;
            let rid = managed_identity.storage_id;

            // Resolve the org's own public key for self-attestation
            let org_resolved = resolver.resolve(controller_did.as_str()).with_context(|| {
                format!(
                    "Failed to resolve public key for org identity: {}",
                    controller_did
                )
            })?;
            let org_pk_bytes = *org_resolved.public_key();

            let now = Utc::now();
            let admin_capabilities = vec![
                Capability::sign_commit(),
                Capability::sign_release(),
                Capability::manage_members(),
                Capability::rotate_keys(),
            ];

            let meta = AttestationMetadata {
                note: Some(format!("Organization '{}' root admin", name)),
                timestamp: Some(now),
                expires_at: None, // Admin attestation doesn't expire
            };

            let signer = StorageSigner::new(get_platform_keychain()?);
            let org_did = DeviceDID::new(controller_did.to_string());

            let attestation = create_signed_attestation(
                now,
                &rid,
                &controller_did,
                &org_did,
                org_pk_bytes.as_bytes(),
                Some(serde_json::json!({
                    "org_role": "admin",
                    "org_name": name
                })),
                &meta,
                &signer,
                passphrase_provider.as_ref(),
                Some(&alias),
                None, // Self-attestation, no device signature
                admin_capabilities,
                Some(Role::Admin),
                None, // Root admin has no delegator
            )
            .context("Failed to create admin attestation")?;

            // Export to Git at the org member ref path
            let attestation_storage = RegistryAttestationStorage::new(repo_path.clone());
            attestation_storage
                .export(&auths_verifier::VerifiedAttestation::dangerous_from_unchecked(attestation))
                .context("Failed to export admin attestation to Git")?;

            println!("\n✅ Organization identity initialized successfully!");
            println!("   Org Identity ID:    {}", controller_did);
            println!("   Org Name:           {}", name);
            println!("   Repo Path:          {:?}", repo_path);
            println!("   Key Alias:          {}", alias);
            println!("   Admin Role:         Granted with all capabilities");

            if let Some(did_prefix) = controller_did.as_str().strip_prefix("did:keri:") {
                println!(
                    "   KEL Ref:            '{}'",
                    layout::keri_kel_ref(&Prefix::new_unchecked(did_prefix.to_string()))
                );
            }

            println!("   Identity Ref:       '{}'", config.identity_ref);
            println!(
                "   Member Ref:         '{}'",
                config.org_member_ref(controller_did.as_str(), &org_did)
            );
            println!("\n🔑 Store your key passphrase securely.");
            println!(
                "   You can now add members with: auths org add-member --org {} --member <identity-id> --role <role>",
                controller_did
            );

            Ok(())
        }

        OrgSubcommand::Attest {
            subject_did,  // The subject DID (String)
            payload_file, // Path to the JSON payload
            note,         // Optional note (String)
            expires_at,   // Optional RFC3339 expiration string
            signer_alias, // Alias of the org's signing key in keychain
        } => {
            let signer_alias = signer_alias
                .ok_or_else(|| anyhow!("Signer key alias must be provided with --signer-alias"))?;
            let signer_alias = KeyAlias::new_unchecked(signer_alias);

            let identity_storage = RegistryIdentityStorage::new(repo_path.clone());
            let managed_identity = identity_storage
                .load_identity()
                .context("Failed to load org identity from Git repository")?;
            let controller_did = managed_identity.controller_did;
            let rid = managed_identity.storage_id;

            let payload_str = fs::read_to_string(&payload_file)
                .with_context(|| format!("Failed to read payload file {:?}", payload_file))?;
            let payload: serde_json::Value =
                serde_json::from_str(&payload_str).context("Invalid JSON in payload file")?;

            let key_storage = get_platform_keychain()?;
            let (stored_did, _role, encrypted_key) = key_storage
                .load_key(&signer_alias)
                .with_context(|| format!("Failed to load signer key '{}'", signer_alias))?;

            if stored_did != controller_did {
                return Err(anyhow!(
                    "Signer key alias '{}' belongs to DID '{}', but loaded org identity is '{}'",
                    signer_alias,
                    stored_did,
                    controller_did
                ));
            }

            let passphrase = passphrase_provider.get_passphrase(&format!(
                "Enter passphrase for org identity key '{}':",
                signer_alias
            ))?;
            let _pkcs8_bytes = decrypt_keypair(&encrypted_key, &passphrase)
                .context("Failed to decrypt signer key (invalid passphrase?)")?;

            let subject_device_did = DeviceDID::new(subject_did.clone());

            // --- Resolve device public key using the custom resolver IF did:key ---
            let device_resolved = resolver.resolve(&subject_did).with_context(|| {
                format!("Failed to resolve public key for subject: {}", subject_did)
            })?;
            let device_pk_bytes = *device_resolved.public_key();

            let now = Utc::now();
            let meta = AttestationMetadata {
                note,
                timestamp: Some(now),
                expires_at: expires_at
                    .as_deref()
                    .map(DateTime::parse_from_rfc3339)
                    .transpose()
                    .map_err(|e| anyhow!("Invalid RFC3339 datetime string: {}", e))?
                    .map(|dt| dt.with_timezone(&Utc)),
            };

            let signer = StorageSigner::new(key_storage);
            let attestation = create_signed_attestation(
                now,
                &rid,
                &controller_did,
                &subject_device_did,
                device_pk_bytes.as_bytes(),
                Some(payload),
                &meta,
                &signer,
                passphrase_provider.as_ref(),
                Some(&signer_alias),
                None, // No device signature for org attestations
                vec![],
                None,
                None,
            )
            .context("Failed to create signed attestation object")?;

            let attestation_storage = RegistryAttestationStorage::new(repo_path.clone());
            attestation_storage
                .export(&auths_verifier::VerifiedAttestation::dangerous_from_unchecked(attestation))
                .context("Failed to export attestation to Git")?;

            println!(
                "\n✅ Org attestation created successfully from '{}' → '{}'",
                controller_did, subject_device_did
            );

            Ok(())
        }

        OrgSubcommand::Revoke {
            subject_did,
            note,
            signer_alias,
        } => {
            println!("🛑 Revoking org authorization for subject: {subject_did}");
            println!("   Using Repository:         {:?}", repo_path);
            println!("   Using Identity Ref:       '{}'", config.identity_ref);
            println!(
                "   Using Attestation Prefix: '{}'",
                config.device_attestation_prefix
            );

            let signer_alias = signer_alias
                .ok_or_else(|| anyhow!("Signer key alias must be provided for revocation"))?;
            let signer_alias = KeyAlias::new_unchecked(signer_alias);

            let identity_storage = RegistryIdentityStorage::new(repo_path.clone());
            let managed_identity = identity_storage
                .load_identity()
                .context("Failed to load identity from Git repository")?;
            let controller_did = managed_identity.controller_did;
            let rid = managed_identity.storage_id;

            let encrypted_key = get_platform_keychain()?
                .load_key(&signer_alias)
                .context("Failed to load signer key")?
                .2;
            let pass = passphrase_provider.get_passphrase(&format!(
                "Enter passphrase for identity key '{}':",
                signer_alias
            ))?;
            let _pkcs8_bytes =
                decrypt_keypair(&encrypted_key, &pass).context("Failed to decrypt identity key")?;

            // Allow both did:key and did:keri as subject input
            let subject_device_did = DeviceDID::new(subject_did.clone());
            let now = Utc::now();

            // Look up the subject's public key from existing attestations
            let attestation_storage = RegistryAttestationStorage::new(repo_path.clone());
            let existing = attestation_storage
                .load_attestations_for_device(&subject_device_did)
                .context("Failed to load attestations for subject")?;
            let device_public_key = existing
                .iter()
                .find(|a| !a.device_public_key.is_zero())
                .map(|a| a.device_public_key)
                .unwrap_or_else(|| Ed25519PublicKey::from_bytes([0u8; 32]));

            println!("🔏 Creating signed revocation...");
            let signer = StorageSigner::new(get_platform_keychain()?);
            let attestation = create_signed_revocation(
                &rid,
                &controller_did,
                &subject_device_did,
                device_public_key.as_bytes(),
                note,
                None,
                now,
                &signer,
                passphrase_provider.as_ref(),
                &signer_alias,
            )
            .context("Failed to create revocation")?;

            println!("💾 Writing revocation to Git...");
            attestation_storage
                .export(&auths_verifier::VerifiedAttestation::dangerous_from_unchecked(attestation))
                .context("Failed to write revocation")?;

            println!("\n✅ Revoked authorization for subject {subject_did}");

            Ok(())
        }

        OrgSubcommand::Show {
            subject_did,
            include_revoked,
        } => {
            let attestation_storage = RegistryAttestationStorage::new(repo_path.clone());
            let resolver = DefaultDidResolver::with_repo(&repo_path);
            let group = AttestationGroup::from_list(attestation_storage.load_all_attestations()?);

            let subject_device_did = DeviceDID(subject_did.clone());
            if let Some(list) = group.by_device.get(subject_device_did.as_str()) {
                for (i, att) in list.iter().enumerate() {
                    if !include_revoked
                        && (att.is_revoked() || att.expires_at.is_some_and(|e| Utc::now() > e))
                    {
                        continue;
                    }

                    let status = match verify_with_resolver(Utc::now(), &resolver, att, None) {
                        Ok(_) => "✅ valid",
                        Err(e) if e.to_string().contains("revoked") => "🛑 revoked",
                        Err(e) if e.to_string().contains("expired") => "⌛ expired",
                        Err(_) => "❌ invalid",
                    };

                    println!(
                        "{i}. [{}] @ {}",
                        status,
                        att.timestamp.unwrap_or(Utc::now())
                    );
                    if let Some(note) = &att.note {
                        println!("   📝 {}", note);
                    }
                    if let Some(payload) = &att.payload {
                        println!("   📦 {}", serde_json::to_string_pretty(payload)?);
                    }
                }
            } else {
                println!("No authorizations found for subject: {}", subject_did);
            }

            Ok(())
        }

        OrgSubcommand::List { include_revoked } => {
            let attestation_storage = RegistryAttestationStorage::new(repo_path.clone());
            let resolver = DefaultDidResolver::with_repo(&repo_path);
            let group = AttestationGroup::from_list(attestation_storage.load_all_attestations()?);

            for (subject, list) in group.by_device.iter() {
                let latest = list.last().unwrap();
                if !include_revoked
                    && (latest.is_revoked() || latest.expires_at.is_some_and(|e| Utc::now() > e))
                {
                    continue;
                }

                let status = match verify_with_resolver(Utc::now(), &resolver, latest, None) {
                    Ok(_) => "✅ valid",
                    Err(e) if e.to_string().contains("revoked") => "🛑 revoked",
                    Err(e) if e.to_string().contains("expired") => "⌛ expired",
                    Err(_) => "❌ invalid",
                };

                println!("- {} [{}]", subject, status);
            }

            Ok(())
        }

        OrgSubcommand::AddMember {
            org,
            member_did: member,
            role: cli_role,
            capabilities,
            signer_alias,
            note,
        } => {
            let role = Role::from(cli_role);
            println!("👥 Adding member to organization...");
            println!("   Org:    {}", org);
            println!("   Member: {}", member);
            println!("   Role:   {}", role);

            // Load invoker's identity and key
            let identity_storage = RegistryIdentityStorage::new(repo_path.clone());
            let managed_identity = identity_storage
                .load_identity()
                .context("Failed to load identity. Are you running this from an org repository?")?;
            let invoker_did = managed_identity.controller_did.clone();
            let rid = managed_identity.storage_id;

            // Determine signer alias
            let signer_alias = KeyAlias::new_unchecked(signer_alias.unwrap_or_else(|| {
                // Try to derive alias from org name in identity metadata
                format!(
                    "org-{}",
                    org.chars()
                        .filter(|c| c.is_alphanumeric())
                        .take(20)
                        .collect::<String>()
                        .to_lowercase()
                )
            }));

            // Verify invoker has ManageMembers capability
            // First, load the invoker's own org attestation to check capabilities
            let attestation_storage = RegistryAttestationStorage::new(repo_path.clone());
            let invoker_did_device = DeviceDID::new(invoker_did.to_string());
            let invoker_attestations = attestation_storage.load_all_attestations()?;

            // Find invoker's attestation for this org
            let invoker_has_manage_members = invoker_attestations.iter().any(|att| {
                att.subject.as_str() == invoker_did_device.as_str()
                    && !att.is_revoked()
                    && att.capabilities.contains(&Capability::manage_members())
            });

            if !invoker_has_manage_members {
                return Err(anyhow!(
                    "You don't have ManageMembers capability for org '{}'. Only org admins can add members.",
                    org
                ));
            }

            // Load signer key and verify passphrase
            let key_storage = get_platform_keychain()?;
            let (stored_did, _role, encrypted_key) = key_storage
                .load_key(&signer_alias)
                .with_context(|| format!("Failed to load signer key '{}'", signer_alias))?;

            if stored_did != invoker_did {
                return Err(anyhow!(
                    "Signer key alias '{}' belongs to DID '{}', but loaded identity is '{}'",
                    signer_alias,
                    stored_did,
                    invoker_did
                ));
            }

            let passphrase = passphrase_provider
                .get_passphrase(&format!("Enter passphrase for org key '{}':", signer_alias))?;
            let _pkcs8_bytes = decrypt_keypair(&encrypted_key, &passphrase)
                .context("Failed to decrypt signer key (invalid passphrase?)")?;

            // Resolve member's public key
            let member_did = DeviceDID::new(member.clone());
            let member_resolved = resolver
                .resolve(&member)
                .with_context(|| format!("Failed to resolve public key for member: {}", member))?;
            let member_pk_bytes = *member_resolved.public_key();

            // Determine capabilities: use override if provided, otherwise use role defaults
            let member_capabilities = if let Some(cap_strs) = capabilities {
                cap_strs
                    .iter()
                    .map(|s| {
                        s.parse::<Capability>().unwrap_or_else(|e| {
                            eprintln!("error: {e}");
                            std::process::exit(2);
                        })
                    })
                    .collect()
            } else {
                role.default_capabilities()
            };

            println!(
                "   Capabilities: {:?}",
                member_capabilities
                    .iter()
                    .map(|c| format!("{:?}", c))
                    .collect::<Vec<_>>()
                    .join(", ")
            );

            // Create the attestation
            let now = Utc::now();
            let meta = AttestationMetadata {
                note: note.or_else(|| Some(format!("Added as {} by {}", role, invoker_did))),
                timestamp: Some(now),
                expires_at: None, // Member attestations don't expire by default
            };

            let signer = StorageSigner::new(key_storage);
            let attestation = create_signed_attestation(
                now,
                &rid,
                &invoker_did,
                &member_did,
                member_pk_bytes.as_bytes(),
                Some(serde_json::json!({
                    "org_role": role.to_string(),
                    "org_did": org
                })),
                &meta,
                &signer,
                passphrase_provider.as_ref(),
                Some(&signer_alias),
                None, // No device signature for org membership attestations
                member_capabilities.clone(),
                Some(role),
                Some(invoker_did.clone()),
            )
            .context("Failed to create member attestation")?;

            // Export to Git at the org member ref path
            let attestation_storage = RegistryAttestationStorage::new(repo_path.clone());
            attestation_storage
                .export(&auths_verifier::VerifiedAttestation::dangerous_from_unchecked(attestation))
                .context("Failed to export member attestation to Git")?;

            println!("\n✅ Member added successfully!");
            println!("   Member ID:    {}", member);
            println!("   Role:         {}", role);
            println!(
                "   Capabilities: {}",
                member_capabilities
                    .iter()
                    .map(|c| format!("{:?}", c))
                    .collect::<Vec<_>>()
                    .join(", ")
            );
            println!("   Delegated by: {}", invoker_did);
            println!(
                "   Stored at:    {}",
                config.org_member_ref(&org, &member_did)
            );

            Ok(())
        }

        OrgSubcommand::RevokeMember {
            org,
            member_did: member,
            note,
            signer_alias,
            dry_run,
        } => {
            println!("🛑 Revoking member from organization...");
            println!("   Org:    {}", org);
            println!("   Member: {}", member);

            // Load invoker's identity and key
            let identity_storage = RegistryIdentityStorage::new(repo_path.clone());
            let managed_identity = identity_storage
                .load_identity()
                .context("Failed to load identity. Are you running this from an org repository?")?;
            let invoker_did = managed_identity.controller_did.clone();
            let rid = managed_identity.storage_id;

            // Determine signer alias
            let signer_alias = KeyAlias::new_unchecked(signer_alias.unwrap_or_else(|| {
                format!(
                    "org-{}",
                    org.chars()
                        .filter(|c| c.is_alphanumeric())
                        .take(20)
                        .collect::<String>()
                        .to_lowercase()
                )
            }));

            // Verify invoker has ManageMembers capability
            let attestation_storage = RegistryAttestationStorage::new(repo_path.clone());
            let invoker_did_device = DeviceDID::new(invoker_did.to_string());
            let all_attestations = attestation_storage.load_all_attestations()?;

            // Find invoker's attestation for this org
            let invoker_has_manage_members = all_attestations.iter().any(|att| {
                att.subject.as_str() == invoker_did_device.as_str()
                    && !att.is_revoked()
                    && att.capabilities.contains(&Capability::manage_members())
            });

            if !invoker_has_manage_members {
                return Err(anyhow!(
                    "You don't have ManageMembers capability for org '{}'. Only org admins can revoke members.",
                    org
                ));
            }

            // Check if member exists and is not already revoked
            let member_did = DeviceDID::new(member.clone());
            let member_attestation = all_attestations
                .iter()
                .find(|att| att.subject.as_str() == member_did.as_str());

            match member_attestation {
                None => {
                    return Err(anyhow!(
                        "Member '{}' is not a member of org '{}'. Cannot revoke.",
                        member,
                        org
                    ));
                }
                Some(att) if att.is_revoked() => {
                    return Err(anyhow!(
                        "Member '{}' is already revoked from org '{}'.",
                        member,
                        org
                    ));
                }
                Some(_) => {} // Member exists and is active, proceed
            }

            if dry_run {
                return display_dry_run_revoke_member(&org, &member, invoker_did.as_ref());
            }

            // Load signer key and verify passphrase
            let key_storage = get_platform_keychain()?;
            let (stored_did, _role, encrypted_key) = key_storage
                .load_key(&signer_alias)
                .with_context(|| format!("Failed to load signer key '{}'", signer_alias))?;

            if stored_did != invoker_did {
                return Err(anyhow!(
                    "Signer key alias '{}' belongs to DID '{}', but loaded identity is '{}'",
                    signer_alias,
                    stored_did,
                    invoker_did
                ));
            }

            let passphrase = passphrase_provider
                .get_passphrase(&format!("Enter passphrase for org key '{}':", signer_alias))?;
            let _pkcs8_bytes = decrypt_keypair(&encrypted_key, &passphrase)
                .context("Failed to decrypt signer key (invalid passphrase?)")?;

            // Look up the member's public key from existing attestations
            let attestation_storage = RegistryAttestationStorage::new(repo_path.clone());
            let existing = attestation_storage
                .load_attestations_for_device(&member_did)
                .context("Failed to load attestations for member")?;
            let member_public_key = existing
                .iter()
                .find(|a| !a.device_public_key.is_zero())
                .map(|a| a.device_public_key)
                .unwrap_or_else(|| Ed25519PublicKey::from_bytes([0u8; 32]));

            // Create revocation
            let now = Utc::now();
            let signer = StorageSigner::new(key_storage);

            println!("🔏 Creating signed revocation...");
            let revocation = create_signed_revocation(
                &rid,
                &invoker_did,
                &member_did,
                member_public_key.as_bytes(),
                note.clone(),
                None, // No expiration for revocations
                now,
                &signer,
                passphrase_provider.as_ref(),
                &signer_alias,
            )
            .context("Failed to create revocation")?;

            // Export to Git
            println!("💾 Writing revocation to Git...");
            attestation_storage
                .export(&auths_verifier::VerifiedAttestation::dangerous_from_unchecked(revocation))
                .context("Failed to export revocation to Git")?;

            println!("\n✅ Member revoked successfully!");
            println!("   Member ID:  {}", member);
            println!("   Revoked by: {}", invoker_did);
            if let Some(n) = note {
                println!("   Note:       {}", n);
            }
            println!(
                "   Stored at:  {}",
                config.org_member_ref(&org, &member_did)
            );

            Ok(())
        }

        OrgSubcommand::ListMembers {
            org,
            include_revoked,
        } => {
            println!("📋 Listing members of organization: {}", org);

            // Load all attestations
            let attestation_storage = RegistryAttestationStorage::new(repo_path.clone());
            let all_attestations = attestation_storage.load_all_attestations()?;

            // Build member list with delegation info
            #[allow(clippy::type_complexity)]
            let mut members: Vec<(
                String,
                Option<Role>,
                Option<String>,
                bool,
                Vec<Capability>,
            )> = Vec::new();

            for att in &all_attestations {
                // Skip if revoked and not including revoked
                if att.is_revoked() && !include_revoked {
                    continue;
                }

                // Skip expired attestations
                if att.expires_at.is_some_and(|e| Utc::now() > e) && !include_revoked {
                    continue;
                }

                members.push((
                    att.subject.to_string(),
                    att.role,
                    att.delegated_by.as_ref().map(|d| d.to_string()),
                    att.is_revoked(),
                    att.capabilities.clone(),
                ));
            }

            if members.is_empty() {
                println!("\nNo members found for organization.");
                return Ok(());
            }

            members.sort_by(|a, b| {
                member_role_order(&a.1)
                    .cmp(&member_role_order(&b.1))
                    .then_with(|| a.0.cmp(&b.0))
            });

            println!("\nOrg: {}", org);
            println!("\nMembers ({} total):", members.len());
            println!("─────────────────────────────────────────");

            for (member_did, role, delegated_by, revoked, capabilities) in &members {
                let role_str = role.as_ref().map(|r| r.as_str()).unwrap_or("unknown");
                let status = if *revoked { " (revoked)" } else { "" };

                // Determine tree prefix based on delegator
                let prefix = if delegated_by.is_none() {
                    "├─ "
                } else {
                    "│  └─ "
                };

                // Format capabilities
                let caps: Vec<String> = capabilities.iter().map(|c| format!("{:?}", c)).collect();
                let caps_str = if caps.is_empty() {
                    String::new()
                } else {
                    format!(" [{}]", caps.join(", "))
                };

                println!(
                    "{}{} [{}]{}{}",
                    prefix, member_did, role_str, status, caps_str
                );

                if let Some(delegator) = delegated_by {
                    println!("│     delegated by: {}", delegator);
                }
            }

            println!("─────────────────────────────────────────");

            if !include_revoked {
                let revoked_count = all_attestations.iter().filter(|a| a.is_revoked()).count();
                if revoked_count > 0 {
                    println!(
                        "\n({} revoked member(s) hidden. Use --include-revoked to show.)",
                        revoked_count
                    );
                }
            }

            Ok(())
        }
    }
}

fn display_dry_run_revoke_member(org: &str, member: &str, invoker_did: &str) -> Result<()> {
    use crate::ux::format::{JsonResponse, is_json_mode};

    if is_json_mode() {
        JsonResponse::success(
            "org revoke-member",
            &serde_json::json!({
                "dry_run": true,
                "org": org,
                "member_did": member,
                "invoker_did": invoker_did,
                "actions": [
                    "Create signed revocation for member",
                    "Store revocation in Git repository",
                    "Member will lose all org capabilities"
                ]
            }),
        )
        .print()
        .map_err(|e| anyhow!("{e}"))
    } else {
        let out = crate::ux::format::Output::new();
        out.print_info("Dry run mode — no changes will be made");
        out.newline();
        out.println(&format!("   Org:    {}", org));
        out.println(&format!("   Member: {}", member));
        out.newline();
        out.println("Would perform the following actions:");
        out.println(&format!(
            "  1. Create signed revocation for member {}",
            member
        ));
        out.println("  2. Store revocation in Git repository");
        out.println("  3. Member will lose all org capabilities");
        Ok(())
    }
}

use crate::commands::executable::ExecutableCommand;
use crate::config::CliConfig;

impl ExecutableCommand for OrgCommand {
    fn execute(&self, ctx: &CliConfig) -> Result<()> {
        handle_org(
            self.clone(),
            ctx.repo_path.clone(),
            self.overrides.identity_ref.clone(),
            self.overrides.identity_blob.clone(),
            self.overrides.attestation_prefix.clone(),
            self.overrides.attestation_blob.clone(),
            ctx.passphrase_provider.clone(),
        )
    }
}