Skip to content

Commit 8ff21de

Browse files
committed
handled more exceptions in gRPC
1 parent b51a123 commit 8ff21de

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/NetCoreKit.Infrastructure.GrpcHost/AuthInterceptor.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,23 @@ public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TR
8888
handler.InboundClaimTypeMap.Clear();
8989

9090
var userToken = context.RequestHeaders.FirstOrDefault(x => x.Key == "authorization")?.Value;
91+
92+
if (string.IsNullOrEmpty(userToken))
93+
{
94+
throw new AuthenticationException("Cannot get authorization on the header");
95+
}
96+
9197
var user = handler.ValidateToken(userToken.TrimStart("Bearer").TrimStart("bearer").TrimStart(" "), parameters, out _);
98+
99+
if (user == null)
100+
{
101+
throw new AuthenticationException(
102+
"Cannot validate jwt token. Check authority and audience configuration.");
103+
}
104+
92105
if (!user.HasClaim(c => c.Value == attribute.Name))
93106
{
94-
throw new AuthenticationException("Couldn't access to this API, please check your permission.");
107+
throw new AuthenticationException("Cannot access to this API, please check your permission.");
95108
}
96109

97110
return await continuation(request, context);

0 commit comments

Comments
 (0)