Example #1
0
def callback_conn(request, type, user, info):
    result_con = 0
    profile = request.user.profile
    if not user and not info:
        result_con = 3
    elif user:
        result_con = 1
    elif type == 'FB' and not profile.facebook_id:
        profile.facebook_id = info['userid']
    elif type == 'TW' and not profile.twitter_id:
        profile.twitter_id = info['userid']
    elif type == 'KAIST' and not profile.kaist_id:
        profile.save_kaist_info(info)
    else:
        return redirect('/account/profile/')

    profile.save()
    request.session['result_con'] = result_con

    log_msg = 'success' if result_con == 0 else 'fail'
    profile_logger.warning(
        f'social.connect.{log_msg}', {
            'r':
            request,
            'extra': [
                ('type', get_social_name(type)),
                ('uid', info['userid'] if info else 'unknown'),
            ],
        })
    return redirect('/account/profile/')
Example #2
0
def disconnect(request, site):
    uid = ''
    profile = request.user.profile
    if profile.test_only:
        result_code = SocialConnectResult.TEST_ONLY
        return HttpResponseRedirect(
            f'/account/profile/?connect_site={site}&connect_result={result_code.name}'
        )

    result_code = SocialConnectResult.DISCONNECT_SUCCESS
    if site == 'FB':
        uid = profile.facebook_id
        profile.facebook_id = ''
    elif site == 'TW':
        uid = profile.twitter_id
        profile.twitter_id = ''

    has_social = (profile.facebook_id or profile.twitter_id
                  or profile.kaist_id)
    if not profile.user.has_usable_password() and not has_social:
        result_code = SocialConnectResult.ONLY_CONNECTION
    else:
        profile.save()
        logger.warning(
            'social.disconnect', {
                'r': request,
                'extra': [
                    ('type', get_social_name(site)),
                    ('uid', uid),
                ],
            })
    request.session['result_con'] = result_code.value
    return HttpResponseRedirect(
        f'/account/profile/?connect_site={site}&connect_result={result_code.name}'
    )
Example #3
0
def callback_renew(request, site, user, info):
    if site != 'KAIST':
        result_code = SocialConnectResult.RENEW_UNNECESSARY
        return HttpResponseRedirect(
            f'/account/profile/?connect_site={site}&connect_result={result_code.name}'
        )

    result_code = SocialConnectResult.CONNECT_SUCCESS
    profile = user.profile
    if profile.kaist_id != info['userid']:
        result_code = SocialConnectResult.KAIST_IDENTITY_MISMATCH
    else:
        user.profile.save_kaist_info(info)

    request.session['result_con'] = result_code.value

    log_msg = 'success' if result_code == SocialConnectResult.CONNECT_SUCCESS else 'fail'
    profile_logger.warning(
        f'social.update.{log_msg}', {
            'r':
            request,
            'extra': [
                ('type', get_social_name(site)),
                ('uid', info['userid'] if info else 'unknown'),
            ],
        })
    return HttpResponseRedirect(
        f'/account/profile/?connect_site={site}&connect_result={result_code.name}'
    )
Example #4
0
def callback_conn(request, site, user, info):
    result_code = SocialConnectResult.CONNECT_SUCCESS
    profile = request.user.profile
    if not user and not info:
        result_code = SocialConnectResult.PERMISSION_NEEDED
    elif user:
        result_code = SocialConnectResult.ALREADY_CONNECTED
    elif site == 'FB' and not profile.facebook_id:
        profile.facebook_id = info['userid']
    elif site == 'TW' and not profile.twitter_id:
        profile.twitter_id = info['userid']
    elif site == 'KAIST' and not profile.kaist_id:
        profile.save_kaist_info(info)
    else:
        result_code = SocialConnectResult.SITE_INVALID

    profile.save()
    request.session['result_con'] = result_code.value

    log_msg = 'success' if result_code == SocialConnectResult.CONNECT_SUCCESS else 'fail'
    profile_logger.warning(
        f'social.connect.{log_msg}', {
            'r':
            request,
            'extra': [
                ('type', get_social_name(site)),
                ('uid', info['userid'] if info else 'unknown'),
            ],
        })
    return HttpResponseRedirect(
        f'/account/profile/?connect_site={site}&connect_result={result_code.name}'
    )
