33// Version 2.0, that can be found in the LICENSE file.
44
55import { Timestamp } from 'google-protobuf/google/protobuf/timestamp_pb' ;
6- import { Request , Response } from 'express' ;
7- import { Strategy as BaseStrategy } from 'passport-strategy' ;
86import passport from 'passport' ;
97import { v4 as uuidV4 } from 'uuid' ;
108import * as logger from 'winston' ;
@@ -24,34 +22,10 @@ export interface VerifiedUserInfo {
2422 providerUserId : string ;
2523}
2624
27- interface StrategyOptions { }
28-
2925interface SerializedSessionUser {
3026 id : string ;
3127}
3228
33- type VerifyDone = ( error : Error | null , user ?: unknown ) => void ;
34-
35- interface VerifyFunction {
36- ( firestoreIdToken : string , done : VerifyDone ) : Promise < void > ;
37- }
38-
39- function toError ( error : unknown ) : Error {
40- if ( error instanceof Error ) {
41- return error ;
42- }
43- if ( typeof error === 'string' ) {
44- return new Error ( error ) ;
45- }
46- if ( typeof error === 'object' && error !== null ) {
47- const message = ( error as Record < string , unknown > ) . message ;
48- if ( typeof message === 'string' ) {
49- return new Error ( message ) ;
50- }
51- }
52- return new Error ( String ( error ) ) ;
53- }
54-
5529function isSerializedSessionUser ( value : unknown ) : value is SerializedSessionUser {
5630 return (
5731 typeof value === 'object' &&
@@ -60,42 +34,6 @@ function isSerializedSessionUser(value: unknown): value is SerializedSessionUser
6034 ) ;
6135}
6236
63- class FirestoreAuthStrategy extends BaseStrategy implements passport . Strategy {
64- readonly name : 'firestore-auth' ;
65- private readonly verify : VerifyFunction ;
66-
67- constructor ( options : StrategyOptions , verify : VerifyFunction ) {
68- super ( ) ;
69- this . name = 'firestore-auth' ;
70- this . verify = verify ;
71- }
72-
73- authenticate ( req : Request , _options ?: unknown ) : void {
74- if ( ! req . body || ! req . body . idToken ) {
75- this . error ( new Error ( 'no idToken in body' ) ) ;
76- return ;
77- }
78-
79- const idToken = req . body . idToken as string ;
80-
81- const verified : VerifyDone = ( error , user ) : void => {
82- if ( error ) {
83- return this . error ( error ) ;
84- }
85- if ( ! user ) {
86- return this . fail ( 401 ) ;
87- }
88- this . success ( user as User ) ;
89- } ;
90-
91- this . verify ( idToken , verified )
92- . then ( ( ) => { } )
93- . catch ( ( err ) => {
94- this . error ( toError ( err ) ) ;
95- } ) ;
96- }
97- }
98-
9937function getProviderFromFirebaseUser ( fbUser : admin . auth . UserRecord ) : AuthProvider {
10038 if ( ! fbUser . providerData || fbUser . providerData . length === 0 ) {
10139 return 'password' ;
@@ -276,25 +214,7 @@ export async function getOrCreateUserFromVerifiedInfo(
276214 return [ user , undefined ] ;
277215}
278216
279- export const authn = ( app : Application , firebaseAuthn : admin . auth . Auth ) : void => {
280- // const config = app.get('authentication');
281-
282- // DEPRECATED: Use /auth/login instead. This endpoint exists for backward
283- // compatibility with existing mobile apps and will be removed in a future release.
284- passport . use (
285- new FirestoreAuthStrategy ( { } , async ( firestoreIdToken : string , done : VerifyDone ) => {
286- const [ user , err ] = await getOrCreateUserFromIdToken ( app . db . user , firebaseAuthn , firestoreIdToken ) ;
287- if ( err !== undefined ) {
288- logger . error ( err ) ;
289- done ( err ) ;
290- } else if ( user ) {
291- done ( null , user ) ;
292- } else {
293- throw new Error ( 'unreachable' ) ;
294- }
295- } ) ,
296- ) ;
297-
217+ export const authn = ( app : Application ) : void => {
298218 passport . serializeUser ( ( rawUser , done ) => {
299219 if ( ! ( rawUser instanceof User ) ) {
300220 done ( new Error ( 'serializeUser expected a User instance' ) ) ;
@@ -325,12 +245,4 @@ export const authn = (app: Application, firebaseAuthn: admin.auth.Auth): void =>
325245
326246 app . use ( passport . initialize ( ) ) ;
327247 app . use ( passport . session ( ) ) ;
328-
329- app . post ( '/session' , passport . authenticate ( 'firestore-auth' , { } ) , ( req : Request , res : Response ) : void => {
330- res . sendStatus ( 200 ) ;
331- } ) ;
332-
333- app . delete ( '/session' , ( _req : Request , _res : Response ) : void => {
334- console . log ( `TODO: unset cookie` ) ;
335- } ) ;
336248} ;
0 commit comments