Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' into more_endpoints
Browse files Browse the repository at this point in the history
* master:
  Apply CORS headers to every endpoint for Web vault (#33)
  • Loading branch information
vvondra committed Sep 2, 2018
2 parents d4592fe + 3e4aa04 commit 0a2b7ef
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 84 deletions.
15 changes: 3 additions & 12 deletions src/ciphers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ export const postHandler = async (event, context, callback) => {

await touch(user);

callback(null, {
statusCode: 200,
body: JSON.stringify({ ...mapCipher(cipher), Edit: true }),
});
callback(null, utils.okResponse({ ...mapCipher(cipher), Edit: true }));
} catch (e) {
callback(null, utils.serverError('Server error saving vault item', e));
}
Expand Down Expand Up @@ -80,10 +77,7 @@ export const putHandler = async (event, context, callback) => {
cipher = await cipher.updateAsync();
await touch(user);

callback(null, {
statusCode: 200,
body: JSON.stringify({ ...mapCipher(cipher), Edit: true }),
});
callback(null, utils.okResponse({ ...mapCipher(cipher), Edit: true }));
} catch (e) {
callback(null, utils.serverError('Server error saving vault item', e));
}
Expand All @@ -108,10 +102,7 @@ export const deleteHandler = async (event, context, callback) => {
await Cipher.destroyAsync(user.get('uuid'), cipherUuid);
await touch(user);

callback(null, {
statusCode: 200,
body: '',
});
callback(null, utils.okResponse(''));
} catch (e) {
callback(null, utils.validationError(e.toString()));
}
Expand Down
15 changes: 3 additions & 12 deletions src/folders.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ export const postHandler = async (event, context, callback) => {
});
await touch(user);

callback(null, {
statusCode: 200,
body: JSON.stringify(mapFolder(folder)),
});
callback(null, utils.okResponse(mapFolder(folder)));
} catch (e) {
callback(null, utils.serverError('Server error saving folder', e));
}
Expand Down Expand Up @@ -82,10 +79,7 @@ export const putHandler = async (event, context, callback) => {

folder = await folder.updateAsync();

callback(null, {
statusCode: 200,
body: JSON.stringify(mapFolder(folder)),
});
callback(null, utils.okResponse(mapFolder(folder)));
} catch (e) {
callback(null, utils.serverError('Server error saving folder', e));
}
Expand All @@ -110,10 +104,7 @@ export const deleteHandler = async (event, context, callback) => {
await Folder.destroyAsync(user.get('uuid'), folderUuid);
await touch(user);

callback(null, {
statusCode: 200,
body: '',
});
callback(null, utils.okResponse(''));
} catch (e) {
callback(null, utils.validationError(e.toString()));
}
Expand Down
7 changes: 2 additions & 5 deletions src/import.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { normalizeBody, validationError } from './lib/api_utils';
import { normalizeBody, validationError, okResponse } from './lib/api_utils';
import { Cipher, Folder } from './lib/models';
import { loadContextFromHeader, buildCipherDocument, touch } from './lib/bitwarden';

