Beispiel #1
0
 static void InstantiateQuizzDatabase()
 {
     StudyBuddyDbAssistant.CreateDatabase("studybuddyquizzes");
     StudyBuddyDbAssistant.CreateQuizzTables("studybuddyquizzes");
 }
        public UserAuthorizationController(ITokenizer tokenizer)
        {
            Post["/login/"] = x =>
            {
                using (var dbWrapper = new DbWrapper("AuthenticationDbCore"))
                {
                    if (!dbWrapper.DoesDbExist())
                    {
                        StudyBuddyDbAssistant.CreateDatabase("AuthenticationDbCore");
                        StudyBuddyDbAssistant.CreateAuthenticationTables("AuthenticationDbCore");
                    }
                }
                var loginData = ParseAuthData(Request.Body);
                var identity  = AuthenticationSingleton.AuthenticateUser(loginData["username"],
                                                                         loginData["password"]);
                if (identity == null)
                {
                    var response = (Response)JsonConvert.SerializeObject(FormErrorResponse(-1));
                    response.ContentType = "application/json";
                    response.StatusCode  = HttpStatusCode.NotAcceptable;
                    return(response);
                }
                else
                {
                    var token = tokenizer.Tokenize(identity, Context);
                    return(new
                    {
                        Token = token
                    });
                }
            };

            Post["/register/"] = x =>
            {
                var regData = ParseAuthData(Request.Body);
                var authenticationStatus = AuthenticationSingleton.RegisterUser(regData["username"], regData["password"]);
                if (authenticationStatus != 0)
                {
                    var response = (Response)JsonConvert.SerializeObject(FormErrorResponse(authenticationStatus));
                    response.ContentType = "application/json";
                    response.StatusCode  = HttpStatusCode.NotAcceptable;
                    return(response);
                }
                using (var dbWrapper = new DbWrapper("AuthenticationDbCore"))
                {
                    if (!dbWrapper.DoesDbExist())
                    {
                        StudyBuddyDbAssistant.CreateDatabase("AuthenticationDbCore");
                        StudyBuddyDbAssistant.CreateAuthenticationTables("AuthenticationDbCore");
                    }
                }

                var identity = AuthenticationSingleton.AuthenticateUser(regData["username"],
                                                                        regData["password"]);
                if (identity == null)
                {
                    var response = (Response)JsonConvert.SerializeObject(FormErrorResponse(-1));
                    response.ContentType = "application/json";
                    response.StatusCode  = HttpStatusCode.NotAcceptable;
                    return(response);
                }
                else
                {
                    var token = tokenizer.Tokenize(identity, Context);
                    return(new
                    {
                        Token = token
                    });
                }
            };
        }