Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
sso.tsx38.6 KB · 1157 lines
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
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
/**
 * Block I10 — Enterprise SSO (OIDC) routes.
 *
 *   GET  /admin/sso                — site-admin config page
 *   POST /admin/sso                — save config
 *   GET  /login/sso                — begin OIDC flow (redirect to IdP)
 *   GET  /login/sso/callback       — handle IdP redirect, create session
 *   POST /settings/sso/unlink      — user drops their SSO link
 *
 * Visual polish (2026): gradient hairline + orb hero + provider preset cards
 * + status pills + section forms. Scoped under `.sso-*` so it can't bleed
 * into other admin pages. Every OIDC flow, redirect URI, state/nonce, and
 * session issuance path is preserved EXACTLY — this is security-critical.
 */

import { Hono } from "hono";
import { eq } from "drizzle-orm";
import { getCookie, setCookie, deleteCookie } from "hono/cookie";
import { db } from "../db";
import { ssoUserLinks } from "../db/schema";
import { Layout } from "../views/layout";
import { softAuth, requireAuth } from "../middleware/auth";
import type { AuthEnv } from "../middleware/auth";
import { isSiteAdmin } from "../lib/admin";
import { audit } from "../lib/notify";
import {
  buildAuthorizeUrl,
  exchangeCode,
  fetchUserinfo,
  findOrCreateUserFromSso,
  getSsoConfig,
  issueSsoSession,
  randomToken,
  ssoRedirectUri,
  upsertSsoConfig,
} from "../lib/sso";
import { sessionCookieOptions } from "../lib/auth";

const sso = new Hono<AuthEnv>();
sso.use("*", softAuth);

// Re-export the shared cookie options under the SSO namespace (buildable types)
// — defined here so we don't double-import under the same name.
function ssoStateCookieOpts(): {
  httpOnly: boolean;
  secure: boolean;
  sameSite: "Lax";
  path: string;
  maxAge: number;
} {
  return {
    httpOnly: true,
    secure: process.env.NODE_ENV === "production",
    sameSite: "Lax",
    path: "/",
    maxAge: 600, // 10 min to complete the flow
  };
}

