-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apps): use syncronous login functions to augment firebase authen…
…tication. Using syncronous login functions, only allow google.com users to login and support capturing github tokens for accounts which link their github accout.
- Loading branch information
1 parent
5f7f2df
commit f6fd0c4
Showing
6 changed files
with
112 additions
and
5 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,30 @@ | ||
load("//tools:defaults.bzl", "esbuild", "ts_library") | ||
|
||
package(default_visibility = ["//visibility:private"]) | ||
|
||
ts_library( | ||
name = "accounts", | ||
srcs = [ | ||
"before-create.ts", | ||
"before-sign-in.ts", | ||
"index.ts", | ||
], | ||
visibility = [ | ||
"//apps/functions:__pkg__", | ||
], | ||
deps = [ | ||
"@npm//gcip-cloud-functions", | ||
], | ||
) | ||
|
||
esbuild( | ||
name = "accounts_compiled", | ||
entry_points = [ | ||
"index.ts", | ||
], | ||
format = "esm", | ||
visibility = ["//apps:__pkg__"], | ||
deps = [ | ||
":accounts", | ||
], | ||
) |
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,10 @@ | ||
import {Auth, https, UserRecord} from 'gcip-cloud-functions'; | ||
|
||
/** Validate accounts before their creation using google cloud before create syncronous function. */ | ||
export const beforeCreate = new Auth().functions().beforeCreateHandler((user: UserRecord) => { | ||
if (user.email && user.email.indexOf('@google.com') === -1) { | ||
throw new https.HttpsError('invalid-argument', `Unauthorized email "${user.email}"`); | ||
} | ||
|
||
return {}; | ||
}); |
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,34 @@ | ||
import { | ||
Auth, | ||
https, | ||
UserRecord, | ||
UserEventUpdateRequest, | ||
AuthEventContext, | ||
} from 'gcip-cloud-functions'; | ||
|
||
/** Validate accounts before sign in using google cloud before sigin in syncronous function. */ | ||
|
||
export const beforeSignIn = new Auth() | ||
.functions() | ||
.beforeSignInHandler( | ||
async (user: UserRecord, context: AuthEventContext): Promise<UserEventUpdateRequest> => { | ||
/** The UserEventUpdate to save based on the signin results. */ | ||
const event: UserEventUpdateRequest = {}; | ||
|
||
// If a user is able to reach this without a login credential being present, throw an auth error. | ||
// Note: This should not be possible, but it doesn't hurt to have this check in place. | ||
if (context.credential === undefined) { | ||
throw new https.HttpsError( | ||
'unauthenticated', | ||
`Cannot sign in as '${user.email}' without credential.`, | ||
); | ||
} | ||
|
||
// When users sign in with github, we save the access token as a claim on the user object. | ||
if (context.credential.providerId === 'github.com') { | ||
event.customClaims = {...event.customClaims, githubToken: context.credential.accessToken}; | ||
} | ||
|
||
return event; | ||
}, | ||
); |
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,2 @@ | ||
export {beforeCreate} from './before-create'; | ||
export {beforeSignIn} from './before-sign-in'; |
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