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

Commit a498963

Browse files
committed
Merge pull request #599 from skinny/master
Added Nancy context to UserMapper
2 parents 0d16452 + 5c3e675 commit a498963

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

Diff for: src/Nancy.Authentication.Forms.Tests/FormsAuthenticationFixture.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public void Should_get_username_from_mapping_service_with_valid_cookie()
348348

349349
fakePipelines.BeforeRequest.Invoke(this.context);
350350

351-
A.CallTo(() => mockMapper.GetUserFromIdentifier(this.userGuid))
351+
A.CallTo(() => mockMapper.GetUserFromIdentifier(this.userGuid, this.context))
352352
.MustHaveHappened(Repeated.Exactly.Once);
353353
}
354354

@@ -359,7 +359,7 @@ public void Should_set_user_in_context_with_valid_cookie()
359359
var fakeMapper = A.Fake<IUserMapper>();
360360
var fakeUser = A.Fake<IUserIdentity>();
361361
fakeUser.UserName = "Bob";
362-
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid)).Returns(fakeUser);
362+
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid, this.context)).Returns(fakeUser);
363363
this.config.UserMapper = fakeMapper;
364364
FormsAuthentication.Enable(fakePipelines, this.config);
365365
this.context.Request.Cookies.Add(FormsAuthentication.FormsAuthenticationCookieName, this.validCookieValue);
@@ -376,7 +376,7 @@ public void Should_not_set_user_in_context_with_invalid_hmac()
376376
var fakeMapper = A.Fake<IUserMapper>();
377377
var fakeUser = A.Fake<IUserIdentity>();
378378
fakeUser.UserName = "Bob";
379-
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid)).Returns(fakeUser);
379+
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid, this.context)).Returns(fakeUser);
380380
this.config.UserMapper = fakeMapper;
381381
FormsAuthentication.Enable(fakePipelines, this.config);
382382
this.context.Request.Cookies.Add(FormsAuthentication.FormsAuthenticationCookieName, this.cookieWithInvalidHmac);
@@ -393,7 +393,7 @@ public void Should_not_set_user_in_context_with_empty_hmac()
393393
var fakeMapper = A.Fake<IUserMapper>();
394394
var fakeUser = A.Fake<IUserIdentity>();
395395
fakeUser.UserName = "Bob";
396-
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid)).Returns(fakeUser);
396+
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid, this.context)).Returns(fakeUser);
397397
this.config.UserMapper = fakeMapper;
398398
FormsAuthentication.Enable(fakePipelines, this.config);
399399
this.context.Request.Cookies.Add(FormsAuthentication.FormsAuthenticationCookieName, this.cookieWithEmptyHmac);
@@ -410,7 +410,7 @@ public void Should_not_set_user_in_context_with_no_hmac()
410410
var fakeMapper = A.Fake<IUserMapper>();
411411
var fakeUser = A.Fake<IUserIdentity>();
412412
fakeUser.UserName = "Bob";
413-
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid)).Returns(fakeUser);
413+
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid, this.context)).Returns(fakeUser);
414414
this.config.UserMapper = fakeMapper;
415415
FormsAuthentication.Enable(fakePipelines, this.config);
416416
this.context.Request.Cookies.Add(FormsAuthentication.FormsAuthenticationCookieName, this.cookieWithNoHmac);
@@ -427,7 +427,7 @@ public void Should_not_set_username_in_context_with_broken_encryption_data()
427427
var fakeMapper = A.Fake<IUserMapper>();
428428
var fakeUser = A.Fake<IUserIdentity>();
429429
fakeUser.UserName = "Bob";
430-
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid)).Returns(fakeUser);
430+
A.CallTo(() => fakeMapper.GetUserFromIdentifier(this.userGuid, this.context)).Returns(fakeUser);
431431
this.config.UserMapper = fakeMapper;
432432
FormsAuthentication.Enable(fakePipelines, this.config);
433433
this.context.Request.Cookies.Add(FormsAuthentication.FormsAuthenticationCookieName, this.cookieWithBrokenEncryptedData);

Diff for: src/Nancy.Authentication.Forms/FormsAuthentication.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ private static Func<NancyContext, Response> GetLoadAuthenticationHook(FormsAuthe
164164

165165
if (userGuid != Guid.Empty)
166166
{
167-
context.CurrentUser =
168-
configuration.UserMapper.GetUserFromIdentifier(userGuid);
167+
168+
context.CurrentUser = configuration.UserMapper.GetUserFromIdentifier(userGuid, context);
169169
}
170170

171171
return null;

Diff for: src/Nancy.Authentication.Forms/IUsernameMapper.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public interface IUserMapper
1414
/// Get the real username from an indentifier
1515
/// </summary>
1616
/// <param name="identifier">User identifier</param>
17-
/// <returns>Matching username, or empty</returns>
18-
IUserIdentity GetUserFromIdentifier(Guid identifier);
17+
/// <param name="context">The current NancyFx context</param>
18+
/// <returns>Matching populated IUserIdentity object, or empty</returns>
19+
IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context);
1920
}
2021
}

Diff for: src/Nancy.Demo.Authentication.Forms/UserDatabase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static UserDatabase()
1818
users.Add(new Tuple<string, string, Guid>("user", "password", new Guid("56E1E49E-B7E8-4EEA-8459-7A906AC4D4C0")));
1919
}
2020

21-
public IUserIdentity GetUserFromIdentifier(Guid identifier)
21+
public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context)
2222
{
2323
var userRecord = users.Where(u => u.Item3 == identifier).FirstOrDefault();
2424

0 commit comments

Comments
 (0)