/// <summary> /// Generate and persist JWT based on authenticated user information /// </summary> /// <param name="authUser"></param> /// <returns></returns> public async Task<string> generateAndSaveJWT(AuthUser authUser) { Guid jti = Guid.NewGuid(); string jwtToken = this.tokenService.generate(authUser.userID, jti); await this.userComponent.saveTokenId(authUser, jti); await this.commitBusinessTransaction(); return jwtToken; }
public async Task saveTokenId(AuthUser authUser, Guid? tokenId) { var user = await this.getByID(authUser.userID); if (user != null) { user.tokenID = tokenId; this.userDao.update(user); } }
/// <summary> /// Convert user entity to authUser /// </summary> /// <param name="user"></param> /// <returns></returns> private AuthUser convertToAuthUser(User user) { AuthUser authUser = new AuthUser() { userID = user.userID, userType = user.userTypeID, name = user.firstName + " " + user.lastName }; return authUser; }
/// <summary> /// Get user by id /// </summary> /// <param name="authUser"></param> /// <param name="id"></param> /// <returns></returns> public async Task<User> getById(AuthUser authUser, int id) { User user = await this.userComponent.getByID(id); return user; }
/// <summary> /// Get all users /// </summary> /// <param name="authUser"></param> /// <returns></returns> public async Task<IEnumerable<User>> getAll(AuthUser authUser) { IEnumerable<User> users = await this.userComponent.getAll(authUser); return users; }
/// <summary> /// Revoke JWT token associated with authenticated user /// </summary> /// <param name="authUser"></param> /// <returns></returns> public async Task revokeJWT(AuthUser authUser) { await this.userComponent.saveTokenId(authUser, null); await this.commitBusinessTransaction(); }
public async Task<IEnumerable<User>> getAll(AuthUser authUser) { return await this.userDao.getAll(); }
/// <summary> /// Authorize authenticated user. /// </summary> /// <remarks> /// User is considered authenticated if: /// is valid {not null} AND (is admin OR (belongs to expected role)) /// </remarks> /// <param name="authUser"></param> /// <returns></returns> internal bool authorize(AuthUser authUser) { bool authorized = authUser != null && (authUser.userType.Equals("ADMIN", StringComparison.OrdinalIgnoreCase) || (this.roles != null && this.roles.Any(r => r.Equals(authUser.userType, StringComparison.OrdinalIgnoreCase)))); return authorized; }