示例#1
0
        /// <summary>
        /// Gets the session identity.
        /// </summary>
        /// <returns>The session identity.</returns>
        /// <param name="ctx">Context.</param>
        public static CustomSessionIdentity GetSessionIdentity(System.Net.HttpListenerContext ctx)
        {
            CustomSessionIdentity ret;

            System.Net.CookieCollection coll = ctx.Request.Cookies;

            if (coll[RESTKeys.SessionCookieId] == null ||
                string.IsNullOrEmpty(coll[RESTKeys.SessionCookieId].Value))
            {
                //needs a new identity
                ret = CustomSessionIdentity.CreateNew();
            }
            else if (coll[RESTKeys.SessionCookieId].Expired)
            {
                //needs a new identity
                ret = CustomSessionIdentity.CreateNew();
            }
            else
            {
                //needs an existing identity
                ret = CustomSessionIdentity.Retrieve(coll[RESTKeys.SessionCookieId].Value,
                                                     coll[RESTKeys.SessionCookieToken].Value);
            }
            foreach (System.Net.Cookie c in ret.GetCookies())
            {
                ctx.Response.Cookies.Add(c);
            }
            ret.ctx = ctx;
            return(ret);
        }
示例#2
0
        /// <summary>
        /// HTTPs the request handler.
        /// </summary>
        /// <returns>The request handler.</returns>
        /// <param name="ctx">Context.</param>
        public static object HTTPRequestHandler(HttpListenerContext ctx)
        {
            switch (ctx.Request.HttpMethod)
            {
            case "GET":
                return(MainClass.HandleGETRequest(ctx));

            case "POST":
                return(MainClass.HandlePOSTRequest(ctx, CustomSessionIdentity.GetSessionIdentity(ctx)));

            case "HEAD":
                return("");

            case "OPTIONS":
                ctx.Response.AddHeader("Allow", "GET,HEAD,POST,OPTIONS");
                return("");

            case "PUT":
            case "DELETE":
            case "TRACE":
            case "CONNECT":
            case "PATCH":
            default:
                ctx.Response.StatusCode = 405;
                return("NOT ALLOWED");
            }
        }
示例#3
0
        /// <summary>
        /// Handles the GET request.
        /// </summary>
        /// <returns>
        /// The GET request.
        /// </returns>
        /// <param name='ctx'>
        /// Context.
        /// </param>
        public static object HandleGETRequest(HttpListenerContext ctx)
        {
            if (!string.IsNullOrEmpty(ctx.Request.Headers [RESTKeys.RESTHandlerRequest]))
            {
                if (!RESTCallbackManager.Instance.GETCallbacks.ContainsKey(ctx.Request.Headers [RESTKeys.RESTHandlerRequest]))
                {
                    ctx.Response.StatusCode = 404;
                    return(HttpResponse.NewResponse().AddErrorCode(GuruMeditation.ErrorCode.RESTHandlerRequestNotFound).ToJSON());
                }

                // Is a REST API request
                SessionIdentity             si      = CustomSessionIdentity.GetSessionIdentity(ctx);
                Dictionary <string, string> Headers = UtilitiesManager.Instance.GetHeaders(ctx);

                // This is where the string specified in the REQUEST header is matched against the
                // GET REST API mappings
                HttpResponse ret = RESTCallbackManager.Instance.GETCallbacks [ctx.Request.Headers [RESTKeys.RESTHandlerRequest]] (Headers, si);
                ctx.Response.ContentType = "application/json; charset=utf-8";
                if (ret.ErrorCode.Count() > 0)
                {
                    ctx.Response.StatusCode = 500;
                }
                return(ret.ToJSON());
            }

            else
            {
                // Not a REST API Request
                string route = RouterManager.Instance.FindRoute(ctx.Request.Url).FirstOrDefault();

                if (!string.IsNullOrEmpty(route) && ContentManager.Instance.content.ContainsKey(route))
                {
                    var item = ContentManager.Instance.content[route];
                    ctx.Response.ContentType     = item.ContentType.MediaType;
                    ctx.Response.ContentEncoding = System.Text.Encoding.UTF8;
                    return(item);
                }
                else if (!string.IsNullOrEmpty(route) && RESTCallbackManager.Instance.GETCallbacks.ContainsKey(route))
                {
                    SessionIdentity             si      = CustomSessionIdentity.GetSessionIdentity(ctx);
                    Dictionary <string, string> Headers = UtilitiesManager.Instance.GetHeaders(ctx);

                    // This is where the string specified in the REQUEST header is matched against the
                    // GET REST API mappings
                    HttpResponse ret = RESTCallbackManager.Instance.GETCallbacks [route] (Headers, si);
                    //ctx.Response.ContentType = "application/json; charset=utf-8";
                    if (ret.ErrorCode.Count() > 0)
                    {
                        ctx.Response.StatusCode = 500;
                    }
                    ctx.Response.Redirect("/");
                    return("");
                }
                else
                {
                    ctx.Response.StatusCode = 404;
                    return("NOT FOUND");
                }
            }
        }