// ----------------------------------------------------------------------------
// Scoped styles — every class prefixed `.sso-` so this surface can't bleed
// into other admin pages.
// ----------------------------------------------------------------------------
const ssoStyles = `
  .sso-wrap { max-width: 1320px; margin: 0 auto; padding: var(--space-6) var(--space-4); }

  /* ─── Hero ─── */
  .sso-hero {
    position: relative;
    margin-bottom: var(--space-5);
    padding: var(--space-5) var(--space-6);
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 16px;
    overflow: hidden;
  }
  .sso-hero::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 2px;
    background: linear-gradient(90deg, transparent 0%, #8c6dff 30%, #36c5d6 70%, transparent 100%);
    opacity: 0.7;
    pointer-events: none;
  }
  .sso-hero-orb {
    position: absolute;
    inset: -20% -10% auto auto;
    width: 380px; height: 380px;
    background: radial-gradient(circle, rgba(140,109,255,0.20), rgba(54,197,214,0.10) 45%, transparent 70%);
    filter: blur(80px);
    opacity: 0.7;
    pointer-events: none;
    z-index: 0;
  }
  .sso-hero-inner { position: relative; z-index: 1; max-width: 680px; }
  .sso-eyebrow {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.18em;
    color: var(--text-muted);
    margin-bottom: var(--space-2);
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-family: var(--font-mono);
    font-weight: 600;
  }
  .sso-eyebrow-dot {
    width: 8px; height: 8px;
    border-radius: 9999px;
    background: linear-gradient(135deg, #8c6dff, #36c5d6);
    box-shadow: 0 0 0 3px rgba(140,109,255,0.18);
  }
  .sso-title {
    font-size: clamp(26px, 3.5vw, 38px);
    font-family: var(--font-display);
    font-weight: 800;
    letter-spacing: -0.028em;
    line-height: 1.06;
    margin: 0 0 var(--space-2);
    color: var(--text-strong);
  }
  .sso-title-grad {
    background-image: linear-gradient(135deg, #a48bff 0%, #8c6dff 50%, #36c5d6 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    color: transparent;
  }
  .sso-sub {
    font-size: 14.5px;
    color: var(--text-muted);
    margin: 0;
    line-height: 1.55;
  }
  .sso-sub code {
    font-family: var(--font-mono);
    font-size: 12.5px;
    background: var(--bg-tertiary);
    padding: 1px 5px;
    border-radius: 4px;
    color: var(--text);
  }

  /* ─── Status pill (hero corner) ─── */
  .sso-status-pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-top: var(--space-2);
    padding: 4px 12px;
    border-radius: 9999px;
    font-size: 11.5px;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
  }
  .sso-status-pill.is-on {
    background: rgba(52,211,153,0.14);
    color: #6ee7b7;
    box-shadow: inset 0 0 0 1px rgba(52,211,153,0.36);
  }
  .sso-status-pill.is-off {
    background: rgba(251,191,36,0.10);
    color: #fde68a;
    box-shadow: inset 0 0 0 1px rgba(251,191,36,0.32);
  }
  .sso-status-pill.is-missing {
    background: rgba(148,163,184,0.10);
    color: #cbd5e1;
    box-shadow: inset 0 0 0 1px rgba(148,163,184,0.28);
  }
  .sso-status-pill .dot { width: 6px; height: 6px; border-radius: 9999px; background: currentColor; }

  /* ─── Banner ─── */
  .sso-banner {
    margin-bottom: var(--space-4);
    padding: 10px 14px;
    border-radius: 10px;
    font-size: 13.5px;
    border: 1px solid var(--border);
    background: rgba(255,255,255,0.025);
    color: var(--text);
  }
  .sso-banner.is-ok {
    border-color: rgba(52,211,153,0.40);
    background: rgba(52,211,153,0.08);
    color: #bbf7d0;
  }
  .sso-banner.is-error {
    border-color: rgba(248,113,113,0.40);
    background: rgba(248,113,113,0.08);
    color: #fecaca;
  }

  /* ─── Section card ─── */
  .sso-section {
    margin-bottom: var(--space-5);
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 14px;
    overflow: hidden;
  }
  .sso-section-head {
    padding: var(--space-4) var(--space-5);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--space-3);
    flex-wrap: wrap;
  }
  .sso-section-head-text { flex: 1; min-width: 240px; }
  .sso-section-title {
    margin: 0;
    font-family: var(--font-display);
    font-size: 16px;
    font-weight: 700;
    letter-spacing: -0.012em;
    color: var(--text-strong);
  }
  .sso-section-sub {
    margin: 4px 0 0;
    font-size: 12.5px;
    color: var(--text-muted);
    line-height: 1.45;
  }
  .sso-section-body { padding: var(--space-4) var(--space-5); }

  /* ─── Redirect-URI callout (copyable) ─── */
  .sso-callout {
    display: flex;
    gap: var(--space-3);
    align-items: center;
    padding: 12px 14px;
    background: rgba(140,109,255,0.05);
    border: 1px dashed rgba(140,109,255,0.30);
    border-radius: 12px;
    flex-wrap: wrap;
  }
  .sso-callout-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.10em;
    color: var(--text-muted);
    font-weight: 700;
    flex-shrink: 0;
  }
  .sso-callout code {
    flex: 1;
    min-width: 0;
    font-family: var(--font-mono);
    font-size: 12.5px;
    color: var(--text);
    background: rgba(255,255,255,0.04);
    border: 1px solid var(--border);
    padding: 6px 10px;
    border-radius: 8px;
    word-break: break-all;
    overflow-wrap: anywhere;
  }

  /* ─── Provider preset cards ─── */
  .sso-providers {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: var(--space-2);
  }
  .sso-provider {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px;
    border: 1px solid var(--border-strong);
    border-radius: 12px;
    background: rgba(255,255,255,0.02);
    color: var(--text);
    cursor: pointer;
    font: inherit;
    text-align: left;
    transition: transform 120ms ease, border-color 120ms ease, background 120ms ease;
  }
  .sso-provider:hover {
    transform: translateY(-1px);
    border-color: rgba(140,109,255,0.45);
    background: rgba(140,109,255,0.06);
  }
  .sso-provider-icon {
    flex-shrink: 0;
    width: 36px; height: 36px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, rgba(140,109,255,0.22), rgba(54,197,214,0.14));
    box-shadow: inset 0 0 0 1px rgba(140,109,255,0.30);
    font-family: var(--font-display);
    font-size: 16px;
    font-weight: 800;
    color: #e9d5ff;
  }
  .sso-provider-name {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-strong);
    line-height: 1.2;
  }
  .sso-provider-kind {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 1px;
  }

  /* ─── Form group ─── */
  .sso-field { margin-bottom: var(--space-4); }
  .sso-field:last-child { margin-bottom: 0; }
  .sso-field-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    margin-bottom: 6px;
  }
  .sso-field label {
    display: block;
    font-family: var(--font-mono);
    font-size: 12.5px;
    font-weight: 600;
    color: var(--text-strong);
    letter-spacing: -0.005em;
  }
  .sso-input {
    width: 100%;
    padding: 9px 12px;
    font-size: 13.5px;
    color: var(--text);
    background: var(--bg);
    border: 1px solid var(--border-strong);
    border-radius: 8px;
    outline: none;
    font-family: var(--font-mono);
    transition: border-color 120ms ease, box-shadow 120ms ease;
    box-sizing: border-box;
  }
  .sso-input:focus {
    border-color: var(--border-focus);
    box-shadow: 0 0 0 3px rgba(140,109,255,0.18);
  }
  .sso-hint {
    font-size: 11.5px;
    color: var(--text-muted);
    margin-top: 6px;
    line-height: 1.45;
  }
  .sso-toggle-row {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 10px 12px;
    background: rgba(255,255,255,0.025);
    border: 1px solid var(--border);
    border-radius: 10px;
    margin-bottom: var(--space-4);
  }
  .sso-toggle-row input[type="checkbox"] { margin-top: 2px; flex-shrink: 0; }
  .sso-toggle-row span {
    font-size: 13px;
    color: var(--text);
    line-height: 1.45;
  }

  /* ─── Buttons ─── */
  .sso-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 11px 18px;
    border-radius: 10px;
    font-size: 13.5px;
    font-weight: 600;
    text-decoration: none;
    border: 1px solid transparent;
    cursor: pointer;
    font: inherit;
    line-height: 1;
    transition: transform 120ms ease, box-shadow 120ms ease, background 120ms ease, border-color 120ms ease, color 120ms ease;
  }
  .sso-btn-primary {
    background: linear-gradient(135deg, #8c6dff 0%, #36c5d6 100%);
    color: #ffffff;
    box-shadow: 0 6px 18px -6px rgba(140,109,255,0.50), inset 0 1px 0 rgba(255,255,255,0.16);
  }
  .sso-btn-primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 10px 24px -8px rgba(140,109,255,0.60), inset 0 1px 0 rgba(255,255,255,0.20);
    color: #ffffff;
    text-decoration: none;
  }
  .sso-btn-ghost {
    background: transparent;
    color: var(--text);
    border-color: var(--border-strong);
  }
  .sso-btn-ghost:hover {
    background: rgba(140,109,255,0.06);
    border-color: rgba(140,109,255,0.45);
    color: var(--text-strong);
    text-decoration: none;
  }

  /* ─── Section foot bar (save button) ─── */
  .sso-foot {
    padding: var(--space-3) var(--space-5);
    border-top: 1px solid var(--border);
    background: rgba(255,255,255,0.012);
    display: flex;
    justify-content: space-between;
    gap: var(--space-2);
    align-items: center;
    flex-wrap: wrap;
  }
  .sso-foot-hint {
    font-size: 12.5px;
    color: var(--text-muted);
  }

  /* ─── 403 ─── */
  .sso-403 {
    max-width: 540px;
    margin: var(--space-12) auto;
    padding: var(--space-6);
    text-align: center;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 16px;
  }
  .sso-403 h2 {
    font-family: var(--font-display);
    font-size: 22px;
    margin: 0 0 8px;
    color: var(--text-strong);
  }
  .sso-403 p { color: var(--text-muted); margin: 0; font-size: 14px; }
`;

