-
Notifications
You must be signed in to change notification settings - Fork 1
/
duo.go
65 lines (54 loc) · 1.37 KB
/
duo.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
package main
import (
"fmt"
"net/http"
"net/url"
"time"
"github.com/cloudflare/golibs/lrucache"
duoapi "github.com/duosecurity/duo_api_golang"
"github.com/duosecurity/duo_api_golang/authapi"
)
var (
duoAuthAuth func(factor string, options ...func(*url.Values)) (*authapi.AuthResult, error)
duoCache = lrucache.NewMultiLRUCache(8, 64)
)
func init() {
go func() {
for {
duoCache.Expire()
time.Sleep(time.Hour)
}
}()
duoAuth := authapi.NewAuthApi(*duoapi.NewDuoApi(*duoIKey, *duoSKey, *duoHost, *duoUA))
duoAuthAuth = duoAuth.Auth
}
func duo(user string, host string, req *http.Request) error {
if *duoSkip {
return nil
}
clientIP := getClientIP(req)
cacheKey := clientIP + user
_, cached := duoCache.GetQuiet(cacheKey)
if cached {
return nil
}
vals := []func(*url.Values){
authapi.AuthDevice("auto"),
authapi.AuthUsername(user),
authapi.AuthIpAddr(clientIP),
authapi.AuthType(host),
authapi.AuthPushinfo("Host=" + host + "&UserAgent=" + req.UserAgent()),
}
res, err := duoAuthAuth("push", vals...)
if err != nil {
return fmt.Errorf("[duo] %s", err.Error())
}
if res.Stat == "FAIL" {
return fmt.Errorf("[duo] %s: %s", *res.StatResult.Message, *res.StatResult.Message_Detail)
}
if res.Response.Status == "allow" {
duoCache.Set(cacheKey, 1, time.Now().Add(*duoTTL))
return nil
}
return fmt.Errorf("[duo] %s", res.Response.Status_Msg)
}