Files
opencloud/services/auth-guest/pkg/server/http/verify_token.go
T

116 lines
3.4 KiB
Go

// Copyright 2026 OpenCloud GmbH <mail@opencloud.eu>
// SPDX-License-Identifier: Apache-2.0
package http
import (
"encoding/json"
"net/http"
"time"
"github.com/opencloud-eu/opencloud/pkg/log"
"github.com/opencloud-eu/opencloud/services/auth-guest/pkg/config"
"github.com/opencloud-eu/opencloud/services/auth-guest/pkg/service/authguest"
"github.com/opencloud-eu/opencloud/services/auth-guest/pkg/service/token"
)
const (
maxVerifyTokenBodySize = 4 << 10
nonceCookieNameHashLength = 8
)
// VerifyTokenRequest is the request body for token verification.
type VerifyTokenRequest struct {
Token string `json:"token"`
}
type sessionResponse struct {
PermissionID string `json:"permissionId"`
ExpirationDateTime time.Time `json:"expirationDateTime"`
}
// VerifyTokenHandler validates the token submitted to the verify token endpoint.
func VerifyTokenHandler(log log.Logger, s authguest.AuthGuest, cfg *config.Config) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxVerifyTokenBodySize)
var req VerifyTokenRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
log.Debug().Err(err).Msg("request body is malformed")
writeError(w, http.StatusBadRequest, errorResponse{ErrorType: "invalidRequest", Message: "The request body is malformed."})
return
}
if req.Token == "" {
log.Debug().Msg("request body is missing the token")
writeError(w, http.StatusBadRequest, errorResponse{ErrorType: "invalidRequest", Message: "The request body is missing the token."})
return
}
result, err := s.VerifyToken(r.Context(), req.Token)
if err != nil {
log.Debug().Err(err).Msg("verify token failed")
writeGuestError(w, err)
return
}
setSessionCookie(w, cfg, result.SessionToken)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(sessionResponse{PermissionID: result.ShareID, ExpirationDateTime: result.ExpiresAt.UTC()})
}
}
// nonceCookieName returns the per-share nonce cookie name
func nonceCookieName(baseName, shareID string) string {
return baseName + "_" + token.Hash(shareID)[:nonceCookieNameHashLength]
}
func setSessionCookie(w http.ResponseWriter, cfg *config.Config, sessionToken string) {
http.SetCookie(w, &http.Cookie{
Name: cfg.JWT.CookieName,
Value: sessionToken,
Path: "/",
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
MaxAge: int(cfg.JWT.TTL.Seconds()),
})
}
func clearSessionCookie(w http.ResponseWriter, cfg *config.Config) {
http.SetCookie(w, &http.Cookie{
Name: cfg.JWT.CookieName,
Value: "",
Path: "/",
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
MaxAge: -1,
})
}
func setNonceCookie(w http.ResponseWriter, cfg *config.Config, shareID, nonce string) {
http.SetCookie(w, &http.Cookie{
Name: nonceCookieName(cfg.Nonce.CookieBaseName, shareID),
Value: nonce,
Path: "/",
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
MaxAge: int(cfg.PinAndNonceTTL.Seconds()),
})
}
func clearNonceCookie(w http.ResponseWriter, baseName, shareID string) {
http.SetCookie(w, &http.Cookie{
Name: nonceCookieName(baseName, shareID),
Value: "",
Path: "/",
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
MaxAge: -1,
})
}