Signed-off-by: Nikolai Rodionov <iam@allanger.xyz>
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
// var _ tokens.TokensServiceServer = (*TokensServer)(nil)
|
||||
@@ -77,7 +78,34 @@ func (t *TokensServer) GetToken(context.Context, *tokens.GetTokenRequest) (*toke
|
||||
}
|
||||
|
||||
// ListTokens implements [v1.TokensServiceServer].
|
||||
func (t *TokensServer) ListTokens(*emptypb.Empty, grpc.ServerStreamingServer[tokens.ListTokensResponse]) error {
|
||||
func (srv *TokensServer) ListTokens(in *emptypb.Empty, stream grpc.ServerStreamingServer[tokens.ListTokensResponse]) error {
|
||||
claims, err := srv.authorizationCtrl.ClaimsFromContext(stream.Context())
|
||||
if err != nil {
|
||||
return status.Error(codes.Aborted, "Context is invalid")
|
||||
}
|
||||
if claims.UserID == "" {
|
||||
return status.Error(codes.Aborted, "Context is invalid")
|
||||
}
|
||||
|
||||
tokensRes, err := srv.tokenCtrl.List(stream.Context(), claims.UserID)
|
||||
if err != nil {
|
||||
if errors.Is(err, controllers.ErrServerError) {
|
||||
return status.Error(codes.Internal, "Something is broken on our side")
|
||||
}
|
||||
return status.Error(codes.Aborted, "Couldn't create a token")
|
||||
}
|
||||
|
||||
for _, tokenRes := range tokensRes {
|
||||
stream.Send(&tokens.ListTokensResponse{
|
||||
TokenUuid: &tokens.TokenUUID{
|
||||
Uuid: tokenRes.UUID,
|
||||
},
|
||||
TokenMetadata: &tokens.TokenMetadata{
|
||||
Name: tokenRes.Name,
|
||||
ExpiresAt: timestamppb.New(tokenRes.ExpiresAt),
|
||||
},
|
||||
})
|
||||
}
|
||||
return status.Error(codes.Unimplemented, "Method is not implemented")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user