microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.8

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

84lines · modeblame

0c0b4844max-mironov8 years ago1// 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 {
19private filePath: string;
20private tokenStoreCache: { [key: string]: TokenValueType } | undefined;
21
22constructor(filePath: string) {
23this.filePath = filePath;
24this.tokenStoreCache = undefined;
25}
26
27public getStoreFilePath(): string {
28return this.filePath;
29}
30
31public list(): rx.Observable<TokenEntry> {
32this.loadTokenStoreCache();
33return rx.Observable.from(toPairs(this.tokenStoreCache)).map(pair => ({ key: pair[0], accessToken: pair[1]}));
34}
35
36public get(key: TokenKeyType): Q.Promise<TokenEntry | null> {
37this.loadTokenStoreCache();
38let token;
39if (this.tokenStoreCache) {
40token = this.tokenStoreCache[key];
41}
42if (!token) {
43return Q.resolve(null);
44}
45return Q<TokenEntry>({key: key, accessToken: token});
46}
47
48public set(key: TokenKeyType, value: TokenValueType): Q.Promise<void> {
49this.loadTokenStoreCache();
50if (this.tokenStoreCache) {
51this.tokenStoreCache[key] = value;
52}
53this.writeTokenStoreCache();
54return Q.resolve(void 0);
55}
56
57public remove(key: TokenKeyType): Q.Promise<void> {
58this.loadTokenStoreCache();
59if (this.tokenStoreCache) {
60delete this.tokenStoreCache[key];
61}
62this.writeTokenStoreCache();
63return Q.resolve(void 0);
64}
65
66private loadTokenStoreCache(): void {
67if (!this.tokenStoreCache) {
68try {
69this.tokenStoreCache = JSON.parse(fs.readFileSync(this.filePath, "utf8"));
70} catch (err) {
71// No token cache file, creating new empty cache
72this.tokenStoreCache = {};
73}
74}
75}
76
77private writeTokenStoreCache(): void {
78fs.writeFileSync(this.filePath, JSON.stringify(this.tokenStoreCache));
79}
80}
81
82export function createFileTokenStore(pathName: string): TokenStore {
83return new FileTokenStore(pathName);
84}