/
main.go
58 lines (47 loc) · 1.39 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
package main
import (
"log"
"os"
"time"
"github.com/Mobilpadde/moths/v5/token"
"github.com/Mobilpadde/moths/v5/token/emojies"
)
func main() {
secret := os.Getenv("MOTHS_SECRET")
amount := 8
validationInterval := time.Second * 3
generationInterval := time.Second * 4
generationTicker := time.NewTicker(generationInterval)
validationTicker := time.NewTicker(validationInterval)
var err error
var gen *token.Generator
if gen, err = token.NewGenerator(
token.OptionWithSecret(secret),
token.OptionWithPeriod(generationInterval),
token.OptionWithAmount(amount),
token.OptionWithEmojies(emojies.CATS),
token.OptionWithTime(time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)),
); 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)
for {
log.Println()
code, err := gen.Next()
if err != nil {
log.Fatalln(err)
}
log.Printf(`Your code is "%s" and code is %s`, code.SpacedString(), code.Token())
<-validationTicker.C
log.Printf("Is this still valid after %s? %t", validationInterval, gen.Validate(code.String()))
<-validationTicker.C
log.Printf("Is this still valid after %s? %t", validationInterval*2, gen.Validate(code.String()))
<-generationTicker.C
}
}