def profile():
    """User profile information. Assocated with a Globus Auth identity."""
    if request.method == 'GET':
        identity_id = session.get('primary_identity')
        profile = database.load_profile(identity_id)

        if profile:
            name, email, institution, source_endpoint = profile

            session['name'] = name
            session['email'] = email
            session['institution'] = institution
            session['source_endpoint'] = source_endpoint
        else:
            flash('Please complete any missing profile fields and press Save.')

        if request.args.get('next'):
            session['next'] = get_safe_redirect()

        return render_template('profile.jinja2')
    elif request.method == 'POST':
        print("inside profile post")
        name = session['name'] = request.form['name']
        email = session['email'] = request.form['email']
        institution = session['institution'] = request.form['institution']
        source_endpoint = session['source_endpoint'] = int(
            request.form['endpoint'])

        database.save_profile(identity_id=session['primary_identity'],
                              name=name,
                              email=email,
                              institution=institution,
                              source_endpoint=int(source_endpoint))

        flash('Thank you! Your profile has been successfully updated.')

        if 'next' in session:
            redirect_to = session['next']
            session.pop('next')
        else:
            redirect_to = url_for('profile')

        return redirect(redirect_to)
Exemple #2
0
def authcallback():
    """Handles the interaction with Agave Auth."""
    # If we're coming back from Agave Auth in an error state, the error
    # will be in the "error" query string parameter.
    if 'error' in request.args:
        flash("You could not be logged into the portal: " +
              request.args.get('error_description', request.args['error']))
        return redirect(url_for('home'))

    redirect_uri = url_for('authcallback', _external=True)
    client = load_portal_client(redirect_uri)
    auth_uri = client.step1_get_authorize_url()
    print 'auth uri', auth_uri

    # If there's no "code" query string parameter, we're in this route
    # starting a Agave Auth login flow.
    if 'code' not in request.args:
        auth_uri = client.step1_get_authorize_url()
        return redirect(auth_uri)
    else:
        # If we do have a "code" param, we're coming back from Agave Auth
        # and can start the process of exchanging an auth code for a token.
        code = request.args.get('code')
        print 'code', code
        tokens = client.step2_exchange(code)
        tokens.revoke_uri = app.config['REVOKE_URL']
        token_json = tokens.to_json()
        print 'token json', token_json

        # user_profile = get_profile(tokens.access_token)
        user_profile = get_result(app.config['PROFILE_URL_BASE'], 'me',
                                  tokens.access_token)
        if user_profile[0]:
            print 'username', user_profile[1]['username']
        else:
            flash("User profile was not retrieved. Error:" + user_profile[1])

        session.update(tokens=tokens.to_json(),
                       is_authenticated=True,
                       name=user_profile[1]['full_name'],
                       email=user_profile[1]['email'],
                       institution='',
                       primary_identity=user_profile[1]['username'])

        profile = database.load_profile(session['primary_identity'])
        if profile:
            name, email, institution = profile

            session['name'] = name
            session['email'] = email
            session['institution'] = institution

            # handle_permission(session['primary_identity'])
        else:
            # take the user profile and save it into the database
            database.save_profile(identity_id=session['primary_identity'],
                                  name=session['name'],
                                  email=session['email'],
                                  institution=session['institution'])
            # set up the permission for new user
            handle_permission(session['primary_identity'])

            return redirect(url_for('profile', next=url_for('submit_job')))

        return redirect(url_for('submit_job'))