示例#4
0
        /// <summary>
        /// Creates the account.
        /// </summary>
        /// <returns>The account.</returns>
        /// <param name="headers">Headers.</param>
        /// <param name="si">Si.</param>
        /// <param name="postData">Post data.</param>
        public HttpResponse CreateAccount(Dictionary <string, string> headers, CustomSessionIdentity si, Dictionary <string, string> postData)
        {
            if (si.Guest)
            {
                var result = CustomUserAccount.UserNameExists(postData["USERNAME"]);

                if (result == BackendQueryStatus.ReturnCode.Exists)
                {
                    return(HttpResponse.NewResponse().AddPOSTDataError(
                               new POSTDataError
                    {
                        FieldName = "USERNAME",
                        Error = "User already exists"
                    }));
                }
                else if (result != BackendQueryStatus.ReturnCode.DoesntExist)
                {
                    return(HttpResponse.NewResponse().AddErrorCode(GuruMeditation.ErrorCode.BackendUserQueryError));
                }

                DomainObjectRepository <DomainObjectBase> repo = new DomainObjectRepository <DomainObjectBase>(si);

                CustomUserAccount zlu = new CustomUserAccount
                {
                    Email = postData["EMAIL"],
                    //FirstName = postData["FIRSTNAME"],
                    //LastName = postData["LASTNAME"],
                    LinkCategories = new string[] { "default" },
                    UserName       = postData["USERNAME"],
                    Password       = BCrypt.Net.BCrypt.HashPassword(postData["PASSWORD"]),
                    Following      = new string[] {},
                };

                repo.Add(zlu);

                if (repo.Save().Any(q => q != BackendQueryStatus.ReturnCode.Success))
                {
                    //TODO Rollback
                    return(HttpResponse.NewResponse().AddErrorCode(GuruMeditation.ErrorCode.BackendUserQueryError));
                }

                return(HttpResponse.NewResponse().AddData(zlu.GetClientSideSafeUserObject()));
            }
            else
            {
                return(HttpResponse.NewResponse().AddErrorCode(GuruMeditation.ErrorCode.UserAlreadyAuthenticated));
            }
        }
示例#5
0
 /// <summary>
 /// Gets the state of the session.
 /// </summary>
 /// <returns>The session state.</returns>
 /// <param name="headers">Headers.</param>
 /// <param name="si">Si.</param>
 public HttpResponse GetSessionState(Dictionary <string, string> headers, CustomSessionIdentity si)
 {
     if (si.Guest)
     {
         CustomUserAccount zlu = new CustomUserAccount();
         zlu.UserName = "******";
         zlu.ObjectID = System.Guid.Empty;
         return(HttpResponse.NewResponse().AddData(zlu));
     }
     else
     {
         CustomUserAccount zlu = CustomUserAccount.Retrieve(si);
         if (zlu != null)
         {
             return(HttpResponse.NewResponse().AddData(zlu.GetClientSideSafeUserObject()));
         }
         else
         {
             return(HttpResponse.NewResponse().AddErrorCode(GuruMeditation.ErrorCode.BackendSessionQueryError));
         }
     }
 }
