146 lines
5.4 KiB
C#
146 lines
5.4 KiB
C#
using System;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Web.Http;
|
|
using Kreta.BusinessLogic.Helpers;
|
|
using Kreta.BusinessLogic.Security;
|
|
using Kreta.BusinessLogic.Utils;
|
|
using Kreta.Core.ConnectionType;
|
|
using Kreta.Framework;
|
|
using Kreta.Framework.Security;
|
|
using Kreta.User.WebApi.Attributes;
|
|
using Kreta.User.WebApi.Infrastructure;
|
|
using static Kreta.Core.Constants;
|
|
|
|
namespace Kreta.User.WebApi.Controllers
|
|
{
|
|
[ApiKeyAuthorization]
|
|
[RoutePrefix(Constants.RoutePrefix + Constants.VersionName)]
|
|
public class AuthenticationController : ApiController
|
|
{
|
|
[Route("authenticate")]
|
|
[HttpPost]
|
|
public IHttpActionResult Authenticate(AuthenticationRequest authenticationRequest)
|
|
{
|
|
var organizationIdentifier = CommonUtils.GetOrganizationIdentifier();
|
|
AuthenticationInfo authenticationInfo;
|
|
|
|
try
|
|
{
|
|
authenticationInfo = !string.IsNullOrWhiteSpace(authenticationRequest.Password)
|
|
? AuthenticateByUserNameAndPassword(authenticationRequest.UserName, authenticationRequest.Password, organizationIdentifier)
|
|
: AuthenticateByUserNameAndUniqueId(authenticationRequest.IdpUniqueId, organizationIdentifier);
|
|
|
|
}
|
|
catch (FrameworkException ex) when (ex is UserNotFoundException || ex is NextTanevBelepesException || ex is ArchivBelepesException)
|
|
{
|
|
return Unauthorized();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return InternalServerError(ex);
|
|
}
|
|
|
|
if (authenticationInfo == null)
|
|
{
|
|
return Unauthorized();
|
|
}
|
|
|
|
return Ok(authenticationInfo);
|
|
}
|
|
|
|
[Route("status")]
|
|
[HttpGet]
|
|
public IHttpActionResult Status()
|
|
{
|
|
return Ok("IAmAlive");
|
|
}
|
|
|
|
[Route("changePassword")]
|
|
[HttpPost]
|
|
public IHttpActionResult ChangePassword(ChangePasswordRequest changePasswordRequest)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
try
|
|
{
|
|
var organizationIdentifier = CommonUtils.GetOrganizationIdentifier();
|
|
|
|
var loginInfo = new FelhasznaloBelepesHelper(new OrganizationConnectionType(0, 0, organizationIdentifier, 0)).AuthenticateUser(changePasswordRequest.Username, changePasswordRequest.Password);
|
|
|
|
if (loginInfo == null || loginInfo.PasswordState == PasswordState.INVALID)
|
|
{
|
|
throw new IncorrectPasswordException(loginInfo.BelepesiNev);
|
|
}
|
|
|
|
new FelhasznaloBelepesHelper(new MobileConnectionType(loginInfo.FelhasznaloId, loginInfo.IntezmenyId, loginInfo.IntezmenyAzonosito, loginInfo.AktivTanevId))
|
|
.ChangeUserPassword(changePasswordRequest.Username, changePasswordRequest.NewPassword);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return InternalServerError(ex);
|
|
}
|
|
|
|
return Ok();
|
|
}
|
|
|
|
private AuthenticationInfo AuthenticateByUserNameAndPassword(string userName, string password, string organizationIdentifier)
|
|
{
|
|
var loginInfo = new FelhasznaloBelepesHelper(new OrganizationConnectionType(0, 0, organizationIdentifier, 0)).AuthenticateUser(userName, password);
|
|
|
|
if (loginInfo == null || loginInfo.PasswordState == PasswordState.INVALID)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var authenticationInfo = new AuthenticationHelper(new MobileConnectionType(loginInfo.FelhasznaloId, loginInfo.IntezmenyId, loginInfo.IntezmenyAzonosito, loginInfo.AktivTanevId)).GetAuthenticationInfo(loginInfo, intezmenyiDictionaryEnvironmentName: string.Empty);
|
|
|
|
return authenticationInfo;
|
|
}
|
|
|
|
private AuthenticationInfo AuthenticateByUserNameAndUniqueId(Guid idpUniqueId, string organizationIdentifier)
|
|
{
|
|
var loginInfo = new FelhasznaloBelepesHelper(new OrganizationConnectionType(0, 0, organizationIdentifier, 0)).GetLoginInfo(bejelentkezesiNev: null, idpUniqueId);
|
|
|
|
if (loginInfo == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var authenticationInfo = new AuthenticationHelper(new MobileConnectionType(loginInfo.FelhasznaloId, loginInfo.IntezmenyId, loginInfo.IntezmenyAzonosito, loginInfo.AktivTanevId)).GetAuthenticationInfo(loginInfo, intezmenyiDictionaryEnvironmentName: string.Empty);
|
|
|
|
var userUniqueId = authenticationInfo.TutelaryIdpUniqueId ?? authenticationInfo.InstituteUserIdpUniqueId;
|
|
|
|
if (idpUniqueId != userUniqueId)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return authenticationInfo;
|
|
}
|
|
|
|
public class AuthenticationRequest
|
|
{
|
|
public string UserName { get; set; }
|
|
|
|
public string Password { get; set; }
|
|
|
|
public Guid IdpUniqueId { get; set; }
|
|
}
|
|
|
|
public class ChangePasswordRequest
|
|
{
|
|
[Required]
|
|
public string Username { get; set; }
|
|
|
|
[Required]
|
|
public string Password { get; set; }
|
|
|
|
[Required]
|
|
[StringLength(General.JelszoMaximumKarakterekSzama, MinimumLength = General.JelszoMinimumKarakterekSzama)]
|
|
public string NewPassword { get; set; }
|
|
}
|
|
}
|
|
}
|