Exemple #1
0
/**
POST: for authentication
request body: username and password(json)
result: token is set in session
success:
{
  "success": true
}
fail:
{
  "success": false
}
*/
func (this *AuthController) Post() {
	var auth types.AuthInfo
	json.Unmarshal(this.Ctx.Input.RequestBody, &auth)

	type Success struct {
		Succ bool `json:"success"`
	}
	if auth.Valid() && models.VerifyAdmin(auth.Username, auth.Password) {
		this.Data["json"] = &Success{true}
		this.SetSession("token", models.GetToken())
		beego.Info("Authenticate success, token: ", models.GetToken())
	} else {
		this.Data["json"] = &Success{false}
		beego.Info("Authenticate fail, username, password = ", auth.Username,
			auth.Password)
	}
	this.ServeJson()
}
Exemple #2
0
package routers

import (
	"encoding/base64"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"

	"controllers"
	"models"
)

func init() {
	//beego.InsertFilter("/*", beego.BeforeRouter, loginChecker)
	beego.SetStaticPath("/web", "web")
	beego.Router("/api/player", &controllers.PlayersController{})
	beego.Router("/api/auth", &controllers.AuthController{})
	beego.Router("/api/rawtrainrecord", &controllers.RawTrainRecordController{})
	beego.Router("/api/trainremark", &controllers.TrainRemarkController{})
	beego.Router("/api/trainhistory", &controllers.TrainHistoryController{})
}

var loginChecker = func(ctx *context.Context) {
	token := ctx.Input.Session("token")

	if ctx.Request.URL.Path != "/api/auth" && token != models.GetToken() {
		ctx.Redirect(302, "/api/auth?req="+
			base64.URLEncoding.EncodeToString([]byte(ctx.Request.URL.String())))
	}
}