All files / src/implementation authwrapper.ts

89.06% Statements 57/64
78.95% Branches 15/19
66.67% Functions 10/15
88.89% Lines 56/63
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182                                  4x 4x   4x 4x 4x       4x 4x                           4x   39x                       39x                 39x 39x 15x 15x 15x     38x 38x 38x 38x 38x 38x 38x     4x     15x 15x     15x 14x     4x     35x         3x   3x 2x   1x               32x       4x 8x     8x               4x                     4x       4x       26x 23x 23x 23x   3x             4x 3x 3x 3x     4x 28x     4x       4x 20x     4x     4x  
/**
 * 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.
 */
import { Reference } from '../reference';
import { Service } from '../service';
import * as constants from './constants';
import * as errorsExports from './error';
import { errors } from './error';
import { FailRequest } from './failrequest';
import { Location } from './location';
import * as promiseimpl from './promise_external';
import { Request } from './request';
import { RequestInfo } from './requestinfo';
import { requestMaker } from './requestmaker';
import { RequestMap } from './requestmap';
import * as type from './type';
import { XhrIoPool } from './xhriopool';
import { FirebaseApp } from '@firebase/app-types';
import {
  _FirebaseApp,
  FirebaseAuthTokenData
} from '@firebase/app-types/private';
 
/**
 * @param app If null, getAuthToken always resolves with null.
 * @param service The storage service associated with this auth wrapper.
 *     Untyped to avoid circular type dependencies.
 * @struct
 */
export class AuthWrapper {
  private app_: FirebaseApp | null;
  private bucket_: string | null = null;
 
  /**
  maker
     */
  private storageRefMaker_: (p1: AuthWrapper, p2: Location) => Reference;
  private requestMaker_: requestMaker;
  private pool_: XhrIoPool;
  private service_: Service;
  private maxOperationRetryTime_: number;
  private maxUploadRetryTime_: number;
  private requestMap_: RequestMap;
  private deleted_: boolean = false;
 
  constructor(
    app: FirebaseApp | null,
    maker: (p1: AuthWrapper, p2: Location) => Reference,
    requestMaker: requestMaker,
    service: Service,
    pool: XhrIoPool
  ) {
    this.app_ = app;
    if (this.app_ !== null) {
      let options = this.app_.options;
      Eif (type.isDef(options)) {
        this.bucket_ = AuthWrapper.extractBucket_(options);
      }
    }
    this.storageRefMaker_ = maker;
    this.requestMaker_ = requestMaker;
    this.pool_ = pool;
    this.service_ = service;
    this.maxOperationRetryTime_ = constants.defaultMaxOperationRetryTime;
    this.maxUploadRetryTime_ = constants.defaultMaxUploadRetryTime;
    this.requestMap_ = new RequestMap();
  }
 
  private static extractBucket_(config: {
    [prop: string]: any;
  }): string | null {
    let bucketString = config[constants.configOption] || null;
    Iif (bucketString == null) {
      return null;
    }
    let loc: Location = Location.makeFromBucketSpec(bucketString);
    return loc.bucket;
  }
 
  getAuthToken(): Promise<string | null> {
    // TODO(andysoto): remove ifDef checks after firebase-app implements stubs
    // (b/28673818).
    if (
      this.app_ !== null &&
      type.isDef((this.app_ as _FirebaseApp).INTERNAL) &&
      type.isDef((this.app_ as _FirebaseApp).INTERNAL.getToken)
    ) {
      return (this.app_ as _FirebaseApp).INTERNAL.getToken().then(
        function(response: FirebaseAuthTokenData | null): string | null {
          if (response !== null) {
            return response.accessToken;
          } else {
            return null;
          }
        },
        function(_error) {
          return null;
        }
      );
    } else {
      return promiseimpl.resolve(null) as Promise<string | null>;
    }
  }
 
  bucket(): string | null {
    Iif (this.deleted_) {
      throw errorsExports.appDeleted();
    } else {
      return this.bucket_;
    }
  }
 
  /**
   * The service associated with this auth wrapper. Untyped to avoid circular
   * type dependencies.
   */
  service(): Service {
    return this.service_;
  }
 
  /**
   * Returns a new firebaseStorage.Reference object referencing this AuthWrapper
   * at the given Location.
   * @param loc The Location.
   * @return Actually a firebaseStorage.Reference, typing not allowed
   *     because of circular dependency problems.
   */
  makeStorageReference(loc: Location): Reference {
    return this.storageRefMaker_(this, loc);
  }
 
  makeRequest<T>(
    requestInfo: RequestInfo<T>,
    authToken: string | null
  ): Request<T> {
    if (!this.deleted_) {
      let request = this.requestMaker_(requestInfo, authToken, this.pool_);
      this.requestMap_.addRequest(request);
      return request;
    } else {
      return new FailRequest(errorsExports.appDeleted());
    }
  }
 
  /**
   * Stop running requests and prevent more from being created.
   */
  deleteApp() {
    this.deleted_ = true;
    this.app_ = null;
    this.requestMap_.clear();
  }
 
  maxUploadRetryTime(): number {
    return this.maxUploadRetryTime_;
  }
 
  setMaxUploadRetryTime(time: number) {
    this.maxUploadRetryTime_ = time;
  }
 
  maxOperationRetryTime(): number {
    return this.maxOperationRetryTime_;
  }
 
  setMaxOperationRetryTime(time: number) {
    this.maxOperationRetryTime_ = time;
  }
}