Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

feat(auth): improve session validation and enforce workspace RBAC

Jonas Kanindaabf5e7051438
diff --git a/internal/middlewares/middleware.go b/internal/middlewares/middleware.go
index abd8481..ccf9d84 100644
--- a/internal/middlewares/middleware.go
+++ b/internal/middlewares/middleware.go
@@ -31,6 +31,8 @@ import (
"github.com/jkaninda/okapi"
)
+const sessionRevokedKey = "session_revoked"
+
func baseJWTAuth(cfg *config.Config) okapi.JWTAuth {
return okapi.JWTAuth{
SigningSecret: []byte(cfg.JWTSecret),
@@ -54,20 +56,29 @@ func sessionValidator(store *sessionpkg.Store) func(c *okapi.Context, claims jwt
}
jti, _ := mapClaims["jti"].(string)
if jti == "" {
- return nil // legacy token without jti, allow through
+ return errors.New("invalid token, missing jti")
}
if store.IsRevoked(c.Request().Context(), jti) {
+ c.Set(sessionRevokedKey, true)
return errors.New("session has been revoked")
}
return nil
}
}
+func sessionAwareUnauthorized(c *okapi.Context) error {
+ if c.GetBool(sessionRevokedKey) {
+ return c.AbortUnauthorized("session has been revoked")
+ }
+ return c.AbortForbidden("Insufficient permissions")
+}
+
// JWTAuth creates user JWT auth middleware. If sessionStore is non-nil, revoked sessions are rejected.
func JWTAuth(cfg *config.Config, sessionStore ...*sessionpkg.Store) okapi.JWTAuth {
auth := baseJWTAuth(cfg)
if len(sessionStore) > 0 && sessionStore[0] != nil {
auth.ValidateClaims = sessionValidator(sessionStore[0])
+ auth.OnUnauthorized = sessionAwareUnauthorized
}
return auth
}
@@ -78,6 +89,7 @@ func JWTAdminAuth(cfg *config.Config, sessionStore ...*sessionpkg.Store) okapi.J
auth.ClaimsExpression = "Equals(`role`,`admin`)"
if len(sessionStore) > 0 && sessionStore[0] != nil {
auth.ValidateClaims = sessionValidator(sessionStore[0])
+ auth.OnUnauthorized = sessionAwareUnauthorized
}
return auth
}
@@ -89,6 +101,7 @@ func JWTAdminQueryAuth(cfg *config.Config, sessionStore ...*sessionpkg.Store) ok
auth.TokenLookup = "query:token"
if len(sessionStore) > 0 && sessionStore[0] != nil {
auth.ValidateClaims = sessionValidator(sessionStore[0])
+ auth.OnUnauthorized = sessionAwareUnauthorized
}
return auth
}
diff --git a/web/src/api/client.ts b/web/src/api/client.ts
index 88e2eb0..383eb68 100644
--- a/web/src/api/client.ts
+++ b/web/src/api/client.ts
@@ -21,8 +21,9 @@ api.interceptors.response.use(
(error) => {
if (error.response?.status === 401 && !error.response?.data?.data?.requires_2fa) {
const auth = useAuthStore()
+ const message = error.response?.data?.error?.message || 'Your session has expired'
auth.logout()
- window.location.href = '/login'
+ window.location.href = `/login?error=${encodeURIComponent(message)}`
}
return Promise.reject(error)
}
diff --git a/web/src/views/auth/Login.vue b/web/src/views/auth/Login.vue
index 9bb4184..1ac749f 100644
--- a/web/src/views/auth/Login.vue
+++ b/web/src/views/auth/Login.vue
@@ -147,9 +147,9 @@ function resetLogin() {
</div>
</div>
- <!-- Login error from OAuth redirect -->
+ <!-- Login error from redirect (OAuth failure, session revoked, etc.) -->
<div v-if="loginError" class="oauth-error-msg">
- Authentication failed: {{ loginError.replace(/_/g, ' ') }}
+ {{ loginError.replace(/_/g, ' ') }}
</div>
<div class="auth-footer">