Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for Digest authorization #4339

Merged
merged 21 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: digest auth in cli
  • Loading branch information
anwarulislam authored and jamesgeorge007 committed Oct 29, 2024
commit f11fe936efe8c5acc62cf90b153666f894f5fc87
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"id": "cm0dm70cw000687bnxi830zz7",
"auth": {
"authType": "digest",
"authActive": true,
"username": "<<username>>",
"password": "<<password>>",
"realm": "",
Expand Down
17 changes: 7 additions & 10 deletions packages/hoppscotch-cli/src/utils/auth/digest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { md5 } from "js-md5";
import * as E from "fp-ts/Either";
import axios from "axios";

export interface DigestAuthParams {
username: string;
Expand Down Expand Up @@ -71,18 +71,15 @@ export async function fetchInitialDigestAuthInfo(
): Promise<DigestAuthInfo> {
console.log("Fetching initial digest auth info...");
try {
const service = getService(InterceptorService);
const initialResponse = await service.runRequest({
const initialResponse = await axios.request({
url,
method,
}).response;

if (E.isLeft(initialResponse))
throw new Error(`Unexpected response: ${initialResponse.left}`);
validateStatus: () => true, // Allow handling of all status codes
});

// Check if the response status is 401 (which is expected in Digest Auth flow)
if (initialResponse.right.status === 401) {
const authHeader = initialResponse.right.headers["www-authenticate"];
if (initialResponse.status === 401) {
const authHeader = initialResponse.headers["www-authenticate"];

if (authHeader) {
const authParams = parseDigestAuthHeader(authHeader);
Expand All @@ -105,7 +102,7 @@ export async function fetchInitialDigestAuthInfo(
"Failed to parse authentication parameters from WWW-Authenticate header"
);
} else {
throw new Error(`Unexpected response: ${initialResponse.right.status}`);
throw new Error(`Unexpected response: ${initialResponse.status}`);
}
} catch (error) {
console.error("Error fetching initial digest auth info:", error);
Expand Down
2 changes: 0 additions & 2 deletions packages/hoppscotch-cli/src/utils/pre-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,6 @@ export async function getEffectiveRESTRequest(
// Step 3: Generate the Authorization header
const authHeaderValue = await generateDigestAuthHeader(digestAuthParams);

console.log("Digest Auth Header:", authHeaderValue);

effectiveFinalHeaders.push({
active: true,
key: "Authorization",
Expand Down