PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/weibo/login.py

https://github.com/Flowerowl/cola
Python | 104 lines | 99 code | 0 blank | 5 comment | 0 complexity | 842911d0a78bff80523d720a7d2567ee MD5 | raw file
Possible License(s): Apache-2.0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. Copyright (c) 2013 Qin Xuye <qin@qinxuye.me>
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. Created on 2013-6-8
  15. @author: Chine
  16. '''
  17. import urllib
  18. import base64
  19. import binascii
  20. import re
  21. import json
  22. from cola.core.errors import DependencyNotInstalledError,\
  23. LoginFailure
  24. try:
  25. import rsa
  26. except ImportError:
  27. raise DependencyNotInstalledError("rsa")
  28. class WeiboLoginFailure(LoginFailure): pass
  29. class WeiboLogin(object):
  30. def __init__(self, opener, username, passwd):
  31. self.opener = opener
  32. self.username = username
  33. self.passwd = passwd
  34. def get_user(self, username):
  35. username = urllib.quote(username)
  36. return base64.encodestring(username)[:-1]
  37. def get_passwd(self, passwd, pubkey, servertime, nonce):
  38. key = rsa.PublicKey(int(pubkey, 16), int('10001', 16))
  39. message = str(servertime) + '\t' + str(nonce) + '\n' + str(passwd)
  40. passwd = rsa.encrypt(message, key)
  41. return binascii.b2a_hex(passwd)
  42. def prelogin(self):
  43. username = self.get_user(self.username)
  44. prelogin_url = 'http://login.sina.com.cn/sso/prelogin.php?entry=sso&callback=sinaSSOController.preloginCallBack&su=%s&rsakt=mod&client=ssologin.js(v1.4.5)' % username
  45. data = self.opener.open(prelogin_url)
  46. regex = re.compile('\((.*)\)')
  47. try:
  48. json_data = regex.search(data).group(1)
  49. data = json.loads(json_data)
  50. return str(data['servertime']), data['nonce'], \
  51. data['pubkey'], data['rsakv']
  52. except:
  53. raise WeiboLoginFailure
  54. def login(self):
  55. login_url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.5)'
  56. try:
  57. servertime, nonce, pubkey, rsakv = self.prelogin()
  58. postdata = {
  59. 'entry': 'weibo',
  60. 'gateway': '1',
  61. 'from': '',
  62. 'savestate': '7',
  63. 'userticket': '1',
  64. 'ssosimplelogin': '1',
  65. 'vsnf': '1',
  66. 'vsnval': '',
  67. 'su': self.get_user(self.username),
  68. 'service': 'miniblog',
  69. 'servertime': servertime,
  70. 'nonce': nonce,
  71. 'pwencode': 'rsa2',
  72. 'sp': self.get_passwd(self.passwd, pubkey, servertime, nonce),
  73. 'encoding': 'UTF-8',
  74. 'prelt': '115',
  75. 'rsakv' : rsakv,
  76. 'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&amp;callback=parent.sinaSSOController.feedBackUrlCallBack',
  77. 'returntype': 'META'
  78. }
  79. postdata = urllib.urlencode(postdata)
  80. text = self.opener.open(login_url, postdata)
  81. regex = re.compile('\((.*)\)')
  82. json_data = json.loads(regex.search(text).group(1))
  83. return json_data['result'] == True
  84. except WeiboLoginFailure:
  85. return False