Skip to content

Commit

Permalink
example improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
Janusz Skonieczny committed May 29, 2014
1 parent 5b1096f commit c5d5053
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 13 deletions.
10 changes: 6 additions & 4 deletions example/gae/auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ def from_profile(cls, user, profile):
if not user or user.is_anonymous():
email = profile.data.get("email")
if not email:
logging.warning("Social connection could not provide email")
raise Exception(_("Social connection could not provide email"))
msg = "Cannot create new user, authentication provider did not not provide email"
logging.warning(msg)
raise Exception(_(msg))
conflict = User.query(User.email == email).get()
if conflict:
msg = _("Cannot create new user, email {} is already used. Login and then connect external profile.")
raise Exception(msg.format(email))
msg = _("Cannot create new user, email {} is already used. Login and then connect external profile.").format(email)
logging.warning(msg)
raise Exception(msg)

now = datetime.now()
user = User(
Expand Down
1 change: 1 addition & 0 deletions example/gae/website/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,6 @@
},
}

# Settings with secrets
if PRODUCTION:
from website.settings_prd import *
13 changes: 9 additions & 4 deletions example/sqla/auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def gravatar(self):
encoded = hashlib.md5(email).hexdigest()
return "https://secure.gravatar.com/avatar/%s.png" % encoded

def social_connections(self):
return SocialConnection.query.filter(SocialConnection.user_id == self.id).all()

# @login_manager.user_loader
# def load_user(user_id):
# return None #User.get(user_id)
Expand Down Expand Up @@ -105,12 +108,14 @@ def from_profile(cls, user, profile):
if not user or user.is_anonymous():
email = profile.data.get("email")
if not email:
logging.warning("Social connection could not provide email")
raise Exception(_("Social connection could not provide email"))
msg = "Cannot create new user, authentication provider did not not provide email"
logging.warning(msg)
raise Exception(_(msg))
conflict = User.query.filter(User.email == email).first()
if conflict:
msg = _("Cannot create new user, email {} is already used. Login and then connect external profile.")
raise Exception(msg.format(email))
msg = _("Cannot create new user, email {} is already used. Login and then connect external profile.").format(email)
logging.warning(msg)
raise Exception(msg)

now = datetime.now()
user = User(
Expand Down
29 changes: 29 additions & 0 deletions example/sqla/auth/templates/User/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,34 @@ <h1 class="text-center">Hi {{ current_user.cn }}</h1>
<a class="btn btn-primary btn-lg" href="{{ url_for_security('logout') }}">Logout</a>
</div>
</div>
{% with connections=current_user.social_connections() %}
{% if connections %}
<div class="row">
<div class="text-center mg-btm">
<h2 class="text-center">Connections</h2>
{% for connection in connections %}
<a href="{{ connection.profile_url }}">
<img src="{{ connection.image_url }}" alt="" width="50" height="50" class="img-circle">
</a>
{% endfor %}

</div>
</div>

{% endif %}
{% endwith %}
<div class="row">
<div class="text-center">
<h2 class="text-center">Connect another account</h2>

<div class="">
<a href="{{ url_for('social.login', provider='Facebook') }}" class="social-facebook btn btn-primary btn-lg"><i class="fa fa-2x fa-facebook-square"></i></a>
<a href="{{ url_for('social.login', provider='Google') }}" class="social-google btn btn-primary btn-lg"><i class="fa fa-2x fa-google-plus"></i></a>
<a href="{{ url_for('social.login', provider='Github') }}" class="social-github btn btn-primary btn-lg"><i class="fa fa-2x fa-github"></i></a>
<a href="{{ url_for('social.login', provider='Twitter') }}" class="social-twitter btn btn-primary btn-lg"><i class="fa fa-2x fa-twitter"></i></a>
</div>

</div>
</div>
</div>
{% endblock %}
Binary file modified example/sqla/db.sqlite
Binary file not shown.
4 changes: 2 additions & 2 deletions example/sqla/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@
app.wsgi_app = DebuggedApplication(app.wsgi_app, True)

if __name__ == "__main__":
# for coonvienience in setting up OAuth ids and secretes we use the example.com domain.
# for convenience in setting up OAuth ids and secretes we use the example.com domain.
# This should allow you to circumvent limits put on localhost/127.0.0.1 usage
# Just map dev.example.com on 127.0.0.1 ip adress.
# Just map dev.example.com on 127.0.0.1 ip address.
logging.debug("PRODUCTION: %s" % PRODUCTION)
logging.debug("app.debug: %s" % app.debug)
logging.debug("app.testing: %s" % app.testing)
Expand Down
15 changes: 14 additions & 1 deletion example/sqla/website/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,35 @@
# Flask-SocialBlueprint
# https://github.com/wooyek/flask-social-blueprint
SOCIAL_BLUEPRINT = {
# https://developers.facebook.com/apps/
"flask_social_blueprint.providers.Facebook": {
# App ID
'consumer_key': '197…',
# App Secret
'consumer_secret': 'c956c1…'
},
# https://apps.twitter.com/app/new
"flask_social_blueprint.providers.Twitter": {
# Your access token from API Keys tab
'consumer_key': 'bkp…',
# access token secret
'consumer_secret': 'pHUx…'
},
# https://console.developers.google.com/project
"flask_social_blueprint.providers.Google": {
# Client ID
'consumer_key': '797….apps.googleusercontent.com',
# Client secret
'consumer_secret': 'bDG…'
},
# https://github.com/settings/applications/new
"flask_social_blueprint.providers.Github": {
# Client ID
'consumer_key': '6f6…',
# Client Secret
'consumer_secret': '1a9…'
},
}

from website.settings_prd import *
# Settings with secrets
from website.settings_local import *
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
# '': ['requirements.txt']
# },

# metadata for upload to PyPI
'author': "Janusz Skonieczny",
'author_email': "[email protected]",
'description': "An OAuth based authentication blueprint for flask. Easy to extend and override",
Expand Down
1 change: 0 additions & 1 deletion src/flask_social_blueprint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def no_connection(self, profile, provider):
except Exception as ex:
logging.warn(ex, exc_info=True)
do_flash(_("Could not register: {}").format(ex.message), "warning")
abort(500)
return self.login_failed_redirect(profile, provider)

return self.login_connection(connection, profile, provider)
Expand Down

0 comments on commit c5d5053

Please sign in to comment.