| Conditions | 11 |
| Total Lines | 69 |
| Lines | 0 |
| Ratio | 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 Auth.run() 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 | #!/usr/bin/env python |
||
| 25 | def run(self, username, factor, |
||
| 26 | ipaddr, device, push_type, passcode, pushinfo): |
||
| 27 | """ |
||
| 28 | Auth against the Duo Platorm. |
||
| 29 | |||
| 30 | Returns: An dict with info returned by Duo. |
||
| 31 | |||
| 32 | Raises: |
||
| 33 | ValueError: 'Duo config not found in config' or 'Invalid factor' |
||
| 34 | RuntimeError: 'Failed auth.' |
||
| 35 | """ |
||
| 36 | |||
| 37 | try: |
||
| 38 | ikey = self.config['auth']['ikey'] |
||
| 39 | skey = self.config['auth']['skey'] |
||
| 40 | host = self.config['auth']['host'] |
||
| 41 | except KeyError: |
||
| 42 | raise ValueError("Duo config not found in config.") |
||
| 43 | |||
| 44 | auth = duo_client.Auth(ikey=ikey, |
||
| 45 | skey=skey, |
||
| 46 | host=host) |
||
| 47 | |||
| 48 | auth_kargs = {} |
||
| 49 | |||
| 50 | if factor == "auto" or factor == "push": |
||
| 51 | auth_kargs['type'] = push_type |
||
| 52 | auth_kargs['device'] = device |
||
| 53 | |||
| 54 | if ipaddr is not None: |
||
| 55 | auth_kargs['ipaddr'] = ipaddr |
||
| 56 | |||
| 57 | if pushinfo is not None: |
||
| 58 | info = {} |
||
| 59 | for value in pushinfo.split('; '): |
||
| 60 | (key, value) = value.split('=') |
||
| 61 | info[key] = value |
||
| 62 | |||
| 63 | encoded = urllib.urlencode(info) |
||
| 64 | auth_kargs['pushinfo'] = encoded |
||
| 65 | elif factor == "passcode": |
||
| 66 | auth_kargs['passcode'] = passcode |
||
| 67 | elif factor == "phone": |
||
| 68 | auth_kargs['device'] = device |
||
| 69 | elif factor == "sms": |
||
| 70 | # As 'sms' just denies and then we do not support it |
||
| 71 | # requires re-authentication. |
||
| 72 | |||
| 73 | print "Denied, we do not support SMS!" |
||
| 74 | raise ValueError("Denied, we do not support SMS!") |
||
| 75 | else: |
||
| 76 | raise ValueError("Invalid factor!") |
||
| 77 | |||
| 78 | try: |
||
| 79 | data = auth.auth(factor=factor, |
||
| 80 | username=username, |
||
| 81 | **auth_kargs) |
||
| 82 | except RuntimeError, e: |
||
| 83 | print "Error: %s" % e |
||
| 84 | raise RuntimeError("Error: %s" % e) |
||
| 85 | else: |
||
| 86 | if data['result'] == "allow": |
||
| 87 | return data |
||
| 88 | elif data['result'] == "deny": |
||
| 89 | print data['status_msg'] |
||
| 90 | raise RuntimeError("{}".format( |
||
| 91 | data['status_msg'])) |
||
| 92 | else: |
||
| 93 | raise RuntimeError("Invalid status") |
||
| 94 |