All files / src/auth/resolvers OnlineUserCredentials.ts

20% Statements 14/70
0% Branches 0/14
0% Functions 0/11
20.29% Lines 14/69
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 2011x 1x 1x 1x 1x 1x 1x 1x     1x         1x 1x 1x   1x   1x                                                                                                                                                                                                                                                                                                                                                                      
import * as Promise from 'bluebird';
import * as url from 'url';
import * as util from 'util';
import * as _ from 'lodash';
import * as fs from 'fs';
import * as request from 'request-promise';
import * as cookie from 'cookie';
import * as path from 'path';
import { IncomingMessage } from 'http';
 
let xmldoc: any = require('xmldoc');
 
import { IAuthResolver } from './../IAuthResolver';
import { IUserCredentials } from './../IAuthOptions';
import { IAuthResponse } from './../IAuthResponse';
import { Cache } from './../../utils/Cache';
import * as consts from './../../Consts';
import { AdfsHelper } from './../../utils/AdfsHelper';
 
export class OnlineUserCredentials implements IAuthResolver {
 
  private static CookieCache: Cache = new Cache();
 
  constructor(private _siteUrl: string, private _authOptions: IUserCredentials) {
    this._authOptions = _.extend<{}, IUserCredentials>({}, _authOptions);
 
    this._authOptions.username = this._authOptions.username
      .replace(/&/g, '&amp;')
      .replace(/"/g, '&quot;')
      .replace(/'/g, '&apos;')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;');
 
    this._authOptions.password = this._authOptions.password
      .replace(/&/g, '&amp;')
      .replace(/"/g, '&quot;')
      .replace(/'/g, '&apos;')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;');
  }
 
  public getAuth(): Promise<IAuthResponse> {
    let parsedUrl: url.Url = url.parse(this._siteUrl);
    let host: string = parsedUrl.host;
    let cacheKey: string = util.format('%s@%s', host, this._authOptions.username);
    let cachedCookie: string = OnlineUserCredentials.CookieCache.get<string>(cacheKey);
 
    if (cachedCookie) {
      return Promise.resolve({
        headers: {
          'Cookie': cachedCookie
        }
      });
    }
 
    return this.getSecurityToken()
      .then(xmlResponse => {
        return this.postToken(xmlResponse);
      })
      .then(data => {
        let response: IncomingMessage = data[1];
        let diffSeconds: number = data[0];
        let fedAuth: string;
        let rtFa: string;
 
        for (let i = 0; i < response.headers['set-cookie'].length; i++) {
          let headerCookie: string = response.headers['set-cookie'][i];
          if (headerCookie.indexOf(consts.FedAuth) !== -1) {
            fedAuth = cookie.parse(headerCookie)[consts.FedAuth];
          }
          if (headerCookie.indexOf(consts.RtFa) !== -1) {
            rtFa = cookie.parse(headerCookie)[consts.RtFa];
          }
        }
 
        let authCookie: string = 'FedAuth=' + fedAuth + '; rtFa=' + rtFa;
 
        OnlineUserCredentials.CookieCache.set(cacheKey, authCookie, diffSeconds);
 
        return {
          headers: {
            'Cookie': authCookie
          }
        };
      });
  };
 
  private getSecurityToken(): Promise<any> {
    return request.post(consts.OnlineUserRealmEndpoint, {
      simple: false,
      strictSSL: false,
      json: true,
      form: {
        'login': this._authOptions.username
      }
    })
      .then(userRealm => {
        let authType: string = userRealm.NameSpaceType;
 
        if (!authType) {
          throw new Error('Unable to define namespace type for Online authentiation');
        }
 
        if (authType === 'Managed') {
          return this.getSecurityTokenWithOnline();
        }
 
        if (authType === 'Federated') {
          return this.getSecurityTokenWithAdfs(userRealm.AuthURL);
        }
 
        throw new Error(`Unable to resolve namespace authentiation type. Type received: ${authType}`);
      });
  }
 
  private getSecurityTokenWithAdfs(adfsUrl: string): Promise<any> {
    return AdfsHelper.getSamlAssertion(this._siteUrl, {
      username: this._authOptions.username,
      password: this._authOptions.password,
      adfsUrl: adfsUrl,
      relyingParty: consts.AdfsOnlineRealm
    })
      .then(samlAssertion => {
 
        let siteUrlParsed: url.Url = url.parse(this._siteUrl);
        let rootSiteUrl: string = siteUrlParsed.protocol + '//' + siteUrlParsed.host;
        let tokenRequest: string = _.template(
          fs.readFileSync(path.join(__dirname, '..', '..', '..', 'templates', 'online_saml_wsfed_adfs.tmpl')).toString())({
            endpoint: rootSiteUrl,
            token: samlAssertion.value
          });
 
        return request.post(consts.MSOnlineSts, {
          body: tokenRequest,
          headers: {
            'Content-Length': tokenRequest.length,
            'Content-Type': 'application/soap+xml; charset=utf-8'
          },
          simple: false,
          strictSSL: false
        });
      });
  }
 
  private getSecurityTokenWithOnline(): Promise<any> {
    let parsedUrl: url.Url = url.parse(this._siteUrl);
    let host: string = parsedUrl.host;
    let spFormsEndPoint = `${parsedUrl.protocol}//${host}/${consts.FormsPath}`;
 
    let samlBody: string = _.template(
      fs.readFileSync(path.join(__dirname, '..', '..', '..', 'templates', 'online_saml_wsfed.tmpl')).toString())({
        username: this._authOptions.username,
        password: this._authOptions.password,
        endpoint: spFormsEndPoint
      });
 
    return request
      .post(consts.MSOnlineSts, {
        body: samlBody,
        simple: false,
        strictSSL: false,
        headers: {
          'Content-Type': 'application/soap+xml; charset=utf-8'
        }
      } as any)
      .then(xmlResponse => {
        return xmlResponse;
      });
  }
 
  private postToken(xmlResponse: any): Promise<[number, any]> {
    let xmlDoc: any = new xmldoc.XmlDocument(xmlResponse);
    let parsedUrl: url.Url = url.parse(this._siteUrl);
    let spFormsEndPoint = `${parsedUrl.protocol}//${parsedUrl.host}/${consts.FormsPath}`;
 
    let securityTokenResponse: any = xmlDoc.childNamed('S:Body').firstChild;
    if (securityTokenResponse.name.indexOf('Fault') !== -1) {
      throw new Error(securityTokenResponse.toString());
    }
 
    let binaryToken: any = securityTokenResponse.childNamed('wst:RequestedSecurityToken').firstChild.val;
    let now: any = new Date().getTime();
    let expires: number = new Date(securityTokenResponse.childNamed('wst:Lifetime').childNamed('wsu:Expires').val).getTime();
    let diff: number = (expires - now) / 1000;
 
    let diffSeconds: number = parseInt(diff.toString(), 10);
 
    return Promise.all([diffSeconds, request
      .post(spFormsEndPoint, {
        headers: {
          'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)',
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: binaryToken,
        rejectUnauthorized: false,
        resolveWithFullResponse: true,
        simple: false
      } as any)]);
  }
}