Validate main PHP sessions against user IDs
Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"honoka-chan/internal/middleware"
|
"honoka-chan/internal/middleware"
|
||||||
|
loginmodel "honoka-chan/internal/model/login"
|
||||||
usermodel "honoka-chan/internal/model/user"
|
usermodel "honoka-chan/internal/model/user"
|
||||||
"honoka-chan/internal/router"
|
"honoka-chan/internal/router"
|
||||||
loginschema "honoka-chan/internal/schema/login"
|
loginschema "honoka-chan/internal/schema/login"
|
||||||
@@ -74,6 +75,15 @@ func login(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ss.SetAuthKey(&loginmodel.AuthKey{
|
||||||
|
AuthorizeToken: authorizeToken,
|
||||||
|
UserID: userID,
|
||||||
|
InsertDate: time.Now().Format("2006-01-02 15:04:05"),
|
||||||
|
})
|
||||||
|
if ss.Done() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ss.Respond(loginschema.LoginResp{
|
ss.Respond(loginschema.LoginResp{
|
||||||
ResponseData: loginschema.LoginData{
|
ResponseData: loginschema.LoginData{
|
||||||
AuthorizeToken: authorizeToken,
|
AuthorizeToken: authorizeToken,
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ func Common(ctx *gin.Context) {
|
|||||||
ctx.Set("request_data", reqData)
|
ctx.Set("request_data", reqData)
|
||||||
|
|
||||||
uid := ctx.GetHeader("User-ID")
|
uid := ctx.GetHeader("User-ID")
|
||||||
ctx.Set("userid", uid)
|
|
||||||
|
|
||||||
ss := session.Attach(ctx)
|
ss := session.Attach(ctx)
|
||||||
ctx.Set("session", ss)
|
ctx.Set("session", ss)
|
||||||
@@ -35,13 +34,6 @@ func Common(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !honokautils.IsMainLoginEndpoint(ctx.Request.URL.Path) {
|
|
||||||
if ss.UserPref.ForceRelogin {
|
|
||||||
ss.AbortWithStatus(http.StatusNotFound, honokautils.NewNotFoundContent(ctx.Request.URL.Path))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
authorize := ctx.GetHeader("Authorize")
|
authorize := ctx.GetHeader("Authorize")
|
||||||
params, err := url.ParseQuery(authorize)
|
params, err := url.ParseQuery(authorize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -56,6 +48,34 @@ func Common(ctx *gin.Context) {
|
|||||||
token := params.Get("token")
|
token := params.Get("token")
|
||||||
ctx.Set("token", token)
|
ctx.Set("token", token)
|
||||||
|
|
||||||
|
if !honokautils.IsMainLoginEndpoint(ctx.Request.URL.Path) {
|
||||||
|
userID, err := strconv.Atoi(uid)
|
||||||
|
if err != nil || userID <= 0 {
|
||||||
|
ss.AbortWithStatus(http.StatusNotFound, honokautils.NewNotFoundContent(ctx.Request.URL.Path))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
valid, err := ss.IsAuthorizeTokenForUser(token, userID)
|
||||||
|
if ss.CheckErr(err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !valid {
|
||||||
|
ss.AbortWithStatus(http.StatusNotFound, honokautils.NewNotFoundContent(ctx.Request.URL.Path))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.Set("userid", uid)
|
||||||
|
ss.LoadUser(uid)
|
||||||
|
if ss.Done() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if ss.UserPref.ForceRelogin {
|
||||||
|
ss.AbortWithStatus(http.StatusNotFound, honokautils.NewNotFoundContent(ctx.Request.URL.Path))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ctx.Header("user_id", uid)
|
ctx.Header("user_id", uid)
|
||||||
ctx.Header("authorize", fmt.Sprintf("consumerKey=lovelive_test&timeStamp=%d&version=1.1&token=%s&nonce=%d&user_id=%s&requestTimeStamp=%d", time.Now().Unix(), token, nonce, uid, time.Now().Unix()))
|
ctx.Header("authorize", fmt.Sprintf("consumerKey=lovelive_test&timeStamp=%d&version=1.1&token=%s&nonce=%d&user_id=%s&requestTimeStamp=%d", time.Now().Unix(), token, nonce, uid, time.Now().Unix()))
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package loginmodel
|
|||||||
type AuthKey struct {
|
type AuthKey struct {
|
||||||
ID int `xorm:"id pk autoincr"`
|
ID int `xorm:"id pk autoincr"`
|
||||||
AuthorizeToken string `xorm:"authorize_token"`
|
AuthorizeToken string `xorm:"authorize_token"`
|
||||||
|
UserID int `xorm:"user_id index"`
|
||||||
ClientToken string `xorm:"client_token"`
|
ClientToken string `xorm:"client_token"`
|
||||||
ServerToken string `xorm:"server_token"`
|
ServerToken string `xorm:"server_token"`
|
||||||
InsertDate string `xorm:"insert_date"`
|
InsertDate string `xorm:"insert_date"`
|
||||||
|
|||||||
@@ -19,3 +19,13 @@ func (ss *Session) SetAuthKey(authKey *loginmodel.AuthKey) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ss *Session) IsAuthorizeTokenForUser(token string, userID int) (bool, error) {
|
||||||
|
if token == "" || userID <= 0 {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return ss.UserEng.Table(new(loginmodel.AuthKey)).
|
||||||
|
Where("authorize_token = ? AND user_id = ?", token, userID).
|
||||||
|
Exist()
|
||||||
|
}
|
||||||
|
|||||||
@@ -53,15 +53,18 @@ func New(ctx *gin.Context) *Session {
|
|||||||
ss.UserEng = db.UserEng.NewSession()
|
ss.UserEng = db.UserEng.NewSession()
|
||||||
ss.UserEng.Begin()
|
ss.UserEng.Begin()
|
||||||
|
|
||||||
userID := ctx.GetString("userid")
|
|
||||||
if userID != "" {
|
|
||||||
ss.UserPref = ss.GetUserPref(userID)
|
|
||||||
ss.UserID = ss.UserPref.UserID
|
|
||||||
}
|
|
||||||
|
|
||||||
return ss
|
return ss
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ss *Session) LoadUser(userID string) {
|
||||||
|
ss.UserPref = ss.GetUserPref(userID)
|
||||||
|
ss.UserID = ss.UserPref.UserID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss *Session) Done() bool {
|
||||||
|
return ss.done
|
||||||
|
}
|
||||||
|
|
||||||
func Get(ctx *gin.Context) *Session {
|
func Get(ctx *gin.Context) *Session {
|
||||||
return ctx.MustGet("session").(*Session)
|
return ctx.MustGet("session").(*Session)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user