mirror of
https://github.com/abhinavxd/libredesk.git
synced 2026-09-11 13:28:57 +00:00
f460f6b163
Copilot and Generate Reply get four read-only, access-filtered tools to look up a customer's history: list the contact's other conversations, search conversations by exact email, fetch one conversation by reference number, and search contacts. Tool output is marked untrusted and capped so a big response can't blow the context window. Copilot panel changes: - per-agent persona picker that borrows an enabled assistant's voice, language and instructions without changing the tool set (stored in localStorage) - answers render as HTML; copy, insert-into-reply, and add-as-private-note actions per answer - chat history moves to its own copilot store; server history is persisted only after a successful reply and read back with a limit Adds AI tag suggestions: a new endpoint suggests up to 3 existing tags for a conversation, applied from the sidebar, never auto-applied. Hardening and fixes: - OIDC callback rejects non-agent users - custom tool URLs and params schema validated on save; tool HTTP client no longer follows redirects; query keys pinned in the tool URL win - GetAllConversationMessages takes an explicit limit (cap 1000) - FAQ mining skips a candidate already pending review - ai agent handoff records its event only after the move actually lands - share chat message role constants; rename v2.7.0 migration to v2.6.0 and add the fix_grammar_spelling prompt
128 lines
4.7 KiB
Go
128 lines
4.7 KiB
Go
package main
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
amodels "github.com/abhinavxd/libredesk/internal/auth/models"
|
|
"github.com/abhinavxd/libredesk/internal/envelope"
|
|
"github.com/abhinavxd/libredesk/internal/stringutil"
|
|
"github.com/abhinavxd/libredesk/internal/user/models"
|
|
realip "github.com/ferluci/fast-realip"
|
|
"github.com/valyala/fasthttp"
|
|
"github.com/zerodha/fastglue"
|
|
)
|
|
|
|
var (
|
|
oidcStateSessKey = "oidc_state"
|
|
oidcNextSessKey = "oidc_next"
|
|
)
|
|
|
|
// handleOIDCLogin redirects to the OIDC provider for login.
|
|
func handleOIDCLogin(r *fastglue.Request) error {
|
|
var (
|
|
app = r.Context.(*App)
|
|
providerID, err = strconv.Atoi(r.RequestCtx.UserValue("id").(string))
|
|
)
|
|
if err != nil {
|
|
app.lo.Error("error parsing provider id", "error", err)
|
|
return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.T("globals.messages.somethingWentWrong"), nil, envelope.GeneralError)
|
|
}
|
|
|
|
// Set a state and save it in the session, to prevent CSRF attacks.
|
|
state, err := stringutil.RandomAlphanumeric(32)
|
|
if err != nil {
|
|
app.lo.Error("error generating state", "error", err)
|
|
return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.T("globals.messages.somethingWentWrong"), nil, envelope.GeneralError)
|
|
}
|
|
|
|
sessionValues := map[string]any{
|
|
oidcStateSessKey: state,
|
|
// For redirecting after login
|
|
oidcNextSessKey: string(r.RequestCtx.QueryArgs().Peek("next")),
|
|
}
|
|
|
|
if err = app.auth.SetSessionValues(r, sessionValues); err != nil {
|
|
app.lo.Error("error saving state in session", "error", err)
|
|
return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.T("globals.messages.somethingWentWrong"), nil, envelope.GeneralError)
|
|
}
|
|
|
|
authURL, err := app.auth.LoginURL(providerID, state)
|
|
if err != nil {
|
|
return sendErrorEnvelope(r, err)
|
|
}
|
|
return r.Redirect(authURL, fasthttp.StatusFound, nil, "")
|
|
}
|
|
|
|
// handleOIDCCallback receives the redirect callback from the OIDC provider and completes the handshake.
|
|
func handleOIDCCallback(r *fastglue.Request) error {
|
|
var (
|
|
app = r.Context.(*App)
|
|
code = string(r.RequestCtx.QueryArgs().Peek("code"))
|
|
state = string(r.RequestCtx.QueryArgs().Peek("state"))
|
|
providerID, err = strconv.Atoi(string(r.RequestCtx.UserValue("id").(string)))
|
|
ip = realip.FromRequest(r.RequestCtx)
|
|
)
|
|
if err != nil {
|
|
app.lo.Error("error parsing provider id", "error", err)
|
|
return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.T("globals.messages.somethingWentWrong"), nil, envelope.GeneralError)
|
|
}
|
|
|
|
// Compare the state from the session with the state from the query.
|
|
sessionState, err := app.auth.GetSessionValue(r, oidcStateSessKey)
|
|
if err != nil {
|
|
app.lo.Error("error getting state from session", "error", err)
|
|
return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.T("globals.messages.somethingWentWrong"), nil, envelope.GeneralError)
|
|
}
|
|
if state != sessionState {
|
|
return r.SendErrorEnvelope(fasthttp.StatusForbidden, app.i18n.T("globals.messages.somethingWentWrong"), nil, envelope.GeneralError)
|
|
}
|
|
|
|
_, claims, err := app.auth.ExchangeOIDCToken(r.RequestCtx, providerID, code)
|
|
if err != nil {
|
|
app.lo.Error("error exchanging oidc token", "error", err)
|
|
return r.SendErrorEnvelope(fasthttp.StatusInternalServerError,
|
|
app.i18n.T("globals.messages.somethingWentWrong"), nil, envelope.GeneralError)
|
|
}
|
|
|
|
// Lookup the user by email and set the session.
|
|
user, err := app.user.GetAgent(0, claims.Email)
|
|
if err != nil {
|
|
return sendErrorEnvelope(r, err)
|
|
}
|
|
// Only agents can log in; GetAgent also resolves ai_assistant identity users.
|
|
if user.Type != models.UserTypeAgent {
|
|
return r.SendErrorEnvelope(fasthttp.StatusForbidden, app.i18n.T("auth.invalidOrExpiredSession"), nil, envelope.PermissionError)
|
|
}
|
|
|
|
if err := app.auth.SaveSession(amodels.User{
|
|
ID: user.ID,
|
|
Email: user.Email.String,
|
|
FirstName: user.FirstName,
|
|
LastName: user.LastName,
|
|
}, r); err != nil {
|
|
return r.SendErrorEnvelope(fasthttp.StatusInternalServerError,
|
|
app.i18n.T("globals.messages.somethingWentWrong"), nil, envelope.GeneralError)
|
|
}
|
|
|
|
// Update last login time.
|
|
if err := app.user.UpdateLastLoginAt(user.ID); err != nil {
|
|
return sendErrorEnvelope(r, err)
|
|
}
|
|
|
|
app.user.InvalidateAgentCache(user.ID)
|
|
|
|
// Insert activity log.
|
|
if err := app.activityLog.Login(user.ID, user.Email.String, ip); err != nil {
|
|
app.lo.Error("error creating login activity log", "error", err)
|
|
}
|
|
|
|
// Read the 'next' parameter from session to redirect after login.
|
|
nextParam, _ := app.auth.GetSessionValue(r, oidcNextSessKey)
|
|
redirectURL := "/"
|
|
if nextStr, ok := nextParam.(string); ok && nextStr != "" {
|
|
redirectURL = nextStr
|
|
}
|
|
|
|
return r.RedirectURI(redirectURL, fasthttp.StatusFound, nil, "")
|
|
}
|