All files / src/auth/resolvers FileConfig.ts

95.35% Statements 41/43
70% Branches 7/10
100% Functions 7/7
95.35% Lines 41/43
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 861x 1x 1x   1x     1x 1x   1x     1x 1x   9x     9x   9x   9x 6x     3x 3x   3x       3x 9x     3x   3x     3x 3x     3x           3x   3x 3x 3x 3x   3x     3x 3x         3x 3x   3x 9x 3x 3x 3x 3x         3x      
import * as Promise from 'bluebird';
import * as fs from 'fs';
import * as path from 'path';
 
import { AuthConfig } from 'node-sp-auth-config';
import { IAuthResolver } from '../IAuthResolver';
import { IAuthResponse } from '../IAuthResponse';
import { FilesHelper } from '../../utils/FilesHelper';
import { AuthResolverFactory } from './../AuthResolverFactory';
import { UrlHelper } from '../../utils/UrlHelper';
import { Cache } from './../../utils/Cache';
import { IAuthOptions } from '../IAuthOptions';
 
export class FileConfig implements IAuthResolver {
  private static CredsCache: Cache = new Cache();
 
  constructor(private _siteUrl: string) { }
 
  public getAuth(): Promise<IAuthResponse> {
    let fileNameTemplate = FilesHelper.resolveFileName(this._siteUrl);
 
    let cachedCreds = FileConfig.CredsCache.get<IAuthOptions>(fileNameTemplate);
 
    if (cachedCreds) {
      return AuthResolverFactory.resolve(this._siteUrl, cachedCreds).getAuth();
    }
 
    let userDataFolder = FilesHelper.getUserDataFolder();
    let credsFolder = path.join(userDataFolder, 'creds');
 
    Iif (!fs.existsSync(credsFolder)) {
      fs.mkdirSync(credsFolder);
    }
 
    let fileNames = fs.readdirSync(credsFolder).map(name => {
      return path.basename(name, path.extname(name));
    });
 
    let configPath = this.findBestMatch(fileNameTemplate, fileNames);
 
    Iif (!configPath) {
      configPath = path.join(credsFolder, `${fileNameTemplate}.json`);
    } else {
      configPath = path.join(credsFolder, `${configPath}.json`);
      console.log(`[node-sp-auth]: reading auth data from ${configPath}`);
    }
 
    let config = new AuthConfig({
      configPath: configPath,
      encryptPassword: true,
      saveConfigOnDisk: true
    });
 
    return Promise.resolve(config.getContext())
      .then(context => {
        let fileNameTemplate = FilesHelper.resolveFileName(context.siteUrl);
        let fileName = path.basename(configPath);
        let newPath = configPath.replace(fileName, `${fileNameTemplate}.json`);
        fs.renameSync(configPath, newPath);
 
        return context.authOptions;
      })
      .then(authOptions => {
        FileConfig.CredsCache.set(fileNameTemplate, authOptions);
        return AuthResolverFactory.resolve(this._siteUrl, authOptions).getAuth();
      });
  }
 
  private findBestMatch(fileNameTemplate: string, fileNames: string[]): string {
    let matchLength = 2048;
    let matchFileName: string = null;
 
    fileNames.forEach(fileName => {
      if (fileNameTemplate.indexOf(fileName) !== -1) {
        let subUrlLength = fileNameTemplate.replace(fileName, '').length;
        Eif (subUrlLength < matchLength) {
          matchLength = subUrlLength;
          matchFileName = fileName;
        }
      }
    });
 
    return matchFileName;
  }
}