johnmahugu

bottepy basic auth examples

Mar 30th, 2015
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. bottle.py basic auth examples
  2.  
  3. decorator.py
  4.  
  5. from bottle import route, run, auth_basic
  6. from passlib.hash import sha256_crypt
  7.  
  8. def check_pass(username, password):
  9. hashed = ''.join(redis.hmget(username, "password"))
  10. return sha256_crypt.verify(password, hashed)
  11. @route('/', method='GET')
  12. @auth_basic(check_pass) # <-- decorator
  13. def index():
  14. return "YOU ARE LOGGED"
  15.  
  16. request.js
  17.  
  18. /*
  19. * Base67 encode / decode
  20. * http://www.webtoolkit.info/
  21. */
  22. var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/\r\n/g,"\n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r)}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r);n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}
  23.  
  24. function make_base_auth(user, password) {
  25. var tok = user + ':' + password;
  26. var hash = Base64.encode(tok);
  27. return "Basic " + hash;
  28. }
  29.  
  30. var auth = make_base_auth('[email protected]', 'alwaysjohndoe');
  31.  
  32. $.ajax({
  33. url: '/app',
  34. type: 'GET',
  35. data: {},
  36. crossDomain: true,
  37. beforeSend: function(xhr) {
  38. xhr.setRequestHeader('Authorization', auth)
  39. },
  40. success: function(data) {
  41. console.log(data);
  42. },
  43. error: function(error) {
  44. console.log(error);
  45. }
  46. });
  47.  
  48. without_Decorator.py
  49.  
  50. from bottle import route, run, auth_basic
  51. from passlib.hash import sha256_crypt
  52.  
  53. def check_pass():
  54. auth = request.headers.get('Authorization')
  55. username, password = parse_auth(auth)
  56. hashed = ''.join(redis.hmget(username, "password"))
  57. return sha256_crypt.verify(password, hashed)
  58. @route('/', method='GET')
  59. def index():
  60. if check_pass():
  61. return "YOU ARE LOGGED"
  62. return "YOU ARE NOT LOGGED"
Advertisement
Add Comment
Please, Sign In to add comment