github oauth should now work will need a few more tweaks
This commit is contained in:
parent
c1b347fc16
commit
2fb9a65117
|
@ -27,6 +27,8 @@ authorize_pages = Blueprint('authorize_pages', __name__, template_folder='templa
|
|||
login_manager = LoginManager()
|
||||
login_manager.login_view = '/login'
|
||||
|
||||
oauth_lookup = {'google':1, 'github':2, 'facebook':3}
|
||||
|
||||
|
||||
def is_weak_password(password1, password2):
|
||||
if password1 != password2:
|
||||
|
@ -48,13 +50,16 @@ def todict(data):
|
|||
|
||||
class User(UserMixin):
|
||||
def __init__(self, user_id, active=True):
|
||||
print user_id
|
||||
user_details = site_user.get_user_details({'id': user_id}).get()
|
||||
self.active = False
|
||||
print 'user'
|
||||
print user_details
|
||||
if user_details:
|
||||
#~ self.check_password(user_details.get('password'))
|
||||
self.id = user_id
|
||||
self.name = user_details.get('username')
|
||||
#~ self.team_id = user_details.get('team_id', 1)
|
||||
print self.name
|
||||
self.active = active
|
||||
|
||||
def get_id(self):
|
||||
|
@ -153,7 +158,7 @@ def oauth(provider, state=None):
|
|||
oauth_access_type = 'offline'
|
||||
oauth_approval_prompt = "force"
|
||||
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
|
||||
print '#####'
|
||||
|
||||
if state:
|
||||
oauth_session = OAuth2Session(
|
||||
oauth_provider.get('client_id'),
|
||||
|
@ -170,33 +175,22 @@ def oauth(provider, state=None):
|
|||
oauth_provider.get('auth_uri'),
|
||||
access_type=oauth_access_type,
|
||||
approval_prompt=oauth_approval_prompt)
|
||||
print state
|
||||
|
||||
# State is used to prevent CSRF, keep this for later, make sure oauth returns to the same url.
|
||||
# if testing and oauth_state errors make sure you logged in with localhost and not 127.0.0.1
|
||||
session['oauth_state'] = state
|
||||
session.modified = True
|
||||
print session
|
||||
print authorization_url
|
||||
return redirect(authorization_url)
|
||||
|
||||
print '-----'
|
||||
print provider
|
||||
print session
|
||||
print session['oauth_state']
|
||||
# allready authorised so lets handle the callback
|
||||
oauth_session = OAuth2Session(
|
||||
oauth_provider.get('client_id'),
|
||||
state=session['oauth_state'],
|
||||
redirect_uri=oauth_provider.get('redirect_uri'))
|
||||
|
||||
#~ if provider == 'facebook':
|
||||
#~ oauth_session = facebook_compliance_fix(oauth_session)
|
||||
if provider == 'facebook':
|
||||
oauth_session = facebook_compliance_fix(oauth_session)
|
||||
|
||||
print '@@@@@@@'
|
||||
print request.url
|
||||
print oauth_provider.get('redirect_uri')
|
||||
print oauth_provider.get('token_uri')
|
||||
print oauth_provider.get('client_secret')
|
||||
# code error is todo with authorisation response
|
||||
oauth_session.fetch_token(
|
||||
oauth_provider.get('token_uri'),
|
||||
|
@ -204,62 +198,48 @@ def oauth(provider, state=None):
|
|||
authorization_response=request.url,
|
||||
verify=oauth_verify)
|
||||
|
||||
#~ r = oauth_session.get('https://api.github.com/user')
|
||||
#~ print r.content
|
||||
|
||||
# Fetch a protected resource, i.e. user profile
|
||||
print oauth_provider.get('user_uri')
|
||||
response = oauth_session.get(oauth_provider.get('user_uri'))
|
||||
oauth_user = response.json()
|
||||
|
||||
if provider is 'github':
|
||||
oauth2_github_handle_user(oauth_user)
|
||||
|
||||
if provider is 'facebook':
|
||||
oauth2_github_handle_user(oauth_user)
|
||||
|
||||
if provider is 'google':
|
||||
oauth2_github_handle_user(oauth_user)
|
||||
|
||||
|
||||
|
||||
print oauth_user
|
||||
email = oauth_user.get('email') or ''
|
||||
#~ email = oauth_user.get('login') or ''
|
||||
provider_id = oauth_lookup.get(provider)
|
||||
user_details = site_user.fetch_oauth_login({
|
||||
'username': oauth_user.get('login') or ''
|
||||
'username': oauth_user.get('login') or '',
|
||||
'provider': provider_id
|
||||
}).get()
|
||||
|
||||
if oauth_user.get('login'):
|
||||
#err what now we should probably error
|
||||
pass
|
||||
|
||||
if not user_details:
|
||||
flash('Your new profile has been created, and your now logged in')
|
||||
site_user.create_oauth_login().execute({
|
||||
'username': oauth_user.get('login') or '',
|
||||
'provider': 'oauth'})
|
||||
|
||||
site_user.create().execute({
|
||||
'email': oauth_user.get('email') or '',
|
||||
'password': 'oauth',
|
||||
'profile_image': oauth_user.get('picture'),
|
||||
'username': oauth_user.get('login'),
|
||||
'first_name': oauth_user.get('given_name') or '',
|
||||
'last_name': oauth_user.get('family_name') or ''})
|
||||
|
||||
user_details = site_user.get_by_ouath_login({
|
||||
'email': oauth_user.get('email')
|
||||
}).get()
|
||||
|
||||
user = User(user_details.get('user_id'))
|
||||
login_user(user)
|
||||
site_user.update_last_login().execute(user_details)
|
||||
# we have matched a user so login and redirect
|
||||
if user_details:
|
||||
print 'oauth login 1'
|
||||
login_user(User(user_details.get('user_id')))
|
||||
return redirect('/profile')
|
||||
|
||||
flash('Your new profile has been created, and your now logged in')
|
||||
|
||||
print oauth_user
|
||||
# create new user from oauth information
|
||||
user_id = site_user.create().execute({
|
||||
'email': oauth_user.get('email') or '',
|
||||
'password': 'oauth',
|
||||
'profile_image': oauth_user.get('picture'),
|
||||
'username': oauth_user.get('login'),
|
||||
'first_name': oauth_user.get('given_name') or '',
|
||||
'last_name': oauth_user.get('family_name') or ''})
|
||||
|
||||
# register oauth login creation
|
||||
site_user.create_oauth_login().execute({
|
||||
'user_id': user_id,
|
||||
'username': oauth_user.get('login') or '',
|
||||
'provider': provider_id})
|
||||
|
||||
login_user(User(user_id))
|
||||
site_user.update_last_login().execute({'id': user_id})
|
||||
return redirect('/profile')
|
||||
|
||||
def oauth2_github_handle_user(user):
|
||||
print user
|
||||
|
||||
|
||||
|
||||
@authorize_pages.route("/change-password/<code>", methods=['GET'])
|
||||
@authorize_pages.route("/change-password", methods=['GET'])
|
||||
|
@ -323,7 +303,6 @@ def change_password_submit(code=None):
|
|||
return make_response(footer())
|
||||
|
||||
|
||||
|
||||
@authorize_pages.route("/reset-password", methods=['GET'])
|
||||
def reset_password():
|
||||
web.template.create('Maidstone Hackspace - Login')
|
||||
|
@ -404,7 +383,7 @@ def login_screen_submit():
|
|||
flash('You have successfully logged in !')
|
||||
#~ session['username'] = user_details.get('username', 'anonymous')
|
||||
#~ session['user_id'] = str(user_details.get('user_id'))
|
||||
site_user.update_last_login(user_details)
|
||||
site_user.update_last_login().execute(user_details)
|
||||
return redirect('/profile')
|
||||
|
||||
|
||||
|
|
|
@ -36,12 +36,10 @@ class create(insert_data):
|
|||
super(create, self).set(data)
|
||||
|
||||
class update_last_login(update_data):
|
||||
#~ table = 'users'
|
||||
debug = True
|
||||
query_str = "update `users` set `last_login`=now() where id=%(user_id)s"
|
||||
required = {'user_id'}
|
||||
#~ columns = {'id'}
|
||||
columns_where = {}
|
||||
query_str = "update `users` set `last_login`=now()"
|
||||
required = {'id'}
|
||||
columns_where = {'id'}
|
||||
|
||||
class update_membership_status(update_data):
|
||||
debug = True
|
||||
|
@ -126,18 +124,20 @@ class authorize(select_data):
|
|||
|
||||
|
||||
class create_oauth_login(insert_data):
|
||||
required = {'username', 'provider'}
|
||||
query_file = 'get_user_by_oauth_username.sql'
|
||||
columns_where = {'username', 'provider'}
|
||||
debug = True
|
||||
table = 'user_oauth'
|
||||
required = {'username', 'provider', 'user_id'}
|
||||
columns = {'username', 'provider', 'user_id'}
|
||||
|
||||
def calculated_data(self):
|
||||
return {'registered': time.strftime('%Y-%m-%d %H:%M:%S')}
|
||||
#~ def calculated_data(self):
|
||||
#~ return {'registered': time.strftime('%Y-%m-%d %H:%M:%S')}
|
||||
|
||||
def set(self, data):
|
||||
data['registered'] = time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
super(create, self).set(data)
|
||||
super(create_oauth_login, self).set(data)
|
||||
|
||||
class update_oauth_login(update_data):
|
||||
table = 'user_oauth'
|
||||
required = {'username', 'provider'}
|
||||
query_file = 'get_user_by_oauth_username.sql'
|
||||
columns_where = {'username', 'provider'}
|
||||
|
@ -147,7 +147,7 @@ class update_oauth_login(update_data):
|
|||
|
||||
def set(self, data):
|
||||
data['registered'] = time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
super(create, self).set(data)
|
||||
super(update_oauth_login, self).set(data)
|
||||
|
||||
class fetch_oauth_login(select_data):
|
||||
required = {'username', 'provider'}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import os
|
||||
import sys
|
||||
from flask import Flask
|
||||
from flask import Flask, send_from_directory
|
||||
from flask import make_response
|
||||
|
||||
sys.path.append(os.path.abspath('../../../scaffold/'))
|
||||
|
@ -19,7 +19,7 @@ from pages.equipment import equipment_pages
|
|||
from pages.profile import profile_pages
|
||||
from authorize import authorize_pages, login_manager
|
||||
|
||||
web_app = Flask(__name__)
|
||||
web_app = Flask(__name__, static_folder='static')
|
||||
web_app.config['PROPAGATE_EXCEPTIONS'] = True
|
||||
web_app.secret_key = settings.flask_secret_key
|
||||
login_manager.init_app(web_app)
|
||||
|
@ -30,10 +30,13 @@ web_app.register_blueprint(profile_pages)
|
|||
web_app.register_blueprint(google_groups_pages)
|
||||
web_app.register_blueprint(donate_pages)
|
||||
|
||||
@web_app.route('/static/<path:path>')
|
||||
def send_js(path):
|
||||
print path
|
||||
return send_from_directory('/static_resources', path)
|
||||
#~ @web_app.route('/static/<path:filename>')
|
||||
#~ def send_js(filename):
|
||||
#~ print filename
|
||||
#~ print send_from_directory('/static_resources/', filename)
|
||||
#~ path = os.path.abspath('./static_resources/')
|
||||
#~ print path + 'css/'
|
||||
#~ return send_from_directory(path + 'css/', 'default.css')
|
||||
|
||||
# local testing server, add your pages here
|
||||
@web_app.route("/examples/", methods=['GET'])
|
||||
|
|
|
@ -23,7 +23,7 @@ image_path = domain + os.sep + 'images' + os.sep
|
|||
|
||||
with web.template as setup:
|
||||
setup.persistent_header('<link rel="stylesheet" id="navigationCss" href="/static/css/default.css" media="" type="text/css" />')
|
||||
setup.persistent_header('<link rel="stylesheet" id="navigationCss" href="/static/js/jquery-ui/themes/base/jquery-ui.css" media="" type="text/css" />')
|
||||
#~ setup.persistent_header('<link rel="stylesheet" id="navigationCss" href="/static/js/jquery-ui/themes/base/jquery-ui.css" media="" type="text/css" />')
|
||||
#setup.persistent_header('<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/0.9.4/angular-material.min.css">')
|
||||
#setup.persistent_header('<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=RobotoDraft:300,400,500,700,400italic">')
|
||||
setup.persistent_header('<link rel="stylesheet" id="navigationCss" href="/static/css/sprite-navigation-white.css" media="" type="text/css" />')
|
||||
|
@ -48,20 +48,18 @@ def header(title, description='Maidstone Hackspace is a shared space where artis
|
|||
web.header_strip.social(web.like.create(url=web.template.domain + url, plus=True, linkedin=True, facebook=True, twitter='MHackspace').render())
|
||||
web.template.body.append(web.header_strip.render())
|
||||
|
||||
# navigation
|
||||
# top menu bar navigation
|
||||
web.menu.create('/' + url).set_id('leftNav')
|
||||
web.menu * site.page_menu
|
||||
if current_user and current_user.is_authenticated:
|
||||
web.menu.append('logout', '/logout')
|
||||
web.menu.append('Group', '/mailing-list')
|
||||
|
||||
if current_user and current_user.is_authenticated():
|
||||
web.menu.append('Logout', '/logout')
|
||||
web.navigation_bar.create(hide=(False if url=='/profile' else True))
|
||||
web.navigation_bar * site.nav_for_authenticated_user
|
||||
#~ web.navigation_bar.append('Profile', '/profile')
|
||||
#~ web.navigation_bar.append('Equipment', '/equipment')
|
||||
#~ web.navigation_bar.append('Members', '/members')
|
||||
#~ web.navigation_bar.append('Mailing List', '/mailing-list')
|
||||
web.navigation_bar * site.nav_for_authenticated_user
|
||||
web.template.body.append(web.navigation_bar.render())
|
||||
else:
|
||||
web.menu.append('login', '/login')
|
||||
web.menu.append('Login', '/login')
|
||||
web.template.body.append(web.menu.render())
|
||||
|
||||
def footer():
|
||||
|
@ -74,7 +72,6 @@ def footer():
|
|||
|
||||
|
||||
class default_page:
|
||||
|
||||
def __enter__(self):
|
||||
header()
|
||||
return self
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -209,7 +209,8 @@ height:48px;width:48px; -webkit-animation:spin 2s linear infinite ;-moz-animatio
|
|||
#login_box label {display:block;margin: 20px 0px 20px;}
|
||||
#login_box input {margin: 20px 0px 0px;float:none;width:100%;}
|
||||
#login_box button {margin: 20px 0px 0px;float:left;width:100%;}
|
||||
#login_box .providers {margin: 20px 0px 20px;}
|
||||
#login_box .providers {margin: 20px 0px 20px;height:48px;}
|
||||
#login_box .providers a {float:left;}
|
||||
|
||||
|
||||
.members .tile{ width:220px;float:left;}
|
||||
|
|
|
@ -17,12 +17,12 @@ class control(base_widget):
|
|||
if self.oauth_enabled:
|
||||
htm += '<div class="providers">'
|
||||
if 'google' in self.oauth_enabled:
|
||||
htm += '<a title="Login with Google" href="/oauth/google/login"><img src="/static/images/oauth/google.png" /></a><br />'
|
||||
if 'facebook' in self.oauth_enabled:
|
||||
htm += '<a title="Login with facebook" href="/oauth/facebook/login">Facebook</a>.<br />'
|
||||
htm += '<a title="Login with Google" href="/oauth/google/login"><img src="/static/images/oauth/google.png" /></a> '
|
||||
#~ if 'facebook' in self.oauth_enabled:
|
||||
#~ htm += '<a title="Login with facebook" class="but row" href="/oauth/facebook/login">Login with Facebook</a> '
|
||||
if 'github' in self.oauth_enabled:
|
||||
htm += '<a title="Login with twitter" href="/oauth/github/login">GitHub</a><br />'
|
||||
htm += '</div>'
|
||||
htm += '<a title="Login with twitter" class="but row" href="/oauth/github/login">Login with GitHub</a>'
|
||||
htm += '<br /></div>'
|
||||
|
||||
htm += '<p>Or alternatively login with your previously created account.</p>'
|
||||
|
||||
|
@ -34,12 +34,6 @@ class control(base_widget):
|
|||
<button class="btn" form="user_login" type="submit" value="Login"/>Login</button>
|
||||
</frameset></form>
|
||||
<a href="/register">Register for an account</a> | <a href="/reset-password">Reset password</a>'''
|
||||
|
||||
|
||||
|
||||
htm += '</div>'
|
||||
|
||||
|
||||
|
||||
return htm
|
||||
|
||||
|
|
Loading…
Reference in New Issue