microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.5

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/appcenter/auth/tokenStore/fileTokenStore.ts

84lines · 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//
5// file-token-store - implementation of token store that stores the data in
6// a JSON encoded file on dist.
7//
8// This doesn't secure the data in any way, relies on the directory having
9// proper security settings.
10//
11
12import * as fs from "fs";
13import * as rx from "rx-lite";
14import { toPairs } from "lodash";
15import * as Q from "q";
16import { TokenEntry, TokenStore, TokenKeyType, TokenValueType } from "../tokenStore/tokenStore";
17
18export class FileTokenStore implements TokenStore {
19 private filePath: string;
20 private tokenStoreCache: { [key: string]: TokenValueType } | undefined;
21
22 constructor(filePath: string) {
23 this.filePath = filePath;
24 this.tokenStoreCache = undefined;
25 }
26
27 public getStoreFilePath(): string {
28 return this.filePath;
29 }
30
31 public list(): rx.Observable<TokenEntry> {
32 this.loadTokenStoreCache();
33 return rx.Observable.from(toPairs(this.tokenStoreCache)).map(pair => ({ key: pair[0], accessToken: pair[1]}));
34 }
35
36 public get(key: TokenKeyType): Q.Promise<TokenEntry | null> {
37 this.loadTokenStoreCache();
38 let token;
39 if (this.tokenStoreCache) {
40 token = this.tokenStoreCache[key];
41 }
42 if (!token) {
43 return Q.resolve(null);
44 }
45 return Q<TokenEntry>({key: key, accessToken: token});
46 }
47
48 public set(key: TokenKeyType, value: TokenValueType): Q.Promise<void> {
49 this.loadTokenStoreCache();
50 if (this.tokenStoreCache) {
51 this.tokenStoreCache[key] = value;
52 }
53 this.writeTokenStoreCache();
54 return Q.resolve(void 0);
55 }
56
57 public remove(key: TokenKeyType): Q.Promise<void> {
58 this.loadTokenStoreCache();
59 if (this.tokenStoreCache) {
60 delete this.tokenStoreCache[key];
61 }
62 this.writeTokenStoreCache();
63 return Q.resolve(void 0);
64 }
65
66 private loadTokenStoreCache(): void {
67 if (!this.tokenStoreCache) {
68 try {
69 this.tokenStoreCache = JSON.parse(fs.readFileSync(this.filePath, "utf8"));
70 } catch (err) {
71 // No token cache file, creating new empty cache
72 this.tokenStoreCache = {};
73 }
74 }
75 }
76
77 private writeTokenStoreCache(): void {
78 fs.writeFileSync(this.filePath, JSON.stringify(this.tokenStoreCache));
79 }
80 }
81
82export function createFileTokenStore(pathName: string): TokenStore {
83 return new FileTokenStore(pathName);
84}