| Conditions | 15 |
| Total Lines | 94 |
| 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 | authorization=None, **kwargs): |
||
| 72 | auth_backend = self._auth_backend.__class__.__name__ |
||
| 73 | |||
| 74 | extra = {'auth_backend': auth_backend, 'remote_addr': remote_addr} |
||
| 75 | |||
| 76 | if not authorization: |
||
| 77 | LOG.audit('Authorization header not provided', extra=extra) |
||
| 78 | abort_request() |
||
| 79 | return |
||
| 80 | |||
| 81 | auth_type, auth_value = authorization |
||
| 82 | if auth_type.lower() not in ['basic']: |
||
| 83 | extra['auth_type'] = auth_type |
||
| 84 | LOG.audit('Unsupported authorization type: %s' % (auth_type), extra=extra) |
||
| 85 | abort_request() |
||
| 86 | return |
||
| 87 | |||
| 88 | try: |
||
| 89 | auth_value = base64.b64decode(auth_value) |
||
| 90 | except Exception: |
||
| 91 | LOG.audit('Invalid authorization header', extra=extra) |
||
| 92 | abort_request() |
||
| 93 | return |
||
| 94 | |||
| 95 | split = auth_value.split(':') |
||
| 96 | if len(split) != 2: |
||
| 97 | LOG.audit('Invalid authorization header', extra=extra) |
||
| 98 | abort_request() |
||
| 99 | return |
||
| 100 | |||
| 101 | username, password = split |
||
| 102 | result = self._auth_backend |
||
| 103 | |||
| 104 | result = self._auth_backend.authenticate(username=username, password=password) |
||
| 105 | if result is True: |
||
| 106 | LOG.audit(request.body) |
||
| 107 | ttl = getattr(request, 'ttl', None) |
||
| 108 | impersonate_user = getattr(request.body, 'user', None) |
||
| 109 | |||
| 110 | if impersonate_user is not None: |
||
| 111 | # check this is a service account |
||
| 112 | if not User.get_by_name(username).is_service: |
||
| 113 | message = "Current user is not a service and cannot " \ |
||
| 114 | "request impersonated tokens" |
||
| 115 | abort_request(status_code=http_client.BAD_REQUEST, |
||
| 116 | message=message) |
||
| 117 | return |
||
| 118 | username = impersonate_user |
||
| 119 | else: |
||
| 120 | impersonate_user = getattr(request, 'impersonate_user', None) |
||
| 121 | nickname_origin = getattr(request, 'nickname_origin', None) |
||
| 122 | if impersonate_user is not None: |
||
| 123 | try: |
||
| 124 | # check this is a service account |
||
| 125 | if not User.get_by_name(username).is_service: |
||
| 126 | raise NotServiceUserError() |
||
| 127 | username = User.get_by_nickname(impersonate_user, |
||
| 128 | nickname_origin).name |
||
| 129 | except NotServiceUserError: |
||
| 130 | message = "Current user is not a service and cannot " \ |
||
| 131 | "request impersonated tokens" |
||
| 132 | abort_request(status_code=http_client.BAD_REQUEST, |
||
| 133 | message=message) |
||
| 134 | return |
||
| 135 | except UserNotFoundError: |
||
| 136 | message = "Could not locate user %s@%s" % \ |
||
| 137 | (impersonate_user, nickname_origin) |
||
| 138 | abort_request(status_code=http_client.BAD_REQUEST, |
||
| 139 | message=message) |
||
| 140 | return |
||
| 141 | except NoNicknameOriginProvidedError: |
||
| 142 | message = "Nickname origin is not provided for nickname '%s'" % \ |
||
| 143 | impersonate_user |
||
| 144 | abort_request(status_code=http_client.BAD_REQUEST, |
||
| 145 | message=message) |
||
| 146 | return |
||
| 147 | except AmbiguousUserError: |
||
| 148 | message = "%s@%s matched more than one username" % \ |
||
| 149 | (impersonate_user, nickname_origin) |
||
| 150 | abort_request(status_code=http_client.BAD_REQUEST, |
||
| 151 | message=message) |
||
| 152 | return |
||
| 153 | try: |
||
| 154 | token = self._create_token_for_user( |
||
| 155 | username=username, ttl=ttl) |
||
| 156 | return token |
||
| 157 | except TTLTooLargeException as e: |
||
| 158 | abort_request(status_code=http_client.BAD_REQUEST, |
||
| 159 | message=e.message) |
||
| 160 | return |
||
| 161 | |||
| 162 | LOG.audit('Invalid credentials provided', extra=extra) |
||
| 163 | abort_request() |
||
| 164 |