/// <summary> /// Updates the details of a Canvas PAT /// </summary> /// <param name="personalAccessToken">The PAT to update</param> /// <returns>Boolean: Whether or not the update operation was successful</returns> public bool UpdateCanvasPat(CanvasPersonalAccessToken personalAccessToken) { var token = _dbCtx.PersonalAccessTokens.Where(x => x.Id == personalAccessToken.Id).FirstOrDefault(); if (token == null) { return(false); } var oldToken = new CanvasPersonalAccessToken() { TokenName = token.TokenName, AccessToken = token.AccessToken }; token.TokenName = personalAccessToken.TokenName; token.AccessToken = personalAccessToken.AccessToken; try { _dbCtx.SaveChanges(); return(true); } catch (DbUpdateConcurrencyException e) { foreach (var item in e.Entries) { if (item.Entity is CanvasPersonalAccessToken) { var currValues = item.CurrentValues; var dbValues = item.GetDatabaseValues(); foreach (var property in currValues.Properties) { var currentValue = currValues[property]; var dbValue = dbValues[property]; } // Refresh the original values to bypass next concurrency check item.OriginalValues.SetValues(dbValues); } else { return(false); } } return(true); } catch (Exception) { // Rollback changes to access token token.TokenName = oldToken.TokenName; token.AccessToken = oldToken.AccessToken; return(false); } }
/// <summary> /// Deletes expired user tokens for a particular user /// </summary> /// <param name="userId">The id of the user being authenticated</param> public void DeleteUserTokens(int userId) { var user = _dbCtx.UserAccounts.Where(x => x.Id == userId).FirstOrDefault(); if (user != null) { var tokenEntries = _dbCtx.UserTokenEntries.Where(x => x.AppUserId == user.Id).ToList(); _dbCtx.UserTokenEntries.RemoveRange(tokenEntries); _dbCtx.SaveChanges(); } }
private void SetupContext() { var testUser = _dbCtx.UserAccounts.Where(x => x.AppUserName.Equals("Fraser")).FirstOrDefault(); if (testUser == null) { _dbCtx.UserAccounts.Add(new ApplicationUserAccount() { AppUserName = "******", UserPassword = _encoder.Encode("password"), UserType = UserType.Instructor }); _dbCtx.SaveChanges(); } }
public IActionResult AddCanvasToken([FromBody] CanvasToken canvasToken) { var encodedToken = _tokenManager.ReadAndValidateToken(Request.Headers[_config.authHeader]); var handler = new JwtSecurityTokenHandler(); JwtSecurityToken decodedToken; try { decodedToken = handler.ReadJwtToken(encodedToken); } catch (ArgumentException) { return(new UnauthorizedObjectResult("User Token Is Not Valid")); } var userId = _tokenManager.GetUserIdFromToken(decodedToken); if (userId == -1) { return(new NotFoundObjectResult("Failed to find User")); } var pat = new CanvasPersonalAccessToken() { TokenName = canvasToken.TokenName, AccessToken = canvasToken.ApiKey, AppUserId = userId, TokenActive = false }; _dbCtx.PersonalAccessTokens.Add(pat); try { _dbCtx.SaveChanges(); return(new OkObjectResult($"Canvas token: {pat.TokenName} was successfully added!")); } catch (Exception) { // Rollback token addition _dbCtx.PersonalAccessTokens.Remove(pat); return(new NotFoundObjectResult("Failed to Add Canvas Token")); } }
private void SetupContext() { var testUser = _dbCtx.UserAccounts.Where(x => x.AppUserName.Equals("Nathan")).FirstOrDefault(); if (testUser == null) { _dbCtx.UserAccounts.AddRange(_accounts); _dbCtx.SaveChanges(); } }
public IActionResult AddUserCustomDataEntry([FromBody] UserCustomDataEntry dataEntry) { var userToken = _tokenManager.ReadAndValidateToken(Request.Headers[_config.authHeader]); JwtSecurityToken decodedToken; var handler = new JwtSecurityTokenHandler(); try { decodedToken = handler.ReadJwtToken(userToken); } catch (ArgumentException) { return(new UnauthorizedObjectResult("User Token Is Not Valid")); } var sysUserId = _tokenManager.GetUserIdFromToken(decodedToken); var scope = _dbCtx.Scopes.Where(x => x.Id == dataEntry.ScopeId).FirstOrDefault(); if (scope == null) { return(new BadRequestObjectResult("The scope does not exist!")); } _dbCtx.UserCustomDataEntries.Add(dataEntry); try { _dbCtx.SaveChanges(); } catch (Exception) { _dbCtx.UserCustomDataEntries.Remove(dataEntry); // Rollback changes return(new BadRequestObjectResult("Failed to save new data item")); } return(Ok($"Data Entry: {dataEntry.ItemName} was added to catergorical scope: {scope.Name}")); }
public IActionResult RegisterUser([FromBody] ApplicationUserAccount userAccount) { // App user name must be at least 3 characters long and must be alphanumeric if (!string.IsNullOrWhiteSpace(userAccount.AppUserName)) { if (userAccount.AppUserName.Length < 3 || !Regex.IsMatch(userAccount.AppUserName, "^[a-zA-Z0-9]+$")) { return(new BadRequestObjectResult("Username cannot be empty and must contain two alphanumeric characters")); } } else { return(new BadRequestObjectResult("Username cannot be empty and must contain two alphanumeric characters")); } // Password must be at least 8 characters long if (!string.IsNullOrWhiteSpace(userAccount.UserPassword)) { if (userAccount.UserPassword.Length < 8) { return(new BadRequestObjectResult("Password must be 8 characters long")); } } else { return(new BadRequestObjectResult("Password must be 8 characters long")); } // Usernames must be also unique and they are also alternate keys in the database var existingUser = _dbCtx.UserAccounts.Where(x => x.AppUserName.Equals(userAccount.AppUserName)).FirstOrDefault(); if (existingUser != null) { return(new BadRequestObjectResult($"Username {userAccount.AppUserName} is already taken")); } var newUser = new ApplicationUserAccount() { AppUserName = userAccount.AppUserName, UserPassword = _scryptHasher.Encode(userAccount.UserPassword), UserType = UserType.Instructor }; _dbCtx.UserAccounts.Add(newUser); try { _dbCtx.SaveChanges(); } catch (Exception) { // Rollback the addition of a new user _dbCtx.UserAccounts.Remove(newUser); return(new NotFoundObjectResult("Failed to Create New User")); } var token = _tokenManager.IssueToken(newUser); if (token.Equals("")) { return(new NotFoundObjectResult("Failed to Generate Token")); } return(new OkObjectResult($"Successfully Registered new User: {userAccount.AppUserName}")); }
private void SetupContext() { var userList = new List <ApplicationUserAccount>() { new ApplicationUserAccount { AppUserName = "******", UserPassword = _encoder.Encode("snacx4life"), UserType = UserType.Instructor }, new ApplicationUserAccount { AppUserName = "******", UserPassword = _encoder.Encode("malay_ice_queen"), UserType = UserType.Instructor }, new ApplicationUserAccount { AppUserName = "******", UserPassword = _encoder.Encode("cupcakes24/7/365"), UserType = UserType.Instructor }, new ApplicationUserAccount { AppUserName = "******", UserPassword = _encoder.Encode("mecha_gamer"), UserType = UserType.Instructor } }; _dbCtx.UserAccounts.AddRange(userList); _dbCtx.SaveChanges(); _accounts.AddRange(userList); var firstUser = _dbCtx.UserAccounts.Where(x => x.AppUserName.Equals("Literal Snacc")).FirstOrDefault(); var firstUserTokens = new List <CanvasPersonalAccessToken>() { new CanvasPersonalAccessToken { TokenName = "FirstToken", AccessToken = _canvasTokenHelper.CanvasTokenGenerator(), AppUserId = firstUser.Id }, new CanvasPersonalAccessToken { TokenName = "SnaccToken", AccessToken = _canvasTokenHelper.CanvasTokenGenerator(), AppUserId = firstUser.Id }, new CanvasPersonalAccessToken { TokenName = "ThirdToken", AccessToken = _canvasTokenHelper.CanvasTokenGenerator(), AppUserId = firstUser.Id } }; _dbCtx.PersonalAccessTokens.AddRange(firstUserTokens); var secondUser = _dbCtx.UserAccounts.Where(x => x.AppUserName.Equals("Queen Nolen")).FirstOrDefault(); var secondUserTokens = new List <CanvasPersonalAccessToken>() { new CanvasPersonalAccessToken { TokenName = "Nolen1", AccessToken = _canvasTokenHelper.CanvasTokenGenerator(), AppUserId = secondUser.Id }, new CanvasPersonalAccessToken { TokenName = "Nolen2", AccessToken = _canvasTokenHelper.CanvasTokenGenerator(), AppUserId = secondUser.Id }, new CanvasPersonalAccessToken { TokenName = "Nolen3", AccessToken = _canvasTokenHelper.CanvasTokenGenerator(), AppUserId = secondUser.Id } }; _dbCtx.PersonalAccessTokens.AddRange(secondUserTokens); var thirdUser = _dbCtx.UserAccounts.Where(x => x.AppUserName.Equals("Cupcake Mistress")).FirstOrDefault(); var thirdUserTokens = new List <CanvasPersonalAccessToken>() { new CanvasPersonalAccessToken { TokenName = "Vanilla Cupcake", AccessToken = _canvasTokenHelper.CanvasTokenGenerator(), AppUserId = thirdUser.Id }, new CanvasPersonalAccessToken { TokenName = "Blueberry Cupcake", AccessToken = _canvasTokenHelper.CanvasTokenGenerator(), AppUserId = thirdUser.Id }, new CanvasPersonalAccessToken { TokenName = "Choc Chip Cupcake", AccessToken = _canvasTokenHelper.CanvasTokenGenerator(), AppUserId = thirdUser.Id } }; _dbCtx.PersonalAccessTokens.AddRange(thirdUserTokens); _dbCtx.SaveChanges(); }