2024-03-22 16:28:27 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-10-30 16:15:25 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
|
2024-10-23 16:46:59 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
2024-09-11 15:04:14 +00:00
|
|
|
|
2024-08-14 14:50:03 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
|
|
|
"github.com/samber/lo"
|
2024-05-22 15:21:31 +00:00
|
|
|
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
|
2024-05-17 12:14:20 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2024-10-23 16:13:16 +00:00
|
|
|
|
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
2024-03-22 16:28:27 +00:00
|
|
|
)
|
|
|
|
|
2024-10-23 16:13:16 +00:00
|
|
|
type authenticateServer struct {
|
|
|
|
proto.UnimplementedAuthServiceServer
|
|
|
|
}
|
|
|
|
|
2024-10-27 04:50:07 +00:00
|
|
|
func (v *App) Authenticate(_ context.Context, in *proto.AuthRequest) (*proto.AuthReply, error) {
|
2024-10-23 16:46:59 +00:00
|
|
|
ticket, perms, err := services.Authenticate(uint(in.GetSessionId()))
|
2024-03-22 16:28:27 +00:00
|
|
|
if err != nil {
|
2024-07-15 16:02:28 +00:00
|
|
|
return &proto.AuthReply{
|
2024-03-22 16:28:27 +00:00
|
|
|
IsValid: false,
|
|
|
|
}, nil
|
|
|
|
} else {
|
2024-10-23 16:46:59 +00:00
|
|
|
user := ticket.Account
|
2024-07-15 16:02:28 +00:00
|
|
|
userinfo := &proto.UserInfo{
|
2024-10-23 16:46:59 +00:00
|
|
|
Id: uint64(user.ID),
|
|
|
|
Name: user.Name,
|
|
|
|
PermNodes: nex.EncodeMap(perms),
|
|
|
|
Metadata: nex.EncodeMap(user),
|
2024-08-24 16:08:06 +00:00
|
|
|
}
|
|
|
|
|
2024-07-15 16:02:28 +00:00
|
|
|
return &proto.AuthReply{
|
2024-07-14 16:01:17 +00:00
|
|
|
IsValid: true,
|
2024-07-15 16:02:28 +00:00
|
|
|
Info: &proto.AuthInfo{
|
2024-10-23 16:46:59 +00:00
|
|
|
SessionId: uint64(ticket.ID),
|
|
|
|
Info: userinfo,
|
2024-07-14 16:01:17 +00:00
|
|
|
},
|
2024-03-22 16:28:27 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
2024-05-17 12:14:20 +00:00
|
|
|
|
2024-10-27 04:50:07 +00:00
|
|
|
func (v *App) EnsurePermGranted(_ context.Context, in *proto.CheckPermRequest) (*proto.CheckPermResponse, error) {
|
2024-10-23 16:46:59 +00:00
|
|
|
ctx, err := services.GetAuthContext(uint(in.GetSessionId()))
|
2024-05-17 12:14:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-17 12:34:34 +00:00
|
|
|
var heldPerms map[string]any
|
|
|
|
rawHeldPerms, _ := jsoniter.Marshal(ctx.Account.PermNodes)
|
|
|
|
_ = jsoniter.Unmarshal(rawHeldPerms, &heldPerms)
|
|
|
|
|
2024-05-17 12:14:20 +00:00
|
|
|
var value any
|
|
|
|
_ = jsoniter.Unmarshal(in.GetValue(), &value)
|
2024-10-23 16:46:59 +00:00
|
|
|
perms := services.FilterPermNodes(heldPerms, ctx.Claims)
|
2024-05-17 12:14:20 +00:00
|
|
|
valid := services.HasPermNode(perms, in.GetKey(), value)
|
|
|
|
|
2024-07-15 16:02:28 +00:00
|
|
|
return &proto.CheckPermResponse{
|
|
|
|
IsValid: valid,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-10-27 04:50:07 +00:00
|
|
|
func (v *App) EnsureUserPermGranted(_ context.Context, in *proto.CheckUserPermRequest) (*proto.CheckUserPermResponse, error) {
|
2024-07-15 16:02:28 +00:00
|
|
|
relation, err := services.GetRelationWithTwoNode(uint(in.GetUserId()), uint(in.GetOtherId()))
|
|
|
|
if err != nil {
|
|
|
|
return &proto.CheckUserPermResponse{
|
|
|
|
IsValid: false,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultPerm := relation.Status == models.RelationshipFriend
|
|
|
|
|
|
|
|
var value any
|
|
|
|
_ = jsoniter.Unmarshal(in.GetValue(), &value)
|
|
|
|
valid := services.HasPermNodeWithDefault(relation.PermNodes, in.GetKey(), value, defaultPerm)
|
|
|
|
|
|
|
|
return &proto.CheckUserPermResponse{
|
2024-05-17 12:14:20 +00:00
|
|
|
IsValid: valid,
|
|
|
|
}, nil
|
|
|
|
}
|
2024-08-14 14:50:03 +00:00
|
|
|
|
2024-10-27 04:50:07 +00:00
|
|
|
func (v *App) ListUserFriends(_ context.Context, in *proto.ListUserRelativeRequest) (*proto.ListUserRelativeResponse, error) {
|
2024-08-14 14:50:03 +00:00
|
|
|
tx := database.C.Preload("Account").Where("status = ?", models.RelationshipFriend)
|
|
|
|
|
|
|
|
if in.GetIsRelated() {
|
|
|
|
tx = tx.Where("related_id = ?", in.GetUserId())
|
|
|
|
} else {
|
|
|
|
tx = tx.Where("account_id = ?", in.GetUserId())
|
|
|
|
}
|
|
|
|
|
|
|
|
var data []models.AccountRelationship
|
|
|
|
if err := tx.Find(&data).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.ListUserRelativeResponse{
|
2024-10-23 16:46:59 +00:00
|
|
|
Data: lo.Map(data, func(item models.AccountRelationship, index int) *proto.UserInfo {
|
|
|
|
val := &proto.UserInfo{
|
2024-08-14 14:50:03 +00:00
|
|
|
Id: uint64(item.AccountID),
|
|
|
|
Name: item.Account.Name,
|
2024-08-24 16:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return val
|
2024-08-14 14:50:03 +00:00
|
|
|
}),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-10-27 04:50:07 +00:00
|
|
|
func (v *App) ListUserBlocklist(_ context.Context, in *proto.ListUserRelativeRequest) (*proto.ListUserRelativeResponse, error) {
|
2024-08-14 14:50:03 +00:00
|
|
|
tx := database.C.Preload("Account").Where("status = ?", models.RelationshipBlocked)
|
|
|
|
|
|
|
|
if in.GetIsRelated() {
|
|
|
|
tx = tx.Where("related_id = ?", in.GetUserId())
|
|
|
|
} else {
|
|
|
|
tx = tx.Where("account_id = ?", in.GetUserId())
|
|
|
|
}
|
|
|
|
|
|
|
|
var data []models.AccountRelationship
|
|
|
|
if err := tx.Find(&data).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.ListUserRelativeResponse{
|
2024-10-23 16:46:59 +00:00
|
|
|
Data: lo.Map(data, func(item models.AccountRelationship, index int) *proto.UserInfo {
|
|
|
|
val := &proto.UserInfo{
|
2024-08-14 14:50:03 +00:00
|
|
|
Id: uint64(item.AccountID),
|
|
|
|
Name: item.Account.Name,
|
2024-08-24 16:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return val
|
2024-08-14 14:50:03 +00:00
|
|
|
}),
|
|
|
|
}, nil
|
|
|
|
}
|