Ejemplo n.º 1
0
def addToken():
    global is_prod
    if is_prod is 'True':
        return 'Not available in production'

    token = request.form.get('token')
    slack_id = request.form.get('user_id')

    if slack_id is None:
        return 'You have to post a User ID (slack ID)'

    if token is None:
        return 'You have to include a TransferWise token'

    profiles = getTransferWiseProfiles(access_token=token)
    if profiles.status_code == 401:
        return str(json.loads(profiles.text))

    profileId = json.loads(profiles.text)[0]['id']
    homeCurrency = decide_user_home_currency(token, profileId)

    user = User.query.filter_by(slack_id=slack_id).first()
    if user is None:
        user = User(slack_id=slack_id,
                    transferwise_profile_id=profileId,
                    home_currency=homeCurrency)
        user.addEncryptedToken(token)
        db.session.add(user)
        db.session.commit()

        return 'New user created with token'

    else:
        user.addEncryptedToken(token)
        user.transferwise_profile_id = profileId
        user.home_currency = homeCurrency
        db.session.commit()
        return 'Token added or updated to existing user'
Ejemplo n.º 2
0
def oauth():
    referrer_url = 'https://transferwise.com/' + \
        'oauth/authorize?response_type=code' + \
        '&client_id=erik-edins-slack-bot&' + \
        'redirect_uri=https://slackwise.herokuapp.com/oauth'

    if request.referrer != referrer_url:
        message = 'Authentication failed.\
 Make sure you use connect the bot from Slack.'

        flash(message, 'alert-warning')
        return render_template('index.html')

    api_key = os.environ.get('TRANSFERWISE_KEY', None)

    if 'slack_id' in session:
        slack_id = session['slack_id']

    elif 'session' in request.cookies:
        request_session = json.loads(
            base64.b64decode(request.cookies['session'].split(".")[0]))
        print(request_session)

        if 'slack_id' in request_session:
            slack_id = request_session['slack_id']

    else:
        message = 'Couldn\'t find a Slack user.\
 Make sure you connect via Slack using /transferwise.'

        flash(message, 'alert-warning')
        return redirect(url_for('index'))

    code = request.args.get('code')
    response = requests.post(
        'https://api.transferwise.com/oauth/token',
        data={
            'grant_type': 'authorization_code',
            'client_id': 'erik-edins-slack-bot',
            'code': code,
            'redirect_uri': 'https://slackwise.herokuapp.com/oauth'
        },
        headers={'Authorization': 'Basic ' + str(api_key)})

    if response.status_code == 401:
        print('Token exchange failed')
        return json.loads(response.text)['error']
    elif response.status_code == 400:
        print('Invalid request')
        return json.loads(response.text)['error']
    else:
        token = json.loads(response.text)['access_token']

    if slack_id is None:
        return 'You need to use this bot via Slack for it to work.'

    profiles = getTransferWiseProfiles(access_token=token)
    if profiles.status_code == 401:
        return str(json.loads(profiles.text))

    if profiles.text == "[]":
        user = User(slack_id=slack_id)
        user.addEncryptedToken(token)
        db.session.commit()
        message = 'Account connected. To get the most out of the SlackWise bot,\
 make sure you complete your TransferWise profile.\
 Go to www.transferwise.com to set up.'

        flash(message, 'alert-warning')
        return render_template('index.html')

    profileId = json.loads(profiles.text)[0]['id']
    homeCurrency = decide_user_home_currency(token, profileId)
    user = User.query.filter_by(slack_id=slack_id).first()

    if user is None:
        flash(
            'That didn\'t work.\
 Make sure you connect your account by using /transferwise from within Slack.',
            'alert-success')
        return render_template('index.html')

    user.addEncryptedToken(token)
    user.transferwise_profile_id = profileId
    user.home_currency = homeCurrency
    db.session.commit()

    flash(
        'Your TransferWise account is set up. \
        Go back to Slack to continue using the TransferWise Slack bot.',
        'alert-success')

    return render_template('index.html')