AccountController.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Web.Http;
  4. using Abp.UI;
  5. using Abp.Web.Models;
  6. using Abp.WebApi.Controllers;
  7. using WePlatform.Api.Models;
  8. using WePlatform.Authorization;
  9. using WePlatform.Authorization.Users;
  10. using WePlatform.MultiTenancy;
  11. using IwbZero;
  12. using IwbZero.Authorization.Base;
  13. using IwbZero.Authorization.Users;
  14. using Microsoft.Extensions.Internal;
  15. using Microsoft.Owin.Security;
  16. using Microsoft.Owin.Security.OAuth;
  17. namespace WePlatform.Api.Controllers
  18. {
  19. public class AccountController : AbpApiController
  20. {
  21. public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; }
  22. private readonly LogInManager _logInManager;
  23. static AccountController()
  24. {
  25. OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
  26. }
  27. public AccountController(LogInManager logInManager)
  28. {
  29. _logInManager = logInManager;
  30. LocalizationSourceName = IwbZeroConsts.LocalizationSourceName;
  31. }
  32. [HttpPost]
  33. public async Task<AjaxResponse> Authenticate(LoginModel loginModel)
  34. {
  35. CheckModelState();
  36. var loginResult = await GetLoginResultAsync(
  37. loginModel.UsernameOrEmailAddress,
  38. loginModel.Password,
  39. loginModel.TenancyName
  40. );
  41. var ticket = new AuthenticationTicket(loginResult.Identity, new AuthenticationProperties());
  42. var currentUtc = new SystemClock().UtcNow;
  43. ticket.Properties.IssuedUtc = currentUtc;
  44. ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(30));
  45. return new AjaxResponse(OAuthBearerOptions.AccessTokenFormat.Protect(ticket));
  46. }
  47. private async Task<IwbLoginResult<Tenant, User>> GetLoginResultAsync(string usernameOrEmailAddress, string password, string tenancyName)
  48. {
  49. var loginResult = await _logInManager.LoginAsync(usernameOrEmailAddress, password, tenancyName);
  50. switch (loginResult.Result)
  51. {
  52. case IwbLoginResultType.Success:
  53. return loginResult;
  54. default:
  55. throw CreateExceptionForFailedLoginAttempt(loginResult.Result, usernameOrEmailAddress, tenancyName);
  56. }
  57. }
  58. private Exception CreateExceptionForFailedLoginAttempt(IwbLoginResultType result, string usernameOrEmailAddress, string tenancyName)
  59. {
  60. switch (result)
  61. {
  62. case IwbLoginResultType.Success:
  63. return new ApplicationException("Don't call this method with a success result!");
  64. case IwbLoginResultType.InvalidUserNameOrEmailAddress:
  65. case IwbLoginResultType.InvalidPassword:
  66. return new UserFriendlyException(L("LoginFailed"), L("InvalidUserNameOrPassword"));
  67. case IwbLoginResultType.InvalidTenancyName:
  68. return new UserFriendlyException(L("LoginFailed"), L("ThereIsNoTenantDefinedWithName{0}", tenancyName));
  69. case IwbLoginResultType.TenantIsNotActive:
  70. return new UserFriendlyException(L("LoginFailed"), L("TenantIsNotActive", tenancyName));
  71. case IwbLoginResultType.UserIsNotActive:
  72. return new UserFriendlyException(L("LoginFailed"), L("UserIsNotActiveAndCanNotLogin", usernameOrEmailAddress));
  73. case IwbLoginResultType.UserEmailIsNotConfirmed:
  74. return new UserFriendlyException(L("LoginFailed"), "Your email address is not confirmed. You can not login"); //TODO: localize message
  75. default: //Can not fall to default actually. But other result types can be added in the future and we may forget to handle it
  76. Logger.Warn("Unhandled login fail reason: " + result);
  77. return new UserFriendlyException(L("LoginFailed"));
  78. }
  79. }
  80. protected virtual void CheckModelState()
  81. {
  82. if (!ModelState.IsValid)
  83. {
  84. throw new UserFriendlyException("Invalid request!");
  85. }
  86. }
  87. }
  88. }