// ----------------------------------------------------------------------------
// Admin config page
// ----------------------------------------------------------------------------

async function adminGate(c: any): Promise<{ user: any } | Response> {
  const user = c.get("user");
  if (!user) return c.redirect("/login?next=/admin/sso");
  if (!(await isSiteAdmin(user.id))) {
    return c.html(
      <Layout title="Forbidden" user={user}>
        <div class="sso-wrap">
          <div class="sso-403">
            <h2>403 — Not a site admin</h2>
            <p>You don't have permission to configure SSO.</p>
          </div>
        </div>
        <style dangerouslySetInnerHTML={{ __html: ssoStyles }} />
      </Layout>,
      403
    );
  }
  return { user };
}

/** Compute the per-card status — used for the provider list + the hero pill. */
function ssoStatus(cfg: Awaited<ReturnType<typeof getSsoConfig>>): {
  kind: "configured" | "incomplete" | "missing";
  label: string;
} {
  if (!cfg) return { kind: "missing", label: "Not configured" };
  const hasAll =
    cfg.issuer &&
    cfg.authorizationEndpoint &&
    cfg.tokenEndpoint &&
    cfg.userinfoEndpoint &&
    cfg.clientId &&
    cfg.clientSecret;
  if (!hasAll) return { kind: "incomplete", label: "Incomplete" };
  if (!cfg.enabled) return { kind: "incomplete", label: "Configured · disabled" };
  return { kind: "configured", label: "Live · accepting sign-ins" };
}

