/// <summary> /// The on action executing. /// </summary> /// <param name="filterContext"> /// The filter context. /// </param> public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); if (filterContext.RequestContext.HttpContext.Request.Cookies["OrionUser"] == null) { filterContext.Result = new RedirectResult("http://orion.shanecraven.com/Federation/Login?returnurl=http://zeus.shanecraven.com/Auth/Orion&appid=Zeus"); return; } var userKey = filterContext.RequestContext.HttpContext.Request.Cookies["OrionUser"].Value; var orion = new Orion(new DeleteMeLogger()); orion.Communicator.ApiAuthenticator = GetAuthenticatior(); var userProfile = Task.Run(() => orion.CreateUserController() .GetUserProfile(new Key() {ApiKey = userKey, Type = KeyType.UserTempKey}, "Zeus")); userProfile.Wait(); var userProfileWaited = userProfile.Result; if (userProfileWaited.Result?.Email == null || userProfileWaited.Result.Email.Equals(string.Empty)) { // filterContext.RequestContext.HttpContext.Request.Cookies["OrionUser"].Expires = DateTime.Now.AddDays(-1); var cookie = new HttpCookie("OrionUser") {Expires = DateTime.Now.AddDays(-1)}; filterContext.HttpContext.Response.Cookies.Add(cookie); filterContext.Result = new RedirectResult("http://orion.shanecraven.com/Federation/Login?returnurl=http://zeus.shanecraven.com/Auth/Orion&appid=Zeus"); return; } filterContext.HttpContext.Items["OrionUser"] = userProfileWaited.Result; filterContext.HttpContext.Items["OrionUserKey"] = userKey; }
/// <summary> /// Called when a request hits a filtered web API action. /// </summary> /// <param name="context"> /// The context. /// </param> /// <param name="cancellationToken"> /// The cancellation token. /// </param> /// <returns> /// The <see cref="Task"/>. /// </returns> public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken) { // Ensure that the auth header and scheme have been set if (context.Request.Headers.Authorization?.Parameter == null || context.Request.Headers.Authorization.Scheme == null) { this.HandleFailCase(context); return; } // Check that the correct scheme was used. if (!context.Request.Headers.Authorization.Scheme.Equals(Scheme)) { this.HandleFailCase(context); return; } var orionContext = new Orion(new DeleteMeLogger()) { Communicator = { ApiAuthenticator = new OrgStandardAuthenticator() { PublicKey = new Key() { ApiKey = this.publicKey, Type = KeyType.ApplicationPublicKey }, SecertKey = new Key() { ApiKey = this.secretKey, Type = KeyType.ApplicationSecretKey } } } }; // Call the Orion API and request the user information bound to the key! var profile = await orionContext.CreateUserController().GetUserProfileAsync(new Key() { ApiKey = context.Request.Headers.Authorization.Parameter, Type = KeyType.UserTempKey }, this.organisationPublicKey, this.allowRoamingUser); if (profile.Result != null) { var principal = new OrionPrincipal(context.Request.Headers.Authorization.Parameter) { User = profile.Result }; context.Principal = principal; } else { this.HandleFailCase(context); } }
/// <summary> /// The handle request. /// </summary> /// <param name="request"> /// The requst. /// </param> /// <param name="response"> /// The response. /// </param> /// <returns> /// The <see cref="Task"/>. /// </returns> public async Task HandleRequest(IServerMessage request, IServerMessage response) { var userOrionKey = ParserFactory.GetDataParser(request.DataType).ParseData <Key>(request.User); if (userOrionKey == null) { // Do something here to stop processing and return some error to the server to // inform the client that auth has failed. request.User = null; await this.Successor.HandleRequest(request, response); } var orionContext = new Orion(new DeleteMeLogger()) { Communicator = { ApiAuthenticator = new OrgStandardAuthenticator() { PublicKey = this.Config.SystemPublicKey, SecertKey = this.Config.SystemSecretKey } } }; // Set the authentication Information var user = await orionContext.CreateUserController().GetUserProfile(userOrionKey, "Zeus"); if (user?.Result?.Meta == null || !user.Result.Meta.Any(x => x.Key.Equals("UserType"))) { request.User = null; } else { // Parse the Orion user data into local user data var firstOrDefault = user.Result.Meta.FirstOrDefault(x => x.Key.Equals("UserType")); if (firstOrDefault != null) { var parsedUser = UserFactory.GetUser( (UserType) (Convert.ToInt32(firstOrDefault.Value))); parsedUser.Email = user.Result.Email; parsedUser.Firstname = user.Result.Firstname; parsedUser.Surname = user.Result.Surname; parsedUser.Phone = user.Result.Phone; request.User = ParserFactory.GetDataParser(request.DataType).SerializeData(parsedUser); } } // Check the user type which should be contained in User Meta. await this.Successor.HandleRequest(request, response); }
/// <summary> /// The handle request. /// </summary> /// <param name="request"> /// The requst. /// </param> /// <param name="response"> /// The response. /// </param> /// <returns> /// The <see cref="Task"/>. /// </returns> public async Task HandleRequest(IServerMessage request, IServerMessage response) { var userOrionKey = ParserFactory.GetDataParser(request.DataType).ParseData<Key>(request.User); if (userOrionKey == null) { // Do something here to stop processing and return some error to the server to // inform the client that auth has failed. request.User = null; await this.Successor.HandleRequest(request, response); } var orionContext = new Orion(new DeleteMeLogger()) { Communicator = { ApiAuthenticator = new OrgStandardAuthenticator() { PublicKey = this.Config.SystemPublicKey, SecertKey = this.Config.SystemSecretKey } } }; // Set the authentication Information var user = await orionContext.CreateUserController().GetUserProfile(userOrionKey, "Zeus"); if (user?.Result?.Meta == null || !user.Result.Meta.Any(x => x.Key.Equals("UserType"))) { request.User = null; } else { // Parse the Orion user data into local user data var firstOrDefault = user.Result.Meta.FirstOrDefault(x => x.Key.Equals("UserType")); if (firstOrDefault != null) { var parsedUser = UserFactory.GetUser( (UserType) (Convert.ToInt32(firstOrDefault.Value))); parsedUser.Email = user.Result.Email; parsedUser.Firstname = user.Result.Firstname; parsedUser.Surname = user.Result.Surname; parsedUser.Phone = user.Result.Phone; request.User = ParserFactory.GetDataParser(request.DataType).SerializeData(parsedUser); } } // Check the user type which should be contained in User Meta. await this.Successor.HandleRequest(request, response); }
/// <summary> /// The on action executing. /// </summary> /// <param name="filterContext"> /// The filter context. /// </param> public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); if (filterContext.RequestContext.HttpContext.Request.Cookies["OrionUser"] == null) { filterContext.Result = new RedirectResult("http://orion.shanecraven.com/Federation/Login?returnurl=http://zeus.shanecraven.com/Auth/Orion&appid=Zeus"); return; } var userKey = filterContext.RequestContext.HttpContext.Request.Cookies["OrionUser"].Value; var orion = new Orion(new DeleteMeLogger()); orion.Communicator.ApiAuthenticator = GetAuthenticatior(); var userProfile = Task.Run(() => orion.CreateUserController() .GetUserProfile(new Key() { ApiKey = userKey, Type = KeyType.UserTempKey }, "Zeus")); userProfile.Wait(); var userProfileWaited = userProfile.Result; if (userProfileWaited.Result?.Email == null || userProfileWaited.Result.Email.Equals(string.Empty)) { // filterContext.RequestContext.HttpContext.Request.Cookies["OrionUser"].Expires = DateTime.Now.AddDays(-1); var cookie = new HttpCookie("OrionUser") { Expires = DateTime.Now.AddDays(-1) }; filterContext.HttpContext.Response.Cookies.Add(cookie); filterContext.Result = new RedirectResult("http://orion.shanecraven.com/Federation/Login?returnurl=http://zeus.shanecraven.com/Auth/Orion&appid=Zeus"); return; } filterContext.HttpContext.Items["OrionUser"] = userProfileWaited.Result; filterContext.HttpContext.Items["OrionUserKey"] = userKey; }
/// <summary> /// Authenticate a request with Orion federation /// </summary> /// <param name="filterContext"> /// The filter context. /// </param> public void OnAuthentication(AuthenticationContext filterContext) { if (filterContext.HttpContext.Request.QueryString["orion_logout"] != null) { if (filterContext.HttpContext.Request.QueryString["orion_logout"].Equals("true")) { this.DeleteOrionCookie(filterContext.HttpContext.Request, filterContext.HttpContext.Response); var returnFrom = string.Empty; if (filterContext.HttpContext.Request.QueryString["return_from"] != null) { returnFrom = "?returnUrl=" + filterContext.HttpContext.Request.QueryString["return_from"]; } filterContext.Result = new RedirectResult($"{this.federationServer}/Logout{Uri.EscapeDataString(returnFrom)}"); return; } } string orionAuthKey = null; var authKeyPresent = false; if (filterContext.HttpContext.Request.QueryString["orion_key"] != null) { authKeyPresent = true; orionAuthKey = filterContext.HttpContext.Request.QueryString["orion_key"]; } else { orionAuthKey = this.ExtractOrionCookie(filterContext.HttpContext.Request); } if (orionAuthKey == null) { this.HandleAuthenticationFailed(filterContext, authKeyPresent); return; } var orion = new Orion(new DeleteMeLogger()) { Communicator = { ApiAuthenticator = new OrgStandardAuthenticator() { PublicKey = new Key() { ApiKey = this.publicKey, Type = KeyType.ApplicationPublicKey }, SecertKey = new Key() { ApiKey = this.secretKey, Type = KeyType.ApplicationSecretKey } } } }; var userProfileTask = Task.Run(() => orion.CreateUserController().GetUserProfileAsync(new Key() { ApiKey = orionAuthKey, Type = KeyType.UserTempKey }, this.applicationId, this.federationMode == FederationMode.RoamEnabled || this.federationMode == FederationMode.RoamOnly)); userProfileTask.Wait(30000); var userProfile = userProfileTask.Result; if (userProfile.Result == null) { this.HandleAuthenticationFailed(filterContext, authKeyPresent); return; } if (this.ExtractOrionCookie(filterContext.HttpContext.Request) == null || authKeyPresent) { var cookie = new HttpCookie("OrionFederationKey") { Expires = DateTime.Now.AddDays(5), Value = orionAuthKey }; filterContext.HttpContext.Response.Cookies.Add(cookie); } if (authKeyPresent) { this.RefreshKeyUrl(filterContext); return; } filterContext.HttpContext.Items["FederationStatus"] = new FederationStatus() { Authenticated = true, IsOptional = this.isOptional }; filterContext.Principal = new OrionPrincipal(orionAuthKey) { User = userProfile.Result, UserKey = new Key() { ApiKey = orionAuthKey, Type = KeyType.UserTempKey } }; }