pardahlman / RawRabbit

A modern .NET framework for communication over RabbitMq
MIT License
747 stars 144 forks source link

Obtain user-id property #334

Open cocowalla opened 6 years ago

cocowalla commented 6 years ago

I'd like to use the user-id property for authorisation within my subscribe and respond handlers.

RabbitMQ has the concept of a 'Validated User-ID', so it should be secure to use this. I also noted that RawRabbit already appears to be setting this property.

What I'm unsure about is how to access message properties in my subscribe and respond handlers (using SubscribeAsync and RespondAsync. Can you advise how this should be done?

pardahlman commented 6 years ago

SubscribeAsync allows you to use any property from the IPipeContext as a message context, including BasicDeliveryArgs, which is what I'm doing here.

However, in your case I would probably try to use a custom message context with UserId property extracted from the basic properties. The ASP.NET sample does this (but with request id from the http context).

HTH!

cocowalla commented 6 years ago

Ah, I hadn't seen the SubscribeAsync overload that has a MessageContext type parameter, and wasn't sure how to use MessageContext - your samples demonstrate it nicely tho. I'll give it a try later and close if it goes to plan.

cocowalla commented 6 years ago

Hmm, I've hit a problem :(

RawRabbit is setting the user ID property here, but in my .UseMessageContext(c => ... call, c.GetBasicProperties().UserId is always null.

I guess something is changing/replacing the IBasicProperties generated by BasicPublishConfigurationFactory.GetBasicProperties?

pardahlman commented 6 years ago

Hmm, I think this needs to be debugged in order to see what's happening. If you want to try something else, you could probably write your own custom middleware that adds the UserId to the basic props - it shouldn't be too difficult :wink:

cocowalla commented 6 years ago

I've debugged as far as I can while working with the Nuget packages, and it's definitely the whole IBasicProperties that is being replaced somewhere down the line (the MessageId changes between BasicPublishConfigurationFactory and UseMessageContext.

In the mean time, are there any docs or samples showing how to implement a simple middleware?

pardahlman commented 6 years ago

That's what I thought, then. I believe that this middleware replaces the configured basic properties created at an earlier stage. This will definitely be addressed sometime soon, in the mean while you can read the section on modifying predefined operations.

Hope this helps!

cocowalla commented 6 years ago

Actually, I noticed that the user ID property remains correctly set for publishes, the problem is with requests.

Anyway, I'll take a look at the docs you pointed me to in the mean time!

cocowalla commented 6 years ago

I was able to create an extension method for working with the RequestPipe, along with a new StagedMiddleware that works at the BasicPropertiesCreated stage and successfully sets the UserId - and it remains set!