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