forked from atakanyenel/cwa-verification-fake
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
83 lines (70 loc) · 1.7 KB
/
main.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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"strconv"
"time"
)
const (
envDelayMillis = "CWA_FAKE_DELAY_MILLIS"
envIp = "CWA_FAKE_IP"
envPort = "CWA_FAKE_PORT"
)
type responseBody struct {
Tan string
}
func (resp responseBody) isValid(validtans []string) bool {
for _, t := range validtans {
if t == resp.Tan {
return true
}
}
return false
}
func loadDelayInMillis() int64 {
delayAsString := os.Getenv(envDelayMillis)
if delayAsString != "" {
delayMillis, err := strconv.ParseInt(delayAsString, 10, 64)
if err != nil {
log.Fatalf("Cannot parse '%s' of '%s' as int.", envDelayMillis, delayAsString)
}
return delayMillis
}
return 0
}
func main() {
validTans := []string{"edc07f08-a1aa-11ea-bb37-0242ac130002"}
serverAddress := os.Getenv(envIp)
if serverAddress == "" {
serverAddress = "0.0.0.0"
}
port := os.Getenv(envPort)
if port == "" {
port = "8004"
}
delayMillis := loadDelayInMillis()
http.HandleFunc("/version/v1/tan/verify", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
decoder := json.NewDecoder(r.Body)
var resp responseBody
if delayMillis > 0 {
time.Sleep(time.Duration(delayMillis) * time.Millisecond)
}
if err := decoder.Decode(&resp); err != nil {
http.Error(w, err.Error(), 404)
}
log.Printf("%+v", resp)
if resp.isValid(validTans) {
w.WriteHeader(200)
} else {
http.Error(w, "wrong tan", 404)
}
} else {
http.Error(w, "Only POST is supported", 404)
}
})
log.Printf("listening on %s, port %s, with delay in millis: %v", serverAddress, port, delayMillis)
log.Fatal(http.ListenAndServe(serverAddress+":"+port, nil)) //if serverAddress is "", listen all
}