Skip to content

Add error handling for optional field when left empty #594

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

Merged
merged 1 commit into from
Apr 25, 2025
Merged
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
7 changes: 6 additions & 1 deletion passkeys-backend/functions/registration/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ exports.handler = async (context, event, callback) => {

const { username, password } = context.getTwilioClient();

const androidOrigins = (keys) => {
if (!keys || keys.trim() === '""') return [];
return keys.split(',');
};

// Request body sent to passkeys verify URL call
/* eslint-disable camelcase */
const requestBody = {
Expand All @@ -35,7 +40,7 @@ exports.handler = async (context, event, callback) => {
name: 'PasskeySample',
origins: [
`https://${DOMAIN_NAME}`,
...(ANDROID_APP_KEYS?.split(',') ?? []),
...androidOrigins(ANDROID_APP_KEYS),
],
},
user: {
Expand Down
28 changes: 28 additions & 0 deletions passkeys-backend/tests/registration-start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,34 @@ describe('registration/start', () => {
);
});

// This is how the CodeExchange is populating the optional field if left empty
it('works with ANDROID_APP_KEYS empty string', (done) => {
const callback = (_, { _body }) => {
expect(axios.post).toHaveBeenCalledWith(
'https://api.com/Factors',
mockRequestBody,
{ auth: { password: 'mockPassword', username: 'mockUsername' } }
);
done();
};

const mockContextWithoutAndroidKeys = {
API_URL: 'https://api.com',
ANDROID_APP_KEYS: '""',
DOMAIN_NAME: 'example.com',
getTwilioClient: () => ({
username: 'mockUsername',
password: 'mockPassword',
}),
};

handlerFunction(
mockContextWithoutAndroidKeys,
{ username: 'user001' },
callback
);
});

it('calls the API with the expected request body', (done) => {
const modifiedRequest = structuredClone(mockRequestBody);
modifiedRequest.content.relying_party.origins.push('key1', 'key2', 'key3');
Expand Down