|
| 1 | +# Copyright 2018 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import sys |
| 16 | +# [START import_sdk] |
| 17 | +import firebase_admin |
| 18 | +# [END import_sdk] |
| 19 | +from firebase_admin import credentials |
| 20 | +from firebase_admin import auth |
| 21 | + |
| 22 | +sys.path.append("lib") |
| 23 | + |
| 24 | +def initialize_sdk_with_service_account(): |
| 25 | + # [START initialize_sdk_with_service_account] |
| 26 | + import firebase_admin |
| 27 | + from firebase_admin import credentials |
| 28 | + |
| 29 | + cred = credentials.Certificate('path/to/serviceAccountKey.json') |
| 30 | + default_app = firebase_admin.initialize_app(cred) |
| 31 | + # [END initialize_sdk_with_service_account] |
| 32 | + firebase_admin.delete_app(default_app) |
| 33 | + |
| 34 | +def initialize_sdk_with_application_default(): |
| 35 | + # [START initialize_sdk_with_application_default] |
| 36 | + default_app = firebase_admin.initialize_app() |
| 37 | + # [END initialize_sdk_with_application_default] |
| 38 | + firebase_admin.delete_app(default_app) |
| 39 | + |
| 40 | +def initialize_sdk_with_refresh_token(): |
| 41 | + # [START initialize_sdk_with_refresh_token] |
| 42 | + cred = credentials.RefreshToken('path/to/refreshToken.json') |
| 43 | + default_app = firebase_admin.initialize_app(cred) |
| 44 | + # [END initialize_sdk_with_refresh_token] |
| 45 | + firebase_admin.delete_app(default_app) |
| 46 | + |
| 47 | +def access_services_default(): |
| 48 | + cred = credentials.Certificate('path/to/service.json') |
| 49 | + # [START access_services_default] |
| 50 | + # Import the Firebase service |
| 51 | + from firebase_admin import auth |
| 52 | + |
| 53 | + # Initialize the default app |
| 54 | + default_app = firebase_admin.initialize_app(cred) |
| 55 | + print default_app.name # "[DEFAULT]" |
| 56 | + |
| 57 | + # Retrieve services via the auth package... |
| 58 | + # auth.create_custom_token(...) |
| 59 | + # [END access_services_default] |
| 60 | + firebase_admin.delete_app(default_app) |
| 61 | + |
| 62 | +def access_services_nondefault(): |
| 63 | + cred = credentials.Certificate('path/to/service.json') |
| 64 | + other_cred = credentials.Certificate('path/to/other_service.json') |
| 65 | + |
| 66 | + # [START access_services_nondefault] |
| 67 | + # Initialize the default app |
| 68 | + default_app = firebase_admin.initialize_app(cred) |
| 69 | + |
| 70 | + # Initialize another app with a different config |
| 71 | + other_app = firebase_admin.initialize_app(cred, name='other') |
| 72 | + |
| 73 | + print default_app.name # "[DEFAULT]" |
| 74 | + print other_app.name # "other" |
| 75 | + |
| 76 | + # Retrieve default services via the auth package... |
| 77 | + # auth.create_custom_token(...) |
| 78 | + |
| 79 | + # Use the `app` argument to retrieve the other app's services |
| 80 | + # auth.create_custom_token(..., app=other_app) |
| 81 | + # [END access_services_nondefault] |
| 82 | + firebase_admin.delete_app(default_app) |
| 83 | + firebase_admin.delete_app(other_app) |
| 84 | + |
| 85 | +def create_token_uid(): |
| 86 | + cred = credentials.Certificate('path/to/service.json') |
| 87 | + default_app = firebase_admin.initialize_app(cred) |
| 88 | + # [START create_token_uid] |
| 89 | + uid = 'some-uid' |
| 90 | + |
| 91 | + custom_token = auth.create_custom_token(uid) |
| 92 | + # [END create_token_uid] |
| 93 | + firebase_admin.delete_app(default_app) |
| 94 | + return custom_token |
| 95 | + |
| 96 | +def create_token_with_claims(): |
| 97 | + cred = credentials.Certificate('path/to/service.json') |
| 98 | + default_app = firebase_admin.initialize_app(cred) |
| 99 | + # [START create_token_with_claims] |
| 100 | + uid = 'some-uid' |
| 101 | + additional_claims = { |
| 102 | + 'premiumAccount': True |
| 103 | + } |
| 104 | + |
| 105 | + custom_token = auth.create_custom_token(uid, additional_claims) |
| 106 | + # [END create_token_with_claims] |
| 107 | + firebase_admin.delete_app(default_app) |
| 108 | + return custom_token |
| 109 | + |
| 110 | +def verify_token_uid(id_token): |
| 111 | + cred = credentials.Certificate('path/to/service.json') |
| 112 | + default_app = firebase_admin.initialize_app(cred) |
| 113 | + # [START verify_token_uid] |
| 114 | + # id_token comes from the client app (shown above) |
| 115 | + |
| 116 | + decoded_token = auth.verify_id_token(id_token) |
| 117 | + uid = decoded_token['uid'] |
| 118 | + # [END verify_token_uid] |
| 119 | + print uid |
| 120 | + firebase_admin.delete_app(default_app) |
| 121 | + |
| 122 | +def verify_token_uid_check_revoke(id_token): |
| 123 | + cred = credentials.Certificate('path/to/service.json') |
| 124 | + default_app = firebase_admin.initialize_app(cred) |
| 125 | + # [START verify_token_id_check_revoked] |
| 126 | + try: |
| 127 | + # Verify the ID token while checking if the token is revoked by |
| 128 | + # passing check_revoked=True. |
| 129 | + decoded_token = auth.verify_id_token(id_token, check_revoked=True) |
| 130 | + # Token is valid and not revoked. |
| 131 | + uid = decoded_token['uid'] |
| 132 | + except auth.AuthError as exc: |
| 133 | + if exc.code == 'ID_TOKEN_REVOKED': |
| 134 | + # Token revoked, inform the user to reauthenticate or signOut(). |
| 135 | + pass |
| 136 | + else: |
| 137 | + # Token is invalid |
| 138 | + pass |
| 139 | + # [END verify_token_id_check_revoked] |
| 140 | + firebase_admin.delete_app(default_app) |
| 141 | + return uid |
| 142 | + |
| 143 | +def revoke_refresh_token_uid(): |
| 144 | + cred = credentials.Certificate('path/to/service.json') |
| 145 | + default_app = firebase_admin.initialize_app(cred) |
| 146 | + # [START revoke_tokens] |
| 147 | + # Revoke tokens on the backend. |
| 148 | + auth.revoke_refresh_tokens(uid) |
| 149 | + user = auth.get_user(uid) |
| 150 | + # Convert to seconds as the auth_time in the token claims is in seconds. |
| 151 | + revocation_second = user.tokens_valid_after_timestamp / 1000 |
| 152 | + print 'Tokens revoked at: {0}'.format(revocation_second) |
| 153 | + # [END revoke_tokens] |
| 154 | + # [START save_revocation_in_db] |
| 155 | + metadata_ref = firebase_admin.db.reference("metadat
93C6
a/" + uid) |
| 156 | + metadata_ref.set({'revokeTime': revocation_second}) |
| 157 | + # [END save_revocation_in_db] |
| 158 | + print uid |
| 159 | + firebase_admin.delete_app(default_app) |
| 160 | + |
| 161 | +def get_user(uid): |
| 162 | + # [START get_user] |
| 163 | + from firebase_admin import auth |
| 164 | + |
| 165 | + user = auth.get_user(uid) |
| 166 | + print 'Successfully fetched user data: {0}'.format(user.uid) |
| 167 | + # [END get_user] |
| 168 | + |
| 169 | +def get_user_by_email(): |
| 170 | + email = 'user@example.com' |
| 171 | + # [START get_user_by_email] |
| 172 | + from firebase_admin import auth |
| 173 | + |
| 174 | + user = auth.get_user_by_email(email) |
| 175 | + print 'Successfully fetched user data: {0}'.format(user.uid) |
| 176 | + # [END get_user_by_email] |
| 177 | + |
| 178 | +def get_user_by_phone_number(): |
| 179 | + phone = '+1 555 555 0100' |
| 180 | + # [START get_user_by_phone] |
| 181 | + from firebase_admin import auth |
| 182 | + |
| 183 | + user = auth.get_user_by_phone_number(phone) |
| 184 | + print 'Successfully fetched user data: {0}'.format(user.uid) |
| 185 | + # [END get_user_by_phone] |
| 186 | + |
| 187 | +def create_user(): |
| 188 | + # [START create_user] |
| 189 | + user = auth.create_user( |
| 190 | + email='user@example.com', |
| 191 | + email_verified=False, |
| 192 | + phone_number='+15555550100', |
| 193 | + password='secretPassword', |
| 194 | + display_name='John Doe', |
| 195 | + photo_url='http://www.example.com/12345678/photo.png', |
| 196 | + disabled=False) |
| 197 | + print 'Sucessfully created new user: {0}'.format(user.uid) |
| 198 | + # [END create_user] |
| 199 | + return user.uid |
| 200 | + |
| 201 | +def create_user_with_id(): |
| 202 | + # [START create_user_with_id] |
| 203 | + user = auth.create_user( |
| 204 | + uid='some-uid', email='user@example.com', phone_number='+15555550100') |
| 205 | + print 'Sucessfully created new user: {0}'.format(user.uid) |
| 206 | + # [END create_user_with_id] |
| 207 | + |
| 208 | +def update_user(uid): |
| 209 | + # [START update_user] |
| 210 | + user = auth.update_user( |
| 211 | + uid, |
| 212 | + email='user@example.com', |
| 213 | + phone_number='+15555550100', |
| 214 | + email_verified=True, |
| 215 | + password='newPassword', |
| 216 | + display_name='John Doe', |
| 217 | + photo_url='http://www.example.com/12345678/photo.png', |
| 218 | + disabled=True) |
| 219 | + print 'Sucessfully updated user: {0}'.format(user.uid) |
| 220 | + # [END update_user] |
| 221 | + |
| 222 | +def delete_user(uid): |
| 223 | + # [START delete_user] |
| 224 | + auth.delete_user(uid) |
| 225 | + print 'Successfully deleted user' |
| 226 | + # [END delete_user] |
| 227 | + |
| 228 | +def set_custom_user_claims(uid): |
| 229 | + # [START set_custom_user_claims] |
| 230 | + # Set admin privilege on the user corresponding to uid. |
| 231 | + auth.set_custom_user_claims(uid, {'admin': True}) |
| 232 | + # The new custom claims will propagate to the user's ID token the |
| 233 | + # next time a new one is issued. |
| 234 | + # [END set_custom_user_claims] |
| 235 | + |
| 236 | + id_token = 'id_token' |
| 237 | + # [START verify_custom_claims] |
| 238 | + # Verify the ID token first. |
| 239 | + claims = auth.verify_id_token(id_token) |
| 240 | + if claims['admin'] is True: |
| 241 | + # Allow access to requested admin resource. |
| 242 | + pass |
| 243 | + # [END verify_custom_claims] |
| 244 | + |
| 245 | + # [START read_custom_user_claims] |
| 246 | + # Lookup the user associated with the specified uid. |
| 247 | + user = auth.get_user(uid) |
| 248 | + # The claims can be accessed on the user record. |
| 249 | + print user.custom_claims.get('admin') |
| 250 | + # [END read_custom_user_claims] |
| 251 | + |
| 252 | +def set_custom_user_claims_script(): |
| 253 | + # [START set_custom_user_claims_script] |
| 254 | + user = auth.get_user_by_email('user@admin.example.com') |
| 255 | + # Confirm user is verified |
| 256 | + if user.email_verified: |
| 257 | + # Add custom claims for additional privileges. |
| 258 | + # This will be picked up by the user on token refresh or next sign in on new device. |
| 259 | + auth.set_custom_user_claims(user.uid, { |
| 260 | + 'admin': True |
| 261 | + }) |
| 262 | + # [END set_custom_user_claims_script] |
| 263 | + |
| 264 | +def set_custom_user_claims_incremental(): |
| 265 | + # [START set_custom_user_claims_incremental] |
| 266 | + user = auth.get_user_by_email('user@admin.example.com') |
| 267 | + # Add incremental custom claim without overwriting existing claims. |
| 268 | + current_custom_claims = user.custom_claims |
| 269 | + if current_custom_claims.get('admin'): |
| 270 | + # Add level. |
| 271 | + current_custom_claims['accessLevel'] = 10 |
| 272 | + # Add custom claims for additional privileges. |
| 273 | + auth.set_custom_user_claims(user.uid, current_custom_claims) |
| 274 | + # [END set_custom_user_claims_incremental] |
| 275 | + |
| 276 | +def list_all_users(): |
| 277 | + # [START list_all_users] |
| 278 | + # Start listing users from the beginning, 1000 at a time. |
| 279 | + page = auth.list_users() |
| 280 | + while page: |
| 281 | + for user in page.users: |
| 282 | + print 'User: ' + user.uid |
| 283 | + # Get next batch of users. |
| 284 | + page = page.get_next_page() |
| 285 | + |
| 286 | + # Iterate through all users. This will still retrieve users in batches, |
| 287 | + # buffering no more than 1000 users in memory at a time. |
| 288 | + for user in auth.list_users().iterate_all(): |
| 289 | + print 'User: ' + user.uid |
| 290 | + # [END list_all_users] |
| 291 | + |
| 292 | +initialize_sdk_with_service_account() |
| 293 | +initialize_sdk_with_application_default() |
| 294 | +#initialize_sdk_with_refresh_token() |
| 295 | +access_services_default() |
| 296 | +access_services_nondefault() |
| 297 | +create_token_uid() |
| 298 | +token_with_claims = create_token_with_claims() |
| 299 | +#verify_token_uid() |
| 300 | + |
| 301 | +uid = create_user() |
| 302 | +create_user_with_id() |
| 303 | +get_user(uid) |
| 304 | +get_user_by_email() |
| 305 | +get_user_by_phone_number() |
| 306 | +update_user(uid) |
| 307 | +set_custom_user_claims(uid) |
| 308 | +list_all_users() |
| 309 | +delete_user(uid) |
0 commit comments