MCPcopy Create free account
hub / github.com/firebase/firebase-tools / requireAuth

Function requireAuth

src/requireAuth.ts:105–162  ·  view source on GitHub ↗
(
  options: any,
  skipAutoAuth: boolean = false,
)

Source from the content-addressed store, hash-verified

103 * @param options CLI options.
104 */
105export 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}

Callers 12

requirePermissionsFunction · 0.90
doSetupFunction · 0.90
getIAMUserFunction · 0.90
getAuthenticatedUserMethod · 0.90
beforeEmulatorCommandFunction · 0.90
tests.tsFile · 0.90
tests.tsFile · 0.90
requireAuthWrapperFunction · 0.90
refreshAuthFunction · 0.85

Calls 5

isFirebaseStudioFunction · 0.90
isExpiredFunction · 0.90
setRefreshTokenFunction · 0.90
setActiveAccountFunction · 0.90
autoAuthFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…