Package mindmeister :: Module auth
[hide private]
[frames] | no frames]

Source Code for Module mindmeister.auth

  1  ''' 
  2  Copyright 2012 Alexey Kravets  <mr.kayrick@gmail.com> 
  3   
  4  This file is part of PythonMindmeister. 
  5   
  6  PythonMindmeister is free software: you can redistribute it and/or modify 
  7  it under the terms of the GNU General Public License as published by 
  8  the Free Software Foundation, either version 3 of the License, or 
  9  (at your option) any later version. 
 10   
 11  PythonMindmeister is distributed in the hope that it will be useful, 
 12  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  GNU General Public License for more details. 
 15   
 16  You should have received a copy of the GNU General Public License 
 17  along with PythonMindmeister.  If not, see <http://www.gnu.org/licenses/>. 
 18   
 19  This file contains MindMeister API calls for authentication. Additional 
 20  information can be found by this URL: 
 21  http://www.mindmeister.com/developers/explore 
 22   
 23  This product uses the MindMeister API but is not endorsed or certified 
 24  by MindMeister. 
 25  ''' 
 26   
 27   
 28  import common 
 29  import webbrowser 
 30  import parser 
 31  import diagnostic 
 32  from lxml import etree 
 33  import os.path 
 34  from diagnostic import MindException 
 35   
 36  token_file = os.path.join(os.environ['HOME'], '.pymind.token') 
 37   
38 -class AuthToken:
39 ''' 40 This class represent authentication token, used to sign API calls. 41 42 Token can be created with API key and secret (provided by mindmeister or 43 application). See 44 http://www.mindmeister.com/developers/authentication 45 for more details. 46 Token can be stored as file for further use. 47 48 In order to use token it must be registered by the following action 49 sequence: 50 1. token = AuthToken(api_key, secret) 51 2. frob = getTokenBegin (token, method) 52 MindMeister.com web page with confirmation dialog will be opened. 53 User should confirm access to the application. 54 4. getTokenEnd (token, frob) 55 56 Method can be: 57 1. read 58 2. write 59 3. delete 60 '''
61 - def __init__(self, key, secret):
62 ''' 63 Create new (unregistered) token. 64 65 Arguments: 66 key -- API key 67 secret -- shared secret 68 Optional (keyword) Arguments: 69 ''' 70 self.api_key = key 71 self.secret = secret 72 self.token = None 73 self.perms = None 74 self.proxy = None
75
76 - def load (self):
77 ''' 78 Load token from file. Returns true if token has been successfully loaded. 79 80 Arguments: 81 Optional (keyword) Arguments: 82 ''' 83 if not os.path.isfile(token_file): 84 return False 85 rawXML = open(token_file).read() 86 parser = etree.XMLParser() 87 root = etree.fromstring(rawXML, parser) 88 for item in root: 89 key = item.attrib['key'] 90 value = item.text 91 setattr (self, key, value) 92 return True
93
94 - def store (self):
95 ''' 96 Store token to a file. 97 98 Arguments: 99 Optional (keyword) Arguments: 100 ''' 101 root = etree.Element("root") 102 store = {} 103 store['token'] = self.token 104 store['username'] = self.username 105 store['fullname'] = self.fullname 106 store['perms'] = self.perms 107 108 for key,value in store.items(): 109 child = etree.SubElement(root, "value", key=key) 110 child.text = value 111 112 FILE = open(token_file,"w") 113 FILE.writelines(etree.tostring(root, pretty_print=True)) 114 FILE.close()
115 116
117 - def getFrob (self):
118 ''' 119 Returns a new frob for authentication. 120 121 Arguments: 122 Optional (keyword) Arguments: 123 124 This function calls mm.auth.getFrob MindMeister API method 125 More documentation can be found by this URL: 126 http://www.mindmeister.com/developers/explore_method?method=mm.auth.getFrob 127 ''' 128 rawFrob = common.performRequest("rest", self, method = "mm.auth.getFrob") 129 root = parser.parse(rawFrob) 130 return root[0].text
131 132
133 - def getTokenBegin (self, perms):
134 ''' 135 Start token registration process. 136 137 Arguments: 138 perms -- permissions (method) 139 Optional (keyword) Arguments: 140 ''' 141 frob = self.getFrob() 142 authUrl = common.createUrl("auth", self.secret, api_key = self.api_key,\ 143 perms = perms, frob = frob) 144 webbrowser.open(authUrl) 145 return frob
146
147 - def getTokenEnd (self, frob):
148 ''' 149 Finish token registration process. 150 151 Arguments: 152 perms -- frob obtained from getTokenBegin call 153 Optional (keyword) Arguments: 154 ''' 155 rawToken = common.performRequest("rest", self, frob = frob, method = "mm.auth.getToken") 156 root = parser.parse(rawToken) 157 auth = root [0] 158 if root.attrib['stat'] != 'ok': 159 raise MindException ("mm.auth.getToken", auth) 160 self.token = auth[0].text 161 self.perms = auth[1].text 162 self.__dict__.update(auth[2].attrib)
163
164 - def check (self):
165 ''' 166 Checks the token for a user. 167 168 Arguments: 169 Optional (keyword) Arguments: 170 171 This function calls mm.auth.checkToken MindMeister API method 172 More documentation can be found by this URL: 173 http://www.mindmeister.com/developers/explore_method?method=mm.auth.checkToken 174 ''' 175 rawToken = common.performRequest("rest", self, method = "mm.auth.checkToken") 176 root = parser.parse(rawToken) 177 auth = root [0] 178 return self.token == auth[0].text and self.perms == auth[1].text
179
180 - def setProxy (self, proxy):
181 ''' 182 Sets proxy to be used with this token. 183 184 Arguments: 185 proxy -- proxy to be used. 186 Optional (keyword) Arguments: 187 188 Sets proxy to be used with this token. Use None to disable proxy usage. 189 ''' 190 self.proxy = proxy
191