( options: any, skipAutoAuth: boolean = false, )
| 103 | * @param options CLI options. |
| 104 | */ |
| 105 | export async function requireAuth( |
| 106 | options: any, |
| 107 | skipAutoAuth: boolean = false, |
| 108 | ): Promise<string | null> { |
| 109 | lastOptions = options; |
| 110 | const requiredScopes = [scopes.CLOUD_PLATFORM]; |
| 111 | if (isFirebaseStudio()) { |
| 112 | requiredScopes.push(scopes.USERINFO_EMAIL); |
| 113 | } |
| 114 | api.setScopes(requiredScopes); |
| 115 | options.authScopes = api.getScopes(); |
| 116 | |
| 117 | const tokens = options.tokens as Tokens | undefined; |
| 118 | const user = options.user as User | undefined; |
| 119 | let tokenOpt = utils.getInheritedOption(options, "token"); |
| 120 | if (tokenOpt) { |
| 121 | logger.debug("> authorizing via --token option"); |
| 122 | utils.logWarning( |
| 123 | "Authenticating with `--token` is deprecated and will be removed in a future major version of `firebase-tools`. " + |
| 124 | "Instead, use a service account key with `GOOGLE_APPLICATION_CREDENTIALS`: https://cloud.google.com/docs/authentication/getting-started", |
| 125 | ); |
| 126 | } else if (process.env.FIREBASE_TOKEN) { |
| 127 | logger.debug("> authorizing via FIREBASE_TOKEN environment variable"); |
| 128 | utils.logWarning( |
| 129 | "Authenticating with `FIREBASE_TOKEN` is deprecated and will be removed in a future major version of `firebase-tools`. " + |
| 130 | "Instead, use a service account key with `GOOGLE_APPLICATION_CREDENTIALS`: https://cloud.google.com/docs/authentication/getting-started", |
| 131 | ); |
| 132 | } else if (user && (!isExpired(tokens) || tokens?.refresh_token)) { |
| 133 | logger.debug(`> authorizing via signed-in user (${user.email})`); |
| 134 | } else if (skipAutoAuth) { |
| 135 | return null; |
| 136 | } else { |
| 137 | try { |
| 138 | return await autoAuth(options, options.authScopes); |
| 139 | } catch (e: any) { |
| 140 | throw new FirebaseError( |
| 141 | `Failed to authenticate, have you run ${clc.bold("firebase login")}?`, |
| 142 | { original: e }, |
| 143 | ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | tokenOpt = tokenOpt || process.env.FIREBASE_TOKEN; |
| 148 | |
| 149 | if (tokenOpt) { |
| 150 | setRefreshToken(tokenOpt); |
| 151 | return null; |
| 152 | } |
| 153 | |
| 154 | if (!user || !tokens) { |
| 155 | throw new FirebaseError(AUTH_ERROR_MESSAGE); |
| 156 | } |
| 157 | |
| 158 | // TODO: 90 percent sure this is redundant, as the only time we hit this is if options.user/options.token is set, and |
| 159 | // setActiveAccount is the only code that sets those. |
| 160 | setActiveAccount(options, { user, tokens }); |
| 161 | return user.email; |
| 162 | } |
no test coverage detected
searching dependent graphs…