| 170 | super(StandaloneAuthHandler, self).__init__(*args, **kwargs) |
| 171 | |
| 172 | def handle_auth( |
| 173 | self, |
| 174 | request, |
| 175 | headers=None, |
| 176 | remote_addr=None, |
| 177 | remote_user=None, |
| 178 | authorization=None, |
| 179 | **kwargs, |
| 180 | ): |
| 181 | auth_backend = self._auth_backend.__class__.__name__ |
| 182 | |
| 183 | extra = {"auth_backend": auth_backend, "remote_addr": remote_addr} |
| 184 | |
| 185 | if not authorization: |
| 186 | LOG.audit("Authorization header not provided", extra=extra) |
| 187 | abort_request() |
| 188 | return |
| 189 | |
| 190 | auth_type, auth_value = authorization |
| 191 | if auth_type.lower() not in ["basic"]: |
| 192 | extra["auth_type"] = auth_type |
| 193 | LOG.audit("Unsupported authorization type: %s" % (auth_type), extra=extra) |
| 194 | abort_request() |
| 195 | return |
| 196 | |
| 197 | try: |
| 198 | auth_value = base64.b64decode(auth_value) |
| 199 | except Exception: |
| 200 | LOG.audit("Invalid authorization header", extra=extra) |
| 201 | abort_request() |
| 202 | return |
| 203 | |
| 204 | split = auth_value.split(b":", 1) |
| 205 | if len(split) != 2: |
| 206 | LOG.audit("Invalid authorization header", extra=extra) |
| 207 | abort_request() |
| 208 | return |
| 209 | |
| 210 | username, password = split |
| 211 | |
| 212 | if six.PY3 and isinstance(username, six.binary_type): |
| 213 | username = username.decode("utf-8") |
| 214 | |
| 215 | if six.PY3 and isinstance(password, six.binary_type): |
| 216 | password = password.decode("utf-8") |
| 217 | |
| 218 | result = self._auth_backend.authenticate(username=username, password=password) |
| 219 | |
| 220 | if result is True: |
| 221 | ttl = getattr(request, "ttl", None) |
| 222 | username = self._get_username_for_request(username, request) |
| 223 | try: |
| 224 | token = self._create_token_for_user(username=username, ttl=ttl) |
| 225 | except TTLTooLargeException as e: |
| 226 | abort_request( |
| 227 | status_code=http_client.BAD_REQUEST, message=six.text_type(e) |
| 228 | ) |
| 229 | return |