microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
6a465861663d7c6f9433e89c6f6619df320dcef3

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/appcenter/auth/profile/profile.ts

115lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as fs from "fs";
5import * as path from "path";
6import * as mkdirp from "mkdirp";
7import * as Q from "q";
8import { TokenValueType, tokenStore } from "../tokenStore/index";
9import { getProfileDir, profileFile } from "./getProfileDir";
10
11export interface Profile {
12 userId: string;
13 userName: string;
14 displayName: string;
15 email: string;
16 readonly accessToken: Q.Promise<string>;
17 save(): Profile;
18 logout(): Q.Promise<void>;
19 }
20
21class ProfileImpl implements Profile {
22 public userId: string;
23 public userName: string;
24 public displayName: string;
25 public email: string;
26
27 constructor(fileContents: any) {
28 this.userId = fileContents.userId || fileContents.id;
29 this.userName = fileContents.userName || fileContents.name;
30 this.displayName = fileContents.displayName;
31 this.email = fileContents.email;
32 }
33
34 get accessToken(): Q.Promise<string> {
35 const getter = tokenStore.get(this.userName)
36 .catch((err: Error) => {
37 // log error?
38 });
39 const emptyToken = "";
40 return getter.then((entry: any) => {
41 if (entry) {
42 return entry.accessToken.token;
43 }
44 return emptyToken;
45 }).catch((err: Error) => {
46 // Failed to get token from porfile, return no result
47 return emptyToken;
48 });
49 }
50
51 public save(): Profile {
52 let profile: any = {
53 userId: this.userId,
54 userName: this.userName,
55 displayName: this.displayName,
56 email: this.email,
57 };
58
59 mkdirp.sync(getProfileDir());
60 fs.writeFileSync(getProfileFilename(), JSON.stringify(profile), { encoding: "utf8" });
61 return this;
62 }
63
64 public logout(): Q.Promise<void> {
65 return tokenStore.remove(this.userName).then(() => {
66 try {
67 fs.unlinkSync(getProfileFilename());
68 } catch (err) {
69 // File not found is ok, probably doesn't exist
70 }
71 });
72 }
73}
74
75let currentProfile: Profile | null;
76
77function getProfileFilename(): string {
78 const profileDir = getProfileDir();
79 return path.join(profileDir, profileFile);
80}
81
82function loadProfile(): Profile | null {
83 const profilePath = getProfileFilename();
84 if (!fs.existsSync(profilePath)) {
85 return null;
86 }
87
88 let profileContents = fs.readFileSync(profilePath, "utf8");
89 let profile: any = JSON.parse(profileContents);
90 return new ProfileImpl(profile);
91}
92
93export function getUser(): Profile | null {
94 if (!currentProfile) {
95 currentProfile = loadProfile();
96 }
97 return currentProfile;
98}
99
100export function saveUser(user: any, token: TokenValueType): Q.Promise<Profile> {
101 return tokenStore.set(user.name, token).then(() => {
102 let profile = new ProfileImpl(user);
103 profile.save();
104 return profile;
105 });
106}
107
108export function deleteUser(): Q.Promise<void> {
109 let profile = getUser();
110 if (profile) {
111 currentProfile = null;
112 return profile.logout();
113 }
114 return Q.resolve(void 0);
115}