Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Added Nancy context to UserMapper #599

Merged
merged 2 commits into from
Apr 24, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public void Should_get_username_from_mapping_service_with_valid_cookie()

fakePipelines.BeforeRequest.Invoke(this.context);

A.CallTo(() => mockMapper.GetUserFromIdentifier(this.userGuid))
A.CallTo(() => mockMapper.GetUserFromIdentifier(this.userGuid, this.context))
.MustHaveHappened(Repeated.Exactly.Once);
}

Expand All @@ -359,7 +359,7 @@ public void Should_set_user_in_context_with_valid_cookie()
var fakeMapper = A.Fake<IUserMapper>();
var fakeUser = A.Fake<IUserIdentity>();
fakeUser.UserName = "Bob";
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid)).Returns(fakeUser);
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid, this.context)).Returns(fakeUser);
this.config.UserMapper = fakeMapper;
FormsAuthentication.Enable(fakePipelines, this.config);
this.context.Request.Cookies.Add(FormsAuthentication.FormsAuthenticationCookieName, this.validCookieValue);
Expand All @@ -376,7 +376,7 @@ public void Should_not_set_user_in_context_with_invalid_hmac()
var fakeMapper = A.Fake<IUserMapper>();
var fakeUser = A.Fake<IUserIdentity>();
fakeUser.UserName = "Bob";
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid)).Returns(fakeUser);
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid, this.context)).Returns(fakeUser);
this.config.UserMapper = fakeMapper;
FormsAuthentication.Enable(fakePipelines, this.config);
this.context.Request.Cookies.Add(FormsAuthentication.FormsAuthenticationCookieName, this.cookieWithInvalidHmac);
Expand All @@ -393,7 +393,7 @@ public void Should_not_set_user_in_context_with_empty_hmac()
var fakeMapper = A.Fake<IUserMapper>();
var fakeUser = A.Fake<IUserIdentity>();
fakeUser.UserName = "Bob";
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid)).Returns(fakeUser);
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid, this.context)).Returns(fakeUser);
this.config.UserMapper = fakeMapper;
FormsAuthentication.Enable(fakePipelines, this.config);
this.context.Request.Cookies.Add(FormsAuthentication.FormsAuthenticationCookieName, this.cookieWithEmptyHmac);
Expand All @@ -410,7 +410,7 @@ public void Should_not_set_user_in_context_with_no_hmac()
var fakeMapper = A.Fake<IUserMapper>();
var fakeUser = A.Fake<IUserIdentity>();
fakeUser.UserName = "Bob";
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid)).Returns(fakeUser);
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid, this.context)).Returns(fakeUser);
this.config.UserMapper = fakeMapper;
FormsAuthentication.Enable(fakePipelines, this.config);
this.context.Request.Cookies.Add(FormsAuthentication.FormsAuthenticationCookieName, this.cookieWithNoHmac);
Expand All @@ -427,7 +427,7 @@ public void Should_not_set_username_in_context_with_broken_encryption_data()
var fakeMapper = A.Fake<IUserMapper>();
var fakeUser = A.Fake<IUserIdentity>();
fakeUser.UserName = "Bob";
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid)).Returns(fakeUser);
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid, this.context)).Returns(fakeUser);
this.config.UserMapper = fakeMapper;
FormsAuthentication.Enable(fakePipelines, this.config);
this.context.Request.Cookies.Add(FormsAuthentication.FormsAuthenticationCookieName, this.cookieWithBrokenEncryptedData);
Expand Down
4 changes: 2 additions & 2 deletions src/Nancy.Authentication.Forms/FormsAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ private static Func<NancyContext, Response> GetLoadAuthenticationHook(FormsAuthe

if (userGuid != Guid.Empty)
{
context.CurrentUser =
configuration.UserMapper.GetUserFromIdentifier(userGuid);

context.CurrentUser = configuration.UserMapper.GetUserFromIdentifier(userGuid, context);
}

return null;
Expand Down
5 changes: 3 additions & 2 deletions src/Nancy.Authentication.Forms/IUsernameMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public interface IUserMapper
/// Get the real username from an indentifier
/// </summary>
/// <param name="identifier">User identifier</param>
/// <returns>Matching username, or empty</returns>
IUserIdentity GetUserFromIdentifier(Guid identifier);
/// <param name="context">The current NancyFx context</param>
/// <returns>Matching populated IUserIdentity object, or empty</returns>
IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context);
}
}
2 changes: 1 addition & 1 deletion src/Nancy.Demo.Authentication.Forms/UserDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static UserDatabase()
users.Add(new Tuple<string, string, Guid>("user", "password", new Guid("56E1E49E-B7E8-4EEA-8459-7A906AC4D4C0")));
}

public IUserIdentity GetUserFromIdentifier(Guid identifier)
public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context)
{
var userRecord = users.Where(u => u.Item3 == identifier).FirstOrDefault();

Expand Down