37 lines
804 B
Go
37 lines
804 B
Go
package controllers
|
|
|
|
import (
|
|
//"fmt"
|
|
"github.com/juusechec/jwt-beego"
|
|
)
|
|
|
|
// BaseController operations for APIs
|
|
type BaseAPIController struct {
|
|
BaseController
|
|
}
|
|
|
|
func (this *BaseAPIController) Prepare() {
|
|
if this.Ctx.Input.Method() != "POST" {
|
|
this.ServeJsonError("Method not allowed")
|
|
}
|
|
|
|
//Lo que quieras hacer en todos los controladores
|
|
// O puede ser leído de una cabecera HEADER!!
|
|
tokenString := this.Ctx.Request.Header.Get("X-JWTtoken")
|
|
et := jwtbeego.EasyToken{}
|
|
valid, issuer, _ := et.ValidateToken(tokenString)
|
|
if !valid {
|
|
this.Ctx.Output.SetStatus(401)
|
|
this.ServeJsonError("Invalid Token")
|
|
}
|
|
|
|
userSession := this.GetSession("username")
|
|
|
|
if userSession == nil || userSession != issuer {
|
|
this.Ctx.Output.SetStatus(401)
|
|
this.ServeJsonError("Invalid Session")
|
|
}
|
|
|
|
return
|
|
}
|