feat(api): enforce roles, org sign-in policies, invitations and api tokens

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
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 00:49:35 +01:00
1 parent 30409697f7
commit d7d4821e3e
24 files changed
+4677 -439

No files matched your search

+7 -7
View File
@@ -91,7 +91,7 @@ func (s *Server) signup(c *gin.Context) {
internalError(c, err)
return
}
s.startSession(c, user.ID, orgID, i18n.Lang(ctx))
s.startSession(c, auth.NewSession{UserID: user.ID, OrgID: orgID, Locale: i18n.Lang(ctx), Method: auth.MethodPassword})
}
func (s *Server) signupAllowed(ctx context.Context) (bool, error) {
@@ -164,7 +164,7 @@ func (s *Server) login(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "totp_required"})
return
}
s.finishLogin(c, user.ID)
s.finishLogin(c, user.ID, auth.MethodPassword)
}
// secondFactorRequest carries a TOTP code or a recovery code.
@@ -211,10 +211,10 @@ func (s *Server) loginTOTP(c *gin.Context) {
}
_ = s.rdb.Del(ctx, mfaKey(token)).Err()
setCookie(c, mfaCookie, "", -1)
s.finishLogin(c, userID)
s.finishLogin(c, userID, auth.MethodPasswordTOTP)
}
func (s *Server) finishLogin(c *gin.Context, userID uuid.UUID) {
func (s *Server) finishLogin(c *gin.Context, userID uuid.UUID, method auth.Method) {
ctx := c.Request.Context()
var (
user store.User
@@ -236,14 +236,14 @@ func (s *Server) finishLogin(c *gin.Context, userID uuid.UUID) {
if len(orgs) > 0 {
org = orgs[0].ID
}
s.startSession(c, userID, org, deref(user.Locale))
s.startSession(c, auth.NewSession{UserID: userID, OrgID: org, Locale: deref(user.Locale), Method: method})
}
func (s *Server) startSession(c *gin.Context, userID, orgID uuid.UUID, locale string) {
func (s *Server) startSession(c *gin.Context, n auth.NewSession) {
if old := auth.Current(c); old != nil {
_ = s.sessions.Delete(c.Request.Context(), old)
}
sess, err := s.sessions.Create(c.Request.Context(), userID, orgID, locale)
sess, err := s.sessions.Create(c.Request.Context(), n)
if err != nil {
internalError(c, err)
return