social.py
62 lines
| 2.4 KiB
| text/x-python
|
PythonLexer
r2 | # -*- coding: utf-8 -*- | |||
r112 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors | |||
r2 | # | |||
r112 | # Licensed under the Apache License, Version 2.0 (the "License"); | |||
# you may not use this file except in compliance with the License. | ||||
# You may obtain a copy of the License at | ||||
r2 | # | |||
r112 | # http://www.apache.org/licenses/LICENSE-2.0 | |||
r2 | # | |||
r112 | # Unless required by applicable law or agreed to in writing, software | |||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
# See the License for the specific language governing permissions and | ||||
# limitations under the License. | ||||
r2 | ||||
r153 | from ziggurat_foundations.models.services.external_identity import ( | |||
ExternalIdentityService, | ||||
) | ||||
r2 | from appenlight.models.external_identity import ExternalIdentity | |||
def handle_social_data(request, user, social_data): | ||||
social_data = social_data | ||||
update_identity = False | ||||
extng_id = ExternalIdentityService.by_external_id_and_provider( | ||||
r153 | social_data["user"]["id"], social_data["credentials"].provider_name | |||
r2 | ) | |||
# fix legacy accounts with wrong google ID | ||||
r153 | if not extng_id and social_data["credentials"].provider_name == "google": | |||
r2 | extng_id = ExternalIdentityService.by_external_id_and_provider( | |||
r153 | social_data["user"]["email"], social_data["credentials"].provider_name | |||
r2 | ) | |||
if extng_id: | ||||
extng_id.delete() | ||||
update_identity = True | ||||
r153 | if not social_data["user"]["id"]: | |||
r2 | request.session.flash( | |||
r153 | "No external user id found? Perhaps permissions for " | |||
"authentication are set incorrectly", | ||||
"error", | ||||
) | ||||
r2 | return False | |||
if not extng_id or update_identity: | ||||
if not update_identity: | ||||
r153 | request.session.flash( | |||
"Your external identity is now " "connected with your account" | ||||
) | ||||
r2 | ex_identity = ExternalIdentity() | |||
r153 | ex_identity.external_id = social_data["user"]["id"] | |||
ex_identity.external_user_name = social_data["user"]["user_name"] | ||||
ex_identity.provider_name = social_data["credentials"].provider_name | ||||
ex_identity.access_token = social_data["credentials"].token | ||||
ex_identity.token_secret = social_data["credentials"].token_secret | ||||
ex_identity.alt_token = social_data["credentials"].refresh_token | ||||
r2 | user.external_identities.append(ex_identity) | |||
r153 | request.session.pop("zigg.social_auth", None) | |||