forked from hoppscotch/hoppscotch
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for
Digest
authorization (hoppscotch#4339)
Co-authored-by: jamesgeorge007 <[email protected]> Co-authored-by: nivedin <[email protected]>
- Loading branch information
Showing
29 changed files
with
964 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/hoppscotch-cli/src/__tests__/e2e/fixtures/collections/digest-auth-coll.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"v": 3, | ||
"name": "Digest Auth - collection", | ||
"folders": [], | ||
"requests": [ | ||
{ | ||
"v": "8", | ||
"id": "cm0dm70cw000687bnxi830zz7", | ||
"auth": { | ||
"authType": "digest", | ||
"authActive": true, | ||
"username": "<<username>>", | ||
"password": "<<password>>", | ||
"realm": "", | ||
"nonce": "", | ||
"algorithm": "MD5", | ||
"qop": "auth", | ||
"nc": "", | ||
"cnonce": "", | ||
"opaque": "", | ||
"disableRetry": false | ||
}, | ||
"body": { | ||
"body": null, | ||
"contentType": null | ||
}, | ||
"name": "digest-auth-headers", | ||
"method": "GET", | ||
"params": [], | ||
"headers": [], | ||
"endpoint": "<<url>>", | ||
"testScript": "pw.test(\"Status code is 200\", ()=> { pw.expect(pw.response.status).toBe(200);}); \n pw.test(\"Receives the www-authenticate header\", ()=> { pw.expect(pw.response.headers['www-authenticate']).toBeType('string');});", | ||
"preRequestScript": "", | ||
"responses": {}, | ||
"requestVariables": [] | ||
} | ||
], | ||
"auth": { | ||
"authType": "inherit", | ||
"authActive": true | ||
}, | ||
"headers": [] | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/hoppscotch-cli/src/__tests__/e2e/fixtures/collections/digest-auth-failure-coll.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"v": 3, | ||
"name": "Digest Auth (failure state) - collection", | ||
"folders": [], | ||
"requests": [ | ||
{ | ||
"v": "8", | ||
"id": "cm0dm70cw000687bnxi830zz7", | ||
"auth": { | ||
"authType": "digest", | ||
"authActive": true, | ||
"username": "<<username>>", | ||
"password": "<<password>>", | ||
"realm": "", | ||
"nonce": "", | ||
"algorithm": "MD5", | ||
"qop": "auth", | ||
"nc": "", | ||
"cnonce": "", | ||
"opaque": "", | ||
"disableRetry": true | ||
}, | ||
"body": { | ||
"body": null, | ||
"contentType": null | ||
}, | ||
"name": "digest-auth-headers", | ||
"method": "GET", | ||
"params": [], | ||
"headers": [], | ||
"endpoint": "<<url>>", | ||
"testScript": "pw.test(\"Status code is not 200\", ()=> { pw.expect(pw.response.status).not.toBe(200);}); \n pw.test(\"Receives the www-authenticate header\", ()=> { pw.expect(pw.response.headers['www-authenticate']).not.toBeType('string');});", | ||
"preRequestScript": "", | ||
"responses": {}, | ||
"requestVariables": [] | ||
} | ||
], | ||
"auth": { | ||
"authType": "inherit", | ||
"authActive": true | ||
}, | ||
"headers": [] | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/hoppscotch-cli/src/__tests__/e2e/fixtures/collections/digest-auth-success-coll.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"v": 3, | ||
"name": "Digest Auth (success state) - collection", | ||
"folders": [], | ||
"requests": [ | ||
{ | ||
"v": "8", | ||
"id": "cm0dm70cw000687bnxi830zz7", | ||
"auth": { | ||
"authType": "digest", | ||
"authActive": true, | ||
"username": "<<username>>", | ||
"password": "<<password>>", | ||
"realm": "", | ||
"nonce": "", | ||
"algorithm": "MD5", | ||
"qop": "auth", | ||
"nc": "", | ||
"cnonce": "", | ||
"opaque": "", | ||
"disableRetry": false | ||
}, | ||
"body": { | ||
"body": null, | ||
"contentType": null | ||
}, | ||
"name": "digest-auth-headers", | ||
"method": "GET", | ||
"params": [], | ||
"headers": [], | ||
"endpoint": "<<url>>", | ||
"testScript": "pw.test(\"Status code is 200\", ()=> { pw.expect(pw.response.status).toBe(200);}); \n pw.test(\"Receives the www-authenticate header\", ()=> { pw.expect(pw.response.headers['www-authenticate']).toBeType('string');});", | ||
"preRequestScript": "", | ||
"responses": {}, | ||
"requestVariables": [] | ||
} | ||
], | ||
"auth": { | ||
"authType": "inherit", | ||
"authActive": true | ||
}, | ||
"headers": [] | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/digest-auth-envs.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"v": 1, | ||
"id": "cm0dsn3v70004p4qk3l9b7sjm", | ||
"name": "Digest Auth - environments", | ||
"variables": [ | ||
{ | ||
"key": "username", | ||
"value": "admin", | ||
"secret": true | ||
}, | ||
{ | ||
"key": "password", | ||
"value": "admin", | ||
"secret": true | ||
}, | ||
{ | ||
"key": "url", | ||
"value": "https://test.insightres.org/digest/" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import axios from "axios"; | ||
import { md5 } from "js-md5"; | ||
|
||
import { exceptionColors } from "../getters"; | ||
|
||
export interface DigestAuthParams { | ||
username: string; | ||
password: string; | ||
realm: string; | ||
nonce: string; | ||
endpoint: string; | ||
method: string; | ||
algorithm: string; | ||
qop: string; | ||
nc?: string; | ||
opaque?: string; | ||
cnonce?: string; // client nonce (optional but typically required in qop='auth') | ||
} | ||
|
||
export interface DigestAuthInfo { | ||
realm: string; | ||
nonce: string; | ||
qop: string; | ||
opaque?: string; | ||
algorithm: string; | ||
} | ||
|
||
// Utility function to parse Digest auth header values | ||
const parseDigestAuthHeader = ( | ||
header: string | ||
): { [key: string]: string } | null => { | ||
const matches = header.match(/([a-z0-9]+)="([^"]+)"/gi); | ||
if (!matches) return null; | ||
|
||
const authParams: { [key: string]: string } = {}; | ||
matches.forEach((match) => { | ||
const parts = match.split("="); | ||
authParams[parts[0]] = parts[1].replace(/"/g, ""); | ||
}); | ||
|
||
return authParams; | ||
}; | ||
|
||
// Function to generate Digest Auth Header | ||
export const generateDigestAuthHeader = async (params: DigestAuthParams) => { | ||
const { | ||
username, | ||
password, | ||
realm, | ||
nonce, | ||
endpoint, | ||
method, | ||
algorithm = "MD5", | ||
qop, | ||
nc = "00000001", | ||
opaque, | ||
cnonce, | ||
} = params; | ||
|
||
const uri = endpoint.replace(/(^\w+:|^)\/\//, ""); | ||
|
||
// Generate client nonce if not provided | ||
const generatedCnonce = cnonce || md5(`${Math.random()}`); | ||
|
||
// Step 1: Hash the username, realm, and password | ||
const ha1 = md5(`${username}:${realm}:${password}`); | ||
|
||
// Step 2: Hash the method and URI | ||
const ha2 = md5(`${method}:${uri}`); | ||
|
||
// Step 3: Compute the response hash | ||
const response = md5( | ||
`${ha1}:${nonce}:${nc}:${generatedCnonce}:${qop}:${ha2}` | ||
); | ||
|
||
// Build the Digest header | ||
let authHeader = `Digest username="${username}", realm="${realm}", nonce="${nonce}", uri="${uri}", algorithm="${algorithm}", response="${response}", qop=${qop}, nc=${nc}, cnonce="${generatedCnonce}"`; | ||
|
||
if (opaque) { | ||
authHeader += `, opaque="${opaque}"`; | ||
} | ||
|
||
return authHeader; | ||
}; | ||
|
||
export const fetchInitialDigestAuthInfo = async ( | ||
url: string, | ||
method: string, | ||
disableRetry: boolean | ||
): Promise<DigestAuthInfo> => { | ||
try { | ||
const initialResponse = await axios.request({ | ||
url, | ||
method, | ||
validateStatus: () => true, // Allow handling of all status codes | ||
}); | ||
|
||
// Check if the response status is 401 (which is expected in Digest Auth flow) | ||
if (initialResponse.status === 401 && !disableRetry) { | ||
const authHeader = initialResponse.headers["www-authenticate"]; | ||
|
||
if (authHeader) { | ||
const authParams = parseDigestAuthHeader(authHeader); | ||
if ( | ||
authParams && | ||
authParams.realm && | ||
authParams.nonce && | ||
authParams.qop | ||
) { | ||
return { | ||
realm: authParams.realm, | ||
nonce: authParams.nonce, | ||
qop: authParams.qop, | ||
opaque: authParams.opaque, | ||
algorithm: authParams.algorithm, | ||
}; | ||
} | ||
} | ||
throw new Error( | ||
"Failed to parse authentication parameters from WWW-Authenticate header" | ||
); | ||
} else if (initialResponse.status === 401 && disableRetry) { | ||
throw new Error( | ||
`401 Unauthorized received. Retry is disabled as specified, so no further attempts will be made.` | ||
); | ||
} else { | ||
throw new Error(`Unexpected response: ${initialResponse.status}`); | ||
} | ||
} catch (error) { | ||
const errMsg = error instanceof Error ? error.message : error; | ||
|
||
console.error( | ||
exceptionColors.FAIL( | ||
`\n Error fetching initial digest auth info: ${errMsg} \n` | ||
) | ||
); | ||
throw error; // Re-throw the error to handle it further up the chain if needed | ||
} | ||
}; |
Oops, something went wrong.