All files / src/sso SSO.ts

96.77% Statements 60/62
83.33% Branches 10/12
100% Functions 5/5
96.67% Lines 58/60

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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 1261x 1x 1x 1x 1x     1x       1x     12x               12x 12x     1x 12x       12x 12x     12x 12x 12x 12x 12x 12x     1x   12x   12x 12x         12x 12x 12x       12x   12x 12x       13x         11x 11x             12x 11x 11x 11x 11x 11x           11x 11x       11x         11x 11x 11x 11x     11x 11x 11x 11x         1x 11x 11x 11x 11x     1x 11x   1x  
import { sspi, CtxtHandle } from '../../lib/api';
import { getUser } from './userdb';
import dbg from 'debug';
import { sso } from '.';
import os from 'os';
import { AuthOptions, User } from './interfaces';
 
const debug = dbg('node-expose-sspi:SSO');
 
export type SSOMethod = 'NTLM' | 'Kerberos';
 
export class SSO {
  user: User;
  owner: User;
  private options: AuthOptions = {
    useActiveDirectory: true,
    useGroups: true,
    useOwner: true,
    groupFilterRegex: '.*',
  };
 
  constructor(
    private serverContextHandle: CtxtHandle,
    public method?: SSOMethod
  ) {}
 
  async load(): Promise<void> {
    const names = sspi.QueryContextAttributes(
      this.serverContextHandle,
      'SECPKG_ATTR_NAMES'
    );
    const [domain, name] = names.sUserName.split('\\');
    this.user = { domain, name };
 
    // impersonate to retrieve the userToken.
    sspi.ImpersonateSecurityContext(this.serverContextHandle);
    debug('impersonate security context ok');
    const userToken = sspi.OpenThreadToken();
    debug('userToken: ', userToken);
    try {
      this.user.displayName = sspi.GetUserNameEx('NameDisplay');
    } catch (e) {
      // exemple of error scenario: local user without displayname.
      this.user.displayName = this.user.name;
    }
    sspi.RevertSecurityContext(this.serverContextHandle);
 
    Eif (this.options.useGroups) {
      const groups = sspi.GetTokenInformation({
        accessToken: userToken,
        tokenInformationClass: 'TokenGroups',
        filter: this.options.groupFilterRegex,
      });
      groups.sort();
      debug('groups: ', groups);
      this.user.groups = groups;
    }
 
    // free the userToken
    sspi.CloseHandle(userToken);
 
    const { sid } = sspi.LookupAccountName(names.sUserName);
    this.user.sid = sid;
 
    try {
      if (
        sso.isOnDomain() &&
        sso.isActiveDirectoryReachable() &&
        os.hostname() !== domain &&
        this.options.useActiveDirectory
      ) {
        const adUser = await getUser(`(sAMAccountName=${name})`);
        this.user.adUser = adUser;
      }
    } catch (e) {
      debug('cannot getUser from AD. e: ', e);
    }
 
    // owner info.
    if (this.options.useOwner) {
      const owner = sspi.GetUserName();
      debug('owner: ', owner);
      this.owner = { name: owner };
      try {
        this.owner.displayName = sspi.GetUserNameEx('NameDisplay');
      } catch (e) {
        // exemple of error scenario: local user without displayname.
        this.owner.displayName = this.owner.name;
      }
 
      Eif (this.options.useGroups) {
        const processToken = sspi.OpenProcessToken([
          'TOKEN_QUERY',
          'TOKEN_QUERY_SOURCE',
        ]);
        const ownerGroups = sspi.GetTokenInformation({
          accessToken: processToken,
          tokenInformationClass: 'TokenGroups',
          filter: this.options.groupFilterRegex,
        });
        ownerGroups.sort();
        debug('ownerGroups: ', ownerGroups);
        this.owner.groups = ownerGroups;
        sspi.CloseHandle(processToken);
      }
 
      try {
        const o = sspi.LookupAccountName(owner);
        this.owner.sid = o.sid;
        this.owner.domain = o.domain;
      } catch (e) {}
    }
  }
 
  getJSON(): SSO {
    const json = { ...this };
    delete json.options;
    delete json.serverContextHandle;
    return json;
  }
 
  setOptions(options: AuthOptions): void {
    Object.assign(this.options, options);
  }
}