| Conditions | 15 |
| Total Lines | 96 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like StandaloneAuthHandler.handle_auth() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
| 70 | def handle_auth(self, request, headers=None, remote_addr=None, remote_user=None, |
||
| 71 | **kwargs): |
||
| 72 | authorization = pecan.request.authorization |
||
| 73 | |||
| 74 | auth_backend = self._auth_backend.__class__.__name__ |
||
| 75 | |||
| 76 | extra = {'auth_backend': auth_backend, 'remote_addr': remote_addr} |
||
| 77 | |||
| 78 | if not authorization: |
||
| 79 | LOG.audit('Authorization header not provided', extra=extra) |
||
| 80 | abort_request() |
||
| 81 | return |
||
| 82 | |||
| 83 | auth_type, auth_value = authorization |
||
| 84 | if auth_type.lower() not in ['basic']: |
||
| 85 | extra['auth_type'] = auth_type |
||
| 86 | LOG.audit('Unsupported authorization type: %s' % (auth_type), extra=extra) |
||
| 87 | abort_request() |
||
| 88 | return |
||
| 89 | |||
| 90 | try: |
||
| 91 | auth_value = base64.b64decode(auth_value) |
||
| 92 | except Exception: |
||
| 93 | LOG.audit('Invalid authorization header', extra=extra) |
||
| 94 | abort_request() |
||
| 95 | return |
||
| 96 | |||
| 97 | split = auth_value.split(':') |
||
| 98 | if len(split) != 2: |
||
| 99 | LOG.audit('Invalid authorization header', extra=extra) |
||
| 100 | abort_request() |
||
| 101 | return |
||
| 102 | |||
| 103 | username, password = split |
||
| 104 | result = self._auth_backend |
||
| 105 | |||
| 106 | result = self._auth_backend.authenticate(username=username, password=password) |
||
| 107 | if result is True: |
||
| 108 | LOG.audit(request.body) |
||
| 109 | ttl = getattr(request, 'ttl', None) |
||
| 110 | impersonate_user = getattr(request.body, 'user', None) |
||
| 111 | |||
| 112 | if impersonate_user is not None: |
||
| 113 | # check this is a service account |
||
| 114 | if not User.get_by_name(username).is_service: |
||
| 115 | message = "Current user is not a service and cannot " \ |
||
| 116 | "request impersonated tokens" |
||
| 117 | abort_request(status_code=http_client.BAD_REQUEST, |
||
| 118 | message=message) |
||
| 119 | return |
||
| 120 | username = impersonate_user |
||
| 121 | else: |
||
| 122 | impersonate_user = getattr(request, 'impersonate_user', None) |
||
| 123 | nickname_origin = getattr(request, 'nickname_origin', None) |
||
| 124 | if impersonate_user is not None: |
||
| 125 | try: |
||
| 126 | # check this is a service account |
||
| 127 | if not User.get_by_name(username).is_service: |
||
| 128 | raise NotServiceUserError() |
||
| 129 | username = User.get_by_nickname(impersonate_user, |
||
| 130 | nickname_origin).name |
||
| 131 | except NotServiceUserError: |
||
| 132 | message = "Current user is not a service and cannot " \ |
||
| 133 | "request impersonated tokens" |
||
| 134 | abort_request(status_code=http_client.BAD_REQUEST, |
||
| 135 | message=message) |
||
| 136 | return |
||
| 137 | except UserNotFoundError: |
||
| 138 | message = "Could not locate user %s@%s" % \ |
||
| 139 | (impersonate_user, nickname_origin) |
||
| 140 | abort_request(status_code=http_client.BAD_REQUEST, |
||
| 141 | message=message) |
||
| 142 | return |
||
| 143 | except NoNicknameOriginProvidedError: |
||
| 144 | message = "Nickname origin is not provided for nickname '%s'" % \ |
||
| 145 | impersonate_user |
||
| 146 | abort_request(status_code=http_client.BAD_REQUEST, |
||
| 147 | message=message) |
||
| 148 | return |
||
| 149 | except AmbiguousUserError: |
||
| 150 | message = "%s@%s matched more than one username" % \ |
||
| 151 | (impersonate_user, nickname_origin) |
||
| 152 | abort_request(status_code=http_client.BAD_REQUEST, |
||
| 153 | message=message) |
||
| 154 | return |
||
| 155 | try: |
||
| 156 | token = self._create_token_for_user( |
||
| 157 | username=username, ttl=ttl) |
||
| 158 | return token |
||
| 159 | except TTLTooLargeException as e: |
||
| 160 | abort_request(status_code=http_client.BAD_REQUEST, |
||
| 161 | message=e.message) |
||
| 162 | return |
||
| 163 | |||
| 164 | LOG.audit('Invalid credentials provided', extra=extra) |
||
| 165 | abort_request() |
||
| 166 |