-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
challenges.go
212 lines (170 loc) · 5.61 KB
/
challenges.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package idp
import (
"errors"
"strings"
"fmt"
"github.com/neo4j/neo4j-go-driver/neo4j"
)
func CreateChallengeUsingTotp(tx neo4j.Transaction, challengeType ChallengeType, newChallenge Challenge) (challenge Challenge, err error) {
newChallenge.Code = "" // Do not set this on TOTP requests
challenge, err = createChallenge(tx, newChallenge, ChallengeAuthenticate)
if err != nil {
return Challenge{}, err
}
return challenge, nil
}
func CreateChallengeUsingOtp(tx neo4j.Transaction, challengeType ChallengeType, newChallenge Challenge) (challenge Challenge, otpCode ChallengeCode, err error) {
otpCode, err = CreateChallengeCode()
if err != nil {
return Challenge{}, ChallengeCode{}, err
}
hashedCode, err := CreatePassword(otpCode.Code)
if err != nil {
return Challenge{}, ChallengeCode{}, err
}
newChallenge.Code = hashedCode
challenge, err = createChallenge(tx, newChallenge, challengeType)
if err != nil {
return Challenge{}, ChallengeCode{}, err
}
return challenge, otpCode, nil
}
func createChallenge(tx neo4j.Transaction, newChallenge Challenge, challengeType ChallengeType) (challenge Challenge, err error) {
var result neo4j.Result
var cypher string
var params = make(map[string]interface{})
if newChallenge.Subject == "" {
return Challenge{}, errors.New("Missing Challenge.Subject")
}
params["sub"] = newChallenge.Subject
if newChallenge.Issuer == "" {
return Challenge{}, errors.New("Missing Challenge.Issuer")
}
params["iss"] = newChallenge.Issuer
params["exp"] = newChallenge.ExpiresAt
params["aud"] = newChallenge.Audience
if newChallenge.RedirectTo == "" {
return Challenge{}, errors.New("Missing Challenge.RedirectTo")
}
params["redirect_to"] = newChallenge.RedirectTo
params["code_type"] = newChallenge.CodeType
params["code"] = newChallenge.Code
cypData := ""
if newChallenge.Data != "" {
cypData = ", data:$data "
params["data"] = newChallenge.Data
}
cypChallengeType := ""
switch (challengeType) {
case ChallengeAuthenticate:
cypChallengeType = ":Authenticate"
case ChallengeRecover:
cypChallengeType = ":Recover"
case ChallengeDelete:
cypChallengeType = ":Delete"
case ChallengeEmailConfirm:
cypChallengeType = ":EmailConfirm"
case ChallengeEmailChange:
cypChallengeType = ":EmailChange"
default:
return Challenge{}, errors.New("Unsupported challenge type")
}
cypher = fmt.Sprintf(`
MATCH (i:Identity {id:$sub})
MERGE (c:Challenge%s {
id:randomUUID(), iat:datetime().epochSeconds, iss:$iss, exp:$exp, aud:$aud, sub:$sub,
redirect_to:$redirect_to,
code_type:$code_type, code:$code,
verified_at:0
%s
})-[:CHALLENGES]->(i)
WITH c
OPTIONAL MATCH (d:Challenge) WHERE id(c) <> id(d) AND d.exp < datetime().epochSeconds DETACH DELETE d
RETURN c
`, cypChallengeType, cypData)
if result, err = tx.Run(cypher, params); err != nil {
return Challenge{}, err
}
if result.Next() {
record := result.Record()
challengeNode := record.GetByIndex(0)
if challengeNode != nil {
challenge = marshalNodeToChallenge(challengeNode.(neo4j.Node))
}
} else {
return Challenge{}, errors.New("Unable to create Challenge")
}
logCypher(cypher, params)
// Check if we encountered any error during record streaming
if err = result.Err(); err != nil {
return Challenge{}, err
}
return challenge, nil
}
func FetchChallenges(tx neo4j.Transaction, iChallenges []Challenge) (challenges []Challenge, err error) {
var result neo4j.Result
var cypher string
var params = make(map[string]interface{})
cypfilterChallenges := ""
if len(iChallenges) > 0 {
var ids []string
for _, challenge := range iChallenges {
ids = append(ids, challenge.Id)
}
cypfilterChallenges = ` AND c.id in split($ids, ",") `
params["ids"] = strings.Join(ids, ",")
}
cypher = fmt.Sprintf(`
MATCH (c:Challenge) WHERE c.exp > datetime().epochSeconds %s
RETURN c
`, cypfilterChallenges)
if result, err = tx.Run(cypher, params); err != nil {
return nil, err
}
for result.Next() {
record := result.Record()
challengeNode := record.GetByIndex(0)
if challengeNode != nil {
i := marshalNodeToChallenge(challengeNode.(neo4j.Node))
challenges = append(challenges, i)
}
}
logCypher(cypher, params)
// Check if we encountered any error during record streaming
if err = result.Err(); err != nil {
return nil, err
}
return challenges, nil
}
func VerifyChallenge(tx neo4j.Transaction, challengeToUpdate Challenge) (updatedChallenge Challenge, err error) {
var result neo4j.Result
var cypher string
var params = make(map[string]interface{})
if challengeToUpdate.Id == "" {
return Challenge{}, errors.New("Missing Challenge.Id")
}
params["id"] = challengeToUpdate.Id
cypher = fmt.Sprintf(`
MATCH (c:Challenge {id:$id}) WHERE c.exp > datetime().epochSeconds
SET c.verified_at = datetime().epochSeconds
RETURN c
`)
if result, err = tx.Run(cypher, params); err != nil {
return Challenge{}, err
}
if result.Next() {
record := result.Record()
challengeNode := record.GetByIndex(0)
if challengeNode != nil {
updatedChallenge = marshalNodeToChallenge(challengeNode.(neo4j.Node))
}
} else {
return Challenge{}, errors.New("Unable to set Challenge verified. Hint: Challenge might be expired or non existant.")
}
logCypher(cypher, params)
// Check if we encountered any error during record streaming
if err = result.Err(); err != nil {
return Challenge{}, err
}
return updatedChallenge, nil
}