feat(training): 企业培训迭代:推送深链接与签名下载、课程管理修复与收集模板调整
推送域(000155):
- 广播通知支持 link_url:demand_collect 场景自动签发教育训练需求表模板
匿名下载直链(HMAC token,20 天有效,免登录路由),企微消息尾部拼
「下载模板 + 查看更多详情」链接;站内文本保持纯文本不受影响
- 配置中心新增「推送链接基址」(notify_link_base_url,多入口/内外网部署固定
权威地址,留空跟随推送人访问地址)与「年培训计划收集推送文案」
(demand_collect_*,{year} 渲染收集年度);空标题/内容按模板回落渲染
- 迁移 000155:tr_training_notifications 增 link_url/link_token 列
课程管理:
- 修复点【发布/下线】报「不支持的操作」:动作取值兼容字面量路由(FullPath
末段)与 :action 通配两种注册形态
- 列表新增「创建人」列:creator_id 联查 users 带出姓名,导出 Excel 同步;
新增课程「所属部门」默认当前登录用户所在部门
- 导入加固:表头归一化 + 别名模糊匹配(兼容内置模板/旧口径/自定义模板变体);
导入与新增行支持按部门名兜底解析为 id(前端名册未命中时传 owner_dept_name)
年培训计划:
- 需求列表新增「创建人/创建时间」列(submitter_id 联查 users 带出姓名)
- 收集模板调整:下载时动态删除「培训费用(RMB)」列(旧版自定义模板同步归一,
右侧列/合并/下拉随删列平移);备注右侧追加「调研维度/培训策划要求」说明列
(合并至数据区末行,自动换行带边框,已带说明的自定义模板不覆盖)
开发工具:
- air 热重载适配 Windows:构建收敛到 scripts/air_build.sh(规避 air 经
PowerShell 执行不支持 bash 命令替换),产物改 .exe 后缀;CGO_CFLAGS 取
持久化值并集追加,不覆盖 go env -w 的头文件包含路径
This commit is contained in:
1 parent
242dd7e1a6
commit
4e25fce9aa
30 files changed
+1150
-143
No files matched your search
@@ -0,0 +1,119 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TrainingTemplateTokenValidity 签名下载链接有效期(2026-11 需求:默认 20 天,
|
||||
// 覆盖一次年度需求收集窗口;过期后链接失效,需重新推送)。
|
||||
const TrainingTemplateTokenValidity = 20 * 24 * time.Hour
|
||||
|
||||
// TrainingTemplateSignedFilePath 匿名签名下载路由路径;企微 footer 链接、
|
||||
// 路由注册与 noAuthAPI 白名单三方共用,防路径漂移。
|
||||
const TrainingTemplateSignedFilePath = "/api/v1/training/templates/signed-file"
|
||||
|
||||
// TrainingTemplateTokenPayload is the signed anonymous download token carried
|
||||
// in the WeCom markdown footer of a demand-collect broadcast. The signature
|
||||
// binds tenant + template key + expiry so a leaked URL can only fetch one
|
||||
// blank template of one tenant and dies with the collection window.
|
||||
type TrainingTemplateTokenPayload struct {
|
||||
TenantID uint64 `json:"tid"`
|
||||
TemplateKey string `json:"key"`
|
||||
ExpiresAt int64 `json:"exp"`
|
||||
}
|
||||
|
||||
var (
|
||||
templateTokenSecretOnce sync.Once
|
||||
templateTokenSecret string
|
||||
)
|
||||
|
||||
// trainingTemplateTokenSigningKey mirrors oidcStateSigningKey: dedicated env
|
||||
// first, JWT_SECRET fallback, per-process random as last resort (tokens then
|
||||
// die on restart — acceptable for dev; production sets JWT_SECRET).
|
||||
func trainingTemplateTokenSigningKey() string {
|
||||
templateTokenSecretOnce.Do(func() {
|
||||
if envSecret := strings.TrimSpace(os.Getenv("TRAINING_TEMPLATE_TOKEN_SECRET")); envSecret != "" {
|
||||
templateTokenSecret = envSecret
|
||||
return
|
||||
}
|
||||
if envSecret := strings.TrimSpace(os.Getenv("JWT_SECRET")); envSecret != "" {
|
||||
templateTokenSecret = envSecret
|
||||
return
|
||||
}
|
||||
randomBytes := make([]byte, 32)
|
||||
if _, err := rand.Read(randomBytes); err != nil {
|
||||
panic(fmt.Sprintf("failed to generate training template token signing key: %v", err))
|
||||
}
|
||||
templateTokenSecret = base64.StdEncoding.EncodeToString(randomBytes)
|
||||
})
|
||||
return templateTokenSecret
|
||||
}
|
||||
|
||||
// SignTrainingTemplateToken returns a tamper-evident download token:
|
||||
// base64url(payload).base64url(hmac). ExpiresAt zero = now + default validity.
|
||||
func SignTrainingTemplateToken(payload *TrainingTemplateTokenPayload) (string, error) {
|
||||
if payload == nil {
|
||||
return "", errors.New("training template token payload is required")
|
||||
}
|
||||
if payload.TenantID == 0 {
|
||||
return "", errors.New("training template token tenant is required")
|
||||
}
|
||||
if strings.TrimSpace(payload.TemplateKey) == "" {
|
||||
return "", errors.New("training template token key is required")
|
||||
}
|
||||
if payload.ExpiresAt == 0 {
|
||||
payload.ExpiresAt = time.Now().Add(TrainingTemplateTokenValidity).Unix()
|
||||
}
|
||||
raw, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("marshal training template token: %w", err)
|
||||
}
|
||||
mac := hmac.New(sha256.New, []byte(trainingTemplateTokenSigningKey()))
|
||||
mac.Write(raw)
|
||||
sig := mac.Sum(nil)
|
||||
return base64.RawURLEncoding.EncodeToString(raw) + "." + base64.RawURLEncoding.EncodeToString(sig), nil
|
||||
}
|
||||
|
||||
// VerifyTrainingTemplateToken validates the HMAC and expiry of a token and
|
||||
// returns its payload for the anonymous download endpoint.
|
||||
func VerifyTrainingTemplateToken(raw string) (*TrainingTemplateTokenPayload, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
parts := strings.Split(raw, ".")
|
||||
if len(parts) != 2 {
|
||||
return nil, errors.New("invalid training template token format")
|
||||
}
|
||||
payloadBytes, err := base64.RawURLEncoding.DecodeString(parts[0])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode training template token payload: %w", err)
|
||||
}
|
||||
sigBytes, err := base64.RawURLEncoding.DecodeString(parts[1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode training template token signature: %w", err)
|
||||
}
|
||||
mac := hmac.New(sha256.New, []byte(trainingTemplateTokenSigningKey()))
|
||||
mac.Write(payloadBytes)
|
||||
if !hmac.Equal(mac.Sum(nil), sigBytes) {
|
||||
return nil, errors.New("training template token signature mismatch")
|
||||
}
|
||||
var payload TrainingTemplateTokenPayload
|
||||
if err := json.Unmarshal(payloadBytes, &payload); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal training template token: %w", err)
|
||||
}
|
||||
if payload.TenantID == 0 || strings.TrimSpace(payload.TemplateKey) == "" {
|
||||
return nil, errors.New("training template token tenant/key is required")
|
||||
}
|
||||
if time.Now().Unix() > payload.ExpiresAt {
|
||||
return nil, errors.New("training template token expired")
|
||||
}
|
||||
return &payload, nil
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTrainingTemplateTokenRoundTrip(t *testing.T) {
|
||||
token, err := SignTrainingTemplateToken(&TrainingTemplateTokenPayload{
|
||||
TenantID: 42,
|
||||
TemplateKey: "demand_collection",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, token)
|
||||
|
||||
payload, err := VerifyTrainingTemplateToken(token)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, uint64(42), payload.TenantID)
|
||||
require.Equal(t, "demand_collection", payload.TemplateKey)
|
||||
|
||||
// 默认有效期 = 20 天(2026-11 需求)
|
||||
until := time.Unix(payload.ExpiresAt, 0)
|
||||
require.WithinDuration(t, time.Now().Add(TrainingTemplateTokenValidity), until, 2*time.Minute)
|
||||
}
|
||||
|
||||
func TestTrainingTemplateTokenExplicitExpiry(t *testing.T) {
|
||||
expiry := time.Now().Add(-time.Hour).Unix() // 已过期
|
||||
token, err := SignTrainingTemplateToken(&TrainingTemplateTokenPayload{
|
||||
TenantID: 42, TemplateKey: "demand_collection", ExpiresAt: expiry,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = VerifyTrainingTemplateToken(token)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "expired")
|
||||
}
|
||||
|
||||
func TestTrainingTemplateTokenTamperRejected(t *testing.T) {
|
||||
token, err := SignTrainingTemplateToken(&TrainingTemplateTokenPayload{
|
||||
TenantID: 42, TemplateKey: "demand_collection",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// 篡改签名段
|
||||
tampered := token[:len(token)-4] + "AAAA"
|
||||
_, err = VerifyTrainingTemplateToken(tampered)
|
||||
require.Error(t, err)
|
||||
|
||||
// 篡改载荷段(换租户)
|
||||
parts := strings.Split(token, ".")
|
||||
require.Len(t, parts, 2)
|
||||
_, err = VerifyTrainingTemplateToken(strings.ToUpper(parts[0]) + "." + parts[1])
|
||||
require.Error(t, err)
|
||||
|
||||
// 空载荷字段
|
||||
_, err = SignTrainingTemplateToken(&TrainingTemplateTokenPayload{TemplateKey: "demand_collection"})
|
||||
require.Error(t, err)
|
||||
_, err = SignTrainingTemplateToken(&TrainingTemplateTokenPayload{TenantID: 42})
|
||||
require.Error(t, err)
|
||||
_, err = SignTrainingTemplateToken(nil)
|
||||
require.Error(t, err)
|
||||
}
|
||||
Reference in new issue
Block a user