示例#6
0
 /// <summary>
 /// Authenticates from facebook.
 /// </summary>
 /// <returns>The from facebook.</returns>
 /// <param name="headers">Headers.</param>
 /// <param name="si">Si.</param>
 /// <param name="postData">Post data.</param>
 public HttpResponse AuthenticateFromFacebook(Dictionary <string, string> headers, CustomSessionIdentity si, Dictionary <string, string> postData)
 {
     return(HttpResponse.NewResponse().AddErrorCode(GuruMeditation.ErrorCode.NotImplemented));
 }
示例#7
0
        /// <summary>
        /// Authenticates from local.
        /// </summary>
        /// <returns>The from local.</returns>
        /// <param name="headers">Headers.</param>
        /// <param name="si">Si.</param>
        /// <param name="postData">Post data.</param>
        public HttpResponse AuthenticateFromLocal(Dictionary <string, string> headers, CustomSessionIdentity si, Dictionary <string, string> postData)
        {
            if (si.Guest)
            {
                string username = postData["USERNAME"];
                string password = postData["PASSWORD"];

                CustomUserAccount zlu = CustomUserAccount.Retrieve(username, si);
                if (zlu == null)
                {
                    return(HttpResponse.NewResponse().AddPOSTDataError(
                               new POSTDataError
                    {
                        FieldName = "USERNAME",
                        Error = "is not valid"
                    }));
                }

                if (BCrypt.Net.BCrypt.Verify(password, zlu.Password))
                {
                    var newSessionIdentity = SessionManager.Instance.CreateAuthenticatedUserSessionIdentity(zlu, si);
                    if (newSessionIdentity == null)
                    {
                        return(HttpResponse.NewResponse().AddErrorCode(GuruMeditation.ErrorCode.BackendSessionQueryError));
                    }

                    return(HttpResponse.NewResponse().AddData(zlu.GetClientSideSafeUserObject()));
                }
                else
                {
                    return(HttpResponse.NewResponse().AddPOSTDataError(
                               new POSTDataError
                    {
                        FieldName = "PASSWORD",
                        Error = "did not match"
                    }));
                }
            }
            else
            {
                return(HttpResponse.NewResponse().AddErrorCode(GuruMeditation.ErrorCode.UserAlreadyAuthenticated));
            }
        }
示例#8
0
 /// <summary>
 /// Logouts the user.
 /// </summary>
 /// <returns>The user.</returns>
 /// <param name="headers">Headers.</param>
 /// <param name="si">Si.</param>
 public HttpResponse LogoutUser(Dictionary <string, string> headers, CustomSessionIdentity si)
 {
     return(HttpResponse.NewResponse().AddErrorCode(GuruMeditation.ErrorCode.NotImplemented));
 }
示例#9
0
 /// <summary>
 /// Deletes the account.
 /// </summary>
 /// <returns>The account.</returns>
 /// <param name="headers">Headers.</param>
 /// <param name="si">Si.</param>
 /// <param name="postData">Post data.</param>
 public HttpResponse DeleteAccount(Dictionary <string, string> headers, CustomSessionIdentity si, Dictionary <string, string> postData)
 {
     return(HttpResponse.NewResponse().AddErrorCode(GuruMeditation.ErrorCode.NotImplemented));
 }
示例#10
0
 /// <summary>
 /// Creates this instance
 /// </summary>
 public override de.netcrave.nMVC.Models.DomainObjectBase Create()
 {
     return(CustomSessionIdentity.CreateNew());
 }
示例#11
0
 /// <summary>
 /// Retrieve the specified si.
 /// </summary>
 /// <param name="si">Si.</param>
 public static CustomUserAccount Retrieve(CustomSessionIdentity si)
 {
     throw new NotImplementedException();
 }