sso.get("/admin/sso", requireAuth, async (c) => {
  const g = await adminGate(c);
  if (g instanceof Response) return g;
  const { user } = g;

  const cfg = await getSsoConfig();
  const success = c.req.query("success");
  const error = c.req.query("error");
  const redirectUri = ssoRedirectUri();
  const status = ssoStatus(cfg);
  const pillClass =
    status.kind === "configured"
      ? "is-on"
      : status.kind === "incomplete"
        ? "is-off"
        : "is-missing";

  return c.html(
    <Layout title="SSO — Admin" user={user}>
      <div class="sso-wrap">
        <section class="sso-hero">
          <div class="sso-hero-orb" aria-hidden="true" />
          <div class="sso-hero-inner">
            <div class="sso-eyebrow">
              <span class="sso-eyebrow-dot" aria-hidden="true" />
              Enterprise SSO · Site admin · {user.username}
            </div>
            <h1 class="sso-title">
              <span class="sso-title-grad">Single sign-on.</span>
            </h1>
            <p class="sso-sub">
              Configure one site-wide OpenID Connect provider. Users see a
              "Sign in with <code>{cfg?.providerName || "SSO"}</code>" button
              on the login page and land in Gluecron without a password.
            </p>
            <span class={"sso-status-pill " + pillClass}>
              <span class="dot" aria-hidden="true" />
              {status.label}
            </span>
          </div>
        </section>

        {success && (
          <div class="sso-banner is-ok">{decodeURIComponent(success)}</div>
        )}
        {error && (
          <div class="sso-banner is-error">{decodeURIComponent(error)}</div>
        )}

        {/* ─── Redirect URI callout ─── */}
        <section class="sso-section">
          <header class="sso-section-head">
            <div class="sso-section-head-text">
              <h3 class="sso-section-title">Redirect URI</h3>
              <p class="sso-section-sub">
                Paste this into your IdP's "Authorized redirect URIs"
                field — exact match, no trailing slash.
              </p>
            </div>
          </header>
          <div class="sso-section-body">
            <div class="sso-callout">
              <span class="sso-callout-label">Callback</span>
              <code>{redirectUri}</code>
            </div>
          </div>
        </section>

        {/* ─── Provider preset cards ─── */}
        <section class="sso-section">
          <header class="sso-section-head">
            <div class="sso-section-head-text">
              <h3 class="sso-section-title">Provider presets</h3>
              <p class="sso-section-sub">
                Quick-fill the OIDC endpoints below for a common IdP — then
                paste your client ID + secret.
              </p>
            </div>
          </header>
          <div class="sso-section-body">
            <div class="sso-providers">
              <button
                type="button"
                class="sso-provider"
                onclick="window.gluecronSsoPreset('google')"
                aria-label="Use Google Workspace preset"
              >
                <span class="sso-provider-icon" aria-hidden="true">G</span>
                <div>
                  <div class="sso-provider-name">Google</div>
                  <div class="sso-provider-kind">Workspace · OIDC</div>
                </div>
              </button>
              <button
                type="button"
                class="sso-provider"
                onclick="window.gluecronSsoPreset('okta')"
                aria-label="Use Okta preset"
              >
                <span class="sso-provider-icon" aria-hidden="true">O</span>
                <div>
                  <div class="sso-provider-name">Okta</div>
                  <div class="sso-provider-kind">OIDC</div>
                </div>
              </button>
              <button
                type="button"
                class="sso-provider"
                onclick="window.gluecronSsoPreset('auth0')"
                aria-label="Use Auth0 preset"
              >
                <span class="sso-provider-icon" aria-hidden="true">A</span>
                <div>
                  <div class="sso-provider-name">Auth0</div>
                  <div class="sso-provider-kind">OIDC</div>
                </div>
              </button>
              <button
                type="button"
                class="sso-provider"
                onclick="window.gluecronSsoPreset('azure')"
                aria-label="Use Microsoft Entra preset"
              >
                <span class="sso-provider-icon" aria-hidden="true">M</span>
                <div>
                  <div class="sso-provider-name">Microsoft</div>
                  <div class="sso-provider-kind">Entra ID · OIDC</div>
                </div>
              </button>
              <button
                type="button"
                class="sso-provider"
                onclick="window.gluecronSsoPreset('github')"
                aria-label="Use GitHub Enterprise preset"
              >
                <span class="sso-provider-icon" aria-hidden="true">H</span>
                <div>
                  <div class="sso-provider-name">GitHub</div>
                  <div class="sso-provider-kind">Enterprise · OIDC</div>
                </div>
              </button>
              <button
                type="button"
                class="sso-provider"
                onclick="window.gluecronSsoPreset('saml')"
                aria-label="SAML not supported — use OIDC"
                title="SAML is not currently supported. Use the equivalent OIDC endpoint from your IdP."
              >
                <span class="sso-provider-icon" aria-hidden="true">S</span>
                <div>
                  <div class="sso-provider-name">SAML</div>
                  <div class="sso-provider-kind">Use OIDC instead</div>
                </div>
              </button>
            </div>
          </div>
        </section>

        <script
          dangerouslySetInnerHTML={{
            __html: /* js */ `
              window.gluecronSsoPreset = function (provider) {
                var P = {
                  google: {
                    provider_name: 'Google',
                    issuer: 'https://accounts.google.com',
                    authorization_endpoint: 'https://accounts.google.com/o/oauth2/v2/auth',
                    token_endpoint: 'https://oauth2.googleapis.com/token',
                    userinfo_endpoint: 'https://openidconnect.googleapis.com/v1/userinfo',
                    scopes: 'openid email profile',
                  },
                  okta: {
                    provider_name: 'Okta',
                    issuer: 'https://YOUR-TENANT.okta.com',
                    authorization_endpoint: 'https://YOUR-TENANT.okta.com/oauth2/v1/authorize',
                    token_endpoint: 'https://YOUR-TENANT.okta.com/oauth2/v1/token',
                    userinfo_endpoint: 'https://YOUR-TENANT.okta.com/oauth2/v1/userinfo',
                    scopes: 'openid email profile',
                  },
                  auth0: {
                    provider_name: 'Auth0',
                    issuer: 'https://YOUR-TENANT.auth0.com',
                    authorization_endpoint: 'https://YOUR-TENANT.auth0.com/authorize',
                    token_endpoint: 'https://YOUR-TENANT.auth0.com/oauth/token',
                    userinfo_endpoint: 'https://YOUR-TENANT.auth0.com/userinfo',
                    scopes: 'openid email profile',
                  },
                  azure: {
                    provider_name: 'Microsoft',
                    issuer: 'https://login.microsoftonline.com/YOUR-TENANT-ID/v2.0',
                    authorization_endpoint: 'https://login.microsoftonline.com/YOUR-TENANT-ID/oauth2/v2.0/authorize',
                    token_endpoint: 'https://login.microsoftonline.com/YOUR-TENANT-ID/oauth2/v2.0/token',
                    userinfo_endpoint: 'https://graph.microsoft.com/oidc/userinfo',
                    scopes: 'openid email profile',
                  },
                  github: {
                    provider_name: 'GitHub Enterprise',
                    issuer: 'https://github.example.com',
                    authorization_endpoint: 'https://github.example.com/login/oauth/authorize',
                    token_endpoint: 'https://github.example.com/login/oauth/access_token',
                    userinfo_endpoint: 'https://github.example.com/api/v3/user',
                    scopes: 'openid read:user user:email',
                  },
                  saml: {
                    provider_name: 'SAML',
                    issuer: '',
                    authorization_endpoint: '',
                    token_endpoint: '',
                    userinfo_endpoint: '',
                    scopes: 'openid email profile',
                  },
                };
                var p = P[provider];
                if (!p) return;
                for (var k in p) {
                  var el = document.getElementById(k);
                  if (el) el.value = p[k];
                }
                if (provider === 'saml') {
                  alert('SAML is not supported. Use your IdP\\'s OIDC equivalentmost SAML IdPs (Okta, Auth0, Azure) also expose OIDC endpoints.');
                }
              };
            `,
          }}
        />

        {/* ─── Main config form ─── */}
        <form method="post" action="/admin/sso">
          <section class="sso-section">
            <header class="sso-section-head">
              <div class="sso-section-head-text">
                <h3 class="sso-section-title">OIDC configuration</h3>
                <p class="sso-section-sub">
                  Fill these from your IdP's OIDC discovery document
                  (usually at <code style="font-family:var(--font-mono);font-size:12px;background:var(--bg-tertiary);padding:1px 5px;border-radius:4px">.well-known/openid-configuration</code>).
                </p>
              </div>
            </header>
            <div class="sso-section-body">
              <div class="sso-toggle-row">
                <input
                  type="checkbox"
                  name="enabled"
                  value="1"
                  checked={!!cfg?.enabled}
                  aria-label="Enable SSO sign-in on /login"
                  id="enabled"
                />
                <span>
                  <strong style="color:var(--text-strong)">Enable SSO sign-in on /login.</strong>
                  {" "}When off, the config is saved but the button doesn't render
                  and the callback endpoint refuses requests.
                </span>
              </div>

              <div class="sso-field">
                <div class="sso-field-row">
                  <label for="provider_name">Button label</label>
                </div>
                <input
                  type="text"
                  id="provider_name"
                  name="provider_name"
                  value={cfg?.providerName || "SSO"}
                  maxLength={120}
                  placeholder="Okta"
                  class="sso-input"
                />
                <div class="sso-hint">
                  Shown on the login page: "Sign in with <code>{cfg?.providerName || "SSO"}</code>".
                </div>
              </div>

              <div class="sso-field">
                <div class="sso-field-row">
                  <label for="issuer">Issuer URL</label>
                </div>
                <input
                  type="text"
                  id="issuer"
                  name="issuer"
                  value={cfg?.issuer || ""}
                  placeholder="https://example.okta.com"
                  class="sso-input"
                />
              </div>

              <div class="sso-field">
                <div class="sso-field-row">
                  <label for="authorization_endpoint">Authorization endpoint</label>
                </div>
                <input
                  type="text"
                  id="authorization_endpoint"
                  name="authorization_endpoint"
                  value={cfg?.authorizationEndpoint || ""}
                  placeholder="https://example.okta.com/oauth2/v1/authorize"
                  class="sso-input"
                />
              </div>

              <div class="sso-field">
                <div class="sso-field-row">
                  <label for="token_endpoint">Token endpoint</label>
                </div>
                <input
                  type="text"
                  id="token_endpoint"
                  name="token_endpoint"
                  value={cfg?.tokenEndpoint || ""}
                  placeholder="https://example.okta.com/oauth2/v1/token"
                  class="sso-input"
                />
              </div>

              <div class="sso-field">
                <div class="sso-field-row">
                  <label for="userinfo_endpoint">Userinfo endpoint</label>
                </div>
                <input
                  type="text"
                  id="userinfo_endpoint"
                  name="userinfo_endpoint"
                  value={cfg?.userinfoEndpoint || ""}
                  placeholder="https://example.okta.com/oauth2/v1/userinfo"
                  class="sso-input"
                />
              </div>
            </div>
          </section>

          <section class="sso-section">
            <header class="sso-section-head">
              <div class="sso-section-head-text">
                <h3 class="sso-section-title">Client credentials</h3>
                <p class="sso-section-sub">
                  Issued by your IdP when you register Gluecron as an
                  application. The secret never leaves the server.
                </p>
              </div>
            </header>
            <div class="sso-section-body">
              <div class="sso-field">
                <div class="sso-field-row">
                  <label for="client_id">Client ID</label>
                </div>
                <input
                  type="text"
                  id="client_id"
                  name="client_id"
                  value={cfg?.clientId || ""}
                  autocomplete="off"
                  class="sso-input"
                />
              </div>

              <div class="sso-field">
                <div class="sso-field-row">
                  <label for="client_secret">Client secret</label>
                  {cfg?.clientSecret && (
                    <span class="sso-status-pill is-on" style="margin-top:0">
                      <span class="dot" aria-hidden="true" />
                      Stored
                    </span>
                  )}
                </div>
                <input
                  type="password"
                  id="client_secret"
                  name="client_secret"
                  value={cfg?.clientSecret || ""}
                  autocomplete="off"
                  placeholder={
                    cfg?.clientSecret ? "(storedleave blank to keep)" : ""
                  }
                  class="sso-input"
                />
              </div>
            </div>
          </section>

          <section class="sso-section">
            <header class="sso-section-head">
              <div class="sso-section-head-text">
                <h3 class="sso-section-title">Scopes &amp; access control</h3>
                <p class="sso-section-sub">
                  Restrict who can sign in and what happens on first login.
                </p>
              </div>
            </header>
            <div class="sso-section-body">
              <div class="sso-field">
                <div class="sso-field-row">
                  <label for="scopes">OIDC scopes</label>
                </div>
                <input
                  type="text"
                  id="scopes"
                  name="scopes"
                  value={cfg?.scopes || "openid profile email"}
                  class="sso-input"
                />
                <div class="sso-hint">
                  Space-separated. Defaults to <code style="font-family:var(--font-mono);font-size:11.5px;background:var(--bg-tertiary);padding:1px 4px;border-radius:4px">openid profile email</code> — enough for username + display name.
                </div>
              </div>

              <div class="sso-field">
                <div class="sso-field-row">
                  <label for="allowed_email_domains">Allowed email domains</label>
                </div>
                <input
                  type="text"
                  id="allowed_email_domains"
                  name="allowed_email_domains"
                  value={cfg?.allowedEmailDomains || ""}
                  placeholder="example.com, acme.io"
                  class="sso-input"
                />
                <div class="sso-hint">
                  Comma-separated. Empty = any domain accepted. Enforced
                  case-insensitively against the email claim.
                </div>
              </div>

              <div class="sso-toggle-row">
                <input
                  type="checkbox"
                  name="auto_create_users"
                  value="1"
                  checked={cfg ? cfg.autoCreateUsers : true}
                  aria-label="Auto-create users on first SSO sign-in"
                  id="auto_create_users"
                />
                <span>
                  <strong style="color:var(--text-strong)">Auto-create local accounts on first SSO sign-in.</strong>
                  {" "}Turn off to require admins to pre-provision users — first
                  login then fails with "user not found".
                </span>
              </div>
            </div>
            <div class="sso-foot">
              <span class="sso-foot-hint">
                Changes apply immediately. Test from <a href="/login" style="color:var(--accent);text-decoration:none">/login</a>.
              </span>
              <button type="submit" class="sso-btn sso-btn-primary">
                Save SSO settings
              </button>
            </div>
          </section>
        </form>
      </div>
      <style dangerouslySetInnerHTML={{ __html: ssoStyles }} />
    </Layout>
  );
});

sso.post("/admin/sso", requireAuth, async (c) => {
  const g = await adminGate(c);
  if (g instanceof Response) return g;
  const { user } = g;

  const body = await c.req.parseBody();
  const existing = await getSsoConfig();
  const secretSubmitted = String(body.client_secret || "");
  const result = await upsertSsoConfig({
    enabled: String(body.enabled || "") === "1",
    providerName: String(body.provider_name || "SSO"),
    issuer: String(body.issuer || ""),
    authorizationEndpoint: String(body.authorization_endpoint || ""),
    tokenEndpoint: String(body.token_endpoint || ""),
    userinfoEndpoint: String(body.userinfo_endpoint || ""),
    clientId: String(body.client_id || ""),
    clientSecret:
      secretSubmitted.trim().length === 0 && existing?.clientSecret
        ? existing.clientSecret
        : secretSubmitted,
    scopes: String(body.scopes || "openid profile email"),
    allowedEmailDomains: String(body.allowed_email_domains || ""),
    autoCreateUsers: String(body.auto_create_users || "") === "1",
  });

  if (!result.ok) {
    return c.redirect(
      `/admin/sso?error=${encodeURIComponent(result.error)}`
    );
  }

  await audit({
    userId: user.id,
    action: "admin.sso.configure",
    metadata: {
      enabled: String(body.enabled || "") === "1",
      provider: String(body.provider_name || "SSO"),
      autoCreateUsers: String(body.auto_create_users || "") === "1",
      allowedDomains: String(body.allowed_email_domains || "") || null,
    },
  });

  return c.redirect(
    `/admin/sso?success=${encodeURIComponent("SSO settings saved.")}`
  );
});

// ----------------------------------------------------------------------------
// OIDC flow
// ----------------------------------------------------------------------------

sso.get("/login/sso", async (c) => {
  const cfg = await getSsoConfig();
  if (!cfg || !cfg.enabled) {
    return c.redirect(
      `/login?error=${encodeURIComponent("SSO is not enabled")}`
    );
  }
  if (
    !cfg.authorizationEndpoint ||
    !cfg.tokenEndpoint ||
    !cfg.userinfoEndpoint ||
    !cfg.clientId ||
    !cfg.clientSecret
  ) {
    return c.redirect(
      `/login?error=${encodeURIComponent("SSO is not fully configured")}`
    );
  }
  const state = randomToken(16);
  const nonce = randomToken(16);
  const redirectUri = ssoRedirectUri();
  let target: string;
  try {
    target = buildAuthorizeUrl(cfg, state, nonce, redirectUri);
  } catch (err) {
    return c.redirect(
      `/login?error=${encodeURIComponent(
        err instanceof Error ? err.message : "SSO misconfigured"
      )}`
    );
  }
  setCookie(c, "sso_state", state, ssoStateCookieOpts());
  setCookie(c, "sso_nonce", nonce, ssoStateCookieOpts());
  return c.redirect(target);
});

sso.get("/login/sso/callback", async (c) => {
  const cfg = await getSsoConfig();
  if (!cfg || !cfg.enabled) {
    return c.redirect(
      `/login?error=${encodeURIComponent("SSO is not enabled")}`
    );
  }

  const code = c.req.query("code");
  const state = c.req.query("state");
  const errCode = c.req.query("error");
  if (errCode) {
    return c.redirect(
      `/login?error=${encodeURIComponent(
        `SSO provider error: ${errCode}`
      )}`
    );
  }
  if (!code || !state) {
    return c.redirect(
      `/login?error=${encodeURIComponent("Missing code or state")}`
    );
  }

  const expectedState = getCookie(c, "sso_state");
  if (!expectedState || expectedState !== state) {
    return c.redirect(
      `/login?error=${encodeURIComponent(
        "SSO state mismatch. Please try again."
      )}`
    );
  }

  // One-shot cookies — burn them even on failure
  deleteCookie(c, "sso_state", { path: "/" });
  deleteCookie(c, "sso_nonce", { path: "/" });

  try {
    const tokens = await exchangeCode(cfg, code, ssoRedirectUri());
    const claims = await fetchUserinfo(cfg, tokens.access_token);
    const result = await findOrCreateUserFromSso(claims, cfg);
    if (!result.ok) {
      return c.redirect(`/login?error=${encodeURIComponent(result.error)}`);
    }

    const token = await issueSsoSession(result.user.id);
    setCookie(c, "session", token, sessionCookieOptions());

    await audit({
      userId: result.user.id,
      action: "auth.sso.login",
      metadata: {
        provider: cfg.providerName,
        sub: claims.sub,
        email: claims.email || null,
      },
    });

    return c.redirect("/");
  } catch (err) {
    console.error("[sso] callback error:", err);
    const friendly = friendlySsoError(err);
    return c.redirect(`/login?error=${encodeURIComponent(friendly)}`);
  }
});

