Compare commits

...

5 Commits

Author SHA1 Message Date
ef05d66787 feat: register signout route 2025-06-11 20:52:30 +02:00
b3296c45ad feat: get request JTI helper 2025-06-11 20:52:22 +02:00
7fd163f957 feat: signout endpoitn 2025-06-11 20:51:39 +02:00
0f0d50a684 feat: set token jti in request 2025-06-11 20:39:34 +02:00
68074e02bc feat: jti request key 2025-06-11 20:39:20 +02:00
5 changed files with 47 additions and 1 deletions

View File

@ -96,6 +96,7 @@ func (h *AuthHandler) RegisterRoutes(api chi.Router) {
protected.Post("/email", h.requestEmailOtp)
protected.Post("/email/otp", h.confirmOtp)
protected.Post("/verify", h.finishVerification)
protected.Post("/signout", h.signOut)
})
r.Post("/login", h.login)

40
internal/auth/signout.go Normal file
View File

@ -0,0 +1,40 @@
package auth
import (
"log"
"net/http"
"gitea.local/admin/hspguard/internal/util"
"github.com/google/uuid"
)
func (h *AuthHandler) signOut(w http.ResponseWriter, r *http.Request) {
defer func() {
w.WriteHeader(http.StatusOK)
w.Write([]byte("{\"status\": \"ok\"}"))
}()
jti, ok := util.GetRequestJTI(r.Context())
if !ok {
log.Println("WARN: No JTI found in request")
return
}
jtiId, err := uuid.Parse(jti)
if err != nil {
log.Printf("ERR: Failed to parse jti '%s' as v4 uuid: %v\n", jti, err)
return
}
session, err := h.repo.GetUserSessionByAccessJTI(r.Context(), &jtiId)
if err != nil {
log.Printf("WARN: Could not find session by jti id '%s': %v\n", jtiId.String(), err)
return
}
if err := h.repo.RevokeUserSession(r.Context(), session.ID); err != nil {
log.Printf("ERR: Failed to revoke session with '%s' id: %v\n", session.ID.String(), err)
} else {
log.Printf("INFO: Revoked session with jti = '%s' and session id = '%s'\n", jtiId.String(), session.ID.String())
}
}

View File

@ -46,6 +46,7 @@ func (m *AuthMiddleware) Runner(next http.Handler) http.Handler {
}
ctx := context.WithValue(r.Context(), types.UserIdKey, userClaims.Subject)
ctx = context.WithValue(ctx, types.JTIKey, userClaims.ID)
next.ServeHTTP(w, r.WithContext(ctx))
})
}

View File

@ -3,4 +3,4 @@ package types
type contextKey string
const UserIdKey contextKey = "userID"
const JTIKey contextKey = "jti"

View File

@ -11,3 +11,7 @@ func GetRequestUserId(ctx context.Context) (string, bool) {
return userId, ok
}
func GetRequestJTI(ctx context.Context) (string, bool) {
jti, ok := ctx.Value(types.JTIKey).(string)
return jti, ok
}