mirror of https://github.com/veypi/OneAuth.git
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.
47 lines
1002 B
Go
47 lines
1002 B
Go
package app
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/veypi/OneAuth/cfg"
|
|
"github.com/veypi/OneAuth/models"
|
|
"github.com/veypi/OneAuth/oalib"
|
|
"github.com/veypi/OneBD"
|
|
"github.com/veypi/OneBD/rfc"
|
|
"github.com/veypi/utils/jwt"
|
|
)
|
|
|
|
func Router(r OneBD.Router) {
|
|
r.Set("/", appHandlerP, rfc.MethodPost, rfc.MethodGet)
|
|
r.Set("/:uuid", appHandlerP, rfc.MethodGet, rfc.MethodPatch)
|
|
r.Set("/:uuid/user/:id", auHandlerP, rfc.MethodAll)
|
|
r.Set("/:uuid/ping", ping, rfc.MethodGet)
|
|
}
|
|
|
|
func ping(m OneBD.Meta) {
|
|
var err error
|
|
defer func() {
|
|
if err != nil {
|
|
m.WriteHeader(rfc.StatusBadRequest)
|
|
m.Write([]byte(err.Error()))
|
|
} else {
|
|
m.WriteHeader(rfc.StatusOK)
|
|
m.Write([]byte("ok"))
|
|
}
|
|
}()
|
|
t := m.GetHeader("auth_token")
|
|
uuid := m.Params("uuid")
|
|
a := &models.App{}
|
|
err = cfg.DB().Where("UUID = ?", uuid).First(a).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
p := &oalib.PayLoad{}
|
|
ok, err := jwt.ParseToken(t, p, []byte(a.Key))
|
|
if err != nil {
|
|
return
|
|
}
|
|
if !ok {
|
|
err = errors.New("invalid key")
|
|
}
|
|
}
|