示例#1
0
        public IActionResult CreatePassword([FromBody] PasswordEntryCreateDto passwordEntryCreateDto)
        {
            bool BOOL_IsJWtTokenRepudied;

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            // Extract the Credential element that has the same username received
            Credential selectedCredential = _vaultDbContext.Credentials.Where(credential => credential.Username.Equals(((ClaimsIdentity)HttpContext.User.Identity).FindFirst("username").Value)).FirstOrDefault();

            // Checks if the JWT token is repudied
            _memoryCache.TryGetValue(Request.Headers[HeaderNames.Authorization], out BOOL_IsJWtTokenRepudied);
            if (null == selectedCredential || BOOL_IsJWtTokenRepudied)
            {
                return(Unauthorized());
            }

            /* Sets the encrypted password using the InitId request header parameter as key*/
            PasswordEntry passwordEntry = _mapper.Map <PasswordEntry>(passwordEntryCreateDto);

            /* Assign the password entry foreign key with the logged credential primary key */
            passwordEntry.CredentialFK = selectedCredential.Id;

            // Adds the entry password to EF and writes to the database
            _vaultDbContext.Passwords.Add(passwordEntry);
            _vaultDbContext.SaveChanges();

            // After, redirect the browser to the ReadPasswordById Action(see the below function)
            return(CreatedAtAction(nameof(ReadPasswordById), new { ID = passwordEntry.Id }, passwordEntry));
        }
示例#2
0
        public IActionResult UpdatePassword(int id, [FromBody] PasswordEntryCreateDto passwordEntryCreateDto)
        {
            bool BOOL_IsJWtTokenRepudied;

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            // Extract the Credential element that has the same username received
            Credential selectedCredential = _vaultDbContext.Credentials.Where(credential => credential.Username.Equals(((ClaimsIdentity)HttpContext.User.Identity).FindFirst("username").Value)).FirstOrDefault();

            // Checks if the JWT token is repudied
            _memoryCache.TryGetValue(Request.Headers[HeaderNames.Authorization], out BOOL_IsJWtTokenRepudied);
            if (null == selectedCredential || BOOL_IsJWtTokenRepudied)
            {
                return(Unauthorized());
            }

            // Extract the password entry with the requested ID
            PasswordEntry passwordEntry = _vaultDbContext.Passwords.FirstOrDefault(element => element.Id == id && element.CredentialFK == selectedCredential.Id);

            if (null == passwordEntry)
            {
                return(NotFound());
            }

            /* Sets the encrypted password using the InitId request header parameter as key*/
            //passwordEntryCreateDto.Password = CryptoHelper.EncryptString(passwordEntryCreateDto.Password, Request.Headers["InitId"]);
            //passwordEntryCreateDto.Password = EncryptProvider.AESEncrypt(passwordEntryCreateDto.Password, Request.Headers["InitId"]);

            _mapper.Map(passwordEntryCreateDto, passwordEntry);
            _vaultDbContext.SaveChanges();


            return(CreatedAtAction(nameof(ReadPasswordById), new { ID = passwordEntry.Id }, passwordEntry));
        }