Example #1
0
 def login(self):
     """
     Login with HumanitarianId
     If no HumanitarianId is found in database
         New User is Created
         If HumanitarianId user's email conflicts with
         existing user, than h_id is stored in existing user
         profile
     """
     if current_user.is_authenticated:
         return True
     if self.status:
         # Search for user for obtain h_id
         user = User.query.filter_by(h_id=self.data['_id']).first()
         if not user:
             # Search for user for obtain email
             user = User.query.filter_by(email=self.data['email']).first()
             if user:
                 # Integrate user with obtain email with obtain hd_id
                 self.create_user(user)
                 flash('You are logged in with email: '+user.email,
                       'warning')
             else:
                 user = self.create_user()
         # Login user if obtain h_id or email match with user
         login_user_bundle(user)
         flash('You are logged in', 'success')
         return True
     return False
Example #2
0
 def login(self):
     """
     Login with HumanitarianId
     If no HumanitarianId is found in database
         New User is Created
         If HumanitarianId user's email conflicts with
         existing user, than h_id is stored in existing user
         profile
     """
     if current_user.is_authenticated:
         return True
     if self.status:
         # Search for user for obtain h_id
         user = User.query.filter_by(h_id=self.data['_id']).first()
         if not user:
             # Search for user for obtain email
             user = User.query.filter_by(email=self.data['email']).first()
             if user:
                 # Integrate user with obtain email with obtain hd_id
                 self.create_user(user)
                 flash('You are logged in with email: ' + user.email,
                       'warning')
             else:
                 user = self.create_user()
         # Login user if obtain h_id or email match with user
         login_user_bundle(user)
         flash('You are logged in', 'success')
         return True
     return False
Example #3
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = SigninForm()
    if form.validate_on_submit():
        login_user_bundle(form.user)
        return form.redirect('home')
    return render_template('login.html', form=form)
Example #4
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = SigninForm()
    if form.validate_on_submit():
        login_user_bundle(form.user)
        return form.redirect('index')
    return render_template('login.html', form=form)
Example #5
0
 def wrapper(*args, **kwargs):
     if request.authorization:
         ucontr = UserController()
         try:
             user = ucontr.get(login=request.authorization.username)
         except NotFound:
             raise Forbidden("Couldn't authenticate your user")
         if not ucontr.check_password(user, request.authorization.password):
             raise Forbidden("Couldn't authenticate your user")
         if not user.is_active:
             raise Forbidden("User is desactivated")
         login_user_bundle(user)
     if current_user.is_authenticated:
         return func(*args, **kwargs)
     raise Unauthorized()
Example #6
0
def signup():
    if not conf.AUTH_ALLOW_SIGNUP:
        flash(gettext("Self-registration is disabled."), 'warning')
        return redirect(url_for('home'))
    if current_user.is_authenticated:
        return redirect(url_for('home'))

    form = SignupForm()
    if form.validate_on_submit():
        user = UserController().create(login=form.login.data,
                email=form.email.data, password=form.password.data)
        login_user_bundle(user)
        return redirect(url_for('home'))

    return render_template('signup.html', form=form)
Example #7
0
def auth_func(*args, **kw):
    if request.authorization:
        user = User.query.filter(name==request.authorization.username).first()
        if not user:
            raise ProcessingException("Couldn't authenticate your user",
                                        code=401)
        if not user.check_password(request.authorization.password):
            raise ProcessingException("Couldn't authenticate your user",
                                        code=401)
        if not user.is_active:
            raise ProcessingException("User is desactivated", code=401)
        login_user_bundle(user)
    if not current_user.is_authenticated:
        raise ProcessingException(description='Not authenticated!', code=401)
    return True
Example #8
0
 def wrapper(*args, **kwargs):
     if request.authorization:
         ucontr = UserController()
         try:
             user = ucontr.get(nickname=request.authorization.username)
         except NotFound:
             raise Forbidden("Couldn't authenticate your user")
         if not ucontr.check_password(user, request.authorization.password):
             raise Forbidden("Couldn't authenticate your user")
         if not user.is_active:
             raise Forbidden("User is desactivated")
         login_user_bundle(user)
     if current_user.is_authenticated:
         return func(*args, **kwargs)
     raise Unauthorized()
Example #9
0
def auth_func(*args, **kw):
    if request.authorization:
        ucontr = UserController()
        try:
            user = ucontr.get(nickname=request.authorization.username)
        except NotFound:
            raise ProcessingException("Couldn't authenticate your user",
                                      code=401)
        if not ucontr.check_password(user, request.authorization.password):
            raise ProcessingException("Couldn't authenticate your user",
                                      code=401)
        if not user.is_active:
            raise ProcessingException("User is deactivated", code=401)
        login_user_bundle(user)
    if not current_user.is_authenticated:
        raise ProcessingException(description='Not authenticated!', code=401)
Example #10
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = LoginForm()
    if form.validate_on_submit():
        flash('You are logged in', 'info')
        login_user_bundle(form.user)
        return form.redirect('index')
    #signup = SignupForm()
    return render_template(
        'login.html',
        humanitarian_id_auth_uri=conf.HUMANITARIAN_ID_AUTH_URI,
        client_id=conf.HUMANITARIAN_ID_CLIENT_ID,
        redirect_uri=conf.HUMANITARIAN_ID_REDIRECT_URI,
        loginForm=form  #, signupForm=signup
    )
