Ejemplo n.º 1
0
    def get(self):
        from flask import request
        provider = request.args.get('provider')

        from app.oauth import OAuthSignIn
        oauth = OAuthSignIn.get_provider(provider)
        social_id, email, fullname, username = oauth.callback()

        if social_id is None:
            return {
                'response': False,
                'message': 'Something is wrong with Facebook API.'
            }, 500

        user = User.query.filter_by(socialId=social_id).first()

        if not user:
            user = User(socialId=social_id,
                        username=username,
                        email=email,
                        fullname=fullname)
            user.save_to_db()

        access_token = create_access_token(identity=user.username)
        refresh_token = create_refresh_token(identity=user.username)

        return {
            'response': True,
            'message': 'Logged in as {}.'.format(user.username),
            'access_token': access_token,
            'refresh_token': refresh_token
        }
Ejemplo n.º 2
0
def oauth_callback(provider):
    next_url = request.args.get('next') or url_for('pages.index')
    if not g.user.is_anonymous:
        return redirect(url_for('pages.index'))
    oauth = OAuthSignIn.get_provider(provider)

    social_id, fname, lname, email = oauth.callback(next_url)

    if social_id is None:
        flash('Authentication failed.')
        return redirect(url_for('pages.index'))

    try:
        user = Profile.objects.get(email=email)
    except Profile.DoesNotExist:
        user = None

    if not user:
        name = "%s %s" % (fname, lname)
        user = Profile(social_id=social_id, name=name, email=email,\
            created_at=datetime.datetime.now())
        user.save()

        emails.welcome_email(user.name, user.email)
    else:
        user.last_login = datetime.datetime.now()
        user.save()

    login_user(user, True)
    return redirect(next_url)
Ejemplo n.º 3
0
def oauth_callback(provider):
    if not current_user.is_anonymous:
        return redirect(url_for('index'))
    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()
    if social_id is None:
        flash('Authentication failed.')
        return redirect(url_for('index'))
    user = User.query.filter_by(social_id=social_id).first()
    if not user:
        print(username)
        user = User(social_id=social_id, nickname=username, email=email)
        db.session.add(user)
        db.session.commit()
        login_user(user, True)
        id = User.query.filter_by(social_id=social_id).first().id
        response = redirect(url_for('index'))
        response.set_cookie('user_id', value=bytes(str(id), 'utf-8'))
        response.set_cookie('new_user', value='')
        return response
    else:
        login_user(user, True)
        id = User.query.filter_by(social_id=social_id).first().id
        response = redirect(url_for('index'))
        response.set_cookie('user_id', value=bytes(str(id), 'utf-8'))
        return response
Ejemplo n.º 4
0
def oauth_callback(provider):
    if not current_user.is_anonymous:
        return redirect(url_for('index'))
    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()
    if social_id is None:
        flash('Authentication failed.')
        return redirect(url_for('index'))
    user = User.query.filter_by(social_id=social_id).first()
    if not user:
        print(username)
        user = User(social_id=social_id, nickname=username, email=email)
        db.session.add(user)
        db.session.commit()
        login_user(user, True)
        id = User.query.filter_by(social_id=social_id).first().id
        response = redirect(url_for('index'))
        response.set_cookie('user_id', value=bytes(str(id), 'utf-8'))
        response.set_cookie('new_user', value='')
        return response
    else:
        login_user(user, True)
        id = User.query.filter_by(social_id=social_id).first().id
        response = redirect(url_for('index'))
        response.set_cookie('user_id', value=bytes(str(id), 'utf-8'))
        return response
Ejemplo n.º 5
0
def oauth_callback(provider):
    if not current_user.is_anonymous:
        return redirect(url_for('index'))

    oauth = OAuthSignIn.get_provider(provider)
    # This is step three. The code from the provider's reply is sent back to
    # the provider and the provider returns an authentication token
    access_token, oauth_id = oauth.callback()

    if access_token is None or oauth_id is None:
        flash('Authentication failed. Please contact an admin if '
                'this problem is persistent')
        return redirect(url_for('login'))

    user = User.query.filter_by(oauth_id=oauth_id).first()

    if not user:
        # Adds any new users directly to the database. And currently only stores
        # their ORCID ID. Probably want to change this...
        user = User(oauth_id=oauth_id)
        db.session.add(user)
        try:
            db.session.commit()
        except:
            flash("Creating new user account failed")
            redirect(url_for("index"))

    login_user(user, remember=True)
    session['active_token'] = access_token

    return redirect(url_for('logged_in'))
Ejemplo n.º 6
0
def oauth_authorize(provider):
    if not g.user.is_anonymous:
        return redirect(url_for('pages.index'))

    next_url = request.args.get('next') or url_for('pages.index')
    oauth = OAuthSignIn.get_provider(provider)
    return oauth.authorize(next_url)
Ejemplo n.º 7
0
def oauth_callback(provider):
    next_url = request.args.get('next') or url_for('pages.index')
    if not g.user.is_anonymous:
        return redirect(url_for('pages.index'))
    oauth = OAuthSignIn.get_provider(provider)

    social_id, fname, lname, email = oauth.callback(next_url)

    if social_id is None:
        flash('Authentication failed.')
        return redirect(url_for('pages.index'))

    try:
        user = Profile.objects.get(email=email)
    except Profile.DoesNotExist:
        user = None

    if not user:
        name = "%s %s" %(fname, lname)
        user = Profile(social_id=social_id, name=name, email=email,\
            created_at=datetime.datetime.now())
        user.save()

        emails.welcome_email(user.name, user.email)
    else:
        user.last_login = datetime.datetime.now()
        user.save()

    login_user(user, True)
    return redirect(next_url)
Ejemplo n.º 8
0
def oauth_authorize(provider):
    if not g.user.is_anonymous:
        return redirect(url_for('pages.index'))

    next_url = request.args.get('next') or url_for('pages.index')
    oauth = OAuthSignIn.get_provider(provider)
    return oauth.authorize(next_url)
Ejemplo n.º 9
0
def oauth_callback(provider):
    if not current_user.is_anonymous():
        return redirect(url_for('simple_page.index'))

    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()
    if social_id is None:
        flash(getttext(u'Authentication failed'), 'danger')
        return redirect(url_for('simple_page.index'))

    # check if user exists and if no creates new
    user = User.query.filter_by(social_id=social_id).first()
    if user is None:
        user = User(
            username=username,
            password='',
            email=email,
            social_id=social_id
        )
        db.session.add(user)
        db.session.commit()

    login_user(user, remember=True)
    user.update_login_info()
    return redirect(url_for('simple_page.index'))
Ejemplo n.º 10
0
def oauth_callback(provider, user_type=1):
    if not current_user.is_anonymous():
        return redirect(url_for('index'))
    oauth = OAuthSignIn.get_provider(provider)
    email, first_name, last_name = oauth.callback()
    if email is None:
        flash(
            'Facebook authentication failed. Try again or register via email.')
        return redirect(url_for('home.register'))
    user = User.query.filter_by(email=email).first()
    if not user:
        user = User(first_name=first_name,
                    last_name=last_name,
                    email=email,
                    password='',
                    user_type=1)
        db.session.add(user)
        if user_type == 1:
            student = Student(user_id=User.query.filter_by(
                email=email).first().id)
            db.session.add(student)
        if user_type == 2:
            donor = Donor(user_id=User.query.filter_by(email=email).first().id)
            db.session.add(donor)
        db.session.commit()
    login_user(user, remember=False)
    return redirect(url_for('home.index'))
Ejemplo n.º 11
0
    def get(self):
        from flask import request
        provider = request.args.get('provider')

        from app.oauth import OAuthSignIn
        oauth = OAuthSignIn.get_provider(provider)
        return oauth.authorize()
Ejemplo n.º 12
0
def oauth_callback(provider):
    if not current_user.is_anonymous:
        return redirect(url_for('home'))
    oauth = OAuthSignIn.get_provider(provider)
    email = oauth.callback()
    if email is None:
        flash('Authentication failed.')
        return redirect(url_for('home'))
    _user = models.User.query.filter_by(email=email).first()
    if not _user:
        return redirect(url_for('signup', email=email))
    _login_user_and_record_ip(_user, True)
    return redirect(url_for('home'))
Ejemplo n.º 13
0
def oauth_callback(provider):
    if not current_user.is_anonymous:
        return redirect(url_for('blogs'))
    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()
    if social_id is None:
        flash('Authentication failed.')
        return redirect(url_for('home'))
    user = User.query.filter_by(social_id=social_id).first()
    if not user:
        user = User(social_id=social_id, username=username, email=email)
        db.session.add(user)
        db.session.commit()
    login_user(user, True)
    return redirect(url_for('blogs'))
