tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

keyfiles.configure (2301B)


      1 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
      2 # vim: set filetype=python:
      3 # This Source Code Form is subject to the terms of the Mozilla Public
      4 # License, v. 2.0. If a copy of the MPL was not distributed with this
      5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      6 
      7 
      8 @template
      9 def keyfile(desc, default=None, help=None, callback=lambda x: x):
     10     help = help or (
     11         "Use the secret key contained in the given keyfile " "for %s requests" % desc
     12     )
     13     name = desc.lower().replace(" ", "-")
     14     no_key = callback("no-%s-key" % name)
     15 
     16     option("--with-%s-keyfile" % name, nargs=1, default=default, help=help)
     17 
     18     @depends("--with-%s-keyfile" % name)
     19     @checking("for the %s key" % desc, lambda x: x and x is not no_key)
     20     @imports(_from="__builtin__", _import="open")
     21     @imports(_from="__builtin__", _import="IOError")
     22     def keyfile(value):
     23         if value:
     24             try:
     25                 with open(value[0]) as fh:
     26                     result = fh.read().strip()
     27                     if result:
     28                         return callback(result)
     29                     raise FatalCheckError("'%s' is empty." % value[0])
     30             except IOError as e:
     31                 raise FatalCheckError("'%s': %s." % (value[0], e.strerror))
     32         return no_key
     33 
     34     return keyfile
     35 
     36 
     37 @template
     38 def simple_keyfile(desc, default=None):
     39     value = keyfile(desc, default=default)
     40     set_config("MOZ_%s_KEY" % desc.upper().replace(" ", "_"), value)
     41 
     42 
     43 @template
     44 def id_and_secret_keyfile(desc, default=None):
     45     def id_and_secret(value):
     46         if value.startswith("no-") and value.endswith("-key"):
     47             id = value[:-3] + "clientid"
     48             secret = value
     49         elif " " in value:
     50             id, secret = value.split(" ", 1)
     51         else:
     52             raise FatalCheckError("%s key file has an invalid format." % desc)
     53         return namespace(
     54             id=id,
     55             secret=secret,
     56         )
     57 
     58     content = keyfile(
     59         desc,
     60         help="Use the client id and secret key contained "
     61         "in the given keyfile for %s requests" % desc,
     62         default=default,
     63         callback=id_and_secret,
     64     )
     65 
     66     name = desc.upper().replace(" ", "_")
     67     set_config("MOZ_%s_CLIENTID" % name, content.id)
     68     set_config("MOZ_%s_KEY" % name, content.secret)