Completed
Pull Request — master (#442)
by
unknown
02:29
created

Auth.run()   D

Complexity

Conditions 10

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 10
dl 0
loc 62
rs 4.6753

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
from st2actions.runners.pythonrunner import Action
19
import duo_client
20
21
22
class Auth(Action):
23
    def run(self, username, factor,
24
            ipaddr, device, push_type, passcode, push_info):
25
        """
26
        Auth against the Duo Platorm.
27
28
        Returns: An dict with info returned by Duo.
29
30
        Raises:
31
          ValueError: On Auth Failure.
32
        """
33
34
        try:
35
            ikey = self.config['auth']['ikey']
36
            skey = self.config['auth']['skey']
37
            host = self.config['auth']['host']
38
        except KeyError:
39
            raise ValueError("Duo config not found in config.")
40
41
        auth = duo_client.Auth(ikey=ikey,
42
                               skey=skey,
43
                               host=host)
44
45
        auth_kargs = {}
46
47
        if factor == "auto" or factor == "push":
48
            auth_kargs['type'] = push_type
49
            auth_kargs['device'] = device
50
51
            if ipaddr is not None:
52
                auth_kargs['ipaddr'] = ipaddr
53
54
            if push_info is not None:
55
                auth_kargs['push_info'] = push_info
56
        elif factor == "passcode":
57
            auth_kargs['passcode'] = passcode
58
        elif factor == "phone":
59
            auth_kargs['device'] = device
60
        elif factor == "sms":
61
            # As 'sms' just denies and then we do not support it
62
            # requires re-authentication.
63
64
            print "Denied, we do not support SMS!"
65
            raise ValueError("Denied, we do not support SMS!")
66
        else:
67
            raise ValueError("Invalid factor!")
68
69
        try:
70
            data = auth.auth(factor=factor,
71
                             username=username,
72
                             **auth_kargs)
73
        except RuntimeError, e:
74
            print "Error: %s" % e
75
            raise ValueError("Error: %s" % e)
76
        else:
77
            if data['status'] == "allow":
78
                return data
79
            elif data['status'] == "deny":
80
                print data['status_msg']
81
                raise ValueError("Duo login denied! {}".format(
82
                    data['status_msg']))
83
            else:
84
                raise ValueError("Invalid status")
85