Commit 73a4f6cf authored by Petar Hristov's avatar Petar Hristov 💬
Browse files

Merge branch 'Issue/1833-newLogin' into 'uiv2'

BREAKING: New Login Behavior for UIv2 (coscine/issues#1833)

See merge request !104
parents d9ce047f ac03dac6
using System;
using System.Security.Claims;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http;
using Coscine.Api.STS.Utils;
using Coscine.ApiCommons;
using Coscine.Database.Models;
using System.Linq;
using Coscine.ActiveDirectory;
using System.Net.Http;
using System.Web;
using Newtonsoft.Json;
using System.Text;
using System.Collections.Generic;
using Coscine.JwtHandler;
namespace Coscine.Api.STS.Controllers
{
public class HomeController : Controller
{
private static HttpClient _httpClient = new HttpClient();
private static readonly HttpClient _httpClient = new();
public const string Action = "wa";
public const string SignInString = "wsignin1.0";
public const string SignOutString = "wsignout1.0";
//[Authorize]
[Route("/")]
public ActionResult Index()
{
if (User.Identity.IsAuthenticated)
{
var res = Request.Query[Action];
if (res.Count > 0)
{
var action = Request.Query[Action][0];
if (action == SignInString)
{
var userIdString = User.Identity.Name;
var userId = new Guid(userIdString);
var userIdString = User.Identity.Name;
var userId = new Guid(userIdString);
if (Request.Cookies.ContainsKey(MergeController.MERGETOKENKEY))
{
string mergeCallbackUrl = UrlGenerator.MergeCallbackRedirectUrl();
return Redirect(mergeCallbackUrl);
}
if (Request.Cookies.ContainsKey(MergeController.MERGETOKENKEY))
{
string mergeCallbackUrl = UrlGenerator.MergeCallbackRedirectUrl();
return Redirect(mergeCallbackUrl);
}
if (!AreTOSAccepted(userId))
{
string tosUrl = UrlGenerator.GetTOSUrl(Request, userIdString);
return Redirect(tosUrl);
}
if (!AreTOSAccepted(userId))
{
string tosUrl = UrlGenerator.GetTOSUrl(Request, userIdString);
return Redirect(tosUrl);
}
var reqFeature = HttpContext.Features.Get<IHttpRequestFeature>();
var rawTarget = reqFeature.RawTarget;
var formData = ProcessSignIn(new Uri(Program.MainUrl + rawTarget), User);
return new ContentResult() { Content = formData, ContentType = "text/html" };
}
ProcessSignIn(User);
var redirects = Request.Query["redirect"];
if (redirects.Count > 0)
{
ViewBag.Redirect = redirects[0];
}
else
{
ViewBag.Redirect = "/";
}
var dictionary = new Dictionary<string, string>
{
{ "UserId", userId.ToString() }
};
var jwtHandler = new JWTHandler(Program.Configuration);
ViewBag.Token = jwtHandler.GenerateJwtToken(dictionary);
// STS Exit Point
return View();
}
string loginUrl = UrlGenerator.GetLoginUrl(Request);
return Redirect(loginUrl);
......@@ -71,41 +71,12 @@ namespace Coscine.Api.STS.Controllers
&& tosAcceptedList.Any((entry) => entry.Version == currentTos);
}
private string ProcessSignIn(Uri url, ClaimsPrincipal user)
private void ProcessSignIn(ClaimsPrincipal user)
{
var userModel = new UserModel();
var realUser = userModel.GetById(Guid.Parse(user.Identity.Name));
ADHandler.AddUser(realUser, Program.Configuration);
var userObject = new
{
realUser.DisplayName,
realUser.Id,
realUser.EmailAddress
};
var signInInstance = new
{
User = new
{
Claims = user.Claims,
Identities = user.Identities,
Identitiy = user.Identity
},
UserObject = userObject
};
var uri = $"http://localhost:{Program.Configuration.GetStringAndWait("coscine/apis/Coscine.Api.LegacySTS/port")}/sts/processSignIn/{HttpUtility.UrlEncode(url.ToString())}/{HttpUtility.UrlEncode(Request.Host.Value)}";
var content = new StringContent(JsonConvert.SerializeObject(signInInstance, Formatting.Indented,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}),
Encoding.UTF8, "application/json");
var result = _httpClient.PostAsync(uri, content).Result;
return result.Content.ReadAsStringAsync().Result;
}
}
}
......@@ -34,40 +34,19 @@ namespace Coscine.Api.STS.Utils
public static string ExtendReturnUrl(string returnUrl, HttpRequest request)
{
string retString = returnUrl ?? "";
string localSharePointUrl = Program.Configuration.GetStringAndWait("coscine/local/sharepoint/additional/url");
if (!retString.Contains("?"))
{
retString += "?wa=wsignin1.0";
}
if (request.Query["wtrealm"].Count != 0 && !retString.Contains("wtrealm="))
{
retString += "&wtrealm=" + request.Query["wtrealm"][0];
}
else if (request.Query["wtrealm"].Count == 0 && !retString.Contains("wtrealm="))
{
retString += "&wtrealm=" + localSharePointUrl + "/_trust/default.aspx";
}
if (request.Query["wctx"].Count != 0 && !retString.Contains("wctx="))
if (request.Query["redirect"].Count != 0 && !retString.Contains("redirect="))
{
retString += "&wctx=" + request.Query["wctx"][0];
}
else if (request.Query["wctx"].Count == 0 && !retString.Contains("wctx="))
{
retString += "&wctx=" + localSharePointUrl + "/_layouts/15/Authenticate.aspx";
}
if (!retString.Contains("?"))
{
retString += "?";
if (request.Query["wreply"].Count != 0 && !retString.Contains("wreply="))
{
retString += "&wreply=" + request.Query["wreply"][0];
}
else if(request.Query["wreply"].Count == 0 && !retString.Contains("wreply="))
{
retString += "&wreply=" + localSharePointUrl + "/_trust/default.aspx";
}
else
{
retString += "&";
}
retString += "redirect=" + request.Query["redirect"][0];
}
return retString;
}
......
......@@ -7,7 +7,6 @@
<html>
<head>
<title>Login - Coscine</title>
<link rel="icon" href="/_layouts/15/images/Coscine.SharePoint.RWTHMaster/Favicon.png" />
<script src="/coscine/apps/i18n/js/app.js"></script>
</head>
<body class="fullwidth">
......
......@@ -6,7 +6,6 @@
<html>
<head>
<title>Logout - Coscine</title>
<link rel="icon" href="/_layouts/15/images/Coscine.SharePoint.RWTHMaster/Favicon.png" />
<script type="text/javascript">
var storedKeys = Object.keys(localStorage);
for (var i = 0; i < storedKeys.length; i++) {
......
......@@ -9,12 +9,10 @@
<html>
<head>
<title>Welcome - Coscine</title>
<link rel="icon" href="/_layouts/15/images/Coscine.SharePoint.RWTHMaster/Favicon.png" />
<script src="/coscine/apps/i18n/js/app.js"></script>
<script>
localStorage.setItem("coscine.authorization.bearer", "@Html.Raw(ViewBag.Token)");
window.location.href = "@Html.Raw(ViewBag.Redirect)";
</script>
</head>
<body>
<div>
<h1>Welcome @User.Identity.Name</h1>
</div>
</body>
</html>
\ No newline at end of file
<body></body>
</html>
......@@ -11,7 +11,6 @@
<head>
<title>Login - Coscine</title>
<link rel="icon" href="/_layouts/15/images/Coscine.SharePoint.RWTHMaster/Favicon.png" />
<script src="/coscine/apps/i18n/js/app.js"></script>
</head>
<body>
<div>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment