-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (68 loc) · 2.1 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 (
"log"
"os"
"time"
"github.com/Mobilpadde/moths/v6/token"
"github.com/Mobilpadde/moths/v6/token/emojies"
"github.com/Mobilpadde/moths/v6/token/option"
)
func main() {
secret := os.Getenv("MOTHS_SECRET") // created as specified in the README: https://github.com/Mobilpadde/moths#how-
amount := 8 // we want to generate a code with 8 emojies
validationInterval := time.Second * 3
generationInterval := time.Second * 4
// Instantiate a new generator
gen, err := token.NewGenerator(
option.OptionWithSecret(secret),
option.OptionWithPeriod(generationInterval),
option.OptionWithAmount(amount),
option.OptionWithEmojies(emojies.CATS),
option.OptionWithTime(time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)),
)
if err != nil {
log.Fatalln(err)
}
// Check if everything is working
if err := gen.Check(); err != nil {
log.Fatalln(err)
}
// Exports the generator's options
s := gen.Export()
// Instantiate a second generator
gen2, _ := token.NewGenerator()
// Import the encoded options into the new generator
if err = gen2.Import(s); err != nil {
log.Println(err)
}
// Check if everything is still working
if err := gen2.Check(); err != nil {
log.Fatalln(err)
}
log.Printf("Setup with validation complete")
log.Printf(
"A new code is generated every %s, and validation happens %s after generation",
generationInterval,
validationInterval,
)
log.Printf("Every code is %d emoji long", amount)
// Flow:
//
// 10 Validate a code after 3 seconds (validationInterval)
// 20 Try validating the code again after 3 more seconds (validationInterval)
// 30 Generate a new code
//
// 40 GOTO 10
for {
log.Println()
code, err := gen2.Next()
if err != nil {
log.Fatalln(err)
}
log.Printf(`Your code is "%s" and code is %s`, code.SpacedString(), code.Token())
<-time.NewTicker(validationInterval).C
log.Printf("Is this still valid after %s? %t", validationInterval, gen2.Validate(code.String()))
<-time.NewTicker(validationInterval).C
log.Printf("Is this still valid after %s? %t", validationInterval*2, gen2.Validate(code.String()))
}
}