|
16 | 16 |
|
17 | 17 | package com.google.firebase.auth;
|
18 | 18 |
|
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | + |
19 | 22 | 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; |
20 | 26 | import com.google.common.base.Supplier;
|
21 | 27 | import com.google.firebase.FirebaseApp;
|
22 | 28 | import com.google.firebase.ImplFirebaseTrampolines;
|
23 | 29 | import com.google.firebase.auth.AbstractFirebaseAuth.Builder;
|
24 | 30 | import com.google.firebase.auth.internal.FirebaseTokenFactory;
|
| 31 | +import com.google.firebase.internal.CallableOperation; |
25 | 32 | import com.google.firebase.internal.FirebaseService;
|
| 33 | +import com.google.firebase.internal.NonNull; |
26 | 34 | import java.util.concurrent.atomic.AtomicBoolean;
|
27 | 35 |
|
28 | 36 | /**
|
@@ -79,6 +87,132 @@ public static synchronized FirebaseAuth getInstance(FirebaseApp app) {
|
79 | 87 | return service.getInstance();
|
80 | 88 | }
|
81 | 89 |
|
| 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 | + |
82 | 216 | @Override
|
83 | 217 | protected void doDestroy() {
|
84 | 218 | // Only destroy the tenant manager if it has been created.
|
|
0 commit comments