113 lines
3.0 KiB
Go
113 lines
3.0 KiB
Go
package httpRequest
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"go-socket/packages/convertions"
|
|
|
|
"go-socket/packages/cache"
|
|
"go-socket/packages/postgresql"
|
|
"go-socket/packages/tokens"
|
|
"go-socket/packages/types"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func getUserById(ctx context.Context, userId uuid.UUID) (*types.User, error) {
|
|
user, err := cache.GetUserById(userId)
|
|
if err != nil {
|
|
user = &types.User{Id: userId, Hubs: make(map[uuid.UUID]*types.Hub)}
|
|
err = postgresql.GetWholeUser(ctx, user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return user, nil
|
|
}
|
|
|
|
func getUserByToken(ctx context.Context, token string) (*types.User, error) {
|
|
userId, err := tokens.TokenValidateGetId(token)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return getUserById(ctx, userId)
|
|
}
|
|
|
|
func getConnectionWithResponseOnFail(response http.ResponseWriter, connectionIdStr string, user *types.User) (*types.Connection, bool) {
|
|
connectionId, err := convertions.StringToUuid(connectionIdStr)
|
|
if err != nil {
|
|
http.Error(response, "invalid connectionid", http.StatusBadRequest)
|
|
return nil, false
|
|
}
|
|
conn, ok := cache.GetConnection(user, connectionId)
|
|
if !ok {
|
|
http.Error(response, "invalid connectionid", http.StatusBadRequest)
|
|
return nil, false
|
|
}
|
|
return conn, true
|
|
}
|
|
|
|
func getHubByIdStr(ctx context.Context, hubId string) (*types.Hub, error) {
|
|
hubUuid, err := convertions.StringToUuid(hubId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
hub, ok := cache.GetHubById(hubUuid)
|
|
|
|
if !ok {
|
|
return nil, errors.New("hub not found")
|
|
}
|
|
|
|
return hub, nil
|
|
}
|
|
|
|
func getHubUserIfValidWithResponseOnFail(ctx context.Context, response http.ResponseWriter, request *http.Request) (
|
|
*types.User, *types.HubUser, *types.Hub, error) {
|
|
user, err := getUserByToken(ctx, request.Header.Get("token"))
|
|
if err != nil {
|
|
http.Error(response, "invalid token", http.StatusBadRequest)
|
|
return nil, nil, nil, errors.New("invalid token")
|
|
}
|
|
|
|
hub, err := getHubByIdStr(ctx, request.Header.Get("hubid"))
|
|
if err != nil {
|
|
http.Error(response, "invalid hubid", http.StatusBadRequest)
|
|
return nil, nil, nil, errors.New("no such hub")
|
|
}
|
|
|
|
hub.Mu.RLock()
|
|
hubUser, ok := hub.Users[user.Id]
|
|
hub.Mu.RUnlock()
|
|
if !ok {
|
|
http.Error(response, "invalid hubid", http.StatusUnauthorized)
|
|
return nil, nil, nil, errors.New("invalid hubid")
|
|
}
|
|
|
|
return user, hubUser, hub, nil
|
|
}
|
|
|
|
func getHubChannelIfValidWithResponseOnFail(ctx context.Context, response http.ResponseWriter, hub *types.Hub, hubUser *types.HubUser, channelId string) (
|
|
*types.HubChannel, error) {
|
|
channelUuid, err := convertions.StringToUuid(channelId)
|
|
if err != nil {
|
|
http.Error(response, "invalid channelid", http.StatusBadRequest)
|
|
return nil, errors.New("invalid channelid")
|
|
}
|
|
channel, ok := hub.Channels[channelUuid]
|
|
if !ok {
|
|
http.Error(response, "invalid channelid", http.StatusBadRequest)
|
|
return nil, errors.New("invalid channelid")
|
|
}
|
|
|
|
if !haveHubUserCachedPermissions(types.CachedUserCanView, hubUser, channel) {
|
|
http.Error(response, "forbidden", http.StatusForbidden)
|
|
return nil, errors.New("forbidden")
|
|
}
|
|
|
|
return channel, nil
|
|
}
|