maps-service/pkg/service/security.go

94 lines
1.7 KiB
Go
Raw Normal View History

2024-11-29 21:58:47 +00:00
package service
import (
"errors"
"context"
"git.itzana.me/strafesnet/maps-service/pkg/api"
"git.itzana.me/strafesnet/go-grpc/auth"
)
var (
// ErrMissingSessionID there is no session id
ErrMissingSessionID = errors.New("SessionID missing")
// ErrInvalidSession caller does not have a valid session
ErrInvalidSession = errors.New("Session invalid")
)
var (
RoleAdmin = 128
RoleReviewer = 64
)
type Roles struct {
2024-12-06 03:10:01 +00:00
// human roles
SubmissionPublish bool
SubmissionReview bool
ScriptWrite bool
// automated roles
2024-11-29 21:58:47 +00:00
Maptest bool
Validator bool
}
type UserInfo struct {
Roles Roles
UserID int64
}
func (usr UserInfo) IsSubmitter(submitter int64) bool{
return usr.UserID == submitter
}
type SecurityHandler struct {
2024-12-10 04:10:23 +00:00
Client auth.AuthServiceClient
2024-11-29 21:58:47 +00:00
}
func (svc SecurityHandler) HandleCookieAuth(ctx context.Context, operationName api.OperationName, t api.CookieAuth) (context.Context, error){
sessionId := t.GetAPIKey()
if sessionId == "" {
return nil, ErrMissingSessionID
}
2024-12-10 04:10:23 +00:00
session, err := svc.Client.GetSessionUser(ctx, &auth.IdMessage{
2024-11-29 21:58:47 +00:00
SessionID: sessionId,
})
if err != nil{
return nil, err
}
2024-12-10 04:10:23 +00:00
role, err := svc.Client.GetGroupRole(ctx, &auth.IdMessage{
2024-11-29 21:58:47 +00:00
SessionID: sessionId,
})
if err != nil{
return nil, err
}
2024-12-10 04:10:23 +00:00
validate, err := svc.Client.ValidateSession(ctx, &auth.IdMessage{
2024-11-29 21:58:47 +00:00
SessionID: sessionId,
})
if err != nil{
return nil, err
}
if !validate.Valid{
return nil, ErrInvalidSession
}
roles := Roles{}
// fix this when roblox udpates group roles
for r := range role.Roles{
if RoleAdmin<=r{
2024-12-06 03:10:01 +00:00
roles.SubmissionPublish = true
2024-11-29 21:58:47 +00:00
}
if RoleReviewer<=r{
2024-12-06 03:10:01 +00:00
roles.SubmissionReview = true
2024-11-29 21:58:47 +00:00
}
}
newCtx := context.WithValue(ctx, "UserInfo", UserInfo{
Roles: roles,
UserID: int64(session.UserID),
})
return newCtx, nil
}