-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
96 lines (86 loc) · 2.19 KB
/
auth.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
package main
import "github.com/mehmooda/acme_client/acme"
import "os"
func PerformAuth(domain string, c *acme.Client, a *acme.Resource) bool {
for n, combin := range a.Authorization.Combinations {
completable := true
for _, chal := range combin {
if !CanCompleteChallenge(domain, a.Authorization.Challenges[chal].Type) {
LogV("Combination", n, "Can not perform", a.Authorization.Challenges[chal].Type)
completable = false
break
}
LogV("Combination", n, "Can perform", a.Authorization.Challenges[chal].Type)
}
if completable {
PerformCombination(domain, c, a, n)
continue
}
}
LogE("No combinations were completeable")
return false
}
func CanCompleteChallenge(domain string, chaltype string) bool {
switch chaltype {
case "http-01":
_, ok := GLOBAL.HOST[domain]["HTTP01"]
if ok {
// Test if we can actually complete the challenge
return true
}
return false
default:
return false
}
}
func PerformCombination(domain string, client *acme.Client, auth *acme.Resource, n int) bool {
for _, chaln := range auth.Authorization.Combinations[n] {
challenge := &auth.Authorization.Challenges[chaln]
switch challenge.Type {
case "http-01":
filename, contents, err := challenge.HTTP01(client)
if err != nil {
LogE(err)
return false
}
if !ChallengeHTTP01CreateFile(domain, filename, contents) {
return false
}
defer ChallengeHTTP01DeleteFile(domain, filename)
default:
return false
}
LogV("Asking for Verification")
err := client.ChallengeAccept(challenge)
if err != nil {
LogE(err)
return false
}
LogV("Polling for Verification")
err = client.ChallengePoll(challenge)
if err != nil {
LogE(err)
return false
}
LogV("Challenge Complete")
}
return true
}
func ChallengeHTTP01CreateFile(domain string, filename string, contents string) bool {
base := GLOBAL.HOST[domain]["HTTP01"]
file, err := os.Create(base + "/" + filename)
if err != nil {
LogE(err)
return false
}
file.Write([]byte(contents))
file.Close()
return true
}
func ChallengeHTTP01DeleteFile(domain string, filename string) {
base := GLOBAL.HOST[domain]["HTTP01"]
err := os.Remove(base + "/" + filename)
if err != nil {
LogE(err)
}
}