Ejemplo n.º 14
0
def oauth_callback(provider):
    if not current_user.is_anonymous:
        return redirect(url_for('index'))
    oauth = OAuthSignIn.get_provider(provider)
    username, email, display_name = oauth.callback()
    if email is None:
        flash('Authentication failed')
        return redirect(url_for('index'))
    user = User.query.filter_by(email=email).first()
    if not user:
        user = User(username=username, email=email, display_name=display_name)
        db.session.add(user)
        db.session.commit()
    login_user(user, True)
    return redirect(url_for('index'))
Ejemplo n.º 15
0
def oauth_callback(provider):
    if not current_user.is_anonymous:
        return redirect(url_for('index'))
    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()
    if social_id is None:
        flash('Authentication failed.')
        return redirect('/')
    try:
        user = User.objects().get(social_id=social_id)
    except User.DoesNotExist:
        user = User(social_id=social_id, username=username, email=email)
        user.save()
    login_user(user, True)
    return redirect(url_for('index'))
Ejemplo n.º 16
0
def oauth_authorize(provider):
    """ Authorize Provider Route

        First step in OATH dance to autorize the use to a provider

        Args:
            provider for oauth

        Returns:
            oauth.authorize function if successful
            redirect to index if failed
    """
    if not current_user.is_anonymous:
        return redirect(url_for('main.index'))
    oauth = OAuthSignIn.get_provider(provider)
    return oauth.authorize()
Ejemplo n.º 17
0
def oauth_callback(provider):
	if not current_user.is_anonymous:
		return redirect(url_for('home'))
	oauth = OAuthSignIn.get_provider(provider)
	social_id, username, email, auth_provider, profile_picture_url = oauth.callback()
	user = User.query.filter_by(social_id=social_id).first()
	
	if social_id is None:
		flash('Authentication failed.', 'danger')
		return redirect(url_for('home'))
	if not user:
		nickname = User.make_unique_nickname(username)
		user = User(social_id=social_id, nickname=nickname, email=email, auth_provider=auth_provider, profile_picture_url=profile_picture_url)
		db.session.add(user)
		db.session.commit()
	login_user(user, True)
	return redirect(url_for('home'))
Ejemplo n.º 18
0
 def oauth_callback(self, provider):
     if not current_user.is_anonymous:
         return redirect(url_for('IndexView:get_0'))
     oauth = OAuthSignIn.get_provider(provider)
     social_id, email, name, picture = oauth.callback()
     if social_id is None:
         flash('Authentication failed.')
         return redirect(url_for('IndexView:get_0'))
     user = User.query.filter_by(social_id=social_id).first()
     if not user:
         user = User(social_id=social_id, full_name=name, email=email, picture=picture)
         db.session.add(user)
         db.session.commit()
         # make the user follow him/herself
         db.session.add(user.follow(user))
         db.session.commit()
     login_user(user, True)
     return redirect(url_for('IndexView:get_0'))
Ejemplo n.º 19
0
def oauth_callback(provider):
    if not current_user.is_anonymous:
        return redirect(url_for('index'))
    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()
    if social_id is None:
        flash('Authentication failed.')
        return redirect(url_for('index'))
    user = User.query.filter_by(social_id=social_id).first()
    if not user:
        user = User(social_id=social_id, nickname=username, email=email)
        db.session.add(user)
        for c in range(1, 100):
            cap = Cap(number=c, count=0, owner=user)
            db.session.add(cap)
        db.session.commit()
    login_user(user, True)
    return redirect(url_for('index'))
Ejemplo n.º 20
0
def oauth_callback(provider):
    if not current_user.is_anonymous:
        return redirect(url_for('index'))
    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()
    if social_id is None:
        flash('Authentication failed.')
        return redirect(url_for('index'))
    user = User.query.filter_by(social_id=social_id).first()
    if not user:
        print(username)
        user = User(social_id=social_id, nickname=username, email=email)
        db.session.add(user)
        db.session.commit()
        login_user(user, True)
        return redirect(url_for('create_profile'))
    else:
        login_user(user, True)
        return redirect(url_for('index'))
