microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
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 | |
| 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 | |
| 11 | export 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 | |
| 21 | class 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 | |
| 75 | let currentProfile: Profile | null; |
| 76 | |
| 77 | function getProfileFilename(): string { |
| 78 | const profileDir = getProfileDir(); |
| 79 | return path.join(profileDir, profileFile); |
| 80 | } |
| 81 | |
| 82 | function 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 | |
| 93 | export function getUser(): Profile | null { |
| 94 | if (!currentProfile) { |
| 95 | currentProfile = loadProfile(); |
| 96 | } |
| 97 | return currentProfile; |
| 98 | } |
| 99 | |
| 100 | export 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 | |
| 108 | export 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 | } |