Example #11
0
def recover(token):
    form = PasswordModForm()
    ucontr = UserController()
    try:
        user = ucontr.get(renew_password_token=token)
    except NotFound:
        return gettext("Token is not valid, please regenerate one")
    if request.method == 'GET':
        return render_template('recover.html', form=form, token=token)

    if form.validate():
        ucontr.update({'id': user.id},
                {'renew_password_token': '', 'password': form.password.data})
        login_user_bundle(user)
        return redirect(url_for('home'))
    return render_template('recover.html', form=form, token=token)
Example #12
0
def auth_func(*args, **kw):
    if request.authorization:
        ucontr = UserController()
        try:
            user = ucontr.get(nickname=request.authorization.username)
        except NotFound:
            raise ProcessingException("Couldn't authenticate your user",
                                        code=401)
        if not ucontr.check_password(user, request.authorization.password):
            raise ProcessingException("Couldn't authenticate your user",
                                        code=401)
        if not user.is_active:
            raise ProcessingException("User is desactivated", code=401)
        login_user_bundle(user)
    if not current_user.is_authenticated:
        raise ProcessingException(description='Not authenticated!', code=401)
def auth_func(*args, **kw):
    if request.authorization:
        user = User.query.filter(
            User.login == request.authorization.username).first()
        if not user:
            raise ProcessingException("Couldn't authenticate your user",
                                      code=401)
        if not user.check_password(request.authorization.password):
            raise ProcessingException("Couldn't authenticate your user",
                                      code=401)
        if not user.is_active:
            raise ProcessingException("Couldn't authenticate your user",
                                      code=401)
        login_user_bundle(user)
    if not current_user.is_authenticated:
        raise ProcessingException(description='Not authenticated!', code=401)
Example #14
0
def recover(token):
    form = PasswordModForm()
    ucontr = UserController()
    try:
        user = ucontr.get(renew_password_token=token)
    except NotFound:
        return gettext("Token is not valid, please regenerate one")
    if request.method == 'GET':
        return render_template('recover.html', form=form, token=token)

    if form.validate():
        ucontr.update({'id': user.id}, {
            'renew_password_token': '',
            'password': form.password.data
        })
        login_user_bundle(user)
        return redirect(url_for('home'))
    return render_template('recover.html', form=form, token=token)
Example #15
0
def oauth_callback(provider):  # pragma: no cover
    if not current_user.is_anonymous:
        return redirect(url_for('home'))
    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()
    if social_id is None:
        flash('Authentication failed.')
        return redirect(url_for('home'))
    ucontr = UserController()
    try:
        user = ucontr.get(**{'%s_identity' % provider: social_id})
    except NotFound:
        user = None
    if not user:
        user = ucontr.create(**{'%s_identity' % provider: social_id,
                                'login': username, 'email': email})
    login_user_bundle(user)
    return redirect(url_for('home'))
Example #16
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    if request.args.get('code'):
        code = request.args.get('code')
        data = {
            'grant_type': 'authorization_code',
            'client_id': conf.OPENIDC_CLIENT_ID,
            'client_secret': conf.OPENIDC_CLIENT_SECRET,
            'redirect_uri': url_for('login', _external=True),
            'code': code
        }
        r = requests.post(conf.OPENIDC_TOKEN_ENDPOINT, data=data)
        tokens = r.json()
        if 'error' not in tokens:
            headers = {
                'Authorization': 'Bearer ' + tokens['access_token'],
            }
            r = requests.post(conf.OPENIDC_USERINFO_ENDPOINT, headers=headers)
            userinfo = r.json()

            ucontr = UserController()
            try:
                user = ucontr.get(**{'external_id': userinfo['sub']})
                ucontr.update({'id': user.id}, {
                    'nickname': userinfo['preferred_username'],
                    'email': userinfo['email'],
                })
            except NotFound:
                user = ucontr.create(nickname=userinfo['preferred_username'],
                                     email=userinfo['email'],
                                     external_id=userinfo['sub'],
                                     is_active=True,
                                     is_api=True)
            login_user_bundle(user)
            return redirect(url_for('home'))

    auth = URL(conf.OPENIDC_AUTHORIZATION_ENDPOINT)
    auth = auth.with_query(client_id=conf.OPENIDC_CLIENT_ID,
                           redirect_uri=url_for('login', _external=True),
                           response_type='code',
                           scope='openid profile email')
    return render_template('login.html', authorization=auth)
Example #17
0
def oauth_callback(provider):  # pragma: no cover
    if not current_user.is_anonymous:
        return redirect(url_for('home'))
    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()
    if social_id is None:
        flash('Authentication failed.')
        return redirect(url_for('home'))
    ucontr = UserController()
    try:
        user = ucontr.get(**{'%s_identity' % provider: social_id})
    except NotFound:
        user = None
    if not user and not conf.OAUTH_ALLOW_SIGNUP:
        flash('Account creation is not allowed through OAuth.')
        return redirect(url_for('home'))
    elif not user:
        user = ucontr.create(**{'%s_identity' % provider: social_id,
                                'login': '******' % (provider, username),
                                'email': email})
    login_user_bundle(user)
    return redirect(url_for('home'))
def signup():
    """if not conf.SELF_REGISTRATION:
        flash("Self-registration is disabled.", 'warning')
        return redirect(url_for('index'))"""
    if current_user.is_authenticated:
        return redirect(url_for('index'))

    form = SignupForm()
    if form.validate_on_submit():
        user = User(name=form.name.data,
                    email=form.email.data,
                    pwdhash=generate_password_hash(form.password.data),
                    is_active=True)
        db.session.add(user)
        db.session.commit()
        flash('Your account has been created. ', 'success')
        login_user_bundle(user)  # automatically log the user

        return form.redirect('index')

    loginForm = LoginForm()
    return render_template('join.html', loginForm=loginForm, signupForm=form)