All files / src/sso spn.ts

13.04% Statements 6/46
0% Branches 0/10
20% Functions 1/5
13.89% Lines 5/36

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 661x               1x     1x                                                                                                       1x 1x  
import { adsi } from '..';
import { IDirectorySearch, ColumnVal, LDAPObject } from '../../lib/adsi';
 
export interface SPNRecord {
  username: string;
  spn: string[];
}
 
export class SPN {
  constructor() {}
 
  async getListAll(): Promise<SPNRecord[]> {
    adsi.CoInitializeEx(['COINIT_MULTITHREADED']);
    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();
    adsi.CoUninitialize();
    return users.map((user) => ({
      username: user.sAMAccountName[0] as string,
      spn: user.servicePrincipalName as string[],
    }));
  }
 
  async add(): Promise<void> {}
}