Conditions | 15 |
Total Lines | 93 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 TokenController._handle_standalone_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 |
||
92 | def _handle_standalone_auth(self, request, **kwargs): |
||
93 | authorization = pecan.request.authorization |
||
94 | |||
95 | auth_backend = self._auth_backend.__class__.__name__ |
||
96 | remote_addr = pecan.request.remote_addr |
||
97 | extra = {'auth_backend': auth_backend, 'remote_addr': remote_addr} |
||
98 | |||
99 | if not authorization: |
||
100 | LOG.audit('Authorization header not provided', extra=extra) |
||
101 | self._abort_request() |
||
102 | return |
||
103 | |||
104 | auth_type, auth_value = authorization |
||
105 | if auth_type.lower() not in ['basic']: |
||
106 | extra['auth_type'] = auth_type |
||
107 | LOG.audit('Unsupported authorization type: %s' % (auth_type), extra=extra) |
||
108 | self._abort_request() |
||
109 | return |
||
110 | |||
111 | try: |
||
112 | auth_value = base64.b64decode(auth_value) |
||
113 | except Exception: |
||
114 | LOG.audit('Invalid authorization header', extra=extra) |
||
115 | self._abort_request() |
||
116 | return |
||
117 | |||
118 | split = auth_value.split(':') |
||
119 | if len(split) != 2: |
||
120 | LOG.audit('Invalid authorization header', extra=extra) |
||
121 | self._abort_request() |
||
122 | return |
||
123 | |||
124 | username, password = split |
||
125 | result = self._auth_backend |
||
126 | |||
127 | result = self._auth_backend.authenticate(username=username, password=password) |
||
128 | if result is True: |
||
129 | ttl = getattr(request, 'ttl', None) |
||
130 | impersonate_user = getattr(request, 'user', None) |
||
131 | |||
132 | if impersonate_user is not None: |
||
133 | # check this is a service account |
||
134 | if not User.get_by_name(username).is_service: |
||
135 | message = "Current user is not a service and cannot " \ |
||
136 | "request impersonated tokens" |
||
137 | self._abort_request(status_code=http_client.BAD_REQUEST, |
||
138 | message=message) |
||
139 | return |
||
140 | username = impersonate_user |
||
141 | else: |
||
142 | impersonate_user = getattr(request, 'impersonate_user', None) |
||
143 | nickname_origin = getattr(request, 'nickname_origin', None) |
||
144 | if impersonate_user is not None: |
||
145 | try: |
||
146 | # check this is a service account |
||
147 | if not User.get_by_name(username).is_service: |
||
148 | raise NotServiceUserError() |
||
149 | username = User.get_by_nickname(impersonate_user, |
||
150 | nickname_origin).name |
||
151 | except NotServiceUserError: |
||
152 | message = "Current user is not a service and cannot " \ |
||
153 | "request impersonated tokens" |
||
154 | self._abort_request(status_code=http_client.BAD_REQUEST, |
||
155 | message=message) |
||
156 | return |
||
157 | except UserNotFoundError: |
||
158 | message = "Could not locate user %s@%s" % \ |
||
159 | (impersonate_user, nickname_origin) |
||
160 | self._abort_request(status_code=http_client.BAD_REQUEST, |
||
161 | message=message) |
||
162 | return |
||
163 | except NoNicknameOriginProvidedError: |
||
164 | message = "Nickname origin is not provided for nickname '%s'" % \ |
||
165 | impersonate_user |
||
166 | self._abort_request(status_code=http_client.BAD_REQUEST, |
||
167 | message=message) |
||
168 | return |
||
169 | except AmbiguousUserError: |
||
170 | message = "%s@%s matched more than one username" % \ |
||
171 | (impersonate_user, nickname_origin) |
||
172 | self._abort_request(status_code=http_client.BAD_REQUEST, |
||
173 | message=message) |
||
174 | return |
||
175 | try: |
||
176 | token = self._create_token_for_user(username=username, ttl=ttl) |
||
177 | return self._process_successful_response(token=token) |
||
178 | except TTLTooLargeException as e: |
||
179 | self._abort_request(status_code=http_client.BAD_REQUEST, |
||
180 | message=e.message) |
||
181 | return |
||
182 | |||
183 | LOG.audit('Invalid credentials provided', extra=extra) |
||
184 | self._abort_request() |
||
185 | |||
198 |