Show challenge when requested by server

This commit is contained in:
Fedor Indutny 2021-05-05 17:09:29 -07:00 committed by GitHub
parent 03c68da17d
commit 986d8a66bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
42 changed files with 1986 additions and 128 deletions

View file

@ -306,7 +306,8 @@ function getContentType(response: Response) {
return null;
}
type HeaderListType = { [name: string]: string };
type FetchHeaderListType = { [name: string]: string };
type HeaderListType = { [name: string]: string | ReadonlyArray<string> };
type HTTPCodeType = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
type RedactUrl = (url: string) => string;
@ -397,7 +398,7 @@ async function _promiseAjax(
'User-Agent': getUserAgent(options.version),
'X-Signal-Agent': 'OWD',
...options.headers,
} as HeaderListType,
} as FetchHeaderListType,
redirect: options.redirect,
agent,
ca: options.certificateAuthority,
@ -500,6 +501,7 @@ async function _promiseAjax(
makeHTTPError(
'promiseAjax: invalid response',
response.status,
response.headers.raw(),
result,
options.stack
)
@ -563,6 +565,7 @@ async function _promiseAjax(
makeHTTPError(
'promiseAjax: error response',
response.status,
response.headers.raw(),
result,
options.stack
)
@ -576,7 +579,7 @@ async function _promiseAjax(
window.log.error(options.type, url, 0, 'Error');
}
const stack = `${e.stack}\nInitial stack:\n${options.stack}`;
reject(makeHTTPError('promiseAjax catch', 0, e.toString(), stack));
reject(makeHTTPError('promiseAjax catch', 0, {}, e.toString(), stack));
});
});
}
@ -614,6 +617,7 @@ declare global {
interface Error {
code?: number | string;
response?: any;
responseHeaders?: HeaderListType;
warn?: boolean;
}
}
@ -621,6 +625,7 @@ declare global {
function makeHTTPError(
message: string,
providedCode: number,
headers: HeaderListType,
response: any,
stack?: string
) {
@ -628,6 +633,7 @@ function makeHTTPError(
const e = new Error(`${message}; code: ${code}`);
e.name = 'HTTPError';
e.code = code;
e.responseHeaders = headers;
if (DEBUG && response) {
e.stack += `\nresponse: ${response}`;
}
@ -670,6 +676,7 @@ const URL_CALLS = {
supportUnauthenticatedDelivery: 'v1/devices/unauthenticated_delivery',
updateDeviceName: 'v1/accounts/name',
whoami: 'v1/accounts/whoami',
challenge: 'v1/challenge',
};
type InitializeOptionsType = {
@ -875,6 +882,7 @@ export type WebAPIType = {
options: GroupCredentialsType
) => Promise<string>;
whoami: () => Promise<any>;
sendChallengeResponse: (challengeResponse: ChallengeType) => Promise<any>;
getConfig: () => Promise<
Array<{ name: string; enabled: boolean; value: string | null }>
>;
@ -912,6 +920,12 @@ export type ServerKeysType = {
identityKey: ArrayBuffer;
};
export type ChallengeType = {
readonly type: 'recaptcha';
readonly token: string;
readonly captcha: string;
};
export type ProxiedRequestOptionsType = {
returnArrayBuffer?: boolean;
start?: number;
@ -1035,6 +1049,7 @@ export function initialize({
updateDeviceName,
uploadGroupAvatar,
whoami,
sendChallengeResponse,
};
async function _ajax(param: AjaxOptionsType): Promise<any> {
@ -1105,6 +1120,14 @@ export function initialize({
});
}
async function sendChallengeResponse(challengeResponse: ChallengeType) {
return _ajax({
call: 'challenge',
httpType: 'PUT',
jsonData: challengeResponse,
});
}
async function getConfig() {
type ResType = {
config: Array<{ name: string; enabled: boolean; value: string | null }>;