예제 #1
0
    def test_me(self):
        def request_callback(request):
            self.assertEqual(request.url,
                             "https://v2.steemconnect.com/api/me/")

            return 200, {}, json.dumps({"access_token": "foo"})

        c = Client(access_token="foo")
        responses.add_callback(
            responses.POST,
            'https://v2.steemconnect.com/api/me/',
            callback=request_callback,
        )

        c.me()
예제 #2
0
    def authenticate(self, **kwargs):
        if 'username' in kwargs:
            return None

        # validate the access token with /me endpoint and get user information
        client = Client(access_token=kwargs.get("access_token"))

        user = client.me()
        if 'name' not in user:
            return None

        user_model = get_user_model()
        metadata = {}
        profile_about, profile_image, profile_location = None, None, None
        if 'json_metadata' in user["account"]:
            metadata = json.loads(user["account"]["json_metadata"])
            profile = metadata.get("profile", {})
            profile_about = profile.get("about")
            profile_image = profile.get("profile_image")
            profile_location = profile.get("location")

        try:
            user = user_model.objects.get(username=user["name"])
        except user_model.DoesNotExist:
            user = user_model.objects.create_user(username=user["name"],
                                                  name=user["name"])
        user.profile_about = profile_about
        user.profile_image = profile_image
        user.profile_location = profile_location
        user.save()

        return user
예제 #3
0
  def wrapper (self, *args, **kwargs) :
    username = ''
    access_token = ''
    request = self.request
    if request.method == 'GET' or request.method == 'DELETE':
      if 'username' in request.GET :
        username = request.GET['username']
      if 'access_token' in request.GET :
        access_token = request.GET['access_token']
    else :
      if 'username' in request.data :
        username = request.data['username']
      if 'access_token' in request.data :
        access_token = request.data['access_token']

    if username == '' or access_token == '' :
      raise PermissionDenied

    token = AccessToken.objects.filter(username = username)

    if len(token) == 0 or token[0].token != sha512(access_token) :
      # Validate access token with Steemconnect
      c = Client(access_token = access_token)

      try: 
        user = c.me()
      except:
        return HttpResponse(status=503)

      if user['user'] != username :
        # Wrong username / access_token pair
        raise PermissionDenied

      if len(token) == 0 :
        token = AccessToken(username = username,
                            token = sha512(access_token))
        token.save()
      else :
        # Access token was updated
        token.update(token = sha512(access_token))

    # User is valid, do we need to create a new SteemUser object?
    users = SteemUser.objects.filter(username = username)
    if len(users) == 0 :
      user = SteemUser(username = username)
      user.save()
    else :
      user = users[0]

    kwargs['user'] = user

    return func(self, *args, **kwargs)
예제 #4
0
def gift_codes():
    sc_client = ScClient(access_token=request.args.get("access_token"),
                         oauth_base_url="https://hivesigner.com/oauth2/",
                         sc2_api_base_url="https://hivesigner.com/api/")
    me = sc_client.me()
    if 'error' in me:
        return "Invalid access token"

    # if the user already claimed their gift codes,
    # then there is no need to create new gift codes
    if g.infestor.gift_code_manager.get_gift_code_count_by_user(
            me["account"]["name"]) > 0:
        gift_codes = g.infestor.gift_code_manager.get_gift_codes_by_user(
            me["account"]["name"])
        return render_template("gift_codes.html",
                               user=me,
                               gift_codes=gift_codes)

    # quick hack
    # no need to fill all account data into Lighthive.helpers.Account
    # since all we're interested in is reputation figure.
    acc = Account(client=g.lightsteem_client)
    acc.raw_data = {"reputation": me["account"]["reputation"]}

    gift_code_count = 0
    if acc.reputation() < g.minimum_rep:
        error = "Your reputation is not enough to claim a free account."
        return render_template("gift_codes.html", error=error)
    else:
        gift_code_count += 1
        # check if the account is eligible for the bonus
        if OPERATOR_WITNESS in me["account"]["witness_votes"]:
            gift_code_count += 1

    # create gift_codes based on the *gift_code_count*
    for i in range(gift_code_count):
        code = random.randint(1000000, 999999999)
        g.infestor.gift_code_manager.add_code(
            code, created_for=me["account"]["name"])
    gift_codes = g.infestor.gift_code_manager.get_gift_codes_by_user(
        me["account"]["name"])

    return render_template("gift_codes.html",
                           user=me,
                           gift_codes=gift_codes,
                           error=None)
예제 #5
0
    def authenticate(self, request, **kwargs):

        if 'username' in kwargs:
            return None

        # validate the access token with /me endpoint and get user information
        client = Client(access_token=kwargs.get("access_token"))

        user = client.me()
        if 'name' not in user:
            return None

        user_model = get_user_model()
        try:
            user_instance = user_model.objects.get(username=user["name"])
            user_instance.save()
        except user_model.DoesNotExist:
            user_instance = user_model.objects.create_user(
                username=user["name"])
        return user_instance
예제 #6
0
def login_complete():
    token = request.args['access_token']
    c = Client(access_token=token, )
    user_info = c.me()
    create_or_update_user(user_info['name'], token)
    return redirect(FE_URL.format(user_info['name']))