-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (86 loc) · 2.64 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"github.com/unickorn/golem-poll-manager/auth"
poll "github.com/unickorn/golem-poll-manager/manager_out"
"github.com/unickorn/golem-poll-manager/verify"
)
func main() {
}
func init() {
i := GolemPollManager{}
poll.SetExportsIlhanGolemPollManagerApi(i)
// Mailjet API Key and Secret
auth.Initialize("xxxxxxx", "xxxxxxx")
// JWT HMAC Secret (unfortunately symmetric for now, RSA is not fully supported yet on tinygo)
verify.Initialize("super-secret-hmac-secret")
}
// GolemPollManager is the implementation of the manager interface of Golem Poll, a poll template for Golem Cloud.
type GolemPollManager struct {
}
// Login sends an authentication email to the user.
func (g GolemPollManager) Login(email string) poll.Result[string, string] {
err := auth.SendAuthenticationEmail(email)
if err != nil {
return poll.Result[string, string]{
Kind: poll.Err,
Err: fmt.Sprintf("Failed to send authentication email: %s", err.Error()),
}
}
return poll.Result[string, string]{
Kind: poll.Ok,
Val: fmt.Sprintf("Authentication email sent to %s!", email),
}
}
// Verify verifies the user's secret code and returns the access and refresh tokens.
func (g GolemPollManager) Verify(code string) poll.Result[string, string] {
if mail, ok := auth.VerifyAuthentication(code); ok {
tokens, err := verify.CreateTokens(mail)
if err != nil {
return poll.Result[string, string]{
Kind: poll.Err,
Err: fmt.Sprintf("Failed to create tokens: %s", err.Error()),
}
}
return poll.Result[string, string]{
Kind: poll.Ok,
Val: tokens,
}
}
return poll.Result[string, string]{
Kind: poll.Err,
Err: "Invalid code, authentication failed!",
}
}
// Refresh refreshes the user's access token.
func (g GolemPollManager) Refresh(email string, refreshToken string) poll.Result[string, string] {
tokens, err := verify.RefreshToken(email, refreshToken)
if err != nil {
return poll.Result[string, string]{
Kind: poll.Err,
Err: fmt.Sprintf("Failed to refresh tokens: %s", err.Error()),
}
}
return poll.Result[string, string]{
Kind: poll.Ok,
Val: tokens,
}
}
// Logout invalidates the user's refresh token.
func (g GolemPollManager) Logout(accessToken string) poll.Result[struct{}, string] {
err := verify.InvalidateToken(accessToken)
if err != nil {
return poll.Result[struct{}, string]{
Kind: poll.Err,
Err: fmt.Sprintf("Failed to invalidate token: %s", err.Error()),
}
}
return poll.Result[struct{}, string]{
Kind: poll.Ok,
Val: struct{}{},
}
}
// Validate validates the user's access token.
func (g GolemPollManager) Validate(accessToken string) bool {
return verify.CheckToken(accessToken) == nil
}