axios-ntlm
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -1,12 +0,21 @@ | ||
import { AxiosInstance } from 'axios'; | ||
import { AxiosInstance, AxiosRequestConfig } from 'axios'; | ||
/** | ||
* @param username The username of the user you are authenticating as. | ||
* @param password The password of the user you are authenticating as. | ||
* @param domain The domain of the user you are authenticating as. | ||
* @param workstation (optional) The workstation in use. Defaults to the current hostname if undefined. | ||
* @param AxiosClient (optional) An existing axios client to attach NTLM Auth interceptors to. | ||
* @property username The username of the user you are authenticating as. | ||
* @property password The password of the user you are authenticating as. | ||
* @property domain The domain of the user you are authenticating as. | ||
* @property workstation The workstation in use. Defaults to the current hostname if undefined. | ||
*/ | ||
export interface NtlmCredentials { | ||
readonly username: string; | ||
readonly password: string; | ||
readonly domain: string; | ||
readonly workstation?: string; | ||
} | ||
/** | ||
* @param credentials An NtlmCredentials object containing the username and password | ||
* @param AxiosConfig The Axios config for the instance you wish to create | ||
* | ||
* @returns This function returns an axios instance configured to use the provided credentials | ||
*/ | ||
export declare function NtlmClient(username: string, password: string, domain: string, workstation?: string, AxiosClient?: AxiosInstance): AxiosInstance; | ||
export declare function NtlmClient(credentials: NtlmCredentials, AxiosConfig?: AxiosRequestConfig): AxiosInstance; | ||
//# sourceMappingURL=ntlmClient.d.ts.map |
@@ -31,34 +31,19 @@ "use strict"; | ||
/** | ||
* @param username The username of the user you are authenticating as. | ||
* @param password The password of the user you are authenticating as. | ||
* @param domain The domain of the user you are authenticating as. | ||
* @param workstation (optional) The workstation in use. Defaults to the current hostname if undefined. | ||
* @param AxiosClient (optional) An existing axios client to attach NTLM Auth interceptors to. | ||
* @param credentials An NtlmCredentials object containing the username and password | ||
* @param AxiosConfig The Axios config for the instance you wish to create | ||
* | ||
* @returns This function returns an axios instance configured to use the provided credentials | ||
*/ | ||
function NtlmClient(username, password, domain, workstation, AxiosClient) { | ||
var client = AxiosClient; | ||
var httpAgent = new http.Agent({ keepAlive: true }); | ||
var httpsAgent = new https.Agent({ keepAlive: true }); | ||
if (!client) { | ||
client = axios_1.default.create({ | ||
httpsAgent: httpsAgent, | ||
httpAgent: httpAgent | ||
}); | ||
function NtlmClient(credentials, AxiosConfig) { | ||
var config = AxiosConfig !== null && AxiosConfig !== void 0 ? AxiosConfig : {}; | ||
if (!config.httpAgent) { | ||
config.httpAgent = new http.Agent({ keepAlive: true }); | ||
} | ||
client.interceptors.request.use(function (req) { | ||
if (!req.httpAgent) { | ||
req.httpAgent = httpAgent; | ||
} | ||
if (!req.httpsAgent) { | ||
req.httpsAgent = httpsAgent; | ||
} | ||
return req; | ||
}); | ||
if (!config.httpsAgent) { | ||
config.httpsAgent = new https.Agent({ keepAlive: true }); | ||
} | ||
var client = axios_1.default.create(config); | ||
client.interceptors.response.use(function (response) { | ||
//console.log('Response:', response); | ||
return response; | ||
}, function (err) { | ||
var _a, _b, _c, _d, _e, _f; | ||
var error = err.response; | ||
@@ -75,11 +60,5 @@ if (error && error.status === 401 | ||
&& !err.config.headers['X-retry']) { | ||
var t1Msg = ntlm.createType1Message(workstation, domain); | ||
var resp = client({ | ||
method: (_a = err.config.method) !== null && _a !== void 0 ? _a : 'get', | ||
url: (_c = (_b = err.response) === null || _b === void 0 ? void 0 : _b.config.url) !== null && _c !== void 0 ? _c : '', | ||
headers: { | ||
'Connection': 'Keep-Alive', | ||
'Authorization': t1Msg | ||
} | ||
}); | ||
var t1Msg = ntlm.createType1Message(credentials.workstation, credentials.domain); | ||
error.config.headers["Authorization"] = t1Msg; | ||
var resp = client(error.config); | ||
return resp; | ||
@@ -97,13 +76,10 @@ } | ||
var t2Msg = ntlm.decodeType2Message((error.headers['www-authenticate'].match(/^NTLM\s+(.+?)(,|\s+|$)/) || [])[1]); | ||
var t3Msg = ntlm.createType3Message(t2Msg, username, password, workstation, domain); | ||
var resp = client({ | ||
method: (_d = err.config.method) !== null && _d !== void 0 ? _d : 'get', | ||
url: (_f = (_e = err.response) === null || _e === void 0 ? void 0 : _e.config.url) !== null && _f !== void 0 ? _f : '', | ||
headers: { | ||
'X-retry': 'false', | ||
'Authorization': t3Msg | ||
} | ||
}); | ||
var t3Msg = ntlm.createType3Message(t2Msg, credentials.username, credentials.password, credentials.workstation, credentials.domain); | ||
error.config.headers["Authorization"] = t3Msg; | ||
var resp = client(error.config); | ||
return resp; | ||
} | ||
else { | ||
return error; | ||
} | ||
}); | ||
@@ -110,0 +86,0 @@ return client; |
{ | ||
"name": "axios-ntlm", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "An NTLM auth extension to the Axios HTTP library", | ||
@@ -22,3 +22,7 @@ "main": "lib/ntlmClient.js", | ||
"axios", | ||
"ntlm" | ||
"ntlm", | ||
"authentication", | ||
"windows authentication", | ||
"windows", | ||
"auth" | ||
], | ||
@@ -25,0 +29,0 @@ "homepage": "https://buttes.dev/axios-ntlm/", |
# Axios-NTLM | ||
This is a helper library for NTLM Authentication using the Axios HTTP library on Node. It attaches interceptors to an axios instance to authenticate using NTLM for any resources that offer it. | ||
This is a helper library for NTLM Authentication using the [Axios](https://github.com/axios/axios) HTTP library on Node. It attaches interceptors to an axios instance to authenticate using NTLM for any resources that offer it. | ||
@@ -17,4 +17,10 @@ ## Examples | ||
let client = NtlmClient('username', 'password', 'domain') | ||
let credentials: NtlmCredentials = { | ||
username: 'username', | ||
password: "password", | ||
domain: 'domain' | ||
} | ||
let client = NtlmClient(credentials) | ||
try { | ||
@@ -35,24 +41,30 @@ let resp = await client({ | ||
``` | ||
### With an existing client | ||
### With a custom Axios config | ||
This shows how to pass in an existing axios instance to have the NTLM Auth interceptors attached. | ||
This shows how to pass in an axios config in the same way that you would when setting up any other axios instance. | ||
**Note:** If doing this, be aware that http(s)Agents need to be attached to keep the connection alive. If there are none attached already, they will be added. If you are providing your own then you will need to set this up. | ||
Note: If doing this, be aware that http(s)Agents need to be attached to keep the connection alive. If there are none attached already, they will be added. If you are providing your own then you will need to set this up. | ||
```ts | ||
import { AxiosRequestConfig } from 'axios'; | ||
import { NtlmClient, NtlmCredentials } from 'axios-ntlm'; | ||
import { NtlmClient } from 'axios-ntlm'; | ||
(async () => { | ||
let client = axios.create(/*Your options here*/) | ||
client = NtlmClient('username', 'password', 'domain', 'workstation', client) | ||
let credentials: NtlmCredentials = { | ||
username: 'username', | ||
password: "password", | ||
domain: 'domain' | ||
} | ||
let config: AxiosRequestConfig = { | ||
baseURL: 'https://protected.site.example.com', | ||
method: 'get' | ||
} | ||
let client = NtlmClient(credentials, config) | ||
try { | ||
let resp = await client({ | ||
url: 'https://protected.site.example.com', | ||
method: 'get' | ||
}); | ||
console.log(resp.data); | ||
let resp = await client.get('/api/123') | ||
console.log(resp); | ||
} | ||
@@ -59,0 +71,0 @@ catch (err) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76
53235
105