@nestjs/common#ImATeapotException TypeScript Examples

The following examples show how to use @nestjs/common#ImATeapotException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: user.service.ts    From barista with Apache License 2.0 6 votes vote down vote up
async validateUser(userName: string, pass: string): Promise<UserInfo> {
    const user = await this.findOneLocal(userName);
    if (user) {
      if (user.isLocal) {
        if (await bcrypt.compare(pass, user.passwordHash)) {
          const { passwordHash, isLocal, createdAt, updatedAt, ...result } = user;
          return {
            displayName: result.displayName,
            role: UserRole[result.role],
            userName: result.userName,
            id: result.id,
            email: 'email@localhost',
            groups: ['group1', 'group2', 'group3'],
          };
        }
      }
    }
    // ImATeapot is a placeholder for now
    throw new ImATeapotException("Invalid Username or Password");

    return null;
  }
Example #2
Source File: ldap.service.ts    From barista with Apache License 2.0 5 votes vote down vote up
validateUser(userName: string, pass: string, applyFilter: boolean): Promise<UserInfo> {
    const client = this.createLdapClient();

    const searchUser = `cn=${userName},${this.ldapConfig.base}`;
    const adGroups = [
      this.ldapConfig.adminGroup,
      this.ldapConfig.licenseAdminGroup,
      this.ldapConfig.securityAdminGroup,
      this.ldapConfig.group,
    ];

    // const adGroupsFilter =
    //   '(|' + adGroups.map((group) => `(memberOf=CN=${group},${this.ldapConfig.base})`).join('') + ')';
    return client
      .bind(searchUser, pass)
      .then(() => {
        return client
          .search(searchUser, {
            scope: 'sub',
            filter: `(&(objectClass=user)(sAMAccountName=${userName}))`,
            attributes: ['cn', 'displayName', 'givenName', 'sn', 'mail', 'group', 'gn', 'memberOf'],
          })
          .then(result => {
            try {
              if (_.isEmpty(result.entries)) {
                // not a primary account
                this.logger.warn(`User ${userName} doesn't belong to any groups.`);
                // ImATeapot is a placeholder for now
                throw new ImATeapotException('Required group membership is not present');
                return null;
              }

              const memberOf = result.entries[0].object.memberOf;

              // Only look at the first response
              const entry = result.entries[0].object;
              const userInfo: UserInfo = {
                id: entry.cn,
                displayName: entry.displayName,
                userName,
                role: this.getUserRole(memberOf),
                email: entry.mail,
                groups: [],
              };

              return userInfo;
            } finally {
              client.unbind();
            }
          });
      })
      .catch(e => {
        this.logger.error(`AD query error: ${e}`);
        if (applyFilter) {
          // ImATeapot is a placeholder for now
          throw new ImATeapotException('Invalid Username or Password');
        }
        return null;
      });
  }