-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathduoauth.go
114 lines (99 loc) · 2.92 KB
/
duoauth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"net/url"
duoapi "github.com/duosecurity/duo_api_golang"
"github.com/duosecurity/duo_api_golang/authapi"
"gopkg.in/ini.v1"
)
type duoCredentials struct {
name string
integration string
secret string
hostname string
lastAuthTime int64
lastIP string
}
func duoReadConfig(cfgFile string, name string) (duoCredentials, error) {
var duoCred duoCredentials
var err error
cfg, err := ini.Load(cfgFile)
if err != nil {
err = fmt.Errorf("Fail to read file: %v", err)
return duoCred, err
}
sectionType := cfg.Section(name).Key("type").String()
if "duo" == sectionType {
duoCred.name = name
duoCred.integration = cfg.Section(name).Key("integration").String()
duoCred.secret = cfg.Section(name).Key("secret").String()
duoCred.hostname = cfg.Section(name).Key("hostname").String()
}
if 0 == len(duoCred.name) {
err = fmt.Errorf("[%s] Duo Config: Invalid user name", name)
} else if len(duoCred.integration) < 15 {
err = fmt.Errorf("[%s] Duo Config: Invalid integration", name)
} else if len(duoCred.secret) < 15 {
err = fmt.Errorf("[%s] Duo Config: Invalid secret", name)
} else if len(duoCred.hostname) < 15 {
err = fmt.Errorf("[%s] Duo Config: Invalid hostname", name)
}
return duoCred, err
}
func duoCheck(duoCred duoCredentials) (bool, error) {
var err error
duoClient := duoapi.NewDuoApi(duoCred.integration, duoCred.secret, duoCred.hostname, "go-client")
if duoClient == nil {
err = fmt.Errorf("Error #100: Failed to create new Duo Api")
return false, err
}
duoAuthClient := authapi.NewAuthApi(*duoClient)
check, err := duoAuthClient.Check()
if err != nil {
err = fmt.Errorf("Error #150: %s", err)
return false, err
}
if check == nil {
err = fmt.Errorf("Error #155: 'check' is nil")
return false, err
}
var msg, detail string
if check.StatResult.Message != nil {
msg = *check.StatResult.Message
}
if check.StatResult.Message_Detail != nil {
detail = *check.StatResult.Message_Detail
}
if check.StatResult.Stat != "OK" {
err = fmt.Errorf("Error #180: Could not connect to Duo: %q (%q)", msg, detail)
return false, err
}
duoUser := duoCred.name
options := []func(*url.Values){authapi.AuthUsername(duoUser)}
options = append(options, authapi.AuthDevice("auto"))
result, err := duoAuthClient.Auth("push", options...)
if err != nil {
err = fmt.Errorf("Error #200: %s", err)
return false, err
}
if result == nil {
err = fmt.Errorf("Error #220: 'result' is nil")
return false, err
}
if false {
fmt.Println("result:", result)
fmt.Println("-----------------------")
fmt.Println("result.StatResult:", result.StatResult)
fmt.Println("-----------------------")
fmt.Println("result.Response:", result.Response)
}
success := false
if result.StatResult.Stat == "OK" && result.Response.Result == "allow" {
success = true
}
if !success {
err = fmt.Errorf("Error #230: 'success' is false")
return false, err
}
return true, nil
}