Skip to content

Commit

Permalink
Add GetConfiguredClients() and HasLocalAccount() methods to OAuthWebS…
Browse files Browse the repository at this point in the history
…ecurity.
  • Loading branch information
dotnetjunky committed May 1, 2012
1 parent d9ebca2 commit 8900271
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/Microsoft.Web.WebPages.OAuth/OAuthWebSecurity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -120,6 +122,16 @@ public static void RegisterClient(IAuthenticationClient client)
_authenticationClients.Add(client);
}

/// <summary>
/// Gets the registered OAuthen &amp; OpenID clients.
/// </summary>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification="This operation can be expensive because it queries from database.")]
public static ICollection<IAuthenticationClient> GetRegisteredClients()
{
return new ReadOnlyCollection<IAuthenticationClient>(_authenticationClients);
}

/// <summary>
/// Requests the specified provider to start the authentication by directing users to an external website
/// </summary>
Expand Down Expand Up @@ -259,6 +271,20 @@ public static ICollection<OAuthAccount> GetAccountsFromUserName(string userName)
return provider.GetAccountsForUser(userName).Select(p => new OAuthAccount(p.Provider, p.ProviderUserId)).ToList();
}

/// <summary>
/// Determines whether there exists a local account (as opposed to OAuth account) with the specified userId.
/// </summary>
/// <param name="userId">The user id to check for local account.</param>
/// <returns>
/// <c>true</c> if there is a local account with the specified user id]; otherwise, <c>false</c>.
/// </returns>
public static bool HasLocalAccount(int userId)
{
ExtendedMembershipProvider provider = VerifyProvider();
Debug.Assert(provider != null); // VerifyProvider checks this
return provider.HasLocalAccount(userId);
}

/// <summary>
/// Delete the specified OAuth &amp; OpenID account
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/WebMatrix.WebData/ExtendedMembershipProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public abstract class ExtendedMembershipProvider : MembershipProvider
/// <returns></returns>
public abstract string GetUserNameFromId(int userId);

/// <summary>
/// Determines whether there exists a local account (as opposed to OAuth account) with the specified userId.
/// </summary>
/// <param name="userId">The user id to check for local account.</param>
/// <returns>
/// <c>true</c> if there is a local account with the specified user id]; otherwise, <c>false</c>.
/// </returns>
public virtual bool HasLocalAccount(int userId)
{
throw new NotImplementedException();
}

/// <summary>
/// Gets all OAuth accounts associated with the specified username
/// </summary>
Expand Down
20 changes: 19 additions & 1 deletion src/WebMatrix.WebData/SimpleMembershipProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,5 +1133,23 @@ public override ICollection<OAuthAccountData> GetAccountsForUser(string userName

return new OAuthAccountData[0];
}

/// <summary>
/// Determines whether there exists a local account (as opposed to OAuth account) with the specified userId.
/// </summary>
/// <param name="userId">The user id to check for local account.</param>
/// <returns>
/// <c>true</c> if there is a local account with the specified user id]; otherwise, <c>false</c>.
/// </returns>
public override bool HasLocalAccount(int userId)
{
VerifyInitialized();

using (var db = ConnectToDatabase())
{
dynamic id = db.QueryValue(@"SELECT UserId FROM [" + MembershipTableName + "] WHERE UserId=@0", userId);
return id != null;
}
}
}
}
}
27 changes: 27 additions & 0 deletions test/Microsoft.Web.WebPages.OAuth.Test/OAuthWebSecurityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
Expand Down Expand Up @@ -111,6 +112,32 @@ public void RegisterOpenIDClient()
}
}

[Fact]
public void GetRegisteredClientsReturnTheCorrectSet()
{
// Arrange
OAuthWebSecurity.RegisterOpenIDClient(BuiltInOpenIDClient.Google);
OAuthWebSecurity.RegisterOpenIDClient(BuiltInOpenIDClient.Yahoo);

OAuthWebSecurity.RegisterOAuthClient(BuiltInOAuthClient.Facebook, "123", "456");
OAuthWebSecurity.RegisterOAuthClient(BuiltInOAuthClient.LinkedIn, "142", "4312");

var client = new Mock<IAuthenticationClient>();
client.Setup(c => c.ProviderName).Returns("awesome provider");
OAuthWebSecurity.RegisterClient(client.Object);

// Act
var results = OAuthWebSecurity.GetRegisteredClients().ToList();

// Assert
Assert.Equal(5, results.Count);
Assert.Equal("google", results[0].ProviderName);
Assert.Equal("yahoo", results[1].ProviderName);
Assert.Equal("facebook", results[2].ProviderName);
Assert.Equal("linkedIn", results[3].ProviderName);
Assert.Equal("awesome provider", results[4].ProviderName);
}

[Fact]
public void RequestAuthenticationRedirectsToProviderWithNullReturnUrl()
{
Expand Down

0 comments on commit 8900271

Please sign in to comment.