Skip to content

Commit

Permalink
Merge ee0e500 into 386fd24
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwski committed Aug 2, 2019
2 parents 386fd24 + ee0e500 commit 01e3d4f
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 18 deletions.
16 changes: 15 additions & 1 deletion packages/database/src/core/AuthTokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,24 @@ import { FirebaseApp } from '@firebase/app-types';
import { FirebaseAuthTokenData } from '@firebase/app-types/private';
import { log, warn } from './util/util';

export interface TokenProvider {
/**
* @param {boolean} forceRefresh
* @return {!Promise<FirebaseAuthTokenData>}
*/
getToken(forceRefresh: boolean): Promise<FirebaseAuthTokenData>;

addTokenChangeListener(listener: (token: string | null) => void);

removeTokenChangeListener(listener: (token: string | null) => void);

notifyForInvalidToken();
}

/**
* Abstraction around FirebaseApp's token fetching capabilities.
*/
export class AuthTokenProvider {
export class AuthTokenProvider implements TokenProvider {
/**
* @param {!FirebaseApp} app_
*/
Expand Down
19 changes: 19 additions & 0 deletions packages/database/src/core/Constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @license
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export const FIREBASE_DATABASE_EMULATOR_HOST_VAR =
'FIREBASE_DATABASE_EMULATOR_HOST';
44 changes: 44 additions & 0 deletions packages/database/src/core/EmulatorAuthTokenProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @license
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { FirebaseApp } from '@firebase/app-types';
import { FirebaseAuthTokenData } from '@firebase/app-types/private';

import { TokenProvider } from './AuthTokenProvider';
import { log, warn } from './util/util';

class EmulatorAuthToken implements FirebaseAuthTokenData {
constructor(public accessToken: string) {}
}

export class EmulatorAuthTokenProvider implements TokenProvider {
constructor(private app_: FirebaseApp) {}

getToken(forceRefresh: boolean): Promise<FirebaseAuthTokenData> {
return Promise.resolve(new EmulatorAuthToken('owner'));
}

addTokenChangeListener(listener: (token: string | null) => void) {}

removeTokenChangeListener(listener: (token: string | null) => void) {}

notifyForInvalidToken() {
let errorMessage =
'Database emulator unexpectedly rejected fake "owner" credentials.';
warn(errorMessage);
}
}
4 changes: 2 additions & 2 deletions packages/database/src/core/PersistentConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { isAdmin, isValidFormat } from '@firebase/util';
import { Connection } from '../realtime/Connection';
import { isMobileCordova, isReactNative, isNodeSdk } from '@firebase/util';
import { ServerActions } from './ServerActions';
import { AuthTokenProvider } from './AuthTokenProvider';
import { TokenProvider } from './AuthTokenProvider';
import { RepoInfo } from './RepoInfo';
import { Query } from '../api/Query';
import { SDK_VERSION } from './version';
Expand Down Expand Up @@ -133,7 +133,7 @@ export class PersistentConnection extends ServerActions {
) => void,
private onConnectStatus_: (a: boolean) => void,
private onServerInfoUpdate_: (a: any) => void,
private authTokenProvider_: AuthTokenProvider,
private authTokenProvider_: TokenProvider,
private authOverride_?: Object | null
) {
super();
Expand Down
6 changes: 3 additions & 3 deletions packages/database/src/core/ReadonlyRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { safeGet } from '@firebase/util';
import { querystring } from '@firebase/util';
import { ServerActions } from './ServerActions';
import { RepoInfo } from './RepoInfo';
import { AuthTokenProvider } from './AuthTokenProvider';
import { TokenProvider } from './AuthTokenProvider';
import { Query } from '../api/Query';

/**
Expand Down Expand Up @@ -67,7 +67,7 @@ export class ReadonlyRestClient extends ServerActions {
/**
* @param {!RepoInfo} repoInfo_ Data about the namespace we are connecting to
* @param {function(string, *, boolean, ?number)} onDataUpdate_ A callback for new data from the server
* @param {AuthTokenProvider} authTokenProvider_
* @param {TokenProvider} authTokenProvider_
* @implements {ServerActions}
*/
constructor(
Expand All @@ -78,7 +78,7 @@ export class ReadonlyRestClient extends ServerActions {
c: boolean,
d: number | null
) => void,
private authTokenProvider_: AuthTokenProvider
private authTokenProvider_: TokenProvider
) {
super();
}
Expand Down
14 changes: 12 additions & 2 deletions packages/database/src/core/Repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import { SnapshotHolder } from './SnapshotHolder';
import { stringify } from '@firebase/util';
import { beingCrawled, each, exceptionGuard, warn, log } from './util/util';
import { map, isEmpty } from '@firebase/util';
import { AuthTokenProvider } from './AuthTokenProvider';
import { AuthTokenProvider, TokenProvider } from './AuthTokenProvider';
import { EmulatorAuthTokenProvider } from './EmulatorAuthTokenProvider';
import { StatsManager } from './stats/StatsManager';
import { StatsReporter } from './stats/StatsReporter';
import { StatsListener } from './stats/StatsListener';
Expand All @@ -37,6 +38,7 @@ import { PersistentConnection } from './PersistentConnection';
import { ReadonlyRestClient } from './ReadonlyRestClient';
import { FirebaseApp } from '@firebase/app-types';
import { RepoInfo } from './RepoInfo';
import { FIREBASE_DATABASE_EMULATOR_HOST_VAR } from './Constants';
import { Database } from '../api/Database';
import { ServerActions } from './ServerActions';
import { Query } from '../api/Query';
Expand Down Expand Up @@ -81,7 +83,15 @@ export class Repo {
forceRestClient: boolean,
public app: FirebaseApp
) {
const authTokenProvider = new AuthTokenProvider(app);
let authTokenProvider: TokenProvider;
if (
typeof process !== 'undefined' &&
process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR]
) {
authTokenProvider = new EmulatorAuthTokenProvider(app);
} else {
authTokenProvider = new AuthTokenProvider(app);
}

this.stats_ = StatsManager.getCollection(repoInfo_);

Expand Down
11 changes: 1 addition & 10 deletions packages/database/src/core/RepoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import { FirebaseApp } from '@firebase/app-types';
import { safeGet } from '@firebase/util';
import { FIREBASE_DATABASE_EMULATOR_HOST_VAR } from './Constants';
import { Repo } from './Repo';
import { fatal } from './util/util';
import { parseRepoInfo, parseURL } from './util/libs/parser';
Expand All @@ -28,16 +29,6 @@ import { RepoInfo } from './RepoInfo';
/** @const {string} */
const DATABASE_URL_OPTION = 'databaseURL';

/**
* This variable is also defined in the firebase node.js admin SDK. Before
* modifying this definition, consult the definition in:
*
* https://github.com/firebase/firebase-admin-node
*
* and make sure the two are consistent.
*/
const FIREBASE_DATABASE_EMULATOR_HOST_VAR = 'FIREBASE_DATABASE_EMULATOR_HOST';

let _staticInstance: RepoManager;

/**
Expand Down

0 comments on commit 01e3d4f

Please sign in to comment.