Skip to content

Commit

Permalink
Merge pull request umbraco#11592 from rickbutterfield/feature/temp-11591
Browse files Browse the repository at this point in the history
v9: Fix for OAuth ExternalLogin
  • Loading branch information
bergmania authored Nov 15, 2021
2 parents c59d799 + fe396ba commit cabc303
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,11 @@ private async Task<IActionResult> ExternalSignInAsync(ExternalLoginInfo loginInf
// Failed only occurs when the user does not exist
errors.Add("The requested provider (" + loginInfo.LoginProvider + ") has not been linked to an account, the provider must be linked from the back office.");
}
else if (result == ExternalLoginSignInResult.NotAllowed)
{
// This occurs when the external provider has approved the login but custom logic in OnExternalLogin has denined it.
errors.Add($"The user {loginInfo.Principal.Identity.Name} for the external provider {loginInfo.ProviderDisplayName} has not been accepted and cannot sign in.");
}
else if (result == AutoLinkSignInResult.FailedNotLinked)
{
errors.Add("The requested provider (" + loginInfo.LoginProvider + ") has not been linked to an account, the provider must be linked from the back office.");
Expand Down
28 changes: 25 additions & 3 deletions src/Umbraco.Web.BackOffice/Security/BackOfficeSignInManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public async Task<SignInResult> ExternalLoginSignInAsync(ExternalLoginInfo login
var shouldSignIn = autoLinkOptions.OnExternalLogin(user, loginInfo);
if (shouldSignIn == false)
{
Logger.LogWarning("The AutoLinkOptions of the external authentication provider '{LoginProvider}' have refused the login based on the OnExternalLogin method. Affected user id: '{UserId}'", loginInfo.LoginProvider, user.Id);
LogFailedExternalLogin(loginInfo, user);
return ExternalLoginSignInResult.NotAllowed;
}
}

Expand Down Expand Up @@ -192,7 +193,16 @@ private async Task<SignInResult> AutoLinkAndSignInExternalAccount(ExternalLoginI
return AutoLinkSignInResult.FailedException(ex.Message);
}

return await LinkUser(autoLinkUser, loginInfo);
var shouldLinkUser = autoLinkOptions.OnExternalLogin == null || autoLinkOptions.OnExternalLogin(autoLinkUser, loginInfo);
if (shouldLinkUser)
{
return await LinkUser(autoLinkUser, loginInfo);
}
else
{
LogFailedExternalLogin(loginInfo, autoLinkUser);
return ExternalLoginSignInResult.NotAllowed;
}
}
else
{
Expand Down Expand Up @@ -225,7 +235,16 @@ private async Task<SignInResult> AutoLinkAndSignInExternalAccount(ExternalLoginI
}
else
{
return await LinkUser(autoLinkUser, loginInfo);
var shouldLinkUser = autoLinkOptions.OnExternalLogin == null || autoLinkOptions.OnExternalLogin(autoLinkUser, loginInfo);
if (shouldLinkUser)
{
return await LinkUser(autoLinkUser, loginInfo);
}
else
{
LogFailedExternalLogin(loginInfo, autoLinkUser);
return ExternalLoginSignInResult.NotAllowed;
}
}
}
}
Expand Down Expand Up @@ -264,5 +283,8 @@ private async Task<SignInResult> LinkUser(BackOfficeIdentityUser autoLinkUser, E
return AutoLinkSignInResult.FailedLinkingUser(errors);
}
}

private void LogFailedExternalLogin(ExternalLoginInfo loginInfo, BackOfficeIdentityUser user) =>
Logger.LogWarning("The AutoLinkOptions of the external authentication provider '{LoginProvider}' have refused the login based on the OnExternalLogin method. Affected user id: '{UserId}'", loginInfo.LoginProvider, user.Id);
}
}
15 changes: 15 additions & 0 deletions src/Umbraco.Web.BackOffice/Security/ExternalLoginSignInResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Identity;

namespace Umbraco.Cms.Web.BackOffice.Security
{
/// <summary>
/// Result returned from signing in when external logins are used.
/// </summary>
public class ExternalLoginSignInResult : SignInResult
{
public static ExternalLoginSignInResult NotAllowed { get; } = new ExternalLoginSignInResult()
{
Succeeded = false
};
}
}

0 comments on commit cabc303

Please sign in to comment.