Completed
Pull Request — master (#449)
by Manas
03:14 queued 32s
created

Auth.run()   F

Complexity

Conditions 12

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 12
dl 0
loc 51
rs 2.8003

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

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
2
3
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
4
# contributor license agreements.  See the NOTICE file distributed with
5
# this work for additional information regarding copyright ownership.
6
# The ASF licenses this file to You under the Apache License, Version 2.0
7
# (the "License"); you may not use this file except in compliance with
8
# the License.  You may obtain a copy of the License at
9
#
10
#     http://www.apache.org/licenses/LICENSE-2.0
11
#
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
17
18
import urllib
19
20
from lib.actions import AuthAction
21
22
23
class Auth(AuthAction):
24
    def run(self, username, factor,
25
            ipaddr, device, push_type, passcode, pushinfo):
26
        """
27
        Auth against the Duo Platorm.
28
29
        Returns: An dict with info returned by Duo.
30
31
        Raises:
32
          ValueError: 'Duo config not found in config' or 'Invalid factor'
33
          RuntimeError: 'Failed auth.'
34
        """
35
36
        auth_kargs = {}
37
38
        if factor == "auto" or factor == "push":
39
            auth_kargs['type'] = push_type
40
            auth_kargs['device'] = device
41
42
            if ipaddr is not None:
43
                auth_kargs['ipaddr'] = ipaddr
44
45
            if pushinfo is not None:
46
                encoded = urllib.urlencode(pushinfo)
47
                auth_kargs['pushinfo'] = encoded
48
        elif factor == "passcode":
49
            auth_kargs['passcode'] = passcode
50
        elif factor == "phone":
51
            auth_kargs['device'] = device
52
        elif factor == "sms":
53
            # As 'sms' just denies and then we do not support it
54
            # requires re-authentication.
55
56
            raise ValueError("Denied, we do not support SMS!")
57
        else:
58
            raise ValueError("Invalid factor!")
59
60
        try:
61
            data = self.duo_auth.auth(factor=factor,
62
                                      username=username,
63
                                      **auth_kargs)
64
        except RuntimeError, e:
65
            raise RuntimeError("Error: %s" % e)
66
        else:
67
            if data['result'] == "allow":
68
                return data
69
            elif data['result'] == "deny":
70
                self.send_user_error(data['status_msg'])
71
                raise RuntimeError("{}".format(
72
                    data['status_msg']))
73
            else:
74
                raise RuntimeError("Invalid status")
75