Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- """
- This script defines a minimal example of a Python CGI
- script for Aloha Wikidocs, based on February 23th 2012 commit
- https://github.com/Aloha-Wikidocs/Example/tree/acb5a6e8b982a0fda9a4e22970eb1ad079dbc07a
- The script should work with Python 2.4 upwards, uncluding Python 3.
- Pre Python 2.6 versions lack json module, and require a dirty hack
- Python 3 has additional requirements in how data is passed to the
- base64 and hmac functions.
- NOTE - uses aloha-config.js from http://aloha-wikidocs.com/example/aloha-config.js
- You are free to modify, distribute and do whatever you like with this.
- Have fun!
- Matti Eiden
- """
- print("Content-type: text/html; charset=UTF-8")
- print("")
- # Define the ID and SECRET keys
- APP_ID = 'APPLICATION ID'
- APP_SC = 'APPLICATION SECRET KEY'
- # Pre-Python 2.6 and 3.0 compability
- import sys
- py_version = sys.version_info[:2]
- if py_version[0] <= 2 and py_version[1] <= 5:
- PY26 = False
- PY3 = False
- elif py_version[0] == 3:
- PY26 = False
- PY3 = True
- import json
- else:
- PY26 = True
- PY3 = False
- import json
- import cgitb,time,base64,hmac,hashlib
- cgitb.enable()
- # This function encodes the strings and performs PHP equivalent
- # of strtr(text,"+/","-_"))
- # Next we define the parameters
- params = {}
- params['timestamp'] = time.time()
- params['appId'] = APP_ID
- params['auth'] = {}
- params['auth']['collaborate'] = ['title','content']
- params['auth']['publish'] = ['*']
- params['auth']['subscribe'] = ['*']
- params['auth']['watchSubscribers'] = ['*']
- # Make the JSON payload string
- # urlsafe_b64encode can not encode text (unicode) in py3,
- # instead it requires data (ascii), thus we use bytes()
- if PY3:
- payload = base64.urlsafe_b64encode(bytes(json.dumps(params),"ascii"))
- elif PY26:
- payload = base64.urlsafe_b64encode(json.dumps(params))
- else:
- payload = base64.urlsafe_b64encode(str(params).replace("'",'"'))
- # Make the signature, Python 3 hmac requires the APP_SC to be bytes too
- if PY3:
- signature = base64.urlsafe_b64encode(hmac.new(bytes(APP_SC,"ascii"),payload,hashlib.sha256).digest())
- signature = str(signature,"ascii")
- payload = str(payload,"ascii")
- else:
- signature = base64.urlsafe_b64encode(hmac.new(APP_SC,payload,hashlib.sha256).digest())
- # Final request
- request = "%s.%s"%(signature,payload)
- # Output
- output = """
- <html>
- <head>
- <script src="http://aloha-wikidocs.com/example/aloha-config.js"></script>
- <script src="http://aloha-wikidocs.com/aloha-wikidocs.js"
- data-aloha-plugins="common/format,
- common/table,
- common/list,
- common/link,
- common/highlighteditables,
- common/contenthandler,
- common/paste,
- common/characterpicker,
- common/commands,
- wikidocs/wikidocs
- "></script>
- <script>WIKIDOCS_SIGNED_REQUEST = "%s";
- </script>
- </head>
- <body>
- <h1 id="title" data-wikidocs-id="title">Stop right there</h1>
- <div id="content" data-wikidocs-id="content">You rebel scum</div>
- <script type="text/javascript">
- Aloha.ready(function(){
- Aloha.require([ "aloha/jquery" ],
- function($) {
- $( "#title" ).aloha();
- $( "#content" ).aloha();
- }
- );
- });
- </script>
- </body>
- </html>"""%(request)
- print(output)
Add Comment
Please, Sign In to add comment