mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-25 07:00:48 +09:00
ea719f7ca7
* refactor: memory provider * fix: migrations * feat: divide collection from different built-in memory * feat: add `MEMORY.md` and `PROFILES.md` * use .env for docker compose. fix #142 (#143) * feat(web): add brand icons for search providers (#144) Add custom FontAwesome icon definitions for all 9 search providers: - Yandex: uses existing faYandex from FA free brands - Tavily, Jina, Exa, Bocha, Serper: custom icons from brand SVGs - DuckDuckGo, SearXNG, Sogou: custom icons from Simple Icons Icons are registered with a custom 'fac' prefix and rendered as monochrome (currentColor) via FontAwesome's standard rendering. * fix: resolve multiple UI bugs (#147) * feat: add email service with multi-adapter support (#146) * feat: add email service with multi-adapter support Implement a full-stack email service with global provider management, per-bot bindings with granular read/write permissions, outbox audit storage, and MCP tool integration for direct mailbox access. Backend: - Email providers: CRUD with dynamic config schema (generic SMTP/IMAP, Mailgun) - Generic adapter: go-mail (SMTP) + go-imap/v2 (IMAP IDLE real-time push via UnilateralDataHandler + UID-based tracking + periodic check fallback) - Mailgun adapter: mailgun-go/v5 with dual inbound mode (webhook + poll) - Bot email bindings: per-bot provider binding with independent r/w permissions - Outbox: outbound email audit log with status tracking - Trigger: inbound emails push notification to bot_inbox (from/subject only, LLM reads full content on demand via MCP tools) - MailboxReader interface: on-demand IMAP queries for listing/reading emails - MCP tools: email_accounts, email_send, email_list (paginated mailbox), email_read (by UID) — all with multi-binding and provider_id selection - Webhook: /email/mailgun/webhook/:config_id (JWT-skipped, signature-verified) - DB migration: 0019_add_email (email_providers, bot_email_bindings, email_outbox) Frontend: - Email Providers page: /email-providers with MasterDetailSidebarLayout - Dynamic config form rendered from ordered provider meta schema with i18n keys - Bot detail: Email tab with bindings management + outbox audit table - Sidebar navigation entry - Full i18n support (en + zh) - Auto-generated SDK from Swagger Closes #17 * feat(email): trigger bot conversation immediately on inbound email Instead of only storing an inbox item and waiting for the next chat, the email trigger now proactively invokes the conversation resolver so the bot processes new emails right away — aligned with the schedule/heartbeat trigger pattern. * fix: lint --------- Co-authored-by: Acbox <acbox0328@gmail.com> * chore: update AGENTS.md * feat: files preview * feat(web): improve MCP details page * refactor(skills): import skill with pure markdown string * merge main into refactor/memory * fix: migration * refactor: temp delete qdrant and bm25 index * fix: clean merge code * fix: update memory handler --------- Co-authored-by: Leohearts <leohearts@leohearts.com> Co-authored-by: Menci <mencici@msn.com> Co-authored-by: Quincy <69751197+dqygit@users.noreply.github.com> Co-authored-by: BBQ <35603386+HoneyBBQ@users.noreply.github.com> Co-authored-by: Ran <16112591+chen-ran@users.noreply.github.com>
97 lines
2.8 KiB
Go
97 lines
2.8 KiB
Go
package auth
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestRefreshTokenFromContext(t *testing.T) {
|
|
e := echo.New()
|
|
req := httptest.NewRequest(http.MethodPost, "/", nil)
|
|
rec := httptest.NewRecorder()
|
|
c := e.NewContext(req, rec)
|
|
|
|
secret := "test-secret"
|
|
userID := "user-123"
|
|
|
|
// Create an initial token with a 5-minute lifespan
|
|
initialDuration := 5 * time.Minute
|
|
initialTokenStr, _, err := GenerateToken(userID, secret, initialDuration)
|
|
assert.NoError(t, err)
|
|
|
|
// Parse the token to place it into the echo context
|
|
token, err := jwt.Parse(initialTokenStr, func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(secret), nil
|
|
})
|
|
assert.NoError(t, err)
|
|
c.Set("user", token)
|
|
|
|
// Simulate some time passing to ensure the new token has a different 'iat' and 'exp'
|
|
time.Sleep(1 * time.Second)
|
|
|
|
// Run the refresh function
|
|
defaultDuration := 1 * time.Hour
|
|
newTokenStr, newExpiresAt, err := RefreshTokenFromContext(c, secret, defaultDuration)
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, newTokenStr)
|
|
|
|
// Parse the original token claims for comparison
|
|
originalClaims, ok := token.Claims.(jwt.MapClaims)
|
|
assert.True(t, ok)
|
|
origIat := int64(originalClaims["iat"].(float64))
|
|
|
|
// Parse the new token
|
|
newToken, err := jwt.Parse(newTokenStr, func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(secret), nil
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.True(t, newToken.Valid)
|
|
|
|
newClaims, ok := newToken.Claims.(jwt.MapClaims)
|
|
assert.True(t, ok)
|
|
|
|
// Ensure standard payload claims are retained
|
|
assert.Equal(t, userID, newClaims[claimSubject])
|
|
assert.Equal(t, userID, newClaims[claimUserID])
|
|
|
|
// Validate the new time bounds
|
|
newIat := int64(newClaims["iat"].(float64))
|
|
newExp := int64(newClaims["exp"].(float64))
|
|
|
|
// 1. Ensure time has advanced
|
|
assert.Greater(t, newIat, origIat)
|
|
|
|
// 2. Ensure the refreshed token has a positive lifetime and does not exceed the configured default duration
|
|
lifetimeSeconds := newExp - newIat
|
|
assert.Greater(t, lifetimeSeconds, int64(0))
|
|
assert.LessOrEqual(t, lifetimeSeconds, int64(defaultDuration.Seconds()))
|
|
|
|
// 3. Ensure the return value matches the claim
|
|
assert.Equal(t, newExpiresAt.Unix(), newExp)
|
|
}
|
|
|
|
func TestRefreshTokenFromContext_MissingUser(t *testing.T) {
|
|
e := echo.New()
|
|
req := httptest.NewRequest(http.MethodPost, "/", nil)
|
|
rec := httptest.NewRecorder()
|
|
c := e.NewContext(req, rec)
|
|
|
|
secret := "test-secret"
|
|
defaultDuration := 1 * time.Hour
|
|
|
|
// Context without the "user" key
|
|
_, _, err := RefreshTokenFromContext(c, secret, defaultDuration)
|
|
assert.Error(t, err)
|
|
|
|
httpErr, ok := err.(*echo.HTTPError)
|
|
assert.True(t, ok)
|
|
assert.Equal(t, http.StatusUnauthorized, httpErr.Code)
|
|
assert.Equal(t, "invalid token", httpErr.Message)
|
|
}
|