feat(profile): expose hmac identifier

This commit is contained in:
2026-06-06 11:36:00 +08:00
parent 9c2a396d69
commit fbc0bb833d
4 changed files with 75 additions and 14 deletions
@@ -2,6 +2,9 @@ package v1
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"mime/multipart"
@@ -58,6 +61,7 @@ func (s *ProfileHandlerTestSuite) SetupSuite() {
&model.AccountUser{},
&model.CustomRole{},
&model.AccessToken{},
&model.InstallationConfig{},
))
s.db = db
@@ -94,7 +98,8 @@ func (s *ProfileHandlerTestSuite) SetupSuite() {
userRepo := repository.NewUserRepo(db)
accountUserRepo := repository.NewAccountUserRepo(db)
accessTokenRepo := repository.NewAccessTokenRepo(db)
profileSvc := service.NewProfileService(userRepo, accountUserRepo, accessTokenRepo)
installationConfigRepo := repository.NewInstallationConfigRepo(db)
profileSvc := service.NewProfileService(userRepo, accountUserRepo, accessTokenRepo, installationConfigRepo)
s.handler = NewProfileHandler(profileSvc)
// Build router with profile routes and auth middleware
@@ -148,6 +153,7 @@ func (s *ProfileHandlerTestSuite) SetupTest() {
})
s.db.Unscoped().Where("account_id = ?", s.accountID).Delete(&model.CustomRole{})
s.db.Unscoped().Where("owner_type = ? AND owner_id = ?", model.AccessTokenOwnerTypeUser, s.userID).Delete(&model.AccessToken{})
s.db.Unscoped().Where("name = ?", "CHATWOOT_INBOX_HMAC_KEY").Delete(&model.InstallationConfig{})
s.Require().NoError(s.db.Create(&model.AccessToken{OwnerType: model.AccessTokenOwnerTypeUser, OwnerID: s.userID, Token: "profile-token-1", TokenPrefix: "profile-", Name: "Personal Access Token"}).Error)
}
@@ -182,6 +188,7 @@ func (s *ProfileHandlerTestSuite) TestGet_Success() {
assert.Equal(s.T(), "Profile Display", dataMap["available_name"])
assert.Equal(s.T(), "Regards", dataMap["message_signature"])
assert.Equal(s.T(), "pubsub-profile-user", dataMap["pubsub_token"])
assert.NotContains(s.T(), dataMap, "hmac_identifier")
assert.Equal(s.T(), "administrator", dataMap["role"])
accounts, ok := dataMap["accounts"].([]interface{})
assert.True(s.T(), ok)
@@ -221,6 +228,22 @@ func (s *ProfileHandlerTestSuite) TestGet_CustomRolePermissions() {
assert.Equal(s.T(), []interface{}{"conversation_manage", "contact_manage"}, customRole["permissions"])
}
func (s *ProfileHandlerTestSuite) TestGet_HMACIdentifierWhenConfigured() {
secret := "random_secret_key"
s.Require().NoError(s.db.Create(&model.InstallationConfig{Name: "CHATWOOT_INBOX_HMAC_KEY", Value: secret}).Error)
req, _ := http.NewRequest("GET", "/api/v1/profile", nil)
w := httptest.NewRecorder()
s.router.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusOK, w.Code)
payload := s.decodeProfileBody(w)
mac := hmac.New(sha256.New, []byte(secret))
_, _ = mac.Write([]byte("profile@example.com"))
expected := hex.EncodeToString(mac.Sum(nil))
assert.Equal(s.T(), expected, payload["hmac_identifier"])
}
func (s *ProfileHandlerTestSuite) TestGet_Unauthorized() {
// Create router without auth middleware — user_id will be 0
r := gin.New()