How to setup custom JWT authentication with Azure Event Grid namespace

Jarik Poplavski 0 Reputation points
2025-02-10T15:18:02.1633333+00:00

Hello,

I am trying to configure custom JWT based authentication for MQTT clients as described in this guide Authenticate with namespaces using JSON Web Tokens.

As a prototype I use the client code example provided in https://github.com/Azure-Samples/MqttApplicationSamples, under jwt_authentication scenario. However, instead of Microsoft Entra ID JWT, I want use it with custom OAuth JWT.

Following the guide, I have registered the custom token issuer with my EG namespace and linked to is public certificate in a key vault. However, I have no success connecting my MQTT client using the issued tokens.

The behavior and logs seems to be different, depending on the MQTT v5 CONNECT packet value. The example above uses OAUTH2-JWT, and if I keep it, I see errors like this in the log space:

ValidateToken error: ValidateJwt client error. CorrelationId: d5d659a6-3832-4e43-ab8c-090b609ff88b. Message: IDX10214: Audience validation failed. Audiences: '$MY_MQTT_HOSTNAME$'. Did not match: validationParameters.ValidAudience: 'null' or validationParameters.ValidAudiences: 'https://eventgrid.azure.net, .. Reason: SecurityTokenInvalidAudienceException

Even if I follow the description from the documentation, eg. "Value must contain standard Event Grid namespace hostname". If I update the audience to https://eventgrid.azure.net, the authentication is still failing, but now with the following error:

ValidateToken error: ValidateJwt client error. CorrelationId: c1dd38eb-1b68-4308-8f04-510f554db7ac. Message: IDX40003: Neither tid nor tenantId claim is present in the token obtained from Microsoft identity platform. . Reason: SecurityTokenInvalidIssuerException

If I change MQTT v5 CONNECT packet value to CUSTOM_JWT, as described in Authentication using Custom JWT, the following error is logged in the EG log space:

OperationName: connect ResultSignature: ServiceError ResultDescription: Internal error has occurred AuthenticationType: AccessToken ClientIdentitySource: JWT ClientIdentity: ..as provided in my app.. SessionName: ..as provided in my app.. Protocol: MQTT5 Type: EGNFailedMqttConnections

and the MQTT client app registers a different MQTT error code: ReturnCode=ConnectionRefusedUnacceptableProtocolVersion, ReasonCode=ImplementationSpecificError.

Is there a way of getting more information about the errors to understand what the exact problem causing the authentication failures?

Thank you!

Best regards,

Jarik

Azure Event Grid
Azure Event Grid
An Azure event routing service designed for high availability, consistent performance, and dynamic scale.
425 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.