You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/libs/base/user_handler.go

48 lines
1015 B
Go

package base
3 years ago
import (
"OneAuth/libs/oerr"
"OneAuth/libs/token"
3 years ago
"OneAuth/models"
"github.com/veypi/OneBD"
"github.com/veypi/OneBD/rfc"
)
type UserHandler struct {
Payload *token.PayLoad
3 years ago
ignoreMethod map[rfc.Method]bool
}
func (a *UserHandler) Init(m OneBD.Meta) error {
3 years ago
if a.ignoreMethod != nil && a.ignoreMethod[m.Method()] {
return nil
}
return a.ParsePayload(m)
}
func (a *UserHandler) ParsePayload(m OneBD.Meta) error {
a.Payload = new(token.PayLoad)
tokenStr := m.GetHeader("auth_token")
if tokenStr == "" {
3 years ago
return oerr.NotLogin
}
ok, err := token.ParseToken(tokenStr, a.Payload)
3 years ago
if ok {
return nil
}
return oerr.NotLogin.Attach(err)
}
func (a *UserHandler) Ignore(methods ...rfc.Method) {
3 years ago
if a.ignoreMethod == nil {
a.ignoreMethod = make(map[rfc.Method]bool)
}
for _, m := range methods {
a.ignoreMethod[m] = true
}
}
func (a *UserHandler) GetAuth(ResourceID string, ResourceUUID ...string) models.AuthLevel {
return a.Payload.GetAuth(ResourceID, ResourceUUID...)
3 years ago
}