bitwarden-estensione-browser/src/services/user.service.ts

176 lines
5.6 KiB
TypeScript
Raw Normal View History

import { StorageService } from '../abstractions/storage.service';
import { TokenService } from '../abstractions/token.service';
2018-05-18 21:26:46 +02:00
import { UserService as UserServiceAbstraction } from '../abstractions/user.service';
import { OrganizationData } from '../models/data/organizationData';
import { Organization } from '../models/domain/organization';
2018-01-09 23:37:50 +01:00
2018-08-14 21:12:10 +02:00
import { KdfType } from '../enums/kdfType';
2018-01-09 23:37:50 +01:00
const Keys = {
userId: 'userId',
userEmail: 'userEmail',
stamp: 'securityStamp',
2018-08-14 21:12:10 +02:00
kdf: 'kdf',
kdfIterations: 'kdfIterations',
2018-05-18 21:26:46 +02:00
organizationsPrefix: 'organizations_',
emailVerified: 'emailVerified',
2018-01-09 23:37:50 +01:00
};
2018-05-18 21:26:46 +02:00
export class UserService implements UserServiceAbstraction {
2018-08-14 21:12:10 +02:00
private userId: string;
private email: string;
private stamp: string;
private kdf: KdfType;
private kdfIterations: number;
private emailVerified: boolean;
2018-01-09 23:37:50 +01:00
2018-08-14 21:12:10 +02:00
constructor(private tokenService: TokenService, private storageService: StorageService) { }
2018-01-09 23:37:50 +01:00
2018-08-14 21:12:10 +02:00
setInformation(userId: string, email: string, kdf: KdfType, kdfIterations: number): Promise<any> {
2018-01-09 23:37:50 +01:00
this.email = email;
this.userId = userId;
2018-08-14 21:12:10 +02:00
this.kdf = kdf;
this.kdfIterations = kdfIterations;
2018-01-09 23:37:50 +01:00
return Promise.all([
this.storageService.save(Keys.userEmail, email),
this.storageService.save(Keys.userId, userId),
2018-08-14 21:12:10 +02:00
this.storageService.save(Keys.kdf, kdf),
this.storageService.save(Keys.kdfIterations, kdfIterations),
2018-01-09 23:37:50 +01:00
]);
}
setSecurityStamp(stamp: string): Promise<any> {
this.stamp = stamp;
return this.storageService.save(Keys.stamp, stamp);
}
setEmailVerified(emailVerified: boolean) {
this.emailVerified = emailVerified;
return this.storageService.save(Keys.emailVerified, emailVerified);
}
2018-01-09 23:37:50 +01:00
async getUserId(): Promise<string> {
2019-04-10 21:40:40 +02:00
if (this.userId == null) {
this.userId = await this.storageService.get<string>(Keys.userId);
2018-01-09 23:37:50 +01:00
}
return this.userId;
}
async getEmail(): Promise<string> {
2019-04-10 21:40:40 +02:00
if (this.email == null) {
this.email = await this.storageService.get<string>(Keys.userEmail);
2018-01-09 23:37:50 +01:00
}
return this.email;
}
async getSecurityStamp(): Promise<string> {
2019-04-10 21:40:40 +02:00
if (this.stamp == null) {
this.stamp = await this.storageService.get<string>(Keys.stamp);
2018-01-09 23:37:50 +01:00
}
return this.stamp;
}
2018-08-14 21:12:10 +02:00
async getKdf(): Promise<KdfType> {
2019-04-10 21:40:40 +02:00
if (this.kdf == null) {
this.kdf = await this.storageService.get<KdfType>(Keys.kdf);
2018-08-14 21:12:10 +02:00
}
return this.kdf;
}
async getKdfIterations(): Promise<number> {
2019-04-10 21:40:40 +02:00
if (this.kdfIterations == null) {
this.kdfIterations = await this.storageService.get<number>(Keys.kdfIterations);
2018-08-14 21:12:10 +02:00
}
return this.kdfIterations;
}
async getEmailVerified(): Promise<boolean> {
if (this.emailVerified == null) {
this.emailVerified = await this.storageService.get<boolean>(Keys.emailVerified);
}
return this.emailVerified;
}
2018-01-09 23:37:50 +01:00
async clear(): Promise<any> {
2018-05-18 21:26:46 +02:00
const userId = await this.getUserId();
2018-01-09 23:37:50 +01:00
await Promise.all([
this.storageService.remove(Keys.userId),
this.storageService.remove(Keys.userEmail),
this.storageService.remove(Keys.stamp),
2018-08-14 21:12:10 +02:00
this.storageService.remove(Keys.kdf),
this.storageService.remove(Keys.kdfIterations),
2018-05-18 21:26:46 +02:00
this.clearOrganizations(userId),
2018-01-09 23:37:50 +01:00
]);
this.userId = this.email = this.stamp = null;
2018-08-14 21:12:10 +02:00
this.kdf = null;
this.kdfIterations = null;
2018-01-09 23:37:50 +01:00
}
async isAuthenticated(): Promise<boolean> {
const token = await this.tokenService.getToken();
if (token == null) {
return false;
}
const userId = await this.getUserId();
return userId != null;
}
2018-05-18 21:26:46 +02:00
2018-08-29 05:17:30 +02:00
async canAccessPremium(): Promise<boolean> {
const authed = await this.isAuthenticated();
if (!authed) {
return false;
}
2019-04-10 21:40:40 +02:00
const tokenPremium = this.tokenService.getPremium();
2018-08-29 05:17:30 +02:00
if (tokenPremium) {
return true;
}
const orgs = await this.getAllOrganizations();
for (let i = 0; i < orgs.length; i++) {
2018-08-29 19:44:55 +02:00
if (orgs[i].usersGetPremium && orgs[i].enabled) {
2018-08-29 05:17:30 +02:00
return true;
}
}
return false;
}
2018-05-18 21:26:46 +02:00
async getOrganization(id: string): Promise<Organization> {
const userId = await this.getUserId();
const organizations = await this.storageService.get<{ [id: string]: OrganizationData; }>(
Keys.organizationsPrefix + userId);
if (organizations == null || !organizations.hasOwnProperty(id)) {
return null;
}
return new Organization(organizations[id]);
}
async getAllOrganizations(): Promise<Organization[]> {
const userId = await this.getUserId();
const organizations = await this.storageService.get<{ [id: string]: OrganizationData; }>(
Keys.organizationsPrefix + userId);
const response: Organization[] = [];
for (const id in organizations) {
if (organizations.hasOwnProperty(id)) {
response.push(new Organization(organizations[id]));
}
}
return response;
}
async replaceOrganizations(organizations: { [id: string]: OrganizationData; }): Promise<any> {
const userId = await this.getUserId();
await this.storageService.save(Keys.organizationsPrefix + userId, organizations);
}
async clearOrganizations(userId: string): Promise<any> {
await this.storageService.remove(Keys.organizationsPrefix + userId);
}
2018-01-09 23:37:50 +01:00
}