snaipperi

Aloha Wikidocs Python CGI minimal example

Feb 24th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5.    This script defines a minimal example of a Python CGI
  6.    script for Aloha Wikidocs, based on February 23th 2012 commit
  7.    https://github.com/Aloha-Wikidocs/Example/tree/acb5a6e8b982a0fda9a4e22970eb1ad079dbc07a
  8.    
  9.    The script should work with Python 2.4 upwards, uncluding Python 3.
  10.    Pre Python 2.6 versions lack json module, and require a dirty hack
  11.    Python 3 has additional requirements in how data is passed to the
  12.    base64 and hmac functions.
  13.    
  14.    NOTE - uses aloha-config.js from http://aloha-wikidocs.com/example/aloha-config.js
  15.    
  16.    You are free to modify, distribute and do whatever you like with this.
  17.    
  18.    Have fun!
  19.    Matti Eiden
  20. """
  21.  
  22. print("Content-type: text/html; charset=UTF-8")
  23. print("")
  24.  
  25.  
  26. # Define the ID and SECRET keys
  27. APP_ID = 'APPLICATION ID'
  28. APP_SC = 'APPLICATION SECRET KEY'
  29.  
  30. # Pre-Python 2.6 and 3.0 compability
  31. import sys
  32. py_version = sys.version_info[:2]
  33. if py_version[0] <= 2 and py_version[1] <= 5:
  34.     PY26 = False
  35.     PY3  = False
  36. elif py_version[0] == 3:
  37.     PY26 = False
  38.     PY3 = True
  39.     import json
  40. else:
  41.     PY26 = True
  42.     PY3 = False
  43.     import json
  44.    
  45. import cgitb,time,base64,hmac,hashlib
  46. cgitb.enable()
  47.  
  48. # This function encodes the strings and performs PHP equivalent
  49. # of strtr(text,"+/","-_"))
  50.  
  51.    
  52.  
  53.  
  54. # Next we define the parameters
  55. params = {}
  56. params['timestamp'] = time.time()
  57. params['appId'] = APP_ID
  58. params['auth'] = {}
  59. params['auth']['collaborate'] = ['title','content']
  60. params['auth']['publish'] = ['*']
  61. params['auth']['subscribe'] = ['*']
  62. params['auth']['watchSubscribers'] = ['*']
  63.  
  64. # Make the JSON payload string
  65. # urlsafe_b64encode can not encode text (unicode) in py3,
  66. # instead it requires data (ascii), thus we use bytes()
  67. if PY3:
  68.     payload = base64.urlsafe_b64encode(bytes(json.dumps(params),"ascii"))
  69. elif PY26:
  70.     payload = base64.urlsafe_b64encode(json.dumps(params))
  71. else:
  72.     payload = base64.urlsafe_b64encode(str(params).replace("'",'"'))
  73.  
  74. # Make the signature, Python 3 hmac requires the APP_SC to be bytes too
  75. if PY3:
  76.     signature = base64.urlsafe_b64encode(hmac.new(bytes(APP_SC,"ascii"),payload,hashlib.sha256).digest())
  77.     signature = str(signature,"ascii")
  78.     payload = str(payload,"ascii")
  79. else:
  80.     signature = base64.urlsafe_b64encode(hmac.new(APP_SC,payload,hashlib.sha256).digest())
  81.  
  82. # Final request
  83. request = "%s.%s"%(signature,payload)
  84.  
  85. # Output
  86. output = """
  87. <html>
  88.    <head>
  89.        <script src="http://aloha-wikidocs.com/example/aloha-config.js"></script>
  90.        <script src="http://aloha-wikidocs.com/aloha-wikidocs.js"
  91.            data-aloha-plugins="common/format,
  92.                common/table,
  93.                common/list,
  94.                common/link,
  95.                common/highlighteditables,
  96.                common/contenthandler,
  97.                common/paste,
  98.                common/characterpicker,
  99.                common/commands,
  100.                wikidocs/wikidocs
  101.        "></script>
  102.        <script>WIKIDOCS_SIGNED_REQUEST = "%s";
  103.        </script>
  104.    </head>
  105.    <body>
  106.        <h1 id="title" data-wikidocs-id="title">Stop right there</h1>
  107.        <div id="content" data-wikidocs-id="content">You rebel scum</div>
  108.  
  109.        <script type="text/javascript">
  110.            Aloha.ready(function(){
  111.                Aloha.require([ "aloha/jquery" ],
  112.                    function($) {
  113.                        $( "#title" ).aloha();
  114.                        $( "#content" ).aloha();
  115.                        }
  116.                );
  117.            });
  118.        </script>
  119.    </body>
  120. </html>"""%(request)
  121.  
  122. print(output)
Add Comment
Please, Sign In to add comment