def get_preauth_token(self, account_name, expires=0): if not self.preauth_key: raise self.NoPreauthKeyProvided ts = int(time.time())*1000 preauth_str = utils.build_preauth_str(self.preauth_key, account_name, ts, expires, admin=self.isadmin) args = urllib.parse.urlencode({ 'account': account_name, 'by': 'name', 'timestamp': ts, 'expires': expires*1000, 'admin': "1" if self.isadmin else "0", 'preauth': preauth_str }) cj = http_cookiejar.CookieJar() browser = build_opener(HTTPCookieProcessor(cj)) try: url = browser.open(self.preauth_url+args) url.read() value = "" for cookie in cj: if cookie.name == self.TOKEN_COOKIE: value = cookie.value url.close() browser.close() return value except HTTPError as e: raise self.RESTBackendError(e)
def mk_auth_token(self, account, admin=False, duration=0): """ Builds an authentification token, using preauth mechanism. See http://wiki.zimbra.com/wiki/Preauth :param duration: in seconds defaults to 0, which means "use account default" :param account: an account object to be used as a selector :returns: the auth string """ domain = account.get_domain() try: preauth_key = self.get_domain(domain)['zimbraPreAuthKey'] except KeyError: raise DomainHasNoPreAuthKey(domain) timestamp = int(time.time())*1000 expires = duration*1000 return utils.build_preauth_str(preauth_key, account.name, timestamp, expires, admin)