package permlib import ( "errors" "net/http" "time" ) type FieldWriteMode int const ( FieldWriteSilent FieldWriteMode = iota FieldWriteReject ) // AuthCallbacks 鉴权回调,调用方必须提供,permlib 不内置任何响应格式 type AuthCallbacks struct { // OnError 鉴权失败时调用 // httpStatus: 建议的 HTTP 状态码(401/403/500) // code: 业务错误码 // msg: 错误信息 OnError func(w http.ResponseWriter, r *http.Request, httpStatus int, code int, msg string) // OnSuccess 鉴权成功时调用 // user: 当前用户信息(含 Perms) // hasDataPerm: 当前请求是否有对应的 data:* 数据权限 // next: 业务 handler,hasDataPerm 为 true 时调用,否则返回空数据 OnSuccess func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc, user *UserInfo, hasDataPerm bool) } type Config struct { ProductCode string AppKey string AppSecret string PermServerAddr string FieldWriteMode FieldWriteMode CacheTTL time.Duration Callbacks AuthCallbacks } func (c *Config) validate() error { if c.Callbacks.OnError == nil { return errors.New("Config.Callbacks.OnError is required") } if c.Callbacks.OnSuccess == nil { return errors.New("Config.Callbacks.OnSuccess is required") } return nil } func (c *Config) defaults() { if c.CacheTTL == 0 { c.CacheTTL = 5 * time.Minute } }