RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
refactor (#21771)
Jin Hai2fc8373d4cca
diff --git a/internal/handler/user.go b/internal/handler/user.goindex 09bc2bbf7e..876bc7693b 100644--- a/internal/handler/user.go+++ b/internal/handler/user.go@@ -77,6 +77,19 @@ func setOAuthAuthCookie(c *gin.Context, token string) {// @Router /api/v1/users [post]func (h *UserHandler) Register(c *gin.Context) {ctx := c.Request.Context()+ startAt := time.Now()+ operationLog := &common.OperationLog{+ EventTime: startAt,+ Operation: "register",+ APIPath: c.FullPath(),+ HTTPMethod: c.Request.Method,+ IPAddress: c.ClientIP(),+ }+ defer func() {+ operationLog.DurationMS = time.Since(startAt).Milliseconds()+ common.Audit(operationLog)+ }()+var req service.RegisterRequestif err := c.ShouldBindJSON(&req); err != nil {common.ResponseWithCodeData(c, common.CodeBadRequest, false, err.Error())@@ -92,15 +105,21 @@ func (h *UserHandler) Register(c *gin.Context) {common.ResponseWithCodeData(c, code, data, err.Error())return}+ operationLog.UserID = user.ID+ operationLog.UserName = user.EmailsecretKey, err := server.GetSecretKey(ctx, kvrocks.Get())if err != nil {common.ResponseWithCodeData(c, common.CodeServerError, false, err.Error())+ operationLog.Code = int(common.CodeBadRequest)+ operationLog.Error = errreturn}authToken, err := issueUserAccessToken(user, secretKey)if err != nil {common.ResponseWithCodeData(c, common.CodeServerError, false, "Failed to generate auth token")+ operationLog.Code = int(common.CodeBadRequest)+ operationLog.Error = errreturn}@@ -527,160 +546,3 @@ func joinStrings(values []string) string {}return result}--// ---- Forgot-password flow (fixes #15282) ------------------------------//-// Mirrors api/apps/restful_apis/user_api.py /auth/password/... endpoints.-//-// Contract divergence from Python: the Python endpoint returns a-// rendered image (Content-Type: image/JPEG) from the python-captcha-// library and stores the captcha under captcha:<email>. This Go port-// returns a server-issued captcha_id plus a PNG captcha image (as a-// data URL the FE drops straight into <img src>), and stores-// captcha:<captcha_id>. The plaintext text only ever appears as-// raster pixels — the OTP step reuses the captcha_id to look the-// expected text up server-side.-//-// The PNG is rendered using stdlib `image/png` + a hand-rolled 5x7-// bitmap font in internal/utility/captcha_png.go, because no Go-// captcha library is vendored in go.mod (no network during build).-// PR #15290 review (Hz-186) explicitly asked for a raster after the-// earlier SVG implementation: the SVG embedded the answer in <text>-// nodes, so a scripted client could base64-decode the response and-// grep the captcha directly. PNG closes that attack — the response-// bytes never reference the original text.--type forgotCaptchaRequest struct {- Email string `form:"email" json:"email"`-}--// ForgotCaptcha POST /api/v1/auth/password/forgot/captcha-// @Summary Issue forgot-password captcha-// @Description Generates a captcha for the email and stores it in Redis-// for 60 seconds keyed by a server-issued captcha_id. Returns the id-// and a PNG image (data URL) the FE renders inside <img src>. The-// plaintext code never appears in the response — only as raster-// pixels — so a scripted client can't regex it out (fixes the-// SVG-text leak from the previous iteration, per PR #15290 review).-// @Tags auth-// @Param email query string false "user email (also accepted in JSON body)"-// @Success 200 {object} map[string]interface{}-// @Router /api/v1/auth/password/forgot/captcha [post]-func (h *UserHandler) ForgotCaptcha(c *gin.Context) {- var req forgotCaptchaRequest- // Python reads from request.args (query string), accept both for parity.- if v := c.Query("email"); v != "" {- req.Email = v- } else {- _ = c.ShouldBindJSON(&req)- }-- ctx := c.Request.Context()- captchaID, captchaImage, errCode, err := h.userService.ForgotIssueCaptcha(ctx, req.Email)- if err != nil {- common.ResponseWithCodeData(c, errCode, false, err.Error())- return- }- common.SuccessWithData(c, gin.H{- "captcha_id": captchaID,- "captcha_image": captchaImage,- }, "captcha issued")-}--type forgotSendOTPRequest struct {- Email string `json:"email"`- CaptchaID string `json:"captcha_id"`- Captcha string `json:"captcha"`-}--// ForgotSendOTP POST /api/v1/auth/password/forgot/otp-// @Summary Send forgot-password OTP-// @Description Validates the captcha (looked up by captcha_id), then-// mints a one-time code, stores a salted hash in Redis (5 min TTL,-// attempt cap, resend cooldown), and emails the OTP to the user.-// @Param request body forgotSendOTPRequest true "email + captcha_id + captcha"-// @Success 200 {object} map[string]interface{}-// @Router /api/v1/auth/password/forgot/otp [post]-func (h *UserHandler) ForgotSendOTP(c *gin.Context) {- var req forgotSendOTPRequest- if err := c.ShouldBindJSON(&req); err != nil {- common.ResponseWithCodeData(c, common.CodeArgumentError, false, err.Error())- return- }- ctx := c.Request.Context()- errCode, err := h.userService.ForgotSendOTP(ctx, req.Email, req.CaptchaID, req.Captcha)- if err != nil {- common.ResponseWithCodeData(c, errCode, false, err.Error())- return- }- common.SuccessWithData(c, true, "verification passed, email sent")-}--type forgotVerifyOTPRequest struct {- Email string `json:"email"`- OTP string `json:"otp"`-}--// ForgotVerifyOTP POST /api/v1/auth/password/forgot/otp/verify-// @Summary Verify forgot-password OTP-// @Description Consumes the OTP if it matches, sets a short-lived-// verified flag the reset endpoint will gate on. Wrong-OTP attempts-// are counted and a 30-minute lockout kicks in at the limit.-// @Tags auth-// @Param request body forgotVerifyOTPRequest true "email + otp"-// @Success 200 {object} map[string]interface{}-// @Router /api/v1/auth/password/forgot/otp/verify [post]-func (h *UserHandler) ForgotVerifyOTP(c *gin.Context) {- var req forgotVerifyOTPRequest- if err := c.ShouldBindJSON(&req); err != nil {- common.ResponseWithCodeData(c, common.CodeArgumentError, false, err.Error())- return- }- ctx := c.Request.Context()- errCode, err := h.userService.ForgotVerifyOTP(ctx, req.Email, req.OTP)- if err != nil {- common.ResponseWithCodeData(c, errCode, false, err.Error())- return- }- common.SuccessWithData(c, true, "otp verified")-}--// ForgotResetPassword POST /api/v1/auth/password/reset-// @Summary Reset password after OTP verification-// @Description Requires a successful prior verify call (verified flag-// set in Redis). Updates the password hash and rotates the access-// token so the response can auto-login the user.-// @Tags auth-// @Param request body service.ForgotResetPasswordRequest true "email + new password"-// @Success 200 {object} map[string]interface{}-// @Router /api/v1/auth/password/reset [post]-func (h *UserHandler) ForgotResetPassword(c *gin.Context) {- ctx := c.Request.Context()- var req service.ForgotResetPasswordRequest- if err := c.ShouldBindJSON(&req); err != nil {- common.ResponseWithCodeData(c, common.CodeArgumentError, false, err.Error())- return- }-- user, code, err := h.userService.ForgotResetPassword(ctx, &req)- if err != nil {- common.ResponseWithCodeData(c, code, false, err.Error())- return- }-- secretKey, err := server.GetSecretKey(ctx, kvrocks.Get())- if err != nil {- common.ResponseWithCodeData(c, common.CodeServerError, false, fmt.Sprintf("Failed to get secret key: %s", err.Error()))- return- }- authToken, err := issueUserAccessToken(user, secretKey)- if err != nil {- common.ResponseWithCodeData(c, common.CodeServerError, false, "Failed to generate auth token")- return- }- c.Header("Authorization", authToken)- c.Header("Access-Control-Expose-Headers", "Authorization")-- profile := h.userService.GetUserProfile(ctx, user)- common.SuccessWithData(c, profile, "Password reset successful. Logged in.")-}diff --git a/internal/router/router.go b/internal/router/router.goindex 845820d7aa..4a8c7b6938 100644--- a/internal/router/router.go+++ b/internal/router/router.go@@ -181,14 +181,6 @@ func (r *Router) Setup(engine *gin.Engine) {apiNoAuth.GET("/connectors/gmail/oauth/web/callback", r.connectorHandler.GmailWebOAuthCallback)apiNoAuth.GET("/connectors/google-drive/oauth/web/callback", r.connectorHandler.GoogleDriveWebOAuthCallback)apiNoAuth.GET("/connectors/box/oauth/web/callback", r.connectorHandler.BoxWebOAuthCallback)- // Forgot-password flow (fixes #15282).- // Routes are intentionally registered before any auth middleware:- // a user who has forgotten their password is, by definition,- // unauthenticated.- apiNoAuth.POST("/auth/password/forgot/captcha", r.userHandler.ForgotCaptcha)- apiNoAuth.POST("/auth/password/forgot/otp", r.userHandler.ForgotSendOTP)- apiNoAuth.POST("/auth/password/forgot/otp/verify", r.userHandler.ForgotVerifyOTP)- apiNoAuth.POST("/auth/password/reset", r.userHandler.ForgotResetPassword)apiNoAuth.GET("/dify/retrieval/health", r.difyRetrievalHandler.HealthCheck)diff --git a/internal/service/user.go b/internal/service/user.goindex 92db76a67e..1d784416c7 100644--- a/internal/service/user.go+++ b/internal/service/user.go@@ -42,8 +42,6 @@ import ("ragflow/internal/dao""ragflow/internal/service/billing"-- "ragflow/internal/utility")// UserService user service@@ -945,260 +943,3 @@ func parseAuthorizationToken(authorization string) (string, error) {return "", fmt.Errorf("invalid authorization format")}}--// ---- Forgot-password flow (mirrors api/apps/restful_apis/user_api.py-// `/auth/password/...` endpoints, fixes #15282) ---------------------------// ForgotIssueCaptcha mints a captcha for the given email and stores the-// expected text in Redis under utility.CaptchaIDRedisKey, keyed by a-// fresh server-side captcha_id, with a 60s TTL. Returns the captcha_id-// and a renderable SVG image (data URL) the FE drops into <img src> so-// the human can read the challenge and type the answer. The plaintext-// code itself is never sent to the client outside the rendered image.-//-// Refuses unknown emails to avoid leaking the user list — matches Python.-func (s *UserService) ForgotIssueCaptcha(ctx context.Context, email string) (captchaID, imageDataURL string, code common.ErrorCode, err error) {- if email == "" {- return "", "", common.CodeArgumentError, fmt.Errorf("email is required")- }- if _, err = s.userDAO.GetByEmail(ctx, dao.DB, email); err != nil {- return "", "", common.CodeDataError, fmt.Errorf("invalid email")- }-- text, err := utility.GenerateCaptchaCode()- if err != nil {- return "", "", common.CodeServerError, err- }- captchaID = common.GenerateToken()- if ok := kvrocks.Get().Set(ctx, utility.CaptchaIDRedisKey(captchaID), text, 60*time.Second); !ok {- return "", "", common.CodeServerError, fmt.Errorf("failed to store captcha")- }- imageDataURL = utility.RenderCaptchaPNGDataURL(text)- return captchaID, imageDataURL, common.CodeSuccess, nil-}--// ForgotSendOTP verifies the captcha (looked up by the server-issued-// captcha_id), then issues an OTP and emails it. Hash-and-salt is-// stored in Redis under the keys returned by utility.OTPRedisKeys.-// Resend cooldown and per-email lockout behaviour otherwise match the-// Python implementation byte-for-byte.-func (s *UserService) ForgotSendOTP(ctx context.Context, email, captchaID, captcha string) (common.ErrorCode, error) {- if email == "" || captchaID == "" || captcha == "" {- return common.CodeArgumentError, fmt.Errorf("email, captcha_id and captcha required")- }- if _, err := s.userDAO.GetByEmail(ctx, dao.DB, email); err != nil {- return common.CodeDataError, fmt.Errorf("invalid email")- }-- rc := kvrocks.Get()- captchaKey := utility.CaptchaIDRedisKey(captchaID)- stored, _ := rc.Get(ctx, captchaKey)- if stored == "" {- return common.CodeNotEffective, fmt.Errorf("invalid or expired captcha")- }- if !strings.EqualFold(strings.TrimSpace(stored), strings.TrimSpace(captcha)) {- return common.CodeAuthenticationError, fmt.Errorf("invalid or expired captcha")- }- // One-shot: consume the captcha so a leaked captcha_id cannot be- // reused for a stream of OTP requests.- rc.Delete(ctx, captchaKey)-- codeKey, attemptsKey, lastSentKey, lockKey := utility.OTPRedisKeys(email)-- // Lockout — a previous verify burst already locked this email; do not- // let a request for a new OTP wipe the lock (deliberate divergence- // from the Python implementation, which deletes the lock here and so- // allows a locked attacker to clear their own lockout by re-requesting).- if locked, _ := rc.Get(ctx, lockKey); locked != "" {- return common.CodeNotEffective, fmt.Errorf("too many attempts, try later")- }-- // Resend cooldown — refuse if we already sent within the window.- if lastSent, _ := rc.Get(ctx, lastSentKey); lastSent != "" {- ts, parseErr := strconv.ParseInt(lastSent, 10, 64)- if parseErr == nil {- elapsed := time.Since(time.Unix(ts, 0))- remaining := utility.OTPResendCooldown - elapsed- if remaining > 0 {- return common.CodeNotEffective, fmt.Errorf("you still have to wait %d seconds", int(remaining.Seconds()))- }- }- }-- otp, err := utility.GenerateOTPCode()- if err != nil {- return common.CodeServerError, err- }- salt, err := utility.GenerateOTPSalt()- if err != nil {- return common.CodeServerError, err- }- codeHash := utility.HashOTPCode(otp, salt)- now := strconv.FormatInt(time.Now().Unix(), 10)-- // Snapshot the previous OTP-flow state so we can restore it if email- // delivery fails — otherwise the user is throttled by lastSentKey- // even though they never received the code.- prevCode, _ := rc.Get(ctx, codeKey)- prevAttempts, _ := rc.Get(ctx, attemptsKey)- prevLastSent, _ := rc.Get(ctx, lastSentKey)-- if !rc.Set(ctx, codeKey, utility.EncodeOTPStorageValue(codeHash, salt), utility.OTPTTL) {- return common.CodeServerError, fmt.Errorf("failed to store otp")- }- rc.Set(ctx, attemptsKey, "0", utility.OTPTTL)- rc.Set(ctx, lastSentKey, now, utility.OTPTTL)- // Note: lockKey is intentionally not cleared here. If the user has- // been locked out by a previous verify burst, requesting a new OTP- // does not lift the lock — we already refused above.-- ttlMin := int(utility.OTPTTL.Minutes())- cfg := server.GetConfig()- if err = utility.SendResetCodeEmail(cfg.GetSMTPConfig(), email, otp, ttlMin); err != nil {- // Roll back: restore prior code/attempts/last-sent or remove the- // keys we just wrote so the next attempt isn't blocked by the- // resend cooldown a failed send just installed.- if prevCode != "" {- rc.Set(ctx, codeKey, prevCode, utility.OTPTTL)- } else {- rc.Delete(ctx, codeKey)- }- if prevAttempts != "" {- rc.Set(ctx, attemptsKey, prevAttempts, utility.OTPTTL)- } else {- rc.Delete(ctx, attemptsKey)- }- if prevLastSent != "" {- rc.Set(ctx, lastSentKey, prevLastSent, utility.OTPTTL)- } else {- rc.Delete(ctx, lastSentKey)- }- return common.CodeServerError, fmt.Errorf("failed to send email")- }- return common.CodeSuccess, nil-}--// ForgotVerifyOTP checks an OTP submitted by the user. On success, it-// consumes the OTP/attempt counters and writes a short-lived "verified"-// flag the reset endpoint will gate on.-func (s *UserService) ForgotVerifyOTP(ctx context.Context, email, otp string) (common.ErrorCode, error) {- if email == "" || otp == "" {- return common.CodeArgumentError, fmt.Errorf("email and otp are required")- }- if _, err := s.userDAO.GetByEmail(ctx, dao.DB, email); err != nil {- return common.CodeDataError, fmt.Errorf("invalid email")- }-- rc := kvrocks.Get()- codeKey, attemptsKey, lastSentKey, lockKey := utility.OTPRedisKeys(email)-- if locked, _ := rc.Get(ctx, lockKey); locked != "" {- return common.CodeNotEffective, fmt.Errorf("too many attempts, try later")- }-- stored, _ := rc.Get(ctx, codeKey)- if stored == "" {- return common.CodeNotEffective, fmt.Errorf("expired otp")- }- storedHash, salt, err := utility.DecodeOTPStorageValue(stored)- if err != nil {- return common.CodeServerError, fmt.Errorf("otp storage corrupted")- }-- if utility.HashOTPCode(strings.ToUpper(strings.TrimSpace(otp)), salt) != storedHash {- // bump attempts; lock on >= limit- attempts := 0- if cur, _ := rc.Get(ctx, attemptsKey); cur != "" {- if n, perr := strconv.Atoi(cur); perr == nil {- attempts = n- }- }- attempts++- rc.Set(ctx, attemptsKey, strconv.Itoa(attempts), utility.OTPTTL)- if attempts >= utility.OTPAttemptLimit {- rc.Set(ctx, lockKey, strconv.FormatInt(time.Now().Unix(), 10), utility.OTPAttemptLockDuration)- }- return common.CodeAuthenticationError, fmt.Errorf("expired otp")- }-- // Success: clear OTP state, mark email verified.- rc.Delete(ctx, codeKey)- rc.Delete(ctx, attemptsKey)- rc.Delete(ctx, lastSentKey)- rc.Delete(ctx, lockKey)- if !rc.Set(ctx, utility.OTPVerifiedRedisKey(email), "1", utility.OTPTTL) {- return common.CodeServerError, fmt.Errorf("failed to set verification state")- }- return common.CodeSuccess, nil-}--// ForgotResetPasswordRequest carries the JSON body of /auth/password/reset.-//-// No `binding` tags on purpose: gin's validator fires inside-// c.ShouldBindJSON and produces a verbose-// `Key: 'ForgotResetPasswordRequest.Email' Error:Field validation ...`-// message that diverges from the Python contract for this endpoint,-// which returns the friendlier `"email and passwords are required"`-// (api/apps/restful_apis/user_api.py:forget_reset_password). Letting-// the binding succeed with zero values means the existing service-// check below produces the matching message, and an entirely missing-// JSON body now gets exactly Python's response.-type ForgotResetPasswordRequest struct {- Email string `json:"email"`- NewPassword string `json:"new_password"`- ConfirmNewPassword string `json:"confirm_new_password"`-}--// ForgotResetPassword finalises the reset: only proceeds if the verified-// flag is set, validates the two ciphertexts match after RSA decryption,-// updates the password hash, and clears the verified flag. Returns the-// user so the handler can auto-login (matching Python's-// `construct_response(auth=user.get_id())`).-func (s *UserService) ForgotResetPassword(ctx context.Context, req *ForgotResetPasswordRequest) (*entity.User, common.ErrorCode, error) {- if req.Email == "" || req.NewPassword == "" || req.ConfirmNewPassword == "" {- return nil, common.CodeArgumentError, fmt.Errorf("email and passwords are required")- }-- rc := kvrocks.Get()- verifiedKey := utility.OTPVerifiedRedisKey(req.Email)- if v, _ := rc.Get(ctx, verifiedKey); v != "1" {- return nil, common.CodeAuthenticationError, fmt.Errorf("email not verified")- }-- plain, err := common.DecryptPassword(req.NewPassword)- if err != nil {- return nil, common.CodeServerError, fmt.Errorf("fail to decrypt password")- }- confirm, err := common.DecryptPassword(req.ConfirmNewPassword)- if err != nil {- return nil, common.CodeServerError, fmt.Errorf("fail to decrypt password")- }- if plain != confirm {- return nil, common.CodeArgumentError, fmt.Errorf("passwords do not match")- }-- user, err := s.userDAO.GetByEmail(ctx, dao.DB, req.Email)- if err != nil {- return nil, common.CodeDataError, fmt.Errorf("invalid email")- }-- hashed, err := common.GenerateWerkzeugPasswordHash(plain)- if err != nil {- return nil, common.CodeServerError, fmt.Errorf("failed to hash new password: %w", err)- }- user.Password = &hashed-- // Auto-login: rotate the access token like LoginByEmail does so the- // handler can immediately mint an Authorization header.- token := common.GenerateToken()- user.AccessToken = &token- now := time.Now().Truncate(time.Second)- user.LastLoginTime = &now-- if err = s.userDAO.Update(ctx, dao.DB, user); err != nil {- return nil, common.CodeServerError, fmt.Errorf("failed to reset password: %w", err)- }-- rc.Delete(ctx, verifiedKey)- return user, common.CodeSuccess, nil-}