You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

49 lines
857 B

package session
import (
"context"
"errors"
"google.golang.org/grpc/metadata"
)
const (
UidKey = "uid"
)
var (
UnauthorizedRequestError = errors.New("unauthorized request")
)
type RpcSubject struct {
Uid string
}
func NewRpcSubject(uid string) *RpcSubject {
return &RpcSubject{
Uid: uid,
}
}
func PutSubject(ctx context.Context, subject *RpcSubject) context.Context {
return metadata.NewOutgoingContext(ctx, metadata.Pairs(UidKey, subject.Uid))
}
func GetSubject(ctx context.Context) (*RpcSubject, error) {
uid, ok := GetUid(ctx)
if !ok {
return nil, UnauthorizedRequestError
}
return &RpcSubject{Uid: uid}, nil
}
func GetUid(ctx context.Context) (string, bool) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return "", false
}
uids := md.Get(UidKey)
if len(uids) == 0 {
return "", false
}
return uids[0], true
}