示例#1
0
def auth_rc(req):
    token = req.GET.get('token')

    if token is None:
        return redirect("https://app.runnersconnect.net")

    user = User.EnsureWithRcToken(req, token)
    uid, authData, extendedAuthData = (token, {}, {"token": token})
    serviceRecord = Service.EnsureServiceRecordWithAuth(RunnersConnectService, uid, authData, extendedAuthData, True)
    User.ConnectService(user, serviceRecord)

    return render(req, "dashboard.html")
示例#2
0
def sync_status_rc(req):
    token = req.GET.get('token')

    if token is None:
        return HttpResponse(status=403)

    user = User.EnsureWithRcToken(req, token)
    uid, authData, extendedAuthData = (token, {}, {"token": token})
    serviceRecord = Service.EnsureServiceRecordWithAuth(RunnersConnectService, uid, authData, extendedAuthData, True)
    User.ConnectService(user, serviceRecord)

    return sync_status(req)
示例#3
0
def auth_do(req, service):
    rc_token = req.POST.get('rc_token')

    if rc_token is None:
        return "No RC token."

    rc_user = User.EnsureWithRcToken(req, rc_token)
    rc_uid, rc_authData, rc_extendedAuthData = (rc_token, {}, {
        "token": rc_token
    })
    rc_serviceRecord = Service.EnsureServiceRecordWithAuth(
        RunnersConnectService, rc_uid, rc_authData, rc_extendedAuthData, True)
    User.ConnectService(rc_user, rc_serviceRecord)

    svc = Service.FromID(service)
    from tapiriik.services.api import APIException
    try:
        if svc.RequiresExtendedAuthorizationDetails:
            uid, authData, extendedAuthData = svc.Authorize(
                req.POST["username"], req.POST["password"])
        else:
            uid, authData = svc.Authorize(req.POST["username"],
                                          req.POST["password"])
    except APIException as e:
        if e.UserException is not None:
            return {
                "type": e.UserException.Type,
                "extra": e.UserException.Extra
            }
        return False
    if authData is not None:
        #serviceRecord = Service.EnsureServiceRecordWithAuth(svc, uid, authData, extendedAuthDetails=extendedAuthData if svc.RequiresExtendedAuthorizationDetails else None, persistExtendedAuthDetails=bool(req.POST.get("persist", None)))
        #always persist
        serviceRecord = Service.EnsureServiceRecordWithAuth(
            svc,
            uid,
            authData,
            extendedAuthDetails=extendedAuthData
            if svc.RequiresExtendedAuthorizationDetails else None,
            persistExtendedAuthDetails=bool(True))
        # auth by this service connection
        #existingUser = User.AuthByService(serviceRecord)
        # only log us in as this different user in the case that we don't already have an account
        #if existingUser is not None and req.user is None:
        #    User.Login(existingUser, req)
        #else:
        #    User.Ensure(req)
        # link service to user account, possible merge happens behind the scenes (but doesn't effect active user)
        User.ConnectService(req.user, serviceRecord)
        return True
    return False
示例#4
0
def authreturn(req, service, level=None):
    rc_token = req.GET.get('rc_token')

    if rc_token is None:
        return redirect("https://app.runnersconnect.net")

    rc_user = User.EnsureWithRcToken(req, rc_token)
    rc_uid, rc_authData, rc_extendedAuthData = (rc_token, {}, {"token": rc_token})
    rc_serviceRecord = Service.EnsureServiceRecordWithAuth(RunnersConnectService, rc_uid, rc_authData, rc_extendedAuthData, True)
    User.ConnectService(rc_user, rc_serviceRecord)

    logger.info("Auto logged user %s " % (req.user['rc_token']))

    if ("error" in req.GET or "not_approved" in req.GET):
        success = False
    else:
        svc = Service.FromID(service)
        try:
            uid, authData = svc.RetrieveAuthorizationToken(req, level)
        except Exception as e:
            logger.info("Errrrr %s " % (str(e)))
            return render(req, "oauth-failure.html", {
                "service": svc,
                "error": str(e)
            })
        serviceRecord = Service.EnsureServiceRecordWithAuth(svc, uid, authData)

        # auth by this service connection
        # we've already created and logged in user with rc token
        #existingUser = User.AuthByService(serviceRecord)

        # only log us in as this different user in the case that we don't already have an account
        #if req.user is None and existingUser is not None:
        #    User.Login(existingUser, req)
        #else:
        #    User.Ensure(req)
        # link service to user account, possible merge happens behind the scenes (but doesn't effect active user)
        User.ConnectService(req.user, serviceRecord)
        success = True

    #return render(req, "oauth-return.html", {"success": 1 if success else 0})
    connectedServices = [s["Service"] for s in req.user['ConnectedServices']]
    logger.info("connected services  %s " % (connectedServices))

    return HttpResponse(json.dumps({"success": success == True, "user": req.user["rc_token"], "connectedServices": connectedServices}), content_type='application/json')
示例#5
0
def schedule_immediate_rc_sync(req):
    token = req.POST.get('token')

    if token is None:
        return HttpResponse(status=403)

    user = User.EnsureWithRcToken(req, token)
    uid, authData, extendedAuthData = (token, {}, {"token": token})
    serviceRecord = Service.EnsureServiceRecordWithAuth(RunnersConnectService, uid, authData, extendedAuthData, True)
    User.ConnectService(user, serviceRecord)

    #if "LastSynchronization" in req.user and req.user["LastSynchronization"] is not None and datetime.utcnow() - req.user["LastSynchronization"] < Sync.MinimumSyncInterval:
    #    return HttpResponse(status=429)
    exhaustive = None
    #if "LastSynchronization" in req.user and req.user["LastSynchronization"] is not None and datetime.utcnow() - req.user["LastSynchronization"] > Sync.MaximumIntervalBeforeExhaustiveSync:
    #    exhaustive = True
    Sync.ScheduleImmediateSync(req.user, exhaustive)
    return HttpResponse()