/**
 * Map the raw OIDC failure shape to a one-sentence message safe to render
 * inside an HTML <div>. We never surface the IdP's response body — those
 * have been Google 404 HTML pages, Azure JSON blobs full of object IDs,
 * etc. The raw `err.message` is logged via console.error above so admins
 * can still diagnose from server logs.
 */
function friendlySsoError(err: unknown): string {
  const raw = err instanceof Error ? err.message : String(err ?? "");
  if (raw.includes("token_endpoint")) {
    if (/\b40[01]\b/.test(raw)) {
      return "SSO sign-in failed: the identity provider rejected our token request (HTTP 4xx). Check the Token endpoint URL and Client Secret at /admin/sso.";
    }
    if (/\b404\b/.test(raw)) {
      return "SSO sign-in failed: the Token endpoint URL returned 404. Verify the URL at /admin/sso — for Google it's https://oauth2.googleapis.com/token.";
    }
    if (/\b5\d\d\b/.test(raw)) {
      return "SSO sign-in failed: the identity provider returned a server error. Try again, or check the IdP's status page.";
    }
    return "SSO sign-in failed at the token exchange step. Check /admin/sso configuration.";
  }
  if (raw.includes("userinfo_endpoint")) {
    return "SSO sign-in failed while fetching profile info. Verify the Userinfo endpoint URL at /admin/sso.";
  }
  if (raw.includes("state cookie") || raw.includes("nonce")) {
    return "SSO sign-in expired before you returned to the site. Please try again.";
  }
  if (raw.includes("email") && raw.includes("not allowed")) {
    return "SSO sign-in failed: your email domain is not on the allowlist for this site.";
  }
  return "SSO sign-in failed. Check /admin/sso configuration or try again.";
}

// ----------------------------------------------------------------------------
// User: unlink SSO
// ----------------------------------------------------------------------------

sso.post("/settings/sso/unlink", requireAuth, async (c) => {
  const user = c.get("user")!;
  const links = await db
    .select({ id: ssoUserLinks.id })
    .from(ssoUserLinks)
    .where(eq(ssoUserLinks.userId, user.id));
  if (links.length === 0) {
    return c.redirect("/settings?error=" + encodeURIComponent("No SSO link"));
  }
  await db.delete(ssoUserLinks).where(eq(ssoUserLinks.userId, user.id));
  await audit({
    userId: user.id,
    action: "auth.sso.unlink",
    metadata: { removedLinks: links.length },
  });
  return c.redirect(
    "/settings?success=" + encodeURIComponent("SSO link removed.")
  );
});

export default sso;