fix: handle profile page after step 9 phone verify
This commit is contained in:
@@ -9494,6 +9494,8 @@ const phoneVerificationHelpers = self.MultiPageBackgroundPhoneVerification?.crea
|
|||||||
DEFAULT_PHONE_CODE_POLL_INTERVAL_SECONDS,
|
DEFAULT_PHONE_CODE_POLL_INTERVAL_SECONDS,
|
||||||
DEFAULT_PHONE_CODE_POLL_ROUNDS,
|
DEFAULT_PHONE_CODE_POLL_ROUNDS,
|
||||||
ensureStep8SignupPageReady,
|
ensureStep8SignupPageReady,
|
||||||
|
generateRandomBirthday,
|
||||||
|
generateRandomName,
|
||||||
getOAuthFlowRemainingMs,
|
getOAuthFlowRemainingMs,
|
||||||
getOAuthFlowStepTimeoutMs,
|
getOAuthFlowStepTimeoutMs,
|
||||||
getState,
|
getState,
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
addLog,
|
addLog,
|
||||||
ensureStep8SignupPageReady,
|
ensureStep8SignupPageReady,
|
||||||
fetchImpl = (...args) => fetch(...args),
|
fetchImpl = (...args) => fetch(...args),
|
||||||
|
generateRandomBirthday,
|
||||||
|
generateRandomName,
|
||||||
getOAuthFlowStepTimeoutMs,
|
getOAuthFlowStepTimeoutMs,
|
||||||
getState,
|
getState,
|
||||||
sendToContentScript,
|
sendToContentScript,
|
||||||
@@ -3295,13 +3297,35 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function submitPhoneVerificationCode(tabId, code) {
|
async function submitPhoneVerificationCode(tabId, code) {
|
||||||
|
const signupProfile = (
|
||||||
|
typeof generateRandomName === 'function'
|
||||||
|
&& typeof generateRandomBirthday === 'function'
|
||||||
|
)
|
||||||
|
? (() => {
|
||||||
|
const name = generateRandomName();
|
||||||
|
const birthday = generateRandomBirthday();
|
||||||
|
if (!name?.firstName || !name?.lastName || !birthday) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
firstName: name.firstName,
|
||||||
|
lastName: name.lastName,
|
||||||
|
year: birthday.year,
|
||||||
|
month: birthday.month,
|
||||||
|
day: birthday.day,
|
||||||
|
};
|
||||||
|
})()
|
||||||
|
: null;
|
||||||
const timeoutMs = typeof getOAuthFlowStepTimeoutMs === 'function'
|
const timeoutMs = typeof getOAuthFlowStepTimeoutMs === 'function'
|
||||||
? await getOAuthFlowStepTimeoutMs(45000, { step: 9, actionLabel: 'submit phone verification code' })
|
? await getOAuthFlowStepTimeoutMs(45000, { step: 9, actionLabel: 'submit phone verification code' })
|
||||||
: 45000;
|
: 45000;
|
||||||
const result = await sendToContentScriptResilient('signup-page', {
|
const result = await sendToContentScriptResilient('signup-page', {
|
||||||
type: 'SUBMIT_PHONE_VERIFICATION_CODE',
|
type: 'SUBMIT_PHONE_VERIFICATION_CODE',
|
||||||
source: 'background',
|
source: 'background',
|
||||||
payload: { code },
|
payload: {
|
||||||
|
code,
|
||||||
|
...(signupProfile ? { signupProfile } : {}),
|
||||||
|
},
|
||||||
}, {
|
}, {
|
||||||
timeoutMs,
|
timeoutMs,
|
||||||
responseTimeoutMs: timeoutMs,
|
responseTimeoutMs: timeoutMs,
|
||||||
|
|||||||
+64
-1
@@ -84,7 +84,7 @@ async function handleCommand(message) {
|
|||||||
case 'SUBMIT_PHONE_NUMBER':
|
case 'SUBMIT_PHONE_NUMBER':
|
||||||
return await phoneAuthHelpers.submitPhoneNumber(message.payload);
|
return await phoneAuthHelpers.submitPhoneNumber(message.payload);
|
||||||
case 'SUBMIT_PHONE_VERIFICATION_CODE':
|
case 'SUBMIT_PHONE_VERIFICATION_CODE':
|
||||||
return await phoneAuthHelpers.submitPhoneVerificationCode(message.payload);
|
return await submitPhoneVerificationCodeWithProfileFallback(message.payload);
|
||||||
case 'RESEND_PHONE_VERIFICATION_CODE':
|
case 'RESEND_PHONE_VERIFICATION_CODE':
|
||||||
return await phoneAuthHelpers.resendPhoneVerificationCode();
|
return await phoneAuthHelpers.resendPhoneVerificationCode();
|
||||||
case 'RETURN_TO_ADD_PHONE':
|
case 'RETURN_TO_ADD_PHONE':
|
||||||
@@ -1338,6 +1338,69 @@ const phoneAuthHelpers = self.MultiPagePhoneAuth?.createPhoneAuthHelpers?.({
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function waitForPhoneVerificationProfileCompletion(timeout = 30000) {
|
||||||
|
const start = Date.now();
|
||||||
|
|
||||||
|
while (Date.now() - start < timeout) {
|
||||||
|
throwIfStopped();
|
||||||
|
|
||||||
|
if (isStep8Ready()) {
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
consentReady: true,
|
||||||
|
url: location.href,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isAddPhonePageReady()) {
|
||||||
|
return {
|
||||||
|
returnedToAddPhone: true,
|
||||||
|
url: location.href,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
await sleep(150);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isStep8Ready()) {
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
consentReady: true,
|
||||||
|
url: location.href,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
assumed: true,
|
||||||
|
url: location.href,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function submitPhoneVerificationCodeWithProfileFallback(payload = {}) {
|
||||||
|
const result = await phoneAuthHelpers.submitPhoneVerificationCode(payload);
|
||||||
|
if (!(isStep5Ready() || isSignupProfilePageUrl(result?.url || location.href))) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
const signupProfile = payload?.signupProfile || {};
|
||||||
|
if (!signupProfile.firstName || !signupProfile.lastName) {
|
||||||
|
throw new Error('手机号验证后进入资料页,但未提供步骤 5 所需的姓名数据。');
|
||||||
|
}
|
||||||
|
|
||||||
|
await step5_fillNameBirthday(signupProfile);
|
||||||
|
const nextState = await waitForPhoneVerificationProfileCompletion();
|
||||||
|
const mergedResult = {
|
||||||
|
...result,
|
||||||
|
...nextState,
|
||||||
|
profileCompleted: true,
|
||||||
|
};
|
||||||
|
if (nextState.consentReady || nextState.returnedToAddPhone) {
|
||||||
|
delete mergedResult.assumed;
|
||||||
|
}
|
||||||
|
return mergedResult;
|
||||||
|
}
|
||||||
|
|
||||||
function normalizeInlineText(text) {
|
function normalizeInlineText(text) {
|
||||||
return (text || '').replace(/\s+/g, ' ').trim();
|
return (text || '').replace(/\s+/g, ' ').trim();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1603,6 +1603,98 @@ test('phone verification helper completes add-phone flow, clears current activat
|
|||||||
assert.deepStrictEqual(actions, ['getPrices', 'getNumber', 'getStatus', 'setStatus']);
|
assert.deepStrictEqual(actions, ['getPrices', 'getNumber', 'getStatus', 'setStatus']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('phone verification helper forwards signup profile payload when submitting the phone verification code', async () => {
|
||||||
|
let currentState = {
|
||||||
|
heroSmsApiKey: 'demo-key',
|
||||||
|
verificationResendCount: 0,
|
||||||
|
currentPhoneActivation: null,
|
||||||
|
reusablePhoneActivation: null,
|
||||||
|
};
|
||||||
|
const submittedPayloads = [];
|
||||||
|
|
||||||
|
const helpers = api.createPhoneVerificationHelpers({
|
||||||
|
addLog: async () => {},
|
||||||
|
ensureStep8SignupPageReady: async () => {},
|
||||||
|
fetchImpl: async (url) => {
|
||||||
|
const parsedUrl = new URL(url);
|
||||||
|
const action = parsedUrl.searchParams.get('action');
|
||||||
|
if (action === 'getPrices') {
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
text: async () => buildHeroSmsPricesPayload(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (action === 'getNumber') {
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
text: async () => 'ACCESS_NUMBER:123456:66959916439',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (action === 'getStatus') {
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
text: async () => 'STATUS_OK:654321',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (action === 'setStatus') {
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
text: async () => 'ACCESS_ACTIVATION',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
throw new Error(`Unexpected HeroSMS action: ${action}`);
|
||||||
|
},
|
||||||
|
generateRandomBirthday: () => ({ year: 2003, month: 6, day: 19 }),
|
||||||
|
generateRandomName: () => ({ firstName: 'Ada', lastName: 'Lovelace' }),
|
||||||
|
getOAuthFlowStepTimeoutMs: async (defaultTimeoutMs) => defaultTimeoutMs,
|
||||||
|
getState: async () => ({ ...currentState }),
|
||||||
|
sendToContentScriptResilient: async (_source, message) => {
|
||||||
|
if (message.type === 'SUBMIT_PHONE_NUMBER') {
|
||||||
|
return {
|
||||||
|
phoneVerificationPage: true,
|
||||||
|
url: 'https://auth.openai.com/phone-verification',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (message.type === 'SUBMIT_PHONE_VERIFICATION_CODE') {
|
||||||
|
submittedPayloads.push(message.payload);
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
consentReady: true,
|
||||||
|
url: 'https://auth.openai.com/authorize',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
throw new Error(`Unexpected content-script message: ${message.type}`);
|
||||||
|
},
|
||||||
|
setState: async (updates) => {
|
||||||
|
currentState = { ...currentState, ...updates };
|
||||||
|
},
|
||||||
|
sleepWithStop: async () => {},
|
||||||
|
throwIfStopped: () => {},
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await helpers.completePhoneVerificationFlow(1, {
|
||||||
|
addPhonePage: true,
|
||||||
|
phoneVerificationPage: false,
|
||||||
|
url: 'https://auth.openai.com/add-phone',
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepStrictEqual(result, {
|
||||||
|
success: true,
|
||||||
|
consentReady: true,
|
||||||
|
url: 'https://auth.openai.com/authorize',
|
||||||
|
});
|
||||||
|
assert.deepStrictEqual(submittedPayloads, [{
|
||||||
|
code: '654321',
|
||||||
|
signupProfile: {
|
||||||
|
firstName: 'Ada',
|
||||||
|
lastName: 'Lovelace',
|
||||||
|
year: 2003,
|
||||||
|
month: 6,
|
||||||
|
day: 19,
|
||||||
|
},
|
||||||
|
}]);
|
||||||
|
});
|
||||||
|
|
||||||
test('phone verification helper uses the configured HeroSMS country for both number acquisition and add-phone submission', async () => {
|
test('phone verification helper uses the configured HeroSMS country for both number acquisition and add-phone submission', async () => {
|
||||||
const requests = [];
|
const requests = [];
|
||||||
const submittedPayloads = [];
|
const submittedPayloads = [];
|
||||||
|
|||||||
@@ -0,0 +1,222 @@
|
|||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
const fs = require('node:fs');
|
||||||
|
|
||||||
|
const source = fs.readFileSync('content/signup-page.js', 'utf8');
|
||||||
|
|
||||||
|
function extractFunction(name) {
|
||||||
|
const markers = [`async function ${name}(`, `function ${name}(`];
|
||||||
|
const start = markers
|
||||||
|
.map((marker) => source.indexOf(marker))
|
||||||
|
.find((index) => index >= 0);
|
||||||
|
if (start < 0) {
|
||||||
|
throw new Error(`missing function ${name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
let parenDepth = 0;
|
||||||
|
let signatureEnded = false;
|
||||||
|
let braceStart = -1;
|
||||||
|
for (let i = start; i < source.length; i += 1) {
|
||||||
|
const ch = source[i];
|
||||||
|
if (ch === '(') {
|
||||||
|
parenDepth += 1;
|
||||||
|
} else if (ch === ')') {
|
||||||
|
parenDepth -= 1;
|
||||||
|
if (parenDepth === 0) {
|
||||||
|
signatureEnded = true;
|
||||||
|
}
|
||||||
|
} else if (ch === '{' && signatureEnded) {
|
||||||
|
braceStart = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (braceStart < 0) {
|
||||||
|
throw new Error(`missing body for function ${name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
let depth = 0;
|
||||||
|
let end = braceStart;
|
||||||
|
for (; end < source.length; end += 1) {
|
||||||
|
const ch = source[end];
|
||||||
|
if (ch === '{') depth += 1;
|
||||||
|
if (ch === '}') {
|
||||||
|
depth -= 1;
|
||||||
|
if (depth === 0) {
|
||||||
|
end += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return source.slice(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
test('step 9 auto-fills profile when phone verification lands on signup profile page', async () => {
|
||||||
|
const api = new Function(`
|
||||||
|
const profilePayloads = [];
|
||||||
|
const logs = [];
|
||||||
|
let consentReady = false;
|
||||||
|
|
||||||
|
const location = {
|
||||||
|
href: 'https://auth.openai.com/create-account/profile',
|
||||||
|
};
|
||||||
|
|
||||||
|
function log(message, level = 'info') {
|
||||||
|
logs.push({ message, level });
|
||||||
|
}
|
||||||
|
|
||||||
|
function isStep5Ready() {
|
||||||
|
return !consentReady;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isSignupProfilePageUrl(url = location.href) {
|
||||||
|
return /\\/create-account\\/profile(?:[/?#]|$)/i.test(String(url || ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
function isStep8Ready() {
|
||||||
|
return consentReady;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isAddPhonePageReady() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sleep() {}
|
||||||
|
function throwIfStopped() {}
|
||||||
|
|
||||||
|
const phoneAuthHelpers = {
|
||||||
|
async submitPhoneVerificationCode() {
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
assumed: true,
|
||||||
|
url: location.href,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
async function step5_fillNameBirthday(payload) {
|
||||||
|
profilePayloads.push(payload);
|
||||||
|
consentReady = true;
|
||||||
|
location.href = 'https://auth.openai.com/authorize';
|
||||||
|
return {
|
||||||
|
skippedPostSubmitCheck: true,
|
||||||
|
directProceedToStep6: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
${extractFunction('waitForPhoneVerificationProfileCompletion')}
|
||||||
|
${extractFunction('submitPhoneVerificationCodeWithProfileFallback')}
|
||||||
|
|
||||||
|
return {
|
||||||
|
run(payload) {
|
||||||
|
return submitPhoneVerificationCodeWithProfileFallback(payload);
|
||||||
|
},
|
||||||
|
snapshot() {
|
||||||
|
return {
|
||||||
|
profilePayloads,
|
||||||
|
logs,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
`)();
|
||||||
|
|
||||||
|
const result = await api.run({
|
||||||
|
code: '123456',
|
||||||
|
signupProfile: {
|
||||||
|
firstName: 'Ada',
|
||||||
|
lastName: 'Lovelace',
|
||||||
|
year: 2003,
|
||||||
|
month: 6,
|
||||||
|
day: 19,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepStrictEqual(result, {
|
||||||
|
success: true,
|
||||||
|
consentReady: true,
|
||||||
|
profileCompleted: true,
|
||||||
|
url: 'https://auth.openai.com/authorize',
|
||||||
|
});
|
||||||
|
assert.deepStrictEqual(api.snapshot().profilePayloads, [{
|
||||||
|
firstName: 'Ada',
|
||||||
|
lastName: 'Lovelace',
|
||||||
|
year: 2003,
|
||||||
|
month: 6,
|
||||||
|
day: 19,
|
||||||
|
}]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('step 9 leaves the existing consent flow untouched when no profile page appears', async () => {
|
||||||
|
const api = new Function(`
|
||||||
|
const profilePayloads = [];
|
||||||
|
const location = {
|
||||||
|
href: 'https://auth.openai.com/authorize',
|
||||||
|
};
|
||||||
|
|
||||||
|
function isStep5Ready() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isSignupProfilePageUrl(url = location.href) {
|
||||||
|
return /\\/create-account\\/profile(?:[/?#]|$)/i.test(String(url || ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
function isStep8Ready() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isAddPhonePageReady() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function throwIfStopped() {}
|
||||||
|
async function sleep() {}
|
||||||
|
|
||||||
|
const phoneAuthHelpers = {
|
||||||
|
async submitPhoneVerificationCode() {
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
consentReady: true,
|
||||||
|
url: location.href,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
async function step5_fillNameBirthday(payload) {
|
||||||
|
profilePayloads.push(payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
${extractFunction('waitForPhoneVerificationProfileCompletion')}
|
||||||
|
${extractFunction('submitPhoneVerificationCodeWithProfileFallback')}
|
||||||
|
|
||||||
|
return {
|
||||||
|
run(payload) {
|
||||||
|
return submitPhoneVerificationCodeWithProfileFallback(payload);
|
||||||
|
},
|
||||||
|
snapshot() {
|
||||||
|
return {
|
||||||
|
profilePayloads,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
`)();
|
||||||
|
|
||||||
|
const result = await api.run({
|
||||||
|
code: '123456',
|
||||||
|
signupProfile: {
|
||||||
|
firstName: 'Ada',
|
||||||
|
lastName: 'Lovelace',
|
||||||
|
year: 2003,
|
||||||
|
month: 6,
|
||||||
|
day: 19,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepStrictEqual(result, {
|
||||||
|
success: true,
|
||||||
|
consentReady: true,
|
||||||
|
url: 'https://auth.openai.com/authorize',
|
||||||
|
});
|
||||||
|
assert.deepStrictEqual(api.snapshot().profilePayloads, []);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user