Dealer/pkg/hyper/auth.go
LittleSheep 466b240e95
Some checks failed
release-nightly / build-docker (push) Has been cancelled
🎉 Initial Commit
2024-07-14 20:25:30 +08:00

66 lines
1.5 KiB
Go

package hyper
import (
"context"
"fmt"
"git.solsynth.dev/hydrogen/dealer/pkg/internal/directory"
"time"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"google.golang.org/grpc"
)
func (v *HyperConn) DoAuthenticate(atk, rtk string) (acc *proto.UserInfo, accessTk string, refreshTk string, err error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
var in *grpc.ClientConn
in, err = v.GetServiceGrpcConn(directory.ServiceTypeAuthProvider)
if err != nil {
return
}
var reply *proto.AuthReply
reply, err = proto.NewAuthClient(in).Authenticate(ctx, &proto.AuthRequest{
AccessToken: atk,
RefreshToken: &rtk,
})
if err != nil {
return
}
if reply != nil {
acc = reply.GetInfo().GetInfo()
accessTk = reply.GetInfo().GetNewAccessToken()
refreshTk = reply.GetInfo().GetNewRefreshToken()
if !reply.IsValid {
err = fmt.Errorf("invalid authorization context")
return
}
}
return
}
func (v *HyperConn) CheckPermGranted(atk string, key string, val []byte) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
in, err := v.GetServiceGrpcConn(directory.ServiceTypeAuthProvider)
if err != nil {
return err
}
reply, err := proto.NewAuthClient(in).EnsurePermGranted(ctx, &proto.CheckPermRequest{
Token: atk,
Key: key,
Value: val,
})
if err != nil {
return err
} else if !reply.GetIsValid() {
return fmt.Errorf("missing permission: %s", key)
}
return nil
}