All files / src/sso spn.ts

10.26% Statements 4/39
0% Branches 0/4
0% Functions 0/4
10.53% Lines 4/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 731x   1x 1x             1x                                                                                                                            
import { adsi } from '..';
import { IDirectorySearch, ColumnVal, LDAPObject } from '../../lib/adsi';
import { closeADConnection, openADConnection } from './adConnection';
import { activeDirectoryMutex } from './mutex';
 
export interface SPNRecord {
  username: string;
  spn: string[];
}
 
export class SPN {
  constructor() {}
 
  async getListAll(): Promise<SPNRecord[]> {
    const adRelease = await activeDirectoryMutex.acquire();
    openADConnection();
    try {
      const root = await adsi.ADsGestObject('LDAP://rootDSE');
      const distinguishedName = await root.Get('defaultNamingContext');
      const dirsearch = await adsi.ADsOpenObject<IDirectorySearch>({
        binding: `LDAP://${distinguishedName}`,
        riid: 'IID_IDirectorySearch',
      });
      dirsearch.SetSearchPreference();
      dirsearch.ExecuteSearch({
        filter:
          '(&(objectClass=user)(objectCategory=person)(servicePrincipalName=*)(!(cn=krbtgt)))',
      });
 
      const users: LDAPObject[] = [];
 
      let hr = dirsearch.GetFirstRow();
      if (hr === adsi.S_ADS_NOMORE_ROWS) {
        throw new Error('GetFirstRow: no more rows');
      }
      const firstRow: { [colName: string]: ColumnVal } = {};
 
      let colName = dirsearch.GetNextColumnName();
      while (colName !== adsi.S_ADS_NOMORE_COLUMNS) {
        const value = await dirsearch.GetColumn(colName as string);
        firstRow[colName] = value;
        colName = dirsearch.GetNextColumnName();
      }
      users.push(firstRow);
 
      while (true) {
        const row: { [colName: string]: ColumnVal } = {};
        hr = dirsearch.GetNextRow();
        if (hr === adsi.S_ADS_NOMORE_ROWS) {
          break;
        }
        colName = dirsearch.GetNextColumnName();
        while (colName !== adsi.S_ADS_NOMORE_COLUMNS) {
          const value = await dirsearch.GetColumn(colName as string);
          row[colName] = value;
          colName = dirsearch.GetNextColumnName();
        }
        users.push(row);
      }
      dirsearch.Release();
      return users.map((user) => ({
        username: user.sAMAccountName[0] as string,
        spn: user.servicePrincipalName as string[],
      }));
    } finally {
      closeADConnection();
      adRelease();
    }
  }
 
  async add(): Promise<void> {}
}