1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 08:13:42 +00:00

convert request/response models to jslib

This commit is contained in:
Kyle Spearrin
2018-01-08 14:45:34 -05:00
parent 5de4aad525
commit a78b8ec79c
38 changed files with 74 additions and 733 deletions

View File

@@ -1,4 +1,4 @@
import { AttachmentResponse } from '../response/attachmentResponse';
import { Response } from '@bitwarden/jslib';
class AttachmentData {
id: string;
@@ -7,7 +7,7 @@ class AttachmentData {
size: number;
sizeName: string;
constructor(response: AttachmentResponse) {
constructor(response: Response.Attachment) {
this.id = response.id;
this.url = response.url;
this.fileName = response.fileName;

View File

@@ -1,4 +1,4 @@
import { Enums } from '@bitwarden/jslib';
import { Data, Enums, Response } from '@bitwarden/jslib';
import { AttachmentData } from './attachmentData';
import { CardData } from './cardData';
@@ -7,8 +7,6 @@ import { IdentityData } from './identityData';
import { LoginData } from './loginData';
import { SecureNoteData } from './secureNoteData';
import { CipherResponse } from '../response/cipherResponse';
class CipherData {
id: string;
organizationId: string;
@@ -30,7 +28,7 @@ class CipherData {
attachments?: AttachmentData[];
collectionIds?: string[];
constructor(response: CipherResponse, userId: string, collectionIds?: string[]) {
constructor(response: Response.Cipher, userId: string, collectionIds?: string[]) {
this.id = response.id;
this.organizationId = response.organizationId;
this.folderId = response.folderId;

View File

@@ -1,11 +1,11 @@
import { CollectionResponse } from '../response/collectionResponse';
import { Response } from '@bitwarden/jslib';
class CollectionData {
id: string;
organizationId: string;
name: string;
constructor(response: CollectionResponse) {
constructor(response: Response.Collection) {
this.id = response.id;
this.organizationId = response.organizationId;
this.name = response.name;

View File

@@ -1,4 +1,4 @@
import { FolderResponse } from '../response/folderResponse';
import { Response } from '@bitwarden/jslib';
class FolderData {
id: string;
@@ -6,7 +6,7 @@ class FolderData {
name: string;
revisionDate: string;
constructor(response: FolderResponse, userId: string) {
constructor(response: Response.Folder, userId: string) {
this.userId = userId;
this.name = response.name;
this.id = response.id;

View File

@@ -1,34 +0,0 @@
import { FolderData } from '../data/folderData';
import { CipherString } from './cipherString';
import Domain from './domain';
class Folder extends Domain {
id: string;
name: CipherString;
constructor(obj?: FolderData, alreadyEncrypted: boolean = false) {
super();
if (obj == null) {
return;
}
this.buildDomainModel(this, obj, {
id: null,
name: null,
}, alreadyEncrypted, ['id']);
}
decrypt(): Promise<any> {
const model = {
id: this.id,
};
return this.decryptObj(model, {
name: null,
}, null);
}
}
export { Folder };
(window as any).Folder = Folder;

View File

@@ -1,89 +0,0 @@
import { Enums } from '@bitwarden/jslib';
class CipherRequest {
type: Enums.CipherType;
folderId: string;
organizationId: string;
name: string;
notes: string;
favorite: boolean;
login: any;
secureNote: any;
card: any;
identity: any;
fields: any[];
constructor(cipher: any) {
this.type = cipher.type;
this.folderId = cipher.folderId;
this.organizationId = cipher.organizationId;
this.name = cipher.name ? cipher.name.encryptedString : null;
this.notes = cipher.notes ? cipher.notes.encryptedString : null;
this.favorite = cipher.favorite;
switch (this.type) {
case Enums.CipherType.Login:
this.login = {
uri: cipher.login.uri ? cipher.login.uri.encryptedString : null,
username: cipher.login.username ? cipher.login.username.encryptedString : null,
password: cipher.login.password ? cipher.login.password.encryptedString : null,
totp: cipher.login.totp ? cipher.login.totp.encryptedString : null,
};
break;
case Enums.CipherType.SecureNote:
this.secureNote = {
type: cipher.secureNote.type,
};
break;
case Enums.CipherType.Card:
this.card = {
cardholderName: cipher.card.cardholderName ? cipher.card.cardholderName.encryptedString : null,
brand: cipher.card.brand ? cipher.card.brand.encryptedString : null,
number: cipher.card.number ? cipher.card.number.encryptedString : null,
expMonth: cipher.card.expMonth ? cipher.card.expMonth.encryptedString : null,
expYear: cipher.card.expYear ? cipher.card.expYear.encryptedString : null,
code: cipher.card.code ? cipher.card.code.encryptedString : null,
};
break;
case Enums.CipherType.Identity:
this.identity = {
title: cipher.identity.title ? cipher.identity.title.encryptedString : null,
firstName: cipher.identity.firstName ? cipher.identity.firstName.encryptedString : null,
middleName: cipher.identity.middleName ? cipher.identity.middleName.encryptedString : null,
lastName: cipher.identity.lastName ? cipher.identity.lastName.encryptedString : null,
address1: cipher.identity.address1 ? cipher.identity.address1.encryptedString : null,
address2: cipher.identity.address2 ? cipher.identity.address2.encryptedString : null,
address3: cipher.identity.address3 ? cipher.identity.address3.encryptedString : null,
city: cipher.identity.city ? cipher.identity.city.encryptedString : null,
state: cipher.identity.state ? cipher.identity.state.encryptedString : null,
postalCode: cipher.identity.postalCode ? cipher.identity.postalCode.encryptedString : null,
country: cipher.identity.country ? cipher.identity.country.encryptedString : null,
company: cipher.identity.company ? cipher.identity.company.encryptedString : null,
email: cipher.identity.email ? cipher.identity.email.encryptedString : null,
phone: cipher.identity.phone ? cipher.identity.phone.encryptedString : null,
ssn: cipher.identity.ssn ? cipher.identity.ssn.encryptedString : null,
username: cipher.identity.username ? cipher.identity.username.encryptedString : null,
passportNumber: cipher.identity.passportNumber ?
cipher.identity.passportNumber.encryptedString : null,
licenseNumber: cipher.identity.licenseNumber ? cipher.identity.licenseNumber.encryptedString : null,
};
break;
default:
break;
}
if (cipher.fields) {
this.fields = [];
cipher.fields.forEach((field: any) => {
this.fields.push({
type: field.type,
name: field.name ? field.name.encryptedString : null,
value: field.value ? field.value.encryptedString : null,
});
});
}
}
}
export { CipherRequest };
(window as any).CipherRequest = CipherRequest;

View File

@@ -1,18 +0,0 @@
import { Abstractions, Enums } from '@bitwarden/jslib';
class DeviceRequest {
type: Enums.DeviceType;
name: string;
identifier: string;
pushToken?: string;
constructor(appId: string, platformUtilsService: Abstractions.PlatformUtilsService) {
this.type = platformUtilsService.getDevice();
this.name = platformUtilsService.getDeviceString();
this.identifier = appId;
this.pushToken = null;
}
}
export { DeviceRequest };
(window as any).DeviceRequest = DeviceRequest;

View File

@@ -1,10 +0,0 @@
class DeviceTokenRequest {
pushToken: string;
constructor() {
this.pushToken = null;
}
}
export { DeviceTokenRequest };
(window as any).DeviceTokenRequest = DeviceTokenRequest;

View File

@@ -1,12 +0,0 @@
import { Folder } from '../domain/folder';
class FolderRequest {
name: string;
constructor(folder: Folder) {
this.name = folder.name ? folder.name.encryptedString : null;
}
}
export { FolderRequest };
(window as any).FolderRequest = FolderRequest;

View File

@@ -1,10 +0,0 @@
class PasswordHintRequest {
email: string;
constructor(email: string) {
this.email = email;
}
}
export { PasswordHintRequest };
(window as any).PasswordHintRequest = PasswordHintRequest;

View File

@@ -1,18 +0,0 @@
class RegisterRequest {
name: string;
email: string;
masterPasswordHash: string;
masterPasswordHint: string;
key: string;
constructor(email: string, masterPasswordHash: string, masterPasswordHint: string, key: string) {
this.name = null;
this.email = email;
this.masterPasswordHash = masterPasswordHash;
this.masterPasswordHint = masterPasswordHint ? masterPasswordHint : null;
this.key = key;
}
}
export { RegisterRequest };
(window as any).RegisterRequest = RegisterRequest;

View File

@@ -1,49 +0,0 @@
import { DeviceRequest } from './deviceRequest';
class TokenRequest {
email: string;
masterPasswordHash: string;
token: string;
provider: number;
remember: boolean;
device?: DeviceRequest;
constructor(email: string, masterPasswordHash: string, provider: number,
token: string, remember: boolean, device?: DeviceRequest) {
this.email = email;
this.masterPasswordHash = masterPasswordHash;
this.token = token;
this.provider = provider;
this.remember = remember;
this.device = device != null ? device : null;
}
toIdentityToken() {
const obj: any = {
grant_type: 'password',
username: this.email,
password: this.masterPasswordHash,
scope: 'api offline_access',
client_id: 'browser',
};
if (this.device) {
obj.deviceType = this.device.type;
obj.deviceIdentifier = this.device.identifier;
obj.deviceName = this.device.name;
// no push tokens for browser apps yet
// obj.devicePushToken = this.device.pushToken;
}
if (this.token && this.provider !== null && (typeof this.provider !== 'undefined')) {
obj.twoFactorToken = this.token;
obj.twoFactorProvider = this.provider;
obj.twoFactorRemember = this.remember ? '1' : '0';
}
return obj;
}
}
export { TokenRequest };
(window as any).TokenRequest = TokenRequest;

View File

@@ -1,12 +0,0 @@
class TwoFactorEmailRequest {
email: string;
masterPasswordHash: string;
constructor(email: string, masterPasswordHash: string) {
this.email = email;
this.masterPasswordHash = masterPasswordHash;
}
}
export { TwoFactorEmailRequest };
(window as any).TwoFactorEmailRequest = TwoFactorEmailRequest;

View File

@@ -1,18 +0,0 @@
class AttachmentResponse {
id: string;
url: string;
fileName: string;
size: number;
sizeName: string;
constructor(response: any) {
this.id = response.Id;
this.url = response.Url;
this.fileName = response.FileName;
this.size = response.Size;
this.sizeName = response.SizeName;
}
}
export { AttachmentResponse };
(window as any).AttachmentResponse = AttachmentResponse;

View File

@@ -1,44 +0,0 @@
import { AttachmentResponse } from './attachmentResponse';
class CipherResponse {
id: string;
organizationId: string;
folderId: string;
type: number;
favorite: boolean;
edit: boolean;
organizationUseTotp: boolean;
data: any;
revisionDate: string;
attachments: AttachmentResponse[];
collectionIds: string[];
constructor(response: any) {
this.id = response.Id;
this.organizationId = response.OrganizationId;
this.folderId = response.FolderId;
this.type = response.Type;
this.favorite = response.Favorite;
this.edit = response.Edit;
this.organizationUseTotp = response.OrganizationUseTotp;
this.data = response.Data;
this.revisionDate = response.RevisionDate;
if (response.Attachments != null) {
this.attachments = [];
response.Attachments.forEach((attachment: any) => {
this.attachments.push(new AttachmentResponse(attachment));
});
}
if (response.CollectionIds) {
this.collectionIds = [];
response.CollectionIds.forEach((id: string) => {
this.collectionIds.push(id);
});
}
}
}
export { CipherResponse };
(window as any).CipherResponse = CipherResponse;

View File

@@ -1,14 +0,0 @@
class CollectionResponse {
id: string;
organizationId: string;
name: string;
constructor(response: any) {
this.id = response.Id;
this.organizationId = response.OrganizationId;
this.name = response.Name;
}
}
export { CollectionResponse };
(window as any).CollectionResponse = CollectionResponse;

View File

@@ -1,20 +0,0 @@
import { Enums } from '@bitwarden/jslib';
class DeviceResponse {
id: string;
name: number;
identifier: string;
type: Enums.DeviceType;
creationDate: string;
constructor(response: any) {
this.id = response.Id;
this.name = response.Name;
this.identifier = response.Identifier;
this.type = response.Type;
this.creationDate = response.CreationDate;
}
}
export { DeviceResponse };
(window as any).DeviceResponse = DeviceResponse;

View File

@@ -1,20 +0,0 @@
import { GlobalDomainResponse } from './globalDomainResponse';
class DomainsResponse {
equivalentDomains: string[][];
globalEquivalentDomains: GlobalDomainResponse[] = [];
constructor(response: any) {
this.equivalentDomains = response.EquivalentDomains;
this.globalEquivalentDomains = [];
if (response.GlobalEquivalentDomains) {
response.GlobalEquivalentDomains.forEach((domain: any) => {
this.globalEquivalentDomains.push(new GlobalDomainResponse(domain));
});
}
}
}
export { DomainsResponse };
(window as any).DomainsResponse = DomainsResponse;

View File

@@ -1,37 +0,0 @@
class ErrorResponse {
message: string;
validationErrors: { [key: string]: string[]; };
statusCode: number;
constructor(response: any, status: number, identityResponse?: boolean) {
let errorModel = null;
if (identityResponse && response && response.ErrorModel) {
errorModel = response.ErrorModel;
} else if (response) {
errorModel = response;
}
if (errorModel) {
this.message = errorModel.Message;
this.validationErrors = errorModel.ValidationErrors;
}
this.statusCode = status;
}
getSingleMessage(): string {
if (this.validationErrors) {
for (const key in this.validationErrors) {
if (!this.validationErrors.hasOwnProperty(key)) {
continue;
}
if (this.validationErrors[key].length) {
return this.validationErrors[key][0];
}
}
}
return this.message;
}
}
export { ErrorResponse };
(window as any).ErrorResponse = ErrorResponse;

View File

@@ -1,14 +0,0 @@
class FolderResponse {
id: string;
name: string;
revisionDate: string;
constructor(response: any) {
this.id = response.Id;
this.name = response.Name;
this.revisionDate = response.RevisionDate;
}
}
export { FolderResponse };
(window as any).FolderResponse = FolderResponse;

View File

@@ -1,14 +0,0 @@
class GlobalDomainResponse {
type: number;
domains: string[];
excluded: number[];
constructor(response: any) {
this.type = response.Type;
this.domains = response.Domains;
this.excluded = response.Excluded;
}
}
export { GlobalDomainResponse };
(window as any).GlobalDomainResponse = GlobalDomainResponse;

View File

@@ -1,24 +0,0 @@
class IdentityTokenResponse {
accessToken: string;
expiresIn: number;
refreshToken: string;
tokenType: string;
privateKey: string;
key: string;
twoFactorToken: string;
constructor(response: any) {
this.accessToken = response.access_token;
this.expiresIn = response.expires_in;
this.refreshToken = response.refresh_token;
this.tokenType = response.token_type;
this.privateKey = response.PrivateKey;
this.key = response.Key;
this.twoFactorToken = response.TwoFactorToken;
}
}
export { IdentityTokenResponse };
(window as any).IdentityTokenResponse = IdentityTokenResponse;

View File

@@ -1,12 +0,0 @@
class KeysResponse {
privateKey: string;
publicKey: string;
constructor(response: any) {
this.privateKey = response.PrivateKey;
this.publicKey = response.PublicKey;
}
}
export { KeysResponse };
(window as any).KeysResponse = KeysResponse;

View File

@@ -1,10 +0,0 @@
class ListResponse {
data: any;
constructor(data: any) {
this.data = data;
}
}
export { ListResponse };
(window as any).ListResponse = ListResponse;

View File

@@ -1,30 +0,0 @@
class ProfileOrganizationResponse {
id: string;
name: string;
useGroups: boolean;
useDirectory: boolean;
useTotp: boolean;
seats: number;
maxCollections: number;
maxStorageGb?: number;
key: string;
status: number; // TODO: map to enum
type: number; // TODO: map to enum
constructor(response: any) {
this.id = response.Id;
this.name = response.Name;
this.useGroups = response.UseGroups;
this.useDirectory = response.UseDirectory;
this.useTotp = response.UseTotp;
this.seats = response.Seats;
this.maxCollections = response.MaxCollections;
this.maxStorageGb = response.MaxStorageGb;
this.key = response.Key;
this.status = response.Status;
this.type = response.Type;
}
}
export { ProfileOrganizationResponse };
(window as any).ProfileOrganizationResponse = ProfileOrganizationResponse;

View File

@@ -1,39 +0,0 @@
import { ProfileOrganizationResponse } from './profileOrganizationResponse';
class ProfileResponse {
id: string;
name: string;
email: string;
emailVerified: boolean;
masterPasswordHint: string;
premium: boolean;
culture: string;
twoFactorEnabled: boolean;
key: string;
privateKey: string;
securityStamp: string;
organizations: ProfileOrganizationResponse[] = [];
constructor(response: any) {
this.id = response.Id;
this.name = response.Name;
this.email = response.Email;
this.emailVerified = response.EmailVerified;
this.masterPasswordHint = response.MasterPasswordHint;
this.premium = response.Premium;
this.culture = response.Culture;
this.twoFactorEnabled = response.TwoFactorEnabled;
this.key = response.Key;
this.privateKey = response.PrivateKey;
this.securityStamp = response.SecurityStamp;
if (response.Organizations) {
response.Organizations.forEach((org: any) => {
this.organizations.push(new ProfileOrganizationResponse(org));
});
}
}
}
export { ProfileResponse };
(window as any).ProfileResponse = ProfileResponse;

View File

@@ -1,44 +0,0 @@
import { CipherResponse } from './cipherResponse';
import { CollectionResponse } from './collectionResponse';
import { DomainsResponse } from './domainsResponse';
import { FolderResponse } from './folderResponse';
import { ProfileResponse } from './profileResponse';
class SyncResponse {
profile?: ProfileResponse;
folders: FolderResponse[] = [];
collections: CollectionResponse[] = [];
ciphers: CipherResponse[] = [];
domains?: DomainsResponse;
constructor(response: any) {
if (response.Profile) {
this.profile = new ProfileResponse(response.Profile);
}
if (response.Folders) {
response.Folders.forEach((folder: any) => {
this.folders.push(new FolderResponse(folder));
});
}
if (response.Collections) {
response.Collections.forEach((collection: any) => {
this.collections.push(new CollectionResponse(collection));
});
}
if (response.Ciphers) {
response.Ciphers.forEach((cipher: any) => {
this.ciphers.push(new CipherResponse(cipher));
});
}
if (response.Domains) {
this.domains = new DomainsResponse(response.Domains);
}
}
}
export { SyncResponse };
(window as any).SyncResponse = SyncResponse;