-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gallery constraints to prevent Organization authentication (#4915)
- Loading branch information
1 parent
42ce453
commit 5f88eec
Showing
10 changed files
with
190 additions
and
24 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
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,51 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Linq; | ||
using System.Net.Mail; | ||
using System.Security.Claims; | ||
using System.Security.Principal; | ||
using NuGetGallery.Authentication; | ||
|
||
namespace NuGetGallery | ||
{ | ||
/// <summary> | ||
/// Extension methods for the NuGetGallery.User entity. | ||
/// </summary> | ||
public static class UserExtensions | ||
{ | ||
/// <summary> | ||
/// Convert a User's email to a System.Net MailAddress. | ||
/// </summary> | ||
public static MailAddress ToMailAddress(this User user) | ||
{ | ||
if (!user.Confirmed) | ||
{ | ||
return new MailAddress(user.UnconfirmedEmailAddress, user.Username); | ||
} | ||
|
||
return new MailAddress(user.EmailAddress, user.Username); | ||
} | ||
|
||
/// <summary> | ||
/// Get the current API key credential, if available. | ||
/// </summary> | ||
public static Credential GetCurrentApiKeyCredential(this User user, IIdentity identity) | ||
{ | ||
var claimsIdentity = identity as ClaimsIdentity; | ||
var apiKey = claimsIdentity.GetClaimOrDefault(NuGetClaims.ApiKey); | ||
|
||
return user.Credentials.FirstOrDefault(c => c.Value == apiKey); | ||
} | ||
|
||
/// <summary> | ||
/// Determines if the User (account) belongs to an organization. | ||
/// </summary> | ||
/// <param name="account">User (account) instance.</param> | ||
/// <returns>True for organizations, false for users.</returns> | ||
public static bool IsOrganization(this User account) | ||
{ | ||
return account.Organization != null; | ||
} | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
|
@@ -21,13 +21,15 @@ public class Fakes | |
|
||
public Fakes() | ||
{ | ||
var credentialBuilder = new CredentialBuilder(); | ||
|
||
User = new User("testUser") | ||
{ | ||
Key = 40, | ||
EmailAddress = "[email protected]", | ||
Credentials = new List<Credential> | ||
{ | ||
new CredentialBuilder().CreatePasswordCredential(Password), | ||
credentialBuilder.CreatePasswordCredential(Password), | ||
TestCredentialHelper.CreateV1ApiKey(Guid.Parse("669e180e-335c-491a-ac26-e83c4bd31d65"), | ||
ExpirationForApiKeyV1), | ||
TestCredentialHelper.CreateV2ApiKey(Guid.Parse("779e180e-335c-491a-ac26-e83c4bd31d87"), | ||
|
@@ -37,6 +39,31 @@ public Fakes() | |
} | ||
}; | ||
|
||
Organization = new User("testOrganization") | ||
{ | ||
Key = 41, | ||
EmailAddress = "[email protected]", | ||
Organization = new Organization() | ||
{ | ||
Key = 1 | ||
}, | ||
// invalid credentials for testing authentication constraints | ||
Credentials = new List<Credential> | ||
{ | ||
credentialBuilder.CreatePasswordCredential(Password) | ||
} | ||
}; | ||
|
||
Organization.Organization.Memberships = new List<Membership>() | ||
{ | ||
new Membership | ||
{ | ||
Organization = Organization.Organization, | ||
Member = User, | ||
IsAdmin = true | ||
} | ||
}; | ||
|
||
Pbkdf2User = new User("testPbkdf2User") | ||
{ | ||
Key = 41, | ||
|
@@ -90,6 +117,8 @@ public Fakes() | |
|
||
public User User { get; } | ||
|
||
public User Organization { get; } | ||
|
||
public User ShaUser { get; } | ||
|
||
public User Pbkdf2User { get; } | ||
|