Example #5
0
def callback(request):
    info_auth = request.session.pop('info_auth', None)
    if not info_auth:
        return redirect('/')

    mode, site = info_auth['mode'], info_auth['type']
    if site == 'FB':
        code = request.GET.get('code')
        callback_url = urljoin(settings.DOMAIN, '/account/callback/')
        profile, info = auth_fb_callback(code, callback_url)
    elif site == 'TW':
        tokens = request.session.get('request_token')
        verifier = request.GET.get('oauth_verifier')
        profile, info = auth_tw_callback(tokens, verifier)
    elif site == 'KAIST':
        token = request.session.get('request_token')
        iam_info = request.POST.get('result')
        profile, info, valid = auth_kaist_callback(token, iam_info)
        if not valid:
            return redirect('/')
    else:
        # Should not reach here!
        return redirect('/')

    uid = info['userid'] if info else 'unknown'
    logger.info(
        'social', {
            'r': request,
            'hide': True,
            'extra': [
                ('type', get_social_name(site)),
                ('uid', uid),
            ],
        })
    user = profile.user if profile else None

    if mode == 'LOGIN':
        response = callback_login(request, site, user, info)
    elif mode == 'CONN':
        response = callback_conn(request, site, user, info)
    elif mode == 'RENEW':
        response = callback_renew(request, site, user, info)

    # TODO: Find out what this is for
    response.delete_cookie('SATHTOKEN')
    return response
Example #6
0
def callback(request):
    auth = request.session.pop('info_auth', None)
    if not auth:
        return redirect('/')

    mode, type = auth['mode'], auth['type']
    if type == 'FB':
        code = request.GET.get('code')
        callback_url = request.build_absolute_uri('/account/callback/')
        profile, info = auth_fb_callback(code, callback_url)
    elif type == 'TW':
        tokens = request.session.get('request_token')
        verifier = request.GET.get('oauth_verifier')
        profile, info = auth_tw_callback(tokens, verifier)
    elif type == 'KAIST':
        token = request.COOKIES.get('SATHTOKEN')
        profile, info = auth_kaist_callback(token)

    userid = info['userid'] if info else 'unknown'
    logger.info(
        'social', {
            'r': request,
            'hide': True,
            'extra': [
                ('type', get_social_name(type)),
                ('uid', userid),
            ],
        })
    user = profile.user if profile else None

    if mode == 'LOGIN':
        response = callback_login(request, type, user, info)
    elif mode == 'CONN':
        response = callback_conn(request, type, user, info)
    elif mode == 'RENEW':
        response = callback_renew(request, type, user, info)

    response.delete_cookie('SATHTOKEN')
    return response
Example #7
0
def disconnect(request, type):
    if request.method != 'POST':
        return redirect('/account/profile/')

    uid = ''
    profile = request.user.profile
    if profile.test_only:
        return redirect('/account/profile/')

    if type == 'FB':
        uid = profile.facebook_id
        profile.facebook_id = ''
    elif type == 'TW':
        uid = profile.twitter_id
        profile.twitter_id = ''

    has_social = (
        profile.facebook_id or
        profile.twitter_id or
        profile.kaist_id
    )
    if not profile.user.has_usable_password() and not has_social:
        request.session['result_con'] = 4
        return redirect('/account/profile/')

    profile.save()

    logger.warning(f'social.disconnect', {
        'r': request,
        'extra': [
            ('type', get_social_name(type)),
            ('uid', uid),
        ],
    })

    request.session['result_con'] = 5
    return redirect('/account/profile/')
Example #8
0
def callback_renew(request, type, user, info):
    if type != 'KAIST':
        return redirect('/account/profile/')

    result_con = 0
    profile = user.profile
    if profile.kaist_id != info['userid']:
        result_con = 2
    else:
        user.profile.save_kaist_info(info)

    request.session['result_con'] = result_con

    log_msg = 'success' if result_con == 0 else 'fail'
    profile_logger.warning(
        f'social.update.{log_msg}', {
            'r':
            request,
            'extra': [
                ('type', get_social_name(type)),
                ('uid', info['userid'] if info else 'unknown'),
            ],
        })
    return redirect('/account/profile/')