Solved some todo(s)

This commit is contained in:
Marco Realacci 2022-11-22 22:47:17 +01:00
parent 05acfb045f
commit a3cf4f17f8
5 changed files with 27 additions and 30 deletions

View file

@ -14,7 +14,7 @@ func BuildAuth(header string) (reqcontext.Authorization, error) {
auth, err := BuildBearer(header) auth, err := BuildBearer(header)
if err != nil { if err != nil {
if err.Error() == "invalid authorization header" { if err.Error() == "invalid authorization header" {
return nil, errors.New("method not supported") // todo: better error description return nil, errors.New("authentication method not supported")
} }
return nil, err return nil, err
} }

View file

@ -0,0 +1,19 @@
package reqcontext
import "github.com/notherealmarco/WASAPhoto/service/database"
type AuthStatus int
const (
AUTHORIZED = 0
UNAUTHORIZED = 1
FORBIDDEN = 2
USER_NOT_FOUND = 3
)
type Authorization interface {
GetType() string
GetUserID() string
Authorized(db database.AppDatabase) (AuthStatus, error)
UserAuthorized(db database.AppDatabase, uid string) (AuthStatus, error)
}

View file

@ -8,19 +8,9 @@ package reqcontext
import ( import (
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"github.com/notherealmarco/WASAPhoto/service/database"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
type AuthStatus int
const (
AUTHORIZED = 0
UNAUTHORIZED = 1
FORBIDDEN = 2
USER_NOT_FOUND = 3
) // todo: here?
// RequestContext is the context of the request, for request-dependent parameters // RequestContext is the context of the request, for request-dependent parameters
type RequestContext struct { type RequestContext struct {
// ReqUUID is the request unique ID // ReqUUID is the request unique ID
@ -31,10 +21,3 @@ type RequestContext struct {
Auth Authorization Auth Authorization
} }
type Authorization interface {
GetType() string
GetUserID() string
Authorized(db database.AppDatabase) (AuthStatus, error)
UserAuthorized(db database.AppDatabase, uid string) (AuthStatus, error)
}

View file

@ -5,10 +5,9 @@ import (
) )
// Get user stream // Get user stream
// todo implement stidx + offset
func (db *appdbimpl) GetUserStream(uid string, start_index int, limit int) (*[]structures.Photo, error) { func (db *appdbimpl) GetUserStream(uid string, start_index int, limit int) (*[]structures.Photo, error) {
// Get photos // Get photos from the database
rows, err := db.c.Query(`SELECT "p"."user", "p"."id", "p"."date", rows, err := db.c.Query(`SELECT "p"."user", "p"."id", "p"."date",
( (
SELECT COUNT(*) AS "likes" FROM "likes" AS "l" SELECT COUNT(*) AS "likes" FROM "likes" AS "l"

View file

@ -8,20 +8,16 @@ import (
"github.com/notherealmarco/WASAPhoto/service/structures" "github.com/notherealmarco/WASAPhoto/service/structures"
) )
//Check if user exists and if exists return the user id by username
//todo
// Check if user exists // Check if user exists
func (db *appdbimpl) UserExists(uid string) (bool, error) { //todo: refactor code func (db *appdbimpl) UserExists(uid string) (bool, error) {
var name string
err := db.c.QueryRow(`SELECT "name" FROM "users" WHERE "uid" = ?`, uid).Scan(&name)
if db_errors.EmptySet(err) { var cnt int
return false, nil err := db.c.QueryRow(`SELECT COUNT(*) FROM "users" WHERE "uid" = ?`, uid).Scan(&cnt)
} else if err != nil {
if err != nil {
return false, err return false, err
} }
return true, nil return cnt > 0, nil
} }
// Get user id by username // Get user id by username