Compare commits

..

No commits in common. "be03a6c6867f305529af90e6206a0597bb84f015" and "892f093d93348459d113041104d773fdd5124a8d" have entirely different histories.

5 changed files with 7 additions and 66 deletions

View File

@ -5,7 +5,6 @@ import (
"git.lumeweb.com/LumeWeb/portal/logger"
"github.com/kataras/iris/v12"
"go.uber.org/zap"
"strconv"
)
func tryParseRequest(r interface{}, ctx iris.Context) (interface{}, bool) {
@ -71,16 +70,3 @@ func (c Controller) respondJSON(data interface{}) {
logger.Get().Error("failed to generate response", zap.Error(err))
}
}
func getCurrentUserId(ctx iris.Context) uint {
usr := ctx.User()
if usr == nil {
return 0
}
sid, _ := usr.GetID()
userID, _ := strconv.Atoi(sid)
return uint(userID)
}

View File

@ -112,25 +112,6 @@ func (f *FilesController) GetStatusBy(cidString string) {
f.respondJSON(&response.StatusResponse{Status: statusCode})
}
func (f *FilesController) PostPinBy(cidString string) {
ctx := f.Ctx
hashHex, valid := validateCid(cidString, true, ctx)
if !valid {
return
}
err := files.Pin(hashHex, getCurrentUserId(ctx))
if internalError(ctx, err) {
logger.Get().Error(err.Error())
return
}
f.Ctx.StatusCode(iris.StatusCreated)
}
func validateCid(cidString string, validateStatus bool, ctx iris.Context) (string, bool) {
_, err := cid.Valid(cidString)
if sendError(ctx, err, iris.StatusBadRequest) {

View File

@ -1,7 +1,6 @@
package middleware
import (
"git.lumeweb.com/LumeWeb/portal/service/account"
"git.lumeweb.com/LumeWeb/portal/service/auth"
"github.com/kataras/iris/v12"
)
@ -14,15 +13,8 @@ func VerifyJwt(ctx iris.Context) {
return
}
acct, err := auth.VerifyLoginToken(token)
if err != nil {
if err := auth.VerifyLoginToken(token); err != nil {
ctx.StopWithError(iris.StatusUnauthorized, auth.ErrInvalidToken)
return
}
err = ctx.SetUser(account.NewUser(acct))
if err != nil {
ctx.StopWithError(iris.StatusInternalServerError, err)
}
}

View File

@ -1,18 +0,0 @@
package account
import (
"git.lumeweb.com/LumeWeb/portal/model"
"strconv"
)
type User struct {
account *model.Account
}
func (u User) GetID() (string, error) {
return strconv.Itoa(int(u.account.ID)), nil
}
func NewUser(account *model.Account) *User {
return &User{account: account}
}

View File

@ -167,30 +167,30 @@ func Logout(token string) error {
return nil
}
func VerifyLoginToken(token string) (*model.Account, error) {
func VerifyLoginToken(token string) error {
uvt, err := jwt.Decode([]byte(token))
if err != nil {
return nil, ErrInvalidToken
return ErrInvalidToken
}
var claim jwt.Claims
err = uvt.Claims(&claim)
if err != nil {
return nil, ErrInvalidToken
return ErrInvalidToken
}
session := model.LoginSession{}
if err := db.Get().Model(session).Where("token = ?", token).First(&session).Error; err != nil {
logger.Get().Debug(ErrInvalidToken.Error(), zap.Error(err), zap.String("token", token))
return nil, ErrInvalidToken
return ErrInvalidToken
}
_, err = jwt.Verify(jwt.HS256, sharedKey, []byte(token), blocklist)
if err != nil {
db.Get().Delete(&session)
return nil, err
return err
}
return &session.Account, nil
return nil
}