Skip to content

Commit 3fa2676

Browse files
committed
feat: identity provider functions
1 parent 44ed9a2 commit 3fa2676

File tree

13 files changed

+1162
-0
lines changed

13 files changed

+1162
-0
lines changed

samples/identity/.firebaserc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "python-functions-testing"
4+
}
5+
}

samples/identity/.gitignore

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
firebase-debug.log*
8+
firebase-debug.*.log*
9+
10+
# Firebase cache
11+
.firebase/
12+
13+
# Firebase config
14+
15+
# Uncomment this if you'd like others to create their own Firebase project.
16+
# For a team working on the same Firebase project(s), it is recommended to leave
17+
# it commented so all members can deploy to the same project(s) in .firebaserc.
18+
# .firebaserc
19+
20+
# Runtime data
21+
pids
22+
*.pid
23+
*.seed
24+
*.pid.lock
25+
26+
# Directory for instrumented libs generated by jscoverage/JSCover
27+
lib-cov
28+
29+
# Coverage directory used by tools like istanbul
30+
coverage
31+
32+
# nyc test coverage
33+
.nyc_output
34+
35+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
36+
.grunt
37+
38+
# Bower dependency directory (https://bower.io/)
39+
bower_components
40+
41+
# node-waf configuration
42+
.lock-wscript
43+
44+
# Compiled binary addons (http://nodejs.org/api/addons.html)
45+
build/Release
46+
47+
# Dependency directories
48+
node_modules/
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Optional REPL history
57+
.node_repl_history
58+
59+
# Output of 'npm pack'
60+
*.tgz
61+
62+
# Yarn Integrity file
63+
.yarn-integrity
64+
65+
# dotenv environment variables file
66+
.env

samples/identity/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Required to avoid a 'duplicate modules' mypy error
2+
# in monorepos that have multiple main.py files.
3+
# https://github.com/python/mypy/issues/4008

samples/identity/client/index.html

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<script type="module">
2+
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.19.1/firebase-app.js";
3+
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/9.19.1/firebase-auth.js";
4+
5+
const firebaseConfig = {
6+
apiKey: "AIzaSyCZ2C2_0jQIkQItbiJ4IGbL8OLObbK2mY0",
7+
authDomain: "python-functions-testing.firebaseapp.com",
8+
databaseURL: "https://python-functions-testing-default-rtdb.europe-west1.firebasedatabase.app",
9+
projectId: "python-functions-testing",
10+
storageBucket: "python-functions-testing.appspot.com",
11+
messagingSenderId: "441947996129",
12+
appId: "1:441947996129:web:227004b738ba64f04edca0",
13+
};
14+
15+
const app = initializeApp(firebaseConfig);
16+
const auth = getAuth(app);
17+
18+
// createUserWithEmailAndPassword(auth, "[email protected]", "password")
19+
signInWithEmailAndPassword(auth, "[email protected]", "password")
20+
.then((userCredential) => {
21+
console.log("signed in", userCredential);
22+
})
23+
.catch(console.error);
24+
</script>

samples/identity/firebase.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"functions": [
3+
{
4+
"source": "functions",
5+
"codebase": "default",
6+
"ignore": [
7+
"venv"
8+
]
9+
}
10+
]
11+
}

samples/identity/functions/.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# pyenv
2+
.python-version
3+
4+
# Installer logs
5+
pip-log.txt
6+
pip-delete-this-directory.txt
7+
8+
# Environments
9+
.env
10+
.venv
11+
venv/
12+
venv.bak/
13+
__pycache__

samples/identity/functions/main.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Firebase Cloud Functions for blocking auth functions example."""
2+
from firebase_functions import identity_fn
3+
4+
5+
@identity_fn.before_user_created(
6+
id_token=True,
7+
access_token=True,
8+
refresh_token=True,
9+
)
10+
def beforeusercreated(
11+
event: identity_fn.AuthBlockingEvent
12+
) -> identity_fn.BeforeCreateResponse | None:
13+
print(event)
14+
if not event.data.email:
15+
return None
16+
if "@cats.com" in event.data.email:
17+
return identity_fn.BeforeCreateResponse(display_name="🐈",
18+
custom_claims={"meow": True})
19+
if "@dogs.com" in event.data.email:
20+
return identity_fn.BeforeCreateResponse(display_name="🐕",
21+
custom_claims={"woof": True})
22+
return None
23+
24+
25+
@identity_fn.before_user_signed_in(
26+
id_token=True,
27+
access_token=True,
28+
refresh_token=True,
29+
)
30+
def beforeusersignedin(
31+
event: identity_fn.AuthBlockingEvent
32+
) -> identity_fn.BeforeSignInResponse | None:
33+
print(event)
34+
if not event.data.email:
35+
return None
36+
if "@cats.com" in event.data.email:
37+
return identity_fn.BeforeSignInResponse(
38+
display_name="🐈", session_claims={"temporary_meow": True})
39+
if "@dogs.com" in event.data.email:
40+
return identity_fn.BeforeSignInResponse(
41+
display_name="🐕", session_claims={"temporary_woof": True})
42+
return None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Not published yet,
2+
# firebase-functions-python >= 0.0.1
3+
# so we use a relative path during development:
4+
./../../../
5+
# Or switch to git ref for deployment testing:
6+
# git+https://github.com/firebase/firebase-functions-python.git@main#egg=firebase-functions
7+
8+
firebase-admin >= 6.0.1

0 commit comments

Comments
 (0)