-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from pusher/user_authentication
Introduce user authentication
- Loading branch information
Showing
23 changed files
with
683 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using PusherServer.Tests.Helpers; | ||
|
||
namespace PusherServer.Tests.AcceptanceTests | ||
{ | ||
[TestFixture] | ||
public class When_authenticating_a_user | ||
{ | ||
[Test] | ||
public async Task the_auth_token_for_a_user_should_be_accepted_by_Pusher() | ||
{ | ||
PusherServer.Pusher pusherServer = new Pusher(Config.AppId, Config.AppKey, Config.AppSecret, new PusherOptions() | ||
{ | ||
HostName = Config.HttpHost | ||
}); | ||
PusherClient.Pusher pusherClient = new PusherClient.Pusher(Config.AppKey, new PusherClient.PusherOptions | ||
{ | ||
UserAuthenticator = new InMemoryUserAuthenticator( | ||
pusherServer, | ||
new UserData() | ||
{ | ||
id = "leggetter", | ||
watchlist = new string[] { "user_1", "user_2" }, | ||
user_info = new { twitter_id = "@leggetter" } | ||
}), | ||
Cluster = Config.Cluster, | ||
TraceLogger = new PusherClient.TraceLogger(), | ||
}); | ||
|
||
await pusherClient.ConnectAsync().ConfigureAwait(false); | ||
pusherClient.User.Signin(); | ||
|
||
await pusherClient.User.SigninDoneAsync().ConfigureAwait(false); | ||
|
||
// No assertions for now. If the above code executes without error then the test passes. | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using PusherClient; | ||
|
||
namespace PusherServer.Tests.Helpers | ||
{ | ||
internal class InMemoryUserAuthenticator: IUserAuthenticator | ||
{ | ||
private readonly PusherServer.Pusher _pusher; | ||
private readonly UserData _userData; | ||
|
||
public InMemoryUserAuthenticator(PusherServer.Pusher pusher, UserData userData) | ||
{ | ||
_pusher = pusher; | ||
_userData = userData; | ||
} | ||
|
||
public string Authenticate(string socketId) | ||
{ | ||
IUserAuthenticationResponse authResponse; | ||
authResponse = _pusher.AuthenticateUser(socketId, _userData); | ||
return authResponse.ToJson(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using NUnit.Framework; | ||
using PusherServer.Exceptions; | ||
using PusherServer.Tests.Helpers; | ||
|
||
namespace PusherServer.Tests.UnitTests | ||
{ | ||
[TestFixture] | ||
public class When_authenticating_a_user | ||
{ | ||
private IPusher _pusher; | ||
|
||
[OneTimeSetUp] | ||
public void Setup() | ||
{ | ||
_pusher = new Pusher(Config.AppId, Config.AppKey, Config.AppSecret); | ||
} | ||
|
||
[Test] | ||
[ExpectedException(typeof(ArgumentNullException))] | ||
public void null_user_data_throw_Exception() | ||
{ | ||
string socketId = "some_socket_id"; | ||
|
||
_pusher.AuthenticateUser(socketId, null); | ||
} | ||
|
||
[Test] | ||
public void the_auth_response_is_valid() | ||
{ | ||
string socketId = "123.456"; | ||
|
||
UserData userData = new UserData() | ||
{ | ||
id = "unique_user_id", | ||
}; | ||
string userDataJson = DefaultSerializer.Default.Serialize(userData); | ||
|
||
string expectedAuthString = Config.AppKey + ":" + CreateSignedString(socketId, userDataJson); | ||
|
||
IUserAuthenticationResponse result = _pusher.AuthenticateUser(socketId, userData); | ||
Assert.AreEqual(expectedAuthString, result.auth); | ||
Assert.AreEqual(userDataJson, result.user_data); | ||
} | ||
|
||
[Test] | ||
public void with_watchlist_the_auth_response_is_valid() | ||
{ | ||
string socketId = "123.456"; | ||
|
||
UserData userData = new UserData() | ||
{ | ||
id = "unique_user_id", | ||
watchlist = new string[] { "user1", "user2" }, | ||
user_info = new { twitter_id = "@leggetter" } | ||
}; | ||
string userDataJson = DefaultSerializer.Default.Serialize(userData); | ||
|
||
string expectedAuthString = Config.AppKey + ":" + CreateSignedString(socketId, userDataJson); | ||
|
||
IUserAuthenticationResponse result = _pusher.AuthenticateUser(socketId, userData); | ||
Assert.AreEqual(expectedAuthString, result.auth); | ||
Assert.AreEqual(userDataJson, result.user_data); | ||
} | ||
|
||
[Test] | ||
public void with_userinfo_the_auth_response_is_valid() | ||
{ | ||
string socketId = "123.456"; | ||
|
||
UserData userData = new UserData() | ||
{ | ||
id = "unique_user_id", | ||
user_info = new { twitter_id = "@leggetter" } | ||
}; | ||
string userDataJson = DefaultSerializer.Default.Serialize(userData); | ||
|
||
string expectedAuthString = Config.AppKey + ":" + CreateSignedString(socketId, userDataJson); | ||
|
||
IUserAuthenticationResponse result = _pusher.AuthenticateUser(socketId, userData); | ||
Assert.AreEqual(expectedAuthString, result.auth); | ||
Assert.AreEqual(userDataJson, result.user_data); | ||
} | ||
|
||
[Test] | ||
public void with_userinfo_and_watchlist_the_auth_response_is_valid() | ||
{ | ||
string socketId = "123.456"; | ||
|
||
UserData userData = new UserData() | ||
{ | ||
id = "unique_user_id", | ||
watchlist = new string[] { "user1", "user2" }, | ||
user_info = new { twitter_id = "@leggetter" } | ||
}; | ||
string userDataJson = DefaultSerializer.Default.Serialize(userData); | ||
|
||
string expectedAuthString = Config.AppKey + ":" + CreateSignedString(socketId, userDataJson); | ||
|
||
IUserAuthenticationResponse result = _pusher.AuthenticateUser(socketId, userData); | ||
Assert.AreEqual(expectedAuthString, result.auth); | ||
Assert.AreEqual(userDataJson, result.user_data); | ||
} | ||
|
||
private string CreateSignedString(string socketId, string userDataJson) | ||
{ | ||
var stringToSign = socketId + "::user::" + userDataJson; | ||
return CryptoHelper.GetHmac256(Config.AppSecret, stringToSign); | ||
} | ||
} | ||
} |
Oops, something went wrong.