2024-03-22 16:28:27 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-10-23 16:46:59 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
2024-09-11 15:04:14 +00:00
|
|
|
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/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-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
|
|
|
|
}
|