-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from dead8309/firebase
Configure Firebase Authentication and Enhancements
- Loading branch information
Showing
17 changed files
with
3,029 additions
and
1,973 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
MONGODB_URL= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
node_modules | ||
lib | ||
lib | ||
!src/lib | ||
src/service.json | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,26 +7,9 @@ | |
"max": 50 | ||
}, | ||
"authentication": { | ||
"entity": "user", | ||
"service": "users", | ||
"oauth": {}, | ||
"secret": "KD3VmDj6rQKZqIAHEXZPEXcsHPs=", | ||
"authStrategies": [ | ||
"jwt", | ||
"local" | ||
], | ||
"jwtOptions": { | ||
"header": { | ||
"typ": "access" | ||
}, | ||
"audience": "https://yourdomain.com", | ||
"issuer": "feathers", | ||
"algorithm": "HS256", | ||
"expiresIn": "1d" | ||
}, | ||
"local": { | ||
"usernameField": "email", | ||
"passwordField": "password" | ||
} | ||
}, | ||
"mongodb": "mongodb+srv://ProgrammerGod:Suvo%[email protected]/faculty_api_nodejs" | ||
"service": "users", | ||
"authStrategies": ["firebase"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"type": "service_account", | ||
"project_id": "", | ||
"private_key_id": "", | ||
"private_key": "", | ||
"client_email": "", | ||
"client_id": "", | ||
"auth_uri": "", | ||
"token_uri": "", | ||
"auth_provider_x509_cert_url": "", | ||
"client_x509_cert_url": "", | ||
"universe_domain": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
import { ServiceAddons } from '@feathersjs/feathers'; | ||
import { AuthenticationService, JWTStrategy } from '@feathersjs/authentication'; | ||
import { LocalStrategy } from '@feathersjs/authentication-local'; | ||
import { expressOauth } from '@feathersjs/authentication-oauth'; | ||
|
||
import { AuthenticationService } from '@feathersjs/authentication'; | ||
import { Application } from './declarations'; | ||
import { FirebaseJWTStrategy } from './firebase'; | ||
import { expressOauth } from '@feathersjs/authentication-oauth'; | ||
|
||
declare module './declarations' { | ||
interface ServiceTypes { | ||
'authentication': AuthenticationService & ServiceAddons<any>; | ||
authentication: AuthenticationService & ServiceAddons<any>; | ||
} | ||
} | ||
|
||
export default function(app: Application): void { | ||
export default function (app: Application): void { | ||
const authentication = new AuthenticationService(app); | ||
authentication.register('firebase', new FirebaseJWTStrategy(app)); | ||
|
||
authentication.register('jwt', new JWTStrategy()); | ||
authentication.register('local', new LocalStrategy()); | ||
|
||
app.use('/authentication', authentication); | ||
app.configure(expressOauth()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import * as firebaseAdmin from 'firebase-admin'; | ||
import logger from './logger'; | ||
import { Application } from './declarations'; | ||
import { AuthenticationRequest, JWTStrategy } from '@feathersjs/authentication'; | ||
import { Params } from '@feathersjs/feathers'; | ||
import { NotAuthenticated } from '@feathersjs/errors'; | ||
import { DecodedIdToken } from 'firebase-admin/lib/auth/token-verifier'; | ||
import { initializeFirebaseAdmin } from './lib/firebase-admin'; | ||
|
||
|
||
export class FirebaseJWTStrategy extends JWTStrategy { | ||
constructor(app: Application) { | ||
super(); | ||
initializeFirebaseAdmin(app); | ||
} | ||
|
||
async authenticate( | ||
authentication: AuthenticationRequest, | ||
params: Params, | ||
..._rest: any[] | ||
): Promise<{ | ||
accessToken: any; | ||
authentication: { strategy: string; accessToken: any; payload: any }; | ||
}> { | ||
try { | ||
const token = authentication.accessToken ?? authentication.access_token; | ||
const user = await firebaseAdmin.auth().verifyIdToken(token); | ||
if (!user) { | ||
throw new NotAuthenticated(); | ||
} | ||
|
||
/** | ||
* Only create/update the user if the params.provider is rest. | ||
* | ||
* Params.provider will be rest when this is triggered by /authentication endpoint. | ||
* Params.provider will be undefined when this is triggered by a service call/authenticate hook (e.g. users.create) | ||
*/ | ||
if (params.provider === 'rest') { | ||
logger.info('params.provider is rest, creating/updating user'); | ||
const existingUser = await this.getEntity(user.uid, params); | ||
!existingUser ? await this.createEntity(user, params) : await this.updateEntity(existingUser, user, params); | ||
} | ||
|
||
return { | ||
accessToken: token, | ||
authentication: { | ||
strategy: this.name!!, | ||
accessToken: token, | ||
payload: { | ||
user: await this.getEntityData(user), | ||
}, | ||
}, | ||
}; | ||
} catch (e) { | ||
logger.error(e); | ||
throw new NotAuthenticated(); | ||
} | ||
} | ||
|
||
async getEntity(id: string, params: Params): Promise<any> { | ||
const result = await this.entityService.find({ | ||
query: { | ||
uid: id, | ||
}, | ||
...params, | ||
}); | ||
const [entity = null] = result.data ? result.data : result; | ||
return entity; | ||
} | ||
|
||
async createEntity(user: DecodedIdToken, params: Params) { | ||
const data = await this.getEntityData(user, null, params); | ||
return this.entityService.create(data, params); | ||
} | ||
|
||
async updateEntity( | ||
existingEntity: any, | ||
user: DecodedIdToken, | ||
params: Params, | ||
) { | ||
const id = existingEntity['_id']; | ||
const data = await this.getEntityData(user, existingEntity, params); | ||
return this.entityService.patch(id, data, params); | ||
} | ||
|
||
async getEntityData( | ||
user: DecodedIdToken, | ||
_existingEntity?: any, | ||
_params?: Params, | ||
) { | ||
return { | ||
uid: user.uid, | ||
email: user.email, | ||
name: user.name, | ||
pictureUrl: user.picture, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as admin from 'firebase-admin'; | ||
import { Application } from '../declarations'; | ||
import * as config from "../service.json" | ||
|
||
export const initializeFirebaseAdmin = (app: Application) => { | ||
const firebaseConfig = config as admin.ServiceAccount; | ||
|
||
try { | ||
admin.initializeApp({ | ||
credential: admin.credential.cert(firebaseConfig), | ||
}); | ||
} catch (e) { | ||
console.log('erorr initializing firebase', e); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.