library-go/jwt/claims.go

69 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-08-27 05:35:08 +00:00
package jwt
type DataClaims map[string]string
const (
idCode = "id"
roleCode = "role"
2024-08-27 05:49:17 +00:00
deviceIDCode = "device_id"
2024-08-27 05:35:08 +00:00
scopeCode = "scope"
uidCode = "uid"
)
// ============ 使用具體的 setter ============
// Set 通用的 setter 方法
func (c DataClaims) Set(key, value string) {
c[key] = value
}
func (c DataClaims) SetID(id string) {
c.Set(idCode, id)
}
func (c DataClaims) SetRole(role string) {
c.Set(roleCode, role)
}
func (c DataClaims) SetDeviceID(deviceID string) {
2024-08-27 05:49:17 +00:00
c.Set(deviceIDCode, deviceID)
2024-08-27 05:35:08 +00:00
}
func (c DataClaims) SetScope(scope string) {
c.Set(scopeCode, scope)
}
func (c DataClaims) SetUID(uid string) {
c.Set(uidCode, uid)
}
// ============ 使用具體的 getter ============
func (c DataClaims) Get(key string) string {
if val, ok := c[key]; ok {
return val
}
2024-08-27 05:49:17 +00:00
2024-08-27 05:35:08 +00:00
return ""
}
func (c DataClaims) Scope() {
c.Get(scopeCode)
}
func (c DataClaims) Role() string {
return c.Get(roleCode)
}
func (c DataClaims) ID() string {
return c.Get(idCode)
}
func (c DataClaims) DeviceID() string {
2024-08-27 05:49:17 +00:00
return c.Get(deviceIDCode)
2024-08-27 05:35:08 +00:00
}
func (c DataClaims) UID() string {
return c.Get(uidCode)
}