Skip to content

Commit c8ebad5

Browse files
authored
Fix identity based negotiation result for Socket.IO binding (Azure#46120)
* Fix aud issue in aad * Update token * Trim some unless codes * Update release history
1 parent 79a69c5 commit c8ebad5

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSubForSocketIO/CHANGELOG.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
# Release History
22

3-
## 1.0.0-beta.3 (Unreleased)
4-
5-
### Features Added
6-
7-
### Breaking Changes
3+
## 1.0.0-beta.3 (2024-09-24)
84

95
### Bugs Fixed
106

11-
### Other Changes
7+
- Fix the bug that identity based negotiation result is not correct
128

139
## 1.0.0-beta.2 (2024-09-02)
1410

sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSubForSocketIO/src/Microsoft.Azure.WebJobs.Extensions.WebPubSubForSocketIO.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<PackageReference Include="Microsoft.Azure.WebJobs" />
2727
<PackageReference Include="Microsoft.IdentityModel.Tokens" />
2828
<PackageReference Include="Microsoft.Extensions.Azure" />
29+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
2930
</ItemGroup>
3031

3132
<ItemGroup>

sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSubForSocketIO/src/Services/WebPubSubForSocketIOService.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
using Azure.Core;
66
using Azure.Messaging.WebPubSub;
77
using System;
8+
using System.IdentityModel.Tokens.Jwt;
9+
using System.Linq;
810
using System.Text;
11+
using System.Web;
912

1013
namespace Microsoft.Azure.WebJobs.Extensions.WebPubSubForSocketIO
1114
{
@@ -52,8 +55,12 @@ internal SocketIONegotiationResult GetNegotiationResult(string userId)
5255
else
5356
{
5457
// For managed identity, the service can handle it.
55-
var url = _client.GetClientAccessUri();
56-
return new SocketIONegotiationResult(url);
58+
// TODO: Currently, there's a bug in `GetClientAccessUri` and we need to get url by ourselves.
59+
var url = _client.GetClientAccessUri(userId: userId);
60+
var token = HttpUtility.ParseQueryString(url.Query)["access_token"];
61+
// The `aud` in token is correct, we use it as the endpoint.
62+
var endpoint = new JwtSecurityTokenHandler().ReadJwtToken(token).Claims.First(c => c.Type == "aud").Value.TrimEnd('/'); // Must have
63+
return new SocketIONegotiationResult(new Uri($"{endpoint}?access_token={token}"));
5764
}
5865
}
5966

sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSubForSocketIO/tests/SocketIOServiceTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using Azure.Identity;
5+
using Azure.Messaging.WebPubSub;
56
using Microsoft.Azure.WebJobs.Extensions.WebPubSubForSocketIO.Config;
7+
using Moq;
68
using NUnit.Framework;
9+
using System;
10+
using System.Collections.Generic;
711
using System.IdentityModel.Tokens.Jwt;
12+
using System.Threading;
813

914
namespace Microsoft.Azure.WebJobs.Extensions.WebPubSubForSocketIO.Tests
1015
{
@@ -74,5 +79,21 @@ public void TestValidAadSchema(string endpoint, string host)
7479
Assert.IsTrue(configs.TryGetKey(host, out var key));
7580
Assert.Null(key);
7681
}
82+
83+
[Test]
84+
public void TestNegotiateResultForAad()
85+
{
86+
var token = "eyJhbGciOiJIUzI1NiIsImtpZCI6InMtZjZlMTVhZmItNjIxZS00OTc5LTgyZTgtN2FiMGQ4ZmIwMDM1IiwidHlwIjoiSldUIn0.eyJuYmYiOjE3MjcwNzAxODQsImV4cCI6MTcyNzA3MzcyNCwiaWF0IjoxNzI3MDcwMTg0LCJpc3MiOiJodHRwczovL3dlYnB1YnN1Yi5henVyZS5jb20iLCJhdWQiOiJodHRwczovL3Npby01a2tmY2dyMm9icXZtLndlYnB1YnN1Yi5henVyZS5jb20vY2xpZW50cy9zb2NrZXRpby9odWJzL2h1YiJ9.h3QkRTQ4";
87+
var clientMoc = new Mock<WebPubSubServiceClient>();
88+
clientMoc.Setup(c => c.GetClientAccessUri(It.IsAny<TimeSpan>(), It.IsAny<string>(), It.IsAny<IEnumerable<string>>(), It.IsAny<IEnumerable<string>>(), It.IsAny<WebPubSubClientProtocol>(), It.IsAny<CancellationToken>()))
89+
.Returns(new Uri($"https://abc.com?access_token={token}"));
90+
91+
var service = new WebPubSubForSocketIOService(clientMoc.Object);
92+
var result = service.GetNegotiationResult("user");
93+
94+
Assert.AreEqual("https://sio-5kkfcgr2obqvm.webpubsub.azure.com/", result.Endpoint.AbsoluteUri);
95+
Assert.AreEqual("/clients/socketio/hubs/hub", result.Path);
96+
Assert.AreEqual(token, result.Token);
97+
}
7798
}
7899
}

0 commit comments

Comments
 (0)