| 59 | | # |
|---|
| 60 | | # Exceptions |
|---|
| 61 | | # |
|---|
| 62 | | |
|---|
| 63 | | |
|---|
| 64 | | |
|---|
| 65 | | |
|---|
| 66 | | # |
|---|
| 67 | | # Default values |
|---|
| 68 | | # |
|---|
| 69 | | |
|---|
| 70 | | #_allowed_options = [ |
|---|
| 71 | | # 'method', |
|---|
| 72 | | # 'catch', |
|---|
| 73 | | # 'exclude', |
|---|
| 74 | | # 'enable', |
|---|
| 75 | | # |
|---|
| 76 | | # 'form_template_obj', |
|---|
| 77 | | # 'form_template_file', |
|---|
| 78 | | # 'form_template', |
|---|
| 79 | | # 'realm', |
|---|
| 80 | | # |
|---|
| 81 | | # 'users_object', |
|---|
| 82 | | # 'users_setup', |
|---|
| 83 | | # 'users_valid', |
|---|
| 84 | | # 'users_digest', |
|---|
| 85 | | # |
|---|
| 86 | | # 'passurl_path_process', |
|---|
| 87 | | # 'passurl_urltouser', |
|---|
| 88 | | # 'passurl_path_verify', |
|---|
| 89 | | # 'passurl_path_signedin', |
|---|
| 90 | | # 'passurl_baseurl', |
|---|
| 91 | | # 'passurl_template', |
|---|
| 92 | | # 'passurl_template_obj', |
|---|
| 93 | | # 'passurl_template_file', |
|---|
| 94 | | # 'passurl_session_middleware', |
|---|
| 95 | | # 'passurl_session_key', |
|---|
| 96 | | # 'passurl_session_secret', |
|---|
| 97 | | # 'passurl_store_config', |
|---|
| 98 | | # 'passurl_store_type', |
|---|
| 99 | | # |
|---|
| 100 | | # 'cookie_secret', |
|---|
| 101 | | # 'cookie_name', |
|---|
| 102 | | # 'cookie_secure', |
|---|
| 103 | | # 'cookie_includeip', |
|---|
| 104 | | # 'cookie_signout', |
|---|
| 105 | | # 'cookie_params', |
|---|
| 106 | | # 'cookie_enforce', |
|---|
| 107 | | # 'signin', |
|---|
| 108 | | #] |
|---|
| 109 | | # |
|---|
| 110 | | ## |
|---|
| 111 | | ## Useful functions |
|---|
| 112 | | ## |
|---|
| 113 | | # |
|---|
| 114 | | #def _get_value(dictionary, option, prefix, allowed_values=[], **p): |
|---|
| 115 | | # """ |
|---|
| 116 | | # Returns the value ``dictionary[option]`` checking it is one of |
|---|
| 117 | | # ``allowed_values`` |
|---|
| 118 | | # """ |
|---|
| 119 | | # if not dictionary.has_key(option): |
|---|
| 120 | | # if p.has_key('default'): |
|---|
| 121 | | # return p['default'] |
|---|
| 122 | | # else: |
|---|
| 123 | | # raise AuthKitConfigError( |
|---|
| 124 | | # "The required option %r was not specified"%_app2key(option, prefix) |
|---|
| 125 | | # ) |
|---|
| 126 | | # else: |
|---|
| 127 | | # result = dictionary[option] |
|---|
| 128 | | # if allowed_values and result not in allowed_values: |
|---|
| 129 | | # if p.has_key('default'): |
|---|
| 130 | | # return p['default'] |
|---|
| 131 | | # else: |
|---|
| 132 | | # raise AuthKitConfigError( |
|---|
| 133 | | # "The option %r takes one of the values %r, not %r"%( |
|---|
| 134 | | # _app2key(option, prefix), |
|---|
| 135 | | # allowed_values, |
|---|
| 136 | | # result, |
|---|
| 137 | | # ) |
|---|
| 138 | | # ) |
|---|
| 139 | | # return result |
|---|
| 140 | | # |
|---|
| 141 | | #def _convert_config(app_conf, prefix): |
|---|
| 142 | | # """ |
|---|
| 143 | | # Return the ``app_conf`` dictionary keys in the form they would have been |
|---|
| 144 | | # entered if the middleware had been setup directly in Python code |
|---|
| 145 | | # """ |
|---|
| 146 | | # authkit_options = {} |
|---|
| 147 | | # for key, value in app_conf.items(): |
|---|
| 148 | | # if key[:len(prefix)].lower() == prefix: |
|---|
| 149 | | # authkit_options[key[len(prefix):].replace('.','_')] = value |
|---|
| 150 | | # return authkit_options |
|---|
| 151 | | # |
|---|
| 152 | | #def _app2key(key, prefix): |
|---|
| 153 | | # """ |
|---|
| 154 | | # Return the name of the key as it would have specified in the config file |
|---|
| 155 | | # """ |
|---|
| 156 | | # return prefix+key.replace('_','.') |
|---|
| 157 | | # |
|---|
| 158 | | #def _get_one_option_name(final, options, prefix, raise_on_no_match=True): |
|---|
| 159 | | # """ |
|---|
| 160 | | # Check that the ``final`` dictionary only contains one of the keys |
|---|
| 161 | | # specified in ``options``. If ``raise_on_no_match`` is ``True`` an |
|---|
| 162 | | # ``AuthKitConfigError`` is raised if none of the options are |
|---|
| 163 | | # present. |
|---|
| 164 | | # """ |
|---|
| 165 | | # found = None |
|---|
| 166 | | # for option in options: |
|---|
| 167 | | # if final.has_key(option): |
|---|
| 168 | | # if option != None: |
|---|
| 169 | | # found = option |
|---|
| 170 | | # else: |
|---|
| 171 | | # raise AuthKitConfigError( |
|---|
| 172 | | # 'You cannot specify both %r and %r'%( |
|---|
| 173 | | # _app2key(found, prefix), |
|---|
| 174 | | # _app2key(option, prefix), |
|---|
| 175 | | # ) |
|---|
| 176 | | # ) |
|---|
| 177 | | # if found: |
|---|
| 178 | | # return found |
|---|
| 179 | | # elif raise_on_no_match: |
|---|
| 180 | | # raise AuthKitConfigError( |
|---|
| 181 | | # 'Your AuthKit configuration reqiuires one of these options to be ' |
|---|
| 182 | | # 'set: %s'%', '.join([_app2key(x, prefix) for x in options]) |
|---|
| 183 | | # ) |
|---|
| 184 | | # return None |
|---|
| 185 | | # |
|---|
| 571 | | |
|---|
| 572 | | return app |
|---|
| 573 | | |
|---|
| 574 | | # XXXXX This stuff is never called now! |
|---|
| 575 | | |
|---|
| 576 | | # |
|---|
| 577 | | # Create the middleware, checking options as we go |
|---|
| 578 | | # |
|---|
| 579 | | |
|---|
| 580 | | |
|---|
| 581 | | |
|---|
| 582 | | |
|---|
| 583 | | |
|---|
| 584 | | # Sign in method |
|---|
| 585 | | methods = ['form','forward','basic','digest','passurl'] |
|---|
| 586 | | if custom_methods: |
|---|
| 587 | | for custom_method in custom_methods.keys(): |
|---|
| 588 | | methods.append(custom_method) |
|---|
| 589 | | |
|---|
| 590 | | if method not in ['forward','passurl']: |
|---|
| 591 | | app, valid, digest = get_authenticate_function(app, final, prefix, method) |
|---|
| 592 | | # define realm |
|---|
| 593 | | if method in ['basic','digest']: |
|---|
| 594 | | if not final.has_key('realm'): |
|---|
| 595 | | final['realm'] = 'AuthKit' |
|---|
| 596 | | # Set up the correct middleware |
|---|
| 597 | | if method == 'basic': |
|---|
| 598 | | from authkit.authenticate.basic import middleware |
|---|
| 599 | | app = MultiHandler(app) |
|---|
| 600 | | app.add_method( |
|---|
| 601 | | 'basic', |
|---|
| 602 | | middleware, |
|---|
| 603 | | final['realm'], |
|---|
| 604 | | valid |
|---|
| 605 | | ) |
|---|
| 606 | | app.add_checker('basic', status_checker) |
|---|
| 607 | | return app |
|---|
| 608 | | elif method == 'digest': |
|---|
| 609 | | from authkit.authenticate.digest import middleware |
|---|
| 610 | | app = MultiHandler(app) |
|---|
| 611 | | app.add_method( |
|---|
| 612 | | 'digest', |
|---|
| 613 | | middleware, |
|---|
| 614 | | final['realm'], |
|---|
| 615 | | digest |
|---|
| 616 | | ) |
|---|
| 617 | | app.add_checker('digest', status_checker) |
|---|
| 618 | | return app |
|---|
| 619 | | elif method == 'form': |
|---|
| 620 | | from authkit.authenticate.form import Form, template |
|---|
| 621 | | template = get_template(final, template, 'form_', prefix) |
|---|
| 622 | | if final.has_key('cookie_signout') and not \ |
|---|
| 623 | | final['cookie_signout'].startswith('/'): |
|---|
| 624 | | raise AuthKitConfigError( |
|---|
| 625 | | "The cookie signout path should start with a '/' character" |
|---|
| 626 | | ) |
|---|
| 627 | | app = MultiHandler(app) |
|---|
| 628 | | app.add_method('form', Form, authfunc=valid, template=template) |
|---|
| 629 | | app.add_checker('form', status_checker) |
|---|
| 630 | | app = load_cookie_middleware(app, final, prefix) |
|---|
| 631 | | return app |
|---|
| 632 | | elif method == 'forward': |
|---|
| 633 | | from authkit.authenticate.forward import Redirect, MyRecursive, RecursiveMiddleware |
|---|
| 634 | | app = MultiHandler(app) |
|---|
| 635 | | app.add_method( |
|---|
| 636 | | 'forward', |
|---|
| 637 | | Redirect, |
|---|
| 638 | | _get_value(final, 'signin', prefix) |
|---|
| 639 | | ) |
|---|
| 640 | | app.add_checker('forward', status_checker) |
|---|
| 641 | | app = MyRecursive(RecursiveMiddleware(app)) |
|---|
| 642 | | app = load_cookie_middleware(app, final, prefix) |
|---|
| 643 | | return app |
|---|
| 644 | | elif method=='passurl': |
|---|
| 645 | | # Note, the session middleware should already be setup by now |
|---|
| 646 | | # if we are not using beaker |
|---|
| 647 | | from authkit.authenticate.passurl import PassURLSignIn, AuthOpenIDHandler, template |
|---|
| 648 | | template = get_template(final, template, 'passurl_', prefix) |
|---|
| 649 | | app = MultiHandler(app) |
|---|
| 650 | | app.add_method( |
|---|
| 651 | | 'passurl', |
|---|
| 652 | | PassURLSignIn, |
|---|
| 653 | | template, |
|---|
| 654 | | path_verify=_get_value(final, 'passurl_path_verify', prefix, default='/verify'), |
|---|
| 655 | | baseurl=_get_value(final, 'passurl_baseurl', prefix, default='') |
|---|
| 656 | | ) |
|---|
| 657 | | app.add_checker('passurl', status_checker) |
|---|
| 658 | | urltouser = _get_value(final, 'passurl_urltouser', prefix, default=None) |
|---|
| 659 | | if isinstance(urltouser, str): |
|---|
| 660 | | urltouser = eval_import(urltouser) |
|---|
| 661 | | app = AuthOpenIDHandler( |
|---|
| 662 | | app, |
|---|
| 663 | | store_type=_get_value(final, 'passurl_store_type', prefix), |
|---|
| 664 | | store_config = _get_value(final, 'passurl_store_config', prefix), |
|---|
| 665 | | baseurl=_get_value(final, 'passurl_baseurl', prefix, default=''), |
|---|
| 666 | | path_signedin=_get_value(final, 'passurl_path_signedin', prefix), |
|---|
| 667 | | path_process=_get_value(final, 'passurl_path_process', prefix, default='/process'), |
|---|
| 668 | | template = template, |
|---|
| 669 | | urltouser = urltouser |
|---|
| 670 | | ) |
|---|
| 671 | | session_middleware = 'beaker.session' |
|---|
| 672 | | session_secret = 'asdasd' |
|---|
| 673 | | session_key = 'authkit_passurl' |
|---|
| 674 | | if session_middleware == 'beaker.session': |
|---|
| 675 | | if not session_secret: |
|---|
| 676 | | raise AuthKitConfigError('No session_secret set') |
|---|
| 677 | | from beaker.session import SessionMiddleware |
|---|
| 678 | | app = SessionMiddleware(app, key=session_key, secret=session_secret) |
|---|
| 679 | | |
|---|
| 680 | | app = load_cookie_middleware(app, final, prefix) |
|---|
| 681 | | return app |
|---|
| 682 | | else: |
|---|
| 683 | | raise NotImplementedError('No %r method has been implemented'%method) |
|---|
| 684 | | |
|---|
| 685 | | |
|---|
| | 398 | return app |
|---|