Expand Down Expand Up @@ -142,8 +142,5 @@ export const postHandler = async (event, context, callback) => {

await touch(user);

callback(null, {
statusCode: 200,
body: '',
});
callback(null, okResponse(''));
};
42 changes: 19 additions & 23 deletions src/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,25 @@ export const handler = async (event, context, callback) => {
await user.updateAsync();

try {
callback(null, {
statusCode: 200,
headers: utils.CORS_HEADERS,
body: JSON.stringify({
access_token: tokens.accessToken,
expires_in: DEFAULT_VALIDITY,
token_type: 'Bearer',
refresh_token: tokens.refreshToken,
Key: user.get('key'),
Id: user.get('uuid'),
Name: user.get('name'),
Email: user.get('email'),
EmailVerified: user.get('emailVerified'),
Premium: user.get('premium'),
MasterPasswordHint: user.get('passwordHint'),
Culture: user.get('culture'),
TwoFactorEnabled: user.get('totpSecret'),
PrivateKey: user.get('privateKey'),
SecurityStamp: user.get('securityStamp'),
Organizations: '[]',
Object: 'profile',
}),
});
callback(null, utils.okResponse({
access_token: tokens.accessToken,
expires_in: DEFAULT_VALIDITY,
token_type: 'Bearer',
refresh_token: tokens.refreshToken,
Key: user.get('key'),
Id: user.get('uuid'),
Name: user.get('name'),
Email: user.get('email'),
EmailVerified: user.get('emailVerified'),
Premium: user.get('premium'),
MasterPasswordHint: user.get('passwordHint'),
Culture: user.get('culture'),
TwoFactorEnabled: user.get('totpSecret'),
PrivateKey: user.get('privateKey'),
SecurityStamp: user.get('securityStamp'),
Organizations: '[]',
Object: 'profile',
}));
} catch (e) {
callback(null, utils.serverError('Internal error', e));
}
Expand Down
9 changes: 9 additions & 0 deletions src/lib/api_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ export const CORS_HEADERS = {
'access-control-allow-headers': 'Content-Type,Authorization,Accept,Device-type,Pragma,Cache-Control',
};

export function okResponse(body) {
console.log('Success response', { body });
return {
statusCode: 200,
headers: CORS_HEADERS,
body: typeof body === 'string' ? body : JSON.stringify(body),
};
}

export function validationError(message) {
console.log('Validation error', { message });
return {
Expand Down
20 changes: 8 additions & 12 deletions src/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,14 @@ export const handler = async (event, context, callback) => {

device = await device.updateAsync();

callback(null, {
statusCode: 200,
headers: utils.CORS_HEADERS,
body: JSON.stringify({
access_token: tokens.accessToken,
expires_in: DEFAULT_VALIDITY,
token_type: 'Bearer',
refresh_token: tokens.refreshToken,
Key: user.get('key'),
PrivateKey: (user.get('privateKey') || '').toString('utf8'),
}),
});
callback(null, utils.okResponse({
access_token: tokens.accessToken,
expires_in: DEFAULT_VALIDITY,
token_type: 'Bearer',
refresh_token: tokens.refreshToken,
Key: user.get('key'),
PrivateKey: (user.get('privateKey') || '').toString('utf8'),
}));
} catch (e) {
callback(null, utils.serverError('Internal error', e));
}
Expand Down
12 changes: 4 additions & 8 deletions src/prelogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ export const handler = async (event, context, callback) => {
return;
}

callback(null, {
statusCode: 200,
headers: utils.CORS_HEADERS,
body: JSON.stringify({
Kdf: KDF_PBKDF2,
KdfIterations: user.get('kdfIterations') || KDF_PBKDF2_ITERATIONS_DEFAULT,
}),
});
callback(null, utils.okResponse({
Kdf: KDF_PBKDF2,
KdfIterations: user.get('kdfIterations') || KDF_PBKDF2_ITERATIONS_DEFAULT,
}));
};
8 changes: 1 addition & 7 deletions src/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,14 @@ export const handler = async (event, context, callback) => {
.select('COUNT')
.execAsync();

console.log(existingUser);

if (existingUser.Count > 0) {
callback(null, utils.validationError('E-mail already taken'));
return;
}

await User.createAsync(buildUserDocument(body));

callback(null, {
statusCode: 200,
headers: utils.CORS_HEADERS,
body: '',
});
callback(null, utils.okResponse(''));
} catch (e) {
callback(null, utils.serverError(e.message, e));
}
Expand Down
6 changes: 1 addition & 5 deletions src/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,5 @@ export const handler = async (event, context, callback) => {
Object: 'sync',
};

callback(null, {
statusCode: 200,
headers: utils.CORS_HEADERS,
body: JSON.stringify(response),
});
callback(null, utils.okResponse(response));
};

0 comments on commit 0a2b7ef

Please sign in to comment.