Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 #12

Open
ghost opened this issue May 27, 2014 · 1 comment
Open

Python 3 #12

ghost opened this issue May 27, 2014 · 1 comment

Comments

@ghost
Copy link

ghost commented May 27, 2014

@ryanvalentin,

Please, provide Python 3 compatible example... https://github.com/disqus/DISQUS-API-Recipes/blob/master/sso/python/sso.py

I confused with encode/decode...

@Cristoph
Copy link

Python3:

import base64
import hashlib
import hmac
import json
import time

DISQUS_SECRET_KEY = '123456'
DISQUS_PUBLIC_KEY = 'abcdef'

def get_disqus_sso():
    # create a JSON packet of our data attributes
    data = json.dumps({
        'id': user['id'],
        'username': user['username'],
        'email': user['email'],
    })
    # encode the data to base64
    message = base64.b64encode(data.encode('utf-8')).decode()
    # generate a timestamp for signing the message
    timestamp = int(time.time())

    key = DISQUS_SECRET_KEY.encode('utf-8')
    msg = ('%s %s' % (message, timestamp)).encode('utf-8')
    digestmod = hashlib.sha1

    # generate our hmac signature
    sig = hmac.HMAC(key, msg, digestmod).hexdigest()


# return a script tag to insert the sso message
    return """<script type="text/javascript">
    var disqus_config = function() {
        this.page.remote_auth_s3 = "%(message)s %(sig)s %(timestamp)s";
        this.page.api_key = "%(pub_key)s";
    }
    </script>""" % dict(
        message=message,
        timestamp=timestamp,
        sig=sig,
        pub_key=DISQUS_PUBLIC_KEY,
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant