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

Commit

Permalink
setup for client event collection
Browse files Browse the repository at this point in the history
  • Loading branch information
kspearrin committed Jun 20, 2019
1 parent 740c01c commit 49a6b50
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/abstractions/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { CollectionRequest } from '../models/request/collectionRequest';
import { DeleteRecoverRequest } from '../models/request/deleteRecoverRequest';
import { EmailRequest } from '../models/request/emailRequest';
import { EmailTokenRequest } from '../models/request/emailTokenRequest';
import { EventRequest } from '../models/request/eventRequest';
import { FolderRequest } from '../models/request/folderRequest';
import { GroupRequest } from '../models/request/groupRequest';
import { ImportCiphersRequest } from '../models/request/importCiphersRequest';
Expand Down Expand Up @@ -98,6 +99,7 @@ export abstract class ApiService {
urlsSet: boolean;
apiBaseUrl: string;
identityBaseUrl: string;
eventsBaseUrl: string;

setUrls: (urls: EnvironmentUrls) => void;
postIdentityToken: (request: TokenRequest) => Promise<IdentityTokenResponse | IdentityTwoFactorResponse>;
Expand Down Expand Up @@ -254,6 +256,7 @@ export abstract class ApiService {
token: string) => Promise<ListResponse<EventResponse>>;
getEventsOrganizationUser: (organizationId: string, id: string,
start: string, end: string, token: string) => Promise<ListResponse<EventResponse>>;
postEventsCollect: (request: EventRequest) => Promise<any>;

getUserPublicKey: (id: string) => Promise<UserKeyResponse>;

Expand Down
1 change: 1 addition & 0 deletions src/abstractions/environment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export abstract class EnvironmentService {
identityUrl: string;
iconsUrl: string;
notificationsUrl: string;
eventsUrl: string;

getWebVaultUrl: () => string;
setUrlsFromStorage: () => Promise<void>;
Expand Down
7 changes: 7 additions & 0 deletions src/enums/eventType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum EventType {
User_Recovered2fa = 1004,
User_FailedLogIn = 1005,
User_FailedLogIn2fa = 1006,
User_ExportedVault = 1007,

Cipher_Created = 1100,
Cipher_Updated = 1101,
Expand All @@ -14,6 +15,12 @@ export enum EventType {
Cipher_AttachmentDeleted = 1104,
Cipher_Shared = 1105,
Cipher_UpdatedCollections = 1106,
Cipher_ClientViewed = 1107,
Cipher_ClientToggledPasswordVisible = 1108,
Cipher_ClientToggledHiddenFieldVisible = 1109,
Cipher_ClientCopiedPassword = 1110,
Cipher_ClientCopedHiddenField = 1111,
Cipher_ClientAutofilled = 1112,

Collection_Created = 1300,
Collection_Updated = 1301,
Expand Down
1 change: 1 addition & 0 deletions src/models/domain/environmentUrls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export class EnvironmentUrls {
base: string;
api: string;
identity: string;
events: string;
}
6 changes: 6 additions & 0 deletions src/models/request/eventRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { EventType } from '../../enums/eventType';

export class EventRequest {
type: EventType;
cipherId: string;
}
47 changes: 34 additions & 13 deletions src/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { CollectionRequest } from '../models/request/collectionRequest';
import { DeleteRecoverRequest } from '../models/request/deleteRecoverRequest';
import { EmailRequest } from '../models/request/emailRequest';
import { EmailTokenRequest } from '../models/request/emailTokenRequest';
import { EventRequest } from '../models/request/eventRequest';
import { FolderRequest } from '../models/request/folderRequest';
import { GroupRequest } from '../models/request/groupRequest';
import { ImportCiphersRequest } from '../models/request/importCiphersRequest';
Expand Down Expand Up @@ -105,6 +106,7 @@ export class ApiService implements ApiServiceAbstraction {
urlsSet: boolean = false;
apiBaseUrl: string;
identityBaseUrl: string;
eventsBaseUrl: string;

private deviceType: string;
private isWebClient = false;
Expand All @@ -130,24 +132,24 @@ export class ApiService implements ApiServiceAbstraction {
this.usingBaseUrl = true;
this.apiBaseUrl = urls.base + '/api';
this.identityBaseUrl = urls.base + '/identity';
this.eventsBaseUrl = urls.base + '/events';
return;
}

if (urls.api != null && urls.identity != null) {
this.apiBaseUrl = urls.api;
this.identityBaseUrl = urls.identity;
return;
}

/* tslint:disable */
// Local Dev
//this.apiBaseUrl = 'http://localhost:4000';
//this.identityBaseUrl = 'http://localhost:33656';
this.apiBaseUrl = urls.api;
this.identityBaseUrl = urls.identity;
this.eventsBaseUrl = urls.events;

// Production
this.apiBaseUrl = 'https://api.bitwarden.com';
this.identityBaseUrl = 'https://identity.bitwarden.com';
/* tslint:enable */
if (this.apiBaseUrl == null) {
this.apiBaseUrl = 'https://api.bitwarden.com';
}
if (this.identityBaseUrl == null) {
this.identityBaseUrl = 'https://identity.bitwarden.com';
}
if (this.eventsBaseUrl == null) {
this.eventsBaseUrl = 'https://events.bitwarden.com';
}
}

// Auth APIs
Expand Down Expand Up @@ -839,6 +841,25 @@ export class ApiService implements ApiServiceAbstraction {
return new ListResponse(r, EventResponse);
}

async postEventsCollect(request: EventRequest): Promise<any> {
const authHeader = await this.getActiveBearerToken();
const headers = new Headers({
'Device-Type': this.deviceType,
'Authorization': 'Bearer ' + authHeader,
'Content-Type': 'application/json; charset=utf-8',
});
const response = await this.fetch(new Request(this.eventsBaseUrl + '/collect', {
cache: 'no-cache',
credentials: this.getCredentials(),
method: 'POST',
body: JSON.stringify(request),
headers: headers,
}));
if (response.status !== 200) {
return Promise.reject('Event post failed.');
}
}

// User APIs

async getUserPublicKey(id: string): Promise<UserKeyResponse> {
Expand Down
7 changes: 7 additions & 0 deletions src/services/environment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
identityUrl: string;
iconsUrl: string;
notificationsUrl: string;
eventsUrl: string;

constructor(private apiService: ApiService, private storageService: StorageService,
private notificationsService: NotificationsService) { }
Expand All @@ -35,6 +36,7 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
identity: null,
icons: null,
notifications: null,
events: null,
webVault: null,
};

Expand All @@ -51,6 +53,7 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
this.identityUrl = envUrls.identity = urls.identity;
this.iconsUrl = urls.icons;
this.notificationsUrl = urls.notifications;
this.eventsUrl = envUrls.events = urls.events;
this.apiService.setUrls(envUrls);
}

Expand All @@ -61,6 +64,7 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
urls.identity = this.formatUrl(urls.identity);
urls.icons = this.formatUrl(urls.icons);
urls.notifications = this.formatUrl(urls.notifications);
urls.events = this.formatUrl(urls.events);

await this.storageService.save(ConstantsService.environmentUrlsKey, {
base: urls.base,
Expand All @@ -69,6 +73,7 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
webVault: urls.webVault,
icons: urls.icons,
notifications: urls.notifications,
events: urls.events,
});

this.baseUrl = urls.base;
Expand All @@ -77,13 +82,15 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
this.identityUrl = urls.identity;
this.iconsUrl = urls.icons;
this.notificationsUrl = urls.notifications;
this.eventsUrl = urls.events;

const envUrls = new EnvironmentUrls();
if (this.baseUrl) {
envUrls.base = this.baseUrl;
} else {
envUrls.api = this.apiUrl;
envUrls.identity = this.identityUrl;
envUrls.events = this.eventsUrl;
}

this.apiService.setUrls(envUrls);
Expand Down

0 comments on commit 49a6b50

Please sign in to comment.