Skip to content

Instantly share code, notes, and snippets.

@creage
Created October 12, 2020 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creage/7854db81c307cf4fd5199e2b4e96a5c7 to your computer and use it in GitHub Desktop.
Save creage/7854db81c307cf4fd5199e2b4e96a5c7 to your computer and use it in GitHub Desktop.
NTLM proxy wrapper around cypress-ntlm-auth
const axios = require('axios');
const ntlmAuth = require('cypress-ntlm-auth/dist/plugin');
const ConfigValidator = require('cypress-ntlm-auth/dist/util/config.validator').ConfigValidator;
const SsoConfigValidator = require('cypress-ntlm-auth/dist/util/sso.config.validator').SsoConfigValidator;
const DI = require('cypress-ntlm-auth/dist/proxy/dependency.injection');
const DITypes = require('cypress-ntlm-auth/dist/proxy/dependency.injection.types');
module.exports = class NTLMProxy {
/**
* Launches NTLM authentication proxy. Sets HTTP_PROXY env var to the launched proxy address.
* @example
```js
proxy.launch();
```
*/
static async launch() {
await NTLMProxy.start();
const container = new DI.DependencyInjection();
const cypressNtlm = container.get(DITypes.TYPES.ICypressNtlm);
const upstreamProxyConfigurator = container.get(DITypes.TYPES.IUpstreamProxyConfigurator);
upstreamProxyConfigurator.processNoProxyLoopback();
const { ntlmProxyUrl } = await cypressNtlm.checkProxyIsRunning(15000, 200); // timeout, interval
// ideally, you should not do this, an this disables TLS certs verification
// but depending on IIS configuration, it might not send you correct certs chain
// leading NodeJS to fail to verify it
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
// +(process.env.npm_lifecycle_event === 'test:ci'); // local test runs should not be blocked by cert issues
process.env.HTTP_PROXY = ntlmProxyUrl;
process.env.HTTPS_PROXY = ntlmProxyUrl;
process.env.NO_PROXY = '<-loopback>';
upstreamProxyConfigurator.removeUnusedProxyEnv();
return ntlmAuth.initNtlmAuth(process);
}
/**
* Adds NTLM authentication support for a specific host.
* You can call this multiple times to register several hosts or
* change credentials.
* @example
```js
proxy.ntlm({ host: 'https://ntlm.acme.com', username: 'TheUser', password: 'ThePassword', domain: 'TheDomain' });
```
*/
static async ntlm({ username, password, host, domain, workstation = 'kmd.at', version = 2 }) {
const proxy = process.env.NTLM_AUTH_PROXY;
const configApi = process.env.NTLM_AUTH_API;
if (!proxy || !configApi) {
throw new Error('The proxy must be launched before using this method');
}
const config = {
username,
password,
domain,
workstation,
ntlmHosts: [host],
ntlmVersion: version
};
const validationResult = ConfigValidator.validate(config);
if (!validationResult.ok) {
throw new Error(validationResult.message);
}
return axios.post(`${configApi}/ntlm-config`, config);
}
/**
* Adds NTLM Single-sign-on authentication support for
* specific hosts. Wildcards are supported.
* Calling this multiple times replaces previous SSO configuration.
* The hosts must NOT include protocol, port or the rest of the url (path and query) - only host level authentication is supported.
* @example
```js
proxy.sso(['localhost', '*.acme.com']);
```
*/
static async sso(ntlmHosts = []) {
const proxy = process.env.NTLM_AUTH_PROXY;
const configApi = process.env.NTLM_AUTH_API;
if (!proxy || !configApi) {
throw new Error('The proxy must be launched before using this method');
}
const config = {
ntlmHosts
};
const validationResult = SsoConfigValidator.validate(config);
if (!validationResult.ok) {
throw new Error(validationResult.message);
}
return axios.post(`${configApi}/ntlm-sso`, config);
}
/**
* Reset NTLM authentication for all configured hosts. Recommended before/after tests.
* @example
```js
proxy.reset();
```
*/
static async reset() {
const proxy = process.env.NTLM_AUTH_PROXY;
const configApi = process.env.NTLM_AUTH_API;
if (!proxy || !configApi) {
throw new Error('The proxy must be launched before using this method');
}
return axios.post(`${configApi}/reset`, {});
}
/**
* Starts NTLM proxy in a separate process.
* @example
```js
proxy.start();
```
*/
static async start() {
return require('cypress-ntlm-auth/dist/launchers/ntlm.proxy.main.js');
}
/**
* Stops NTLM proxy. Call it when your test runner tears down.
* @example
```js
proxy.exit();
```
*/
static async exit() {
// since proxy is running in a separate process, stopping it is not so trivial
return require('cypress-ntlm-auth/dist/launchers/ntlm.proxy.exit.main.js');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment