-
Notifications
You must be signed in to change notification settings - Fork 4
/
challenge.go
165 lines (151 loc) · 4.49 KB
/
challenge.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"context"
"encoding/binary"
"encoding/hex"
"fmt"
"time"
"github.com/agl/ed25519"
couch "github.com/fjl/go-couchdb"
"github.com/juju/errors"
"golang.org/x/time/rate"
)
type alldocsResult struct {
Rows []map[string]interface{}
}
// attempt to loop through users and delete last challenges
// no error handling here as it is not mission critical
// conflicts etc are fine, just move on to the next user
func deleteLastChallenges(ctx context.Context) {
limit := rate.NewLimiter(rate.Every(time.Second), 2)
var result alldocsResult
for limit.Wait(ctx) == nil { // ends on context close
usersDB.AllDocs(&result, couch.Options{"include_docs": true})
for _, row := range result.Rows {
docInterface, ok := row["doc"]
if !ok {
continue
}
doc, ok := docInterface.(map[string]interface{})
if !ok {
continue
}
lastChallenge, ok := doc["lastChallenge"]
if !ok {
continue
}
// Go only lets us cast this to a float64, even though it was originally an int64
lastChallengeFloat64, ok := lastChallenge.(float64)
if !ok {
continue
}
if lastChallengeFloat64 == 0 {
continue
}
// check if it was more than 6 mins ago
if time.Unix(0, int64(lastChallengeFloat64)).Before(time.Now().Add(time.Minute * -6)) {
idInterface, ok := row["id"]
if !ok {
continue
}
id, ok := idInterface.(string)
if !ok {
continue
}
revInterface, ok := doc["_rev"]
if !ok {
continue
}
rev, ok := revInterface.(string)
if !ok {
continue
}
doc["lastChallenge"] = int64(0)
_, _ = usersDB.Put(id, doc, rev)
continue
}
}
}
}
// keep this in parity with the identical function in proxy
func validateChallenge(signedChallenge []byte) error {
// 64 byte signature, 8 byte challenge
if len(signedChallenge) != 72 {
return fmt.Errorf("Signed challenge is of wrong length: %v", len(signedChallenge))
}
challenge := signedChallenge[64:] // first 64 bytes are sig
challengeInt := binary.BigEndian.Uint64(challenge)
challengeInt64 := int64(challengeInt)
if challengeInt64 == 0 { // zero doesn't play nice with the time package
return errTimeWindow
}
if time.Unix(0, challengeInt64).After(time.Now().Add(time.Minute * 3)) {
return errTimeWindow
}
if time.Unix(0, challengeInt64).Before(time.Now().Add(time.Minute * -3)) {
return errTimeWindow
}
return nil
}
// pass in the loginKey
// keep this in parity with the identical function in proxy
func verifyChallenge(identifier string, signedChallenge []byte) (bool, error) {
var challengeData map[string]interface{}
err := usersDB.Get(identifier, &challengeData, nil)
if err != nil {
if couch.NotFound(err) {
return false, errUserNotFound
}
return false, err
}
pubKey, ok1 := challengeData["publicKey"]
pubKeyString, ok2 := pubKey.(string)
if !(ok1 && ok2) {
err = errors.New("Problem reading PublicKey from database")
log.Error(errors.Trace(err), challengeData)
return false, err
}
pubBytes, err := hex.DecodeString(pubKeyString)
if err != nil {
log.Error(errors.Trace(err), pubKeyString)
return false, errors.New("Problem decoding hex of PublicKey")
}
var pubByteArray [32]byte
var sigByteArray [64]byte
copy(pubByteArray[:], pubBytes[:])
copy(sigByteArray[:], signedChallenge[:])
if !ed25519.Verify(&pubByteArray, signedChallenge[64:], &sigByteArray) {
return false, errChallengeSig
}
lastChallenge, ok1 := challengeData["lastChallenge"]
// Go only lets us cast this to a float64, even though it was originally an int64
lastChallengeFloat64, ok2 := lastChallenge.(float64)
if !(ok1 && ok2) {
err = errors.New("Problem reading lastChallenge from database")
log.Error(errors.Trace(err), challengeData)
return false, err
}
challenge := signedChallenge[64:] // first 64 bytes are sig
challengeUInt := binary.BigEndian.Uint64(challenge)
challengeInt64 := int64(challengeUInt)
if challengeInt64 <= int64(lastChallengeFloat64) {
return false, errChallengeUsed
}
challengeData["lastChallenge"] = challengeInt64
rev, ok1 := challengeData["_rev"]
revString, ok2 := rev.(string)
if !(ok1 && ok2) {
err = errors.New("Problem reading rev from database")
log.Error(errors.Trace(err), challengeData)
return false, err
}
_, err = usersDB.Put(identifier, challengeData, revString)
if err != nil {
if couch.Conflict(err) {
return verifyChallenge(identifier, signedChallenge) // try again
}
log.Error(errors.Trace(err), challengeData)
return false, errors.New("Problem updating last challenge")
}
return true, nil
}