179 lines
4.2 KiB
Go
179 lines
4.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"git.itzana.me/strafesnet/go-grpc/auth"
|
|
"git.itzana.me/strafesnet/maps-service/pkg/api"
|
|
)
|
|
|
|
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")
|
|
)
|
|
|
|
// Submissions roles bitflag
|
|
type Roles int32
|
|
var (
|
|
RolesScriptWrite Roles = 8
|
|
RolesSubmissionPublish Roles = 4
|
|
RolesSubmissionReview Roles = 2
|
|
RolesMapDownload Roles = 1
|
|
RolesEmpty Roles = 0
|
|
)
|
|
|
|
// StrafesNET group roles
|
|
type Role int32
|
|
var (
|
|
// has ScriptWrite
|
|
RoleQuat Role = 240
|
|
RolesQuat Roles = RolesScriptWrite|RolesSubmissionPublish|RolesSubmissionReview|RolesMapDownload
|
|
// has SubmissionPublish
|
|
RoleMapAdmin Role = 128
|
|
RolesMapAdmin Roles = RolesSubmissionPublish|RolesSubmissionReview|RolesMapDownload
|
|
// has SubmissionReview
|
|
RoleMapCouncil Role = 64
|
|
RolesMapCouncil Roles = RolesSubmissionReview|RolesMapDownload
|
|
// access to downloading maps
|
|
RoleMapAccess Role = 32
|
|
RolesMapAccess Roles = RolesMapDownload
|
|
)
|
|
|
|
type UserInfoHandle struct {
|
|
// Would love to know a better way to do this
|
|
svc *SecurityHandler
|
|
ctx *context.Context
|
|
sessionId string
|
|
}
|
|
type UserInfo struct {
|
|
UserID uint64
|
|
Username string
|
|
AvatarURL string
|
|
}
|
|
|
|
func (usr UserInfoHandle) GetUserInfo() (userInfo UserInfo, err error) {
|
|
session, err := usr.svc.Client.GetSessionUser(*usr.ctx, &auth.IdMessage{
|
|
SessionID: usr.sessionId,
|
|
})
|
|
if err != nil {
|
|
return userInfo, err
|
|
}
|
|
userInfo.UserID = session.UserID
|
|
userInfo.Username = session.Username
|
|
userInfo.AvatarURL = session.AvatarURL
|
|
return userInfo, nil
|
|
}
|
|
func (usr UserInfoHandle) GetUserID() (uint64, error) {
|
|
session, err := usr.svc.Client.GetSessionUser(*usr.ctx, &auth.IdMessage{
|
|
SessionID: usr.sessionId,
|
|
})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return session.UserID, nil
|
|
}
|
|
func (usr UserInfoHandle) Validate() (bool, error) {
|
|
validate, err := usr.svc.Client.ValidateSession(*usr.ctx, &auth.IdMessage{
|
|
SessionID: usr.sessionId,
|
|
})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return validate.Valid, nil
|
|
}
|
|
func (usr UserInfoHandle) IsSubmitter(submitter uint64) (bool, error) {
|
|
userId, err := usr.GetUserID()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return userId == submitter, nil
|
|
}
|
|
func (usr UserInfoHandle) hasRole(role Role) (bool, error) {
|
|
roles, err := usr.svc.Client.GetGroupRole(*usr.ctx, &auth.IdMessage{
|
|
SessionID: usr.sessionId,
|
|
})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
for _, r := range roles.Roles {
|
|
if int32(role) <= r.Rank {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|
|
func (usr UserInfoHandle) GetRoles() (Roles, error) {
|
|
roles, err := usr.svc.Client.GetGroupRole(*usr.ctx, &auth.IdMessage{
|
|
SessionID: usr.sessionId,
|
|
})
|
|
|
|
var rolesBitflag = RolesEmpty;
|
|
if err != nil {
|
|
return rolesBitflag, err
|
|
}
|
|
|
|
// map roles into bitflag
|
|
for _, r := range roles.Roles {
|
|
switch Role(r.Rank){
|
|
case RoleQuat:
|
|
rolesBitflag|=RolesQuat;
|
|
case RoleMapAdmin:
|
|
rolesBitflag|=RolesMapAdmin;
|
|
case RoleMapCouncil:
|
|
rolesBitflag|=RolesMapCouncil;
|
|
case RoleMapAccess:
|
|
rolesBitflag|=RolesMapAccess;
|
|
}
|
|
}
|
|
return rolesBitflag, nil
|
|
}
|
|
|
|
// RoleThumbnail
|
|
// RoleMapDownload
|
|
func (usr UserInfoHandle) HasRoleSubmissionRelease() (bool, error) {
|
|
return usr.hasRole(RoleMapAdmin)
|
|
}
|
|
func (usr UserInfoHandle) HasRoleSubmissionReview() (bool, error) {
|
|
return usr.hasRole(RoleMapCouncil)
|
|
}
|
|
func (usr UserInfoHandle) HasRoleScriptWrite() (bool, error) {
|
|
return usr.hasRole(RoleQuat)
|
|
}
|
|
/// Not implemented
|
|
func (usr UserInfoHandle) HasRoleMaptest() (bool, error) {
|
|
println("HasRoleMaptest is not implemented!")
|
|
return false, nil
|
|
}
|
|
|
|
type SecurityHandler struct {
|
|
Client auth.AuthServiceClient
|
|
}
|
|
|
|
func (svc SecurityHandler) HandleCookieAuth(ctx context.Context, operationName api.OperationName, t api.CookieAuth) (context.Context, error) {
|
|
sessionId := t.GetAPIKey()
|
|
if sessionId == "" {
|
|
return nil, ErrMissingSessionID
|
|
}
|
|
|
|
validate, err := svc.Client.ValidateSession(ctx, &auth.IdMessage{
|
|
SessionID: sessionId,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !validate.Valid {
|
|
return nil, ErrInvalidSession
|
|
}
|
|
|
|
newCtx := context.WithValue(ctx, "UserInfo", UserInfoHandle{
|
|
svc: &svc,
|
|
ctx: &ctx,
|
|
sessionId: sessionId,
|
|
})
|
|
|
|
return newCtx, nil
|
|
}
|