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

62 lines
1.3 KiB
Go

package base
import (
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/libs/oerr"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneAuth/oalib"
"github.com/veypi/OneBD"
"github.com/veypi/OneBD/rfc"
)
type UserHandler struct {
Payload *oalib.PayLoad
ignoreMethod map[rfc.Method]bool
}
func (h *UserHandler) Init(m OneBD.Meta) error {
if h.ignoreMethod != nil && h.ignoreMethod[m.Method()] {
return nil
}
return h.ParsePayload(m)
}
func (h *UserHandler) ParsePayload(m OneBD.Meta) error {
h.Payload = new(oalib.PayLoad)
tokenStr := m.GetHeader("auth_token")
if tokenStr == "" {
return oerr.NotLogin
}
uuid := m.GetHeader("uuid")
var ok bool
var err error
if uuid != "" {
a := &models.App{}
err = cfg.DB().Where("UUID = ?", uuid).First(a).Error
if err != nil {
return err
}
ok, err = h.Payload.ParseToken(tokenStr, []byte(a.Key))
h.Payload.Auth = nil
} else {
ok, err = h.Payload.ParseToken(tokenStr, cfg.CFG.APPKey)
}
if ok {
return nil
}
return oerr.NotLogin.Attach(err)
}
func (h *UserHandler) Ignore(methods ...rfc.Method) {
if h.ignoreMethod == nil {
h.ignoreMethod = make(map[rfc.Method]bool)
}
for _, m := range methods {
h.ignoreMethod[m] = true
}
}
func (h *UserHandler) GetAuth(ResourceID string, ResourceUUID ...string) oalib.AuthLevel {
return h.Payload.GetAuth(ResourceID, ResourceUUID...)
}