Skip to content

[INS-1805] Add Auth Header for WebSocket - Part 1 #5108

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions packages/insomnia/src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,20 @@ export const AUTH_NETRC = 'netrc';
export const AUTH_ASAP = 'asap';
export const HAWK_ALGORITHM_SHA256 = 'sha256';
export const HAWK_ALGORITHM_SHA1 = 'sha1';
export type AuthType =
| 'none'
| 'oauth2'
| 'oauth1'
| 'basic'
| 'digest'
| 'bearer'
| 'ntlm'
| 'hawk'
| 'iam'
| 'netrc'
| 'asap'
| 'sha256'
| 'sha1';

// json-order constants
export const JSON_ORDER_PREFIX = '&';
Expand Down
98 changes: 0 additions & 98 deletions packages/insomnia/src/models/request.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
import { deconstructQueryStringToParams } from 'insomnia-url';

import {
AUTH_ASAP,
AUTH_AWS_IAM,
AUTH_BASIC,
AUTH_DIGEST,
AUTH_HAWK,
AUTH_NETRC,
AUTH_NONE,
AUTH_NTLM,
AUTH_OAUTH_1,
AUTH_OAUTH_2,
CONTENT_TYPE_FILE,
CONTENT_TYPE_FORM_DATA,
CONTENT_TYPE_FORM_URLENCODED,
CONTENT_TYPE_GRAPHQL,
CONTENT_TYPE_JSON,
CONTENT_TYPE_OTHER,
getContentTypeFromHeaders,
HAWK_ALGORITHM_SHA256,
METHOD_GET,
METHOD_POST,
} from '../common/constants';
import { database as db } from '../common/database';
import { getContentTypeHeader } from '../common/misc';
import { SIGNATURE_METHOD_HMAC_SHA1 } from '../network/o-auth-1/constants';
import { GRANT_TYPE_AUTHORIZATION_CODE } from '../network/o-auth-2/constants';
import type { BaseModel } from './index';

export const name = 'Request';
Expand Down Expand Up @@ -121,92 +109,6 @@ export function init(): BaseRequest {
};
}

export function newAuth(type: string, oldAuth: RequestAuthentication = {}): RequestAuthentication {
switch (type) {
// No Auth
case AUTH_NONE:
return {};

// HTTP Basic Authentication
case AUTH_BASIC:
return {
type,
useISO88591: oldAuth.useISO88591 || false,
disabled: oldAuth.disabled || false,
username: oldAuth.username || '',
password: oldAuth.password || '',
};

case AUTH_DIGEST:
case AUTH_NTLM:
return {
type,
disabled: oldAuth.disabled || false,
username: oldAuth.username || '',
password: oldAuth.password || '',
};

case AUTH_OAUTH_1:
return {
type,
disabled: false,
signatureMethod: SIGNATURE_METHOD_HMAC_SHA1,
consumerKey: '',
consumerSecret: '',
tokenKey: '',
tokenSecret: '',
privateKey: '',
version: '1.0',
nonce: '',
timestamp: '',
callback: '',
};

// OAuth 2.0
case AUTH_OAUTH_2:
return {
type,
grantType: GRANT_TYPE_AUTHORIZATION_CODE,
};

// Aws IAM
case AUTH_AWS_IAM:
return {
type,
disabled: oldAuth.disabled || false,
accessKeyId: oldAuth.accessKeyId || '',
secretAccessKey: oldAuth.secretAccessKey || '',
sessionToken: oldAuth.sessionToken || '',
};

// Hawk
case AUTH_HAWK:
return {
type,
algorithm: HAWK_ALGORITHM_SHA256,
};

// Atlassian ASAP
case AUTH_ASAP:
return {
type,
issuer: '',
subject: '',
audience: '',
additionalClaims: '',
keyId: '',
privateKey: '',
};

// Types needing no defaults
case AUTH_NETRC:
default:
return {
type,
};
}
}

export function newBodyNone(): RequestBody {
return {};
}
Expand Down
22 changes: 20 additions & 2 deletions packages/insomnia/src/models/websocket-request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { database } from '../common/database';
import type { BaseModel } from '.';
import { RequestHeader } from './request';
import { RequestAuthentication, RequestHeader } from './request';

export const name = 'WebSocket Request';

Expand All @@ -18,6 +18,7 @@ export interface BaseWebSocketRequest {
url: string;
metaSortKey: number;
headers: RequestHeader[];
authentication: RequestAuthentication;
}

export type WebSocketRequest = BaseModel & BaseWebSocketRequest & { type: typeof type };
Expand All @@ -35,9 +36,26 @@ export const init = (): BaseWebSocketRequest => ({
url: '',
metaSortKey: -1 * Date.now(),
headers: [],
authentication: {},
});
/**
* Ensure the request.authentication.type property is added
* @param request
*/
function migrateAuthType(request: WebSocketRequest) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussed to remove this

const isAuthSet = request.authentication && request.authentication.username;

if (isAuthSet && !request.authentication.type) {
request.authentication.type = 'basic';
}

return request;
}

export const migrate = (doc: WebSocketRequest) => doc;
export const migrate = (doc: WebSocketRequest) => {
doc = migrateAuthType(doc);
return doc;
};

export const create = (patch: Partial<WebSocketRequest> = {}) => {
if (!patch.parentId) {
Expand Down
Loading