Ejemplo n.º 21
0
def oauth_callback(provider):
    if not current_user.is_anonymous():
        return redirect(url_for('index'))
    oauth = OAuthSignIn.get_provider(provider)
    social_id, nickname, email = oauth.callback()
    if social_id is None:
        flash('Authentication failed.')
        return redirect(url_for('index'))
    user = User.query.filter_by(social_id=social_id).first()
    if not user:
        trusted = Trusted.query.filter_by(email=email).first()
        if trusted:
            nickname = User.make_unique_nickname(nickname)
            user = User(social_id=social_id, nickname=nickname, email=email)
            db.session.add(user)
            db.session.commit()
        else:
            flash("Oops, Seems like you are not in the file. Please, contact the site administration.")
            return redirect(url_for('index'))
    login_user(user, True)
    return redirect(url_for('user', nickname=g.user.nickname))
Ejemplo n.º 22
0
    def get(self):
        from flask import request, redirect
        provider = request.args.get('provider')
        
        from app.oauth import OAuthSignIn
        from app import db

        oauth = OAuthSignIn.get_provider(provider)
        social_id, email, fullname = oauth.callback()
        username = fullname + str(db.session.query(User).count())

        email = 'google$' + email

        if social_id is None:
            return {
                'response': False,
                'message': 'Something is wrong with Facebook API.'
            }, 500

        user = User.query.filter_by(socialId=social_id).first()

        if not user:
            user = User(
                socialId=social_id, 
                username=username, 
                email=email,
                fullname=fullname
            )
            user.active = True
            user.subType = 'basic'
            user.save_to_db()
        
        access_token = create_access_token(identity = {'username': user.username, 'subscription': user.subType})
        refresh_token = create_refresh_token(identity = {'username': user.username, 'subscription': user.subType})

        return redirect('http://localhost:4200/oauth_redirect?access_token={0}&refresh_token={1}'.format(
            access_token, refresh_token
        ))
Ejemplo n.º 23
0
def oauth_callback(provider):
    if not current_user.is_anonymous:

        return redirect(url_for('public'))

    oauth = OAuthSignIn.get_provider(provider)
    # This is step three. The code from the provider's reply is sent back to
    # the provider and the provider returns an authentication token
    access_token, oauth_id = oauth.callback()

    if access_token is None or oauth_id is None:
        flash('Authentication failed. Please contact an admin if '
                'this problem is persistent')
        return redirect(url_for('login'))

    user = User.query.filter_by(oauth_id=oauth_id).first()
    if user is None:
        return redirect(url_for("register"))

    login_user(user, remember=True)
    session['active_token'] = access_token

    return redirect(url_for('logged_in'))
Ejemplo n.º 24
0
def oauth_callback(provider):
    """ Callback Provider Route

        This is the second step in the OAuth process that assigns
        a token to the session

        Args:
            provider for oauth

        Returns:
            Adds token to session if successful
            If successful and no user is assigned to the oauth_id
                redirects to register account
            If unsuccessful, returns redirect to login
    """
    if not current_user.is_anonymous:
        return redirect(url_for('main.public'))

    oauth = OAuthSignIn.get_provider(provider)
    # This is step three. The code from the provider's reply is sent back to
    # the provider and the provider returns an authentication token
    access_token, oauth_id = oauth.callback()

    if access_token is None or oauth_id is None:
        flash('Authentication failed. Please contact an admin if '
              'this problem is persistent')
        return redirect(url_for('auth.login'))

    user = User.query.filter_by(oauth_id=oauth_id).first()
    if user is None:
        return redirect(url_for("auth.register"))

    login_user(user, remember=True)
    session['active_token'] = access_token

    return redirect(url_for('auth.logged_in'))
Ejemplo n.º 25
0
def oauth_authorize(provider):
    if not current_user.is_anonymous:
        return redirect(url_for('index'))
    ##OAuthSignIn
    oauth = OAuthSignIn.get_provider(provider)
    return oauth.authorize()
Ejemplo n.º 26
0
def oauth_authorize(provider):
    if not current_user.is_anonymous():
        return redirect(url_for('simple_page.index'))

    oauth = OAuthSignIn.get_provider(provider)
    return oauth.authorize()
Ejemplo n.º 27
0
 def oauth_authorize(self, provider):
     if g.user is not None and g.user.is_authenticated:
         return redirect(url_for('IndexView:get_0'))
     oauth = OAuthSignIn.get_provider(provider)
     return oauth.authorize()