Compare commits

...

5 Commits

5 changed files with 66 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import (
"git.lumeweb.com/LumeWeb/portal/logger" "git.lumeweb.com/LumeWeb/portal/logger"
"github.com/kataras/iris/v12" "github.com/kataras/iris/v12"
"go.uber.org/zap" "go.uber.org/zap"
"strconv"
) )
func tryParseRequest(r interface{}, ctx iris.Context) (interface{}, bool) { func tryParseRequest(r interface{}, ctx iris.Context) (interface{}, bool) {
@ -70,3 +71,16 @@ func (c Controller) respondJSON(data interface{}) {
logger.Get().Error("failed to generate response", zap.Error(err)) 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,6 +112,25 @@ func (f *FilesController) GetStatusBy(cidString string) {
f.respondJSON(&response.StatusResponse{Status: statusCode}) 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) { func validateCid(cidString string, validateStatus bool, ctx iris.Context) (string, bool) {
_, err := cid.Valid(cidString) _, err := cid.Valid(cidString)
if sendError(ctx, err, iris.StatusBadRequest) { if sendError(ctx, err, iris.StatusBadRequest) {

View File

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

18
service/account/user.go Normal file
View File

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