Skip to content

Commit c90a7d5

Browse files
committed
Move createSessionCookie and verifySessionCookie back to FirebaseAuth.
1 parent b38cadc commit c90a7d5

File tree

2 files changed

+143
-127
lines changed

2 files changed

+143
-127
lines changed

src/main/java/com/google/firebase/auth/AbstractFirebaseAuth.java

Lines changed: 9 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -180,132 +180,6 @@ public String execute() throws FirebaseAuthException {
180180
};
181181
}
182182

183-
/**
184-
* Creates a new Firebase session cookie from the given ID token and options. The returned JWT can
185-
* be set as a server-side session cookie with a custom cookie policy.
186-
*
187-
* @param idToken The Firebase ID token to exchange for a session cookie.
188-
* @param options Additional options required to create the cookie.
189-
* @return A Firebase session cookie string.
190-
* @throws IllegalArgumentException If the ID token is null or empty, or if options is null.
191-
* @throws FirebaseAuthException If an error occurs while generating the session cookie.
192-
*/
193-
public String createSessionCookie(@NonNull String idToken, @NonNull SessionCookieOptions options)
194-
throws FirebaseAuthException {
195-
return createSessionCookieOp(idToken, options).call();
196-
}
197-
198-
/**
199-
* Similar to {@link #createSessionCookie(String, SessionCookieOptions)} but performs the
200-
* operation asynchronously.
201-
*
202-
* @param idToken The Firebase ID token to exchange for a session cookie.
203-
* @param options Additional options required to create the cookie.
204-
* @return An {@code ApiFuture} which will complete successfully with a session cookie string. If
205-
* an error occurs while generating the cookie or if the specified ID token is invalid, the
206-
* future throws a {@link FirebaseAuthException}.
207-
* @throws IllegalArgumentException If the ID token is null or empty, or if options is null.
208-
*/
209-
public ApiFuture<String> createSessionCookieAsync(
210-
@NonNull String idToken, @NonNull SessionCookieOptions options) {
211-
return createSessionCookieOp(idToken, options).callAsync(firebaseApp);
212-
}
213-
214-
private CallableOperation<String, FirebaseAuthException> createSessionCookieOp(
215-
final String idToken, final SessionCookieOptions options) {
216-
checkNotDestroyed();
217-
checkArgument(!Strings.isNullOrEmpty(idToken), "idToken must not be null or empty");
218-
checkNotNull(options, "options must not be null");
219-
final FirebaseUserManager userManager = getUserManager();
220-
return new CallableOperation<String, FirebaseAuthException>() {
221-
@Override
222-
protected String execute() throws FirebaseAuthException {
223-
return userManager.createSessionCookie(idToken, options);
224-
}
225-
};
226-
}
227-
228-
/**
229-
* Parses and verifies a Firebase session cookie.
230-
*
231-
* <p>If verified successfully, returns a parsed version of the cookie from which the UID and the
232-
* other claims can be read. If the cookie is invalid, throws a {@link FirebaseAuthException}.
233-
*
234-
* <p>This method does not check whether the cookie has been revoked. See {@link
235-
* #verifySessionCookie(String, boolean)}.
236-
*
237-
* @param cookie A Firebase session cookie string to verify and parse.
238-
* @return A {@link FirebaseToken} representing the verified and decoded cookie.
239-
*/
240-
public FirebaseToken verifySessionCookie(String cookie) throws FirebaseAuthException {
241-
return verifySessionCookie(cookie, false);
242-
}
243-
244-
/**
245-
* Parses and verifies a Firebase session cookie.
246-
*
247-
* <p>If {@code checkRevoked} is true, additionally verifies that the cookie has not been revoked.
248-
*
249-
* <p>If verified successfully, returns a parsed version of the cookie from which the UID and the
250-
* other claims can be read. If the cookie is invalid or has been revoked while {@code
251-
* checkRevoked} is true, throws a {@link FirebaseAuthException}.
252-
*
253-
* @param cookie A Firebase session cookie string to verify and parse.
254-
* @param checkRevoked A boolean indicating whether to check if the cookie was explicitly revoked.
255-
* @return A {@link FirebaseToken} representing the verified and decoded cookie.
256-
*/
257-
public FirebaseToken verifySessionCookie(String cookie, boolean checkRevoked)
258-
throws FirebaseAuthException {
259-
return verifySessionCookieOp(cookie, checkRevoked).call();
260-
}
261-
262-
/**
263-
* Similar to {@link #verifySessionCookie(String)} but performs the operation asynchronously.
264-
*
265-
* @param cookie A Firebase session cookie string to verify and parse.
266-
* @return An {@code ApiFuture} which will complete successfully with the parsed cookie, or
267-
* unsuccessfully with the failure Exception.
268-
*/
269-
public ApiFuture<FirebaseToken> verifySessionCookieAsync(String cookie) {
270-
return verifySessionCookieAsync(cookie, false);
271-
}
272-
273-
/**
274-
* Similar to {@link #verifySessionCookie(String, boolean)} but performs the operation
275-
* asynchronously.
276-
*
277-
* @param cookie A Firebase session cookie string to verify and parse.
278-
* @param checkRevoked A boolean indicating whether to check if the cookie was explicitly revoked.
279-
* @return An {@code ApiFuture} which will complete successfully with the parsed cookie, or
280-
* unsuccessfully with the failure Exception.
281-
*/
282-
public ApiFuture<FirebaseToken> verifySessionCookieAsync(String cookie, boolean checkRevoked) {
283-
return verifySessionCookieOp(cookie, checkRevoked).callAsync(firebaseApp);
284-
}
285-
286-
private CallableOperation<FirebaseToken, FirebaseAuthException> verifySessionCookieOp(
287-
final String cookie, final boolean checkRevoked) {
288-
checkNotDestroyed();
289-
checkArgument(!Strings.isNullOrEmpty(cookie), "Session cookie must not be null or empty");
290-
final FirebaseTokenVerifier sessionCookieVerifier = getSessionCookieVerifier(checkRevoked);
291-
return new CallableOperation<FirebaseToken, FirebaseAuthException>() {
292-
@Override
293-
public FirebaseToken execute() throws FirebaseAuthException {
294-
return sessionCookieVerifier.verifyToken(cookie);
295-
}
296-
};
297-
}
298-
299-
@VisibleForTesting
300-
FirebaseTokenVerifier getSessionCookieVerifier(boolean checkRevoked) {
301-
FirebaseTokenVerifier verifier = cookieVerifier.get();
302-
if (checkRevoked) {
303-
FirebaseUserManager userManager = getUserManager();
304-
verifier = RevocationCheckDecorator.decorateSessionCookieVerifier(verifier, userManager);
305-
}
306-
return verifier;
307-
}
308-
309183
/**
310184
* Parses and verifies a Firebase ID Token.
311185
*
@@ -1043,6 +917,14 @@ public ApiFuture<String> generateSignInWithEmailLinkAsync(
1043917
.callAsync(firebaseApp);
1044918
}
1045919

920+
FirebaseApp getFirebaseApp() {
921+
return this.firebaseApp;
922+
}
923+
924+
FirebaseTokenVerifier getCookieVerifier() {
925+
return this.cookieVerifier.get();
926+
}
927+
1046928
FirebaseUserManager getUserManager() {
1047929
return this.userManager.get();
1048930
}
@@ -1077,7 +959,7 @@ public T get() {
1077959
});
1078960
}
1079961

1080-
private void checkNotDestroyed() {
962+
void checkNotDestroyed() {
1081963
synchronized (lock) {
1082964
checkState(
1083965
!destroyed.get(),

src/main/java/com/google/firebase/auth/FirebaseAuth.java

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@
1616

1717
package com.google.firebase.auth;
1818

19+
import static com.google.common.base.Preconditions.checkArgument;
20+
import static com.google.common.base.Preconditions.checkNotNull;
21+
1922
import com.google.api.client.util.Clock;
23+
import com.google.api.core.ApiFuture;
24+
import com.google.common.annotations.VisibleForTesting;
25+
import com.google.common.base.Strings;
2026
import com.google.common.base.Supplier;
2127
import com.google.firebase.FirebaseApp;
2228
import com.google.firebase.ImplFirebaseTrampolines;
2329
import com.google.firebase.auth.AbstractFirebaseAuth.Builder;
2430
import com.google.firebase.auth.internal.FirebaseTokenFactory;
31+
import com.google.firebase.internal.CallableOperation;
2532
import com.google.firebase.internal.FirebaseService;
33+
import com.google.firebase.internal.NonNull;
2634
import java.util.concurrent.atomic.AtomicBoolean;
2735

2836
/**
@@ -79,6 +87,132 @@ public static synchronized FirebaseAuth getInstance(FirebaseApp app) {
7987
return service.getInstance();
8088
}
8189

90+
/**
91+
* Creates a new Firebase session cookie from the given ID token and options. The returned JWT can
92+
* be set as a server-side session cookie with a custom cookie policy.
93+
*
94+
* @param idToken The Firebase ID token to exchange for a session cookie.
95+
* @param options Additional options required to create the cookie.
96+
* @return A Firebase session cookie string.
97+
* @throws IllegalArgumentException If the ID token is null or empty, or if options is null.
98+
* @throws FirebaseAuthException If an error occurs while generating the session cookie.
99+
*/
100+
public String createSessionCookie(@NonNull String idToken, @NonNull SessionCookieOptions options)
101+
throws FirebaseAuthException {
102+
return createSessionCookieOp(idToken, options).call();
103+
}
104+
105+
/**
106+
* Similar to {@link #createSessionCookie(String, SessionCookieOptions)} but performs the
107+
* operation asynchronously.
108+
*
109+
* @param idToken The Firebase ID token to exchange for a session cookie.
110+
* @param options Additional options required to create the cookie.
111+
* @return An {@code ApiFuture} which will complete successfully with a session cookie string. If
112+
* an error occurs while generating the cookie or if the specified ID token is invalid, the
113+
* future throws a {@link FirebaseAuthException}.
114+
* @throws IllegalArgumentException If the ID token is null or empty, or if options is null.
115+
*/
116+
public ApiFuture<String> createSessionCookieAsync(
117+
@NonNull String idToken, @NonNull SessionCookieOptions options) {
118+
return createSessionCookieOp(idToken, options).callAsync(getFirebaseApp());
119+
}
120+
121+
private CallableOperation<String, FirebaseAuthException> createSessionCookieOp(
122+
final String idToken, final SessionCookieOptions options) {
123+
checkNotDestroyed();
124+
checkArgument(!Strings.isNullOrEmpty(idToken), "idToken must not be null or empty");
125+
checkNotNull(options, "options must not be null");
126+
final FirebaseUserManager userManager = getUserManager();
127+
return new CallableOperation<String, FirebaseAuthException>() {
128+
@Override
129+
protected String execute() throws FirebaseAuthException {
130+
return userManager.createSessionCookie(idToken, options);
131+
}
132+
};
133+
}
134+
135+
/**
136+
* Parses and verifies a Firebase session cookie.
137+
*
138+
* <p>If verified successfully, returns a parsed version of the cookie from which the UID and the
139+
* other claims can be read. If the cookie is invalid, throws a {@link FirebaseAuthException}.
140+
*
141+
* <p>This method does not check whether the cookie has been revoked. See {@link
142+
* #verifySessionCookie(String, boolean)}.
143+
*
144+
* @param cookie A Firebase session cookie string to verify and parse.
145+
* @return A {@link FirebaseToken} representing the verified and decoded cookie.
146+
*/
147+
public FirebaseToken verifySessionCookie(String cookie) throws FirebaseAuthException {
148+
return verifySessionCookie(cookie, false);
149+
}
150+
151+
/**
152+
* Parses and verifies a Firebase session cookie.
153+
*
154+
* <p>If {@code checkRevoked} is true, additionally verifies that the cookie has not been revoked.
155+
*
156+
* <p>If verified successfully, returns a parsed version of the cookie from which the UID and the
157+
* other claims can be read. If the cookie is invalid or has been revoked while {@code
158+
* checkRevoked} is true, throws a {@link FirebaseAuthException}.
159+
*
160+
* @param cookie A Firebase session cookie string to verify and parse.
161+
* @param checkRevoked A boolean indicating whether to check if the cookie was explicitly revoked.
162+
* @return A {@link FirebaseToken} representing the verified and decoded cookie.
163+
*/
164+
public FirebaseToken verifySessionCookie(String cookie, boolean checkRevoked)
165+
throws FirebaseAuthException {
166+
return verifySessionCookieOp(cookie, checkRevoked).call();
167+
}
168+
169+
/**
170+
* Similar to {@link #verifySessionCookie(String)} but performs the operation asynchronously.
171+
*
172+
* @param cookie A Firebase session cookie string to verify and parse.
173+
* @return An {@code ApiFuture} which will complete successfully with the parsed cookie, or
174+
* unsuccessfully with the failure Exception.
175+
*/
176+
public ApiFuture<FirebaseToken> verifySessionCookieAsync(String cookie) {
177+
return verifySessionCookieAsync(cookie, false);
178+
}
179+
180+
/**
181+
* Similar to {@link #verifySessionCookie(String, boolean)} but performs the operation
182+
* asynchronously.
183+
*
184+
* @param cookie A Firebase session cookie string to verify and parse.
185+
* @param checkRevoked A boolean indicating whether to check if the cookie was explicitly revoked.
186+
* @return An {@code ApiFuture} which will complete successfully with the parsed cookie, or
187+
* unsuccessfully with the failure Exception.
188+
*/
189+
public ApiFuture<FirebaseToken> verifySessionCookieAsync(String cookie, boolean checkRevoked) {
190+
return verifySessionCookieOp(cookie, checkRevoked).callAsync(getFirebaseApp());
191+
}
192+
193+
protected CallableOperation<FirebaseToken, FirebaseAuthException> verifySessionCookieOp(
194+
final String cookie, final boolean checkRevoked) {
195+
checkNotDestroyed();
196+
checkArgument(!Strings.isNullOrEmpty(cookie), "Session cookie must not be null or empty");
197+
final FirebaseTokenVerifier sessionCookieVerifier = getSessionCookieVerifier(checkRevoked);
198+
return new CallableOperation<FirebaseToken, FirebaseAuthException>() {
199+
@Override
200+
public FirebaseToken execute() throws FirebaseAuthException {
201+
return sessionCookieVerifier.verifyToken(cookie);
202+
}
203+
};
204+
}
205+
206+
@VisibleForTesting
207+
FirebaseTokenVerifier getSessionCookieVerifier(boolean checkRevoked) {
208+
FirebaseTokenVerifier verifier = getCookieVerifier();
209+
if (checkRevoked) {
210+
FirebaseUserManager userManager = getUserManager();
211+
verifier = RevocationCheckDecorator.decorateSessionCookieVerifier(verifier, userManager);
212+
}
213+
return verifier;
214+
}
215+
82216
@Override
83217
protected void doDestroy() {
84218
// Only destroy the tenant manager if it has been created.

0 commit comments

Comments
 (0)