Ejemplo n.º 1
0
    /// <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;
    }
Ejemplo n.º 2
0
 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);
   }
 }
Ejemplo n.º 3
0
 /// <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;
 }
Ejemplo n.º 4
0
 /// <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;
 }
Ejemplo n.º 5
0
 /// <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;
 }
Ejemplo n.º 6
0
 /// <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();
 }
Ejemplo n.º 7
0
 public async Task<IEnumerable<User>> getAll(AuthUser authUser)
 {
   return await this.userDao.getAll();
 }
Ejemplo n.º 8
0
    /// <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;
    }