Every org route resolves a principal from the session or a bearer token and checks a permission. Nobody can grant, revoke or remove more than they hold. Tokens are capped by their owner's current rights and cannot manage credentials. Orgs can require TOTP or SSO; the owner keeps a password fallback for SSO. Closes #6 Closes #7 Closes #8 Closes #9 Closes #41 Closes #45 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
172 lines
7.1 KiB
Templ
172 lines
7.1 KiB
Templ
package web
|
|
|
|
import "git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
|
|
|
|
// Me is the signed-in user as the console templates see it.
|
|
type Me struct {
|
|
// IsOwner refers to the active organization.
|
|
IsOwner bool
|
|
ActiveSlug string
|
|
Email string
|
|
DisplayName string
|
|
TOTP bool
|
|
ActiveOrg string
|
|
Orgs []Org
|
|
// Can holds the permissions in the active organization.
|
|
Can map[string]bool
|
|
// Policy is the error code of an unmet sign-in policy of the active
|
|
// organization, or empty.
|
|
Policy string
|
|
// RecoveryCodesLeft counts unused TOTP recovery codes.
|
|
RecoveryCodesLeft int
|
|
}
|
|
|
|
// Org is one entry of the organization switcher.
|
|
type Org struct {
|
|
ID string
|
|
Name string
|
|
Slug string
|
|
IsOwner bool
|
|
}
|
|
|
|
templ consoleShell(p Page, me Me) {
|
|
@Layout(p) {
|
|
<header class="border-b border-rule">
|
|
<div class="mx-auto flex max-w-5xl items-center gap-6 px-6 py-3 text-sm">
|
|
<a href="/" class="font-mono text-xs tracking-wide">uptimesebi</a>
|
|
if len(me.Orgs) > 1 {
|
|
<select data-switch-org class="border border-rule bg-transparent px-1.5 py-0.5 text-sm">
|
|
for _, o := range me.Orgs {
|
|
<option value={ o.ID } selected?={ o.ID == me.ActiveOrg }>{ o.Name }</option>
|
|
}
|
|
</select>
|
|
} else if len(me.Orgs) == 1 {
|
|
<span class="text-muted">{ me.Orgs[0].Name }</span>
|
|
}
|
|
<nav class="ml-auto flex items-center gap-5">
|
|
if me.Can["members.read"] {
|
|
<a href="/settings/members" class="text-muted hover:text-ink">{ i18n.T(ctx, "console.nav.members") }</a>
|
|
<a href="/settings/roles" class="text-muted hover:text-ink">{ i18n.T(ctx, "console.nav.roles") }</a>
|
|
}
|
|
if len(me.Orgs) > 0 {
|
|
<a href="/settings/tokens" class="text-muted hover:text-ink">{ i18n.T(ctx, "console.nav.tokens") }</a>
|
|
}
|
|
if me.Can["org.manage"] {
|
|
<a href="/settings/org" class="text-muted hover:text-ink">{ i18n.T(ctx, "console.nav.org") }</a>
|
|
<a href="/settings/sso" class="text-muted hover:text-ink">{ i18n.T(ctx, "console.nav.sso") }</a>
|
|
}
|
|
<a href="/settings/security" class="text-muted hover:text-ink">{ i18n.T(ctx, "console.nav.security") }</a>
|
|
<button data-logout class="text-muted hover:text-ink">{ i18n.T(ctx, "console.nav.logout") }</button>
|
|
@languageSwitch(p.Languages)
|
|
</nav>
|
|
</div>
|
|
</header>
|
|
switch me.Policy {
|
|
case "totp_required_by_org":
|
|
@policyBanner("error.totp_required_by_org", "console.totp_missing_action", "/settings/security")
|
|
case "sso_required_by_org":
|
|
@policyBanner("error.sso_required_by_org", "console.sso_required_action", "/sso/"+me.ActiveSlug)
|
|
}
|
|
<main class="mx-auto max-w-5xl px-6 py-10">
|
|
@flash(p)
|
|
{ children... }
|
|
</main>
|
|
}
|
|
}
|
|
|
|
templ Dashboard(p Page, me Me) {
|
|
@consoleShell(p, me) {
|
|
<h1 class="text-lg font-semibold">{ i18n.T(ctx, "console.monitors.title") }</h1>
|
|
<p class="mt-2 text-sm text-muted">{ i18n.T(ctx, "console.monitors.empty") }</p>
|
|
}
|
|
}
|
|
|
|
// Identity is an SSO sign-in linked to the user in the active organization.
|
|
type Identity struct {
|
|
Provider string
|
|
Email string
|
|
}
|
|
|
|
templ Security(p Page, me Me, identities []Identity, canLink bool) {
|
|
@consoleShell(p, me) {
|
|
<h1 class="mb-8 text-lg font-semibold">{ i18n.T(ctx, "security.title") }</h1>
|
|
<section class="max-w-md">
|
|
<h2 class="mb-1 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "security.totp.heading") }</h2>
|
|
if me.TOTP {
|
|
<p class="mb-1 text-sm"><span class="text-up">●</span> { i18n.T(ctx, "security.totp.active") }</p>
|
|
<p class={ "mb-6 text-sm", templ.KV("text-down", me.RecoveryCodesLeft < 3), templ.KV("text-muted", me.RecoveryCodesLeft >= 3) }>
|
|
{ i18n.T(ctx, "security.recovery.left", "Count", me.RecoveryCodesLeft) }
|
|
</p>
|
|
<details class="mb-6">
|
|
<summary class="cursor-pointer text-sm text-muted hover:text-ink">{ i18n.T(ctx, "security.recovery.regenerate") }</summary>
|
|
<form data-api="/me/totp/recovery-codes" data-show-codes class="mt-4">
|
|
@formError()
|
|
@field(i18n.T(ctx, "auth.field.password"), "password", "password", "current-password", true)
|
|
@field(i18n.T(ctx, "security.totp.current_code"), "code", "text", "one-time-code", true)
|
|
@submit(i18n.T(ctx, "security.recovery.regenerate"))
|
|
@recoveryOutput()
|
|
</form>
|
|
</details>
|
|
<details>
|
|
<summary class="cursor-pointer text-sm text-muted hover:text-ink">{ i18n.T(ctx, "security.totp.disable") }</summary>
|
|
<form data-api="/me/totp/disable" data-next="/settings/security" class="mt-4">
|
|
@formError()
|
|
@field(i18n.T(ctx, "auth.field.password"), "password", "password", "current-password", true)
|
|
@field(i18n.T(ctx, "security.totp.current_code"), "code", "text", "one-time-code", true)
|
|
@submit(i18n.T(ctx, "security.totp.disable"))
|
|
</form>
|
|
</details>
|
|
} else {
|
|
<p class="mb-6 text-sm"><span class="text-unknown">●</span> { i18n.T(ctx, "security.totp.inactive") }</p>
|
|
<div data-totp-setup>
|
|
<button data-totp-start class="border border-ink px-3 py-1.5 text-sm hover:bg-ink hover:text-paper">{ i18n.T(ctx, "security.totp.start") }</button>
|
|
<div data-totp-panel hidden>
|
|
<img data-totp-qr alt={ i18n.T(ctx, "security.totp.qr_alt") } class="mb-3 h-48 w-48 border border-rule bg-white p-2"/>
|
|
<p class="mb-6 break-all font-mono text-xs text-muted" data-totp-secret></p>
|
|
<form data-api="/me/totp/enable" data-show-codes>
|
|
@formError()
|
|
@field(i18n.T(ctx, "security.totp.app_code"), "code", "text", "one-time-code", true)
|
|
@submit(i18n.T(ctx, "security.totp.confirm"))
|
|
@recoveryOutput()
|
|
</form>
|
|
</div>
|
|
</div>
|
|
}
|
|
</section>
|
|
<section class="mt-12 max-w-md">
|
|
<h2 class="mb-3 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "security.sso.heading") }</h2>
|
|
if len(identities) == 0 {
|
|
<p class="mb-4 text-sm text-muted">{ i18n.T(ctx, "security.sso.none") }</p>
|
|
} else {
|
|
<ul class="mb-4 divide-y divide-rule border-y border-rule text-sm">
|
|
for _, id := range identities {
|
|
<li class="flex justify-between gap-4 py-2"><span>{ id.Provider }</span><span class="font-mono text-xs text-muted">{ id.Email }</span></li>
|
|
}
|
|
</ul>
|
|
}
|
|
if canLink {
|
|
<a href={ templ.SafeURL("/sso/" + me.ActiveSlug + "?link=1") } class="inline-block border border-ink px-3 py-1.5 text-sm hover:bg-ink hover:text-paper">{ i18n.T(ctx, "security.sso.link") }</a>
|
|
}
|
|
</section>
|
|
}
|
|
}
|
|
|
|
// recoveryOutput is filled by app.js with codes returned by the API; they
|
|
// are shown once and never rendered by the server.
|
|
templ recoveryOutput() {
|
|
<div data-recovery-output hidden class="mt-6 border border-ink p-4">
|
|
<p class="mb-3 text-sm">{ i18n.T(ctx, "security.recovery.save_now") }</p>
|
|
<ol data-recovery-list class="mb-4 grid grid-cols-2 gap-x-6 gap-y-1 font-mono text-sm"></ol>
|
|
<a href="/settings/security" class="text-sm underline underline-offset-2">{ i18n.T(ctx, "security.recovery.saved") }</a>
|
|
</div>
|
|
}
|
|
|
|
templ policyBanner(msgID, actionID, href string) {
|
|
<div class="border-b border-degraded/40 bg-degraded/10">
|
|
<p class="mx-auto max-w-5xl px-6 py-2 text-sm">
|
|
{ i18n.T(ctx, msgID) }
|
|
<a href={ templ.SafeURL(href) } class="underline underline-offset-2">{ i18n.T(ctx, actionID) }</a>
|
|
</p>
|
|
</div>
|
|
}
|