Ejemplo n.º 1
0
        public IActionResult Token()
        {
            //string tokenString = "test";
            var header = Request.Headers["Authorization"];

            if (header.ToString().StartsWith("Basic"))
            {
                var credValue          = header.ToString().Substring("Basic ".Length).Trim();
                var usernameAndPassenc = Encoding.UTF8.GetString(Convert.FromBase64String(credValue)); //admin:pass
                var usernameAndPass    = usernameAndPassenc.Split(":");
                //check in DB username and pass exist

                var account = _context.GetOne(usernameAndPass[0]);
                if (account is null || !account.EmailVerified)
                {
                    return(BadRequest());
                }
                var password = AccountController.CreateHash(usernameAndPass[1]);
                usernameAndPass[1] = password;
                if (account.Password != usernameAndPass[1])
                {
                    return(Unauthorized());
                }
                var claimsdata = new[] { new Claim(ClaimTypes.Name, usernameAndPass[0]) };
                var key        = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("ahbasshfbsahjfbshajbfhjasbfashjbfsajhfvashjfashfbsahfbsahfksdjf"));
                var signInCred = new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature);
                var token      = new JwtSecurityToken(
                    issuer: "mysite.com",
                    audience: "mysite.com",
                    expires: DateTime.Now.AddDays(62),
                    claims: claimsdata,
                    signingCredentials: signInCred
                    );
                var tokenString = new JwtSecurityTokenHandler().WriteToken(token);
                var theme       = _themesContext.GetOne(account.ThemeId);
                var language    = _languageContext.GetOne(account.LanguageId);
                return(Ok(new string[] { tokenString, theme.Theme, language.LanguageAccount }));
            }
            return(BadRequest("bad request"));
        }
Ejemplo n.º 2
0
        public IActionResult PatchAccount(string username, [FromBody] JsonPatchDocument <Account> newAccount)
        {
            /*
             * this endpoint modifies the event given an id and an Price model that is necessary to specify which property of an object
             * the user wishes to modify and make sure that the data that is inserted follows the rules that have the Price model
             * The body should be as follows:
             * "op" is the operation to be performed
             * "path" is the parameter to be changed
             * "value" is the new value
             */
            var operation = newAccount.Operations.First().path.ToLower();
            var account   = _context.GetOne(username);

            switch (operation)
            {
            case "/themeid":
                var themeId = _themeContext.GetOne(newAccount.Operations.First());
                newAccount.Operations.First().value = themeId;
                _context.Patch(account, newAccount);
                return(Ok(account));

            case "/languageid":
                var languageId = _languageContext.GetOne(newAccount.Operations.First());
                newAccount.Operations.First().value = languageId;
                _context.Patch(account, newAccount);
                return(Ok(account));

            default:
                if (account is null)
                {
                    return(NotFound());
                }
                _context.Patch(account, newAccount);
                return(Ok(account));
            }
        }