-
Notifications
You must be signed in to change notification settings - Fork 1
/
temporaryLinks.go
184 lines (158 loc) · 5.12 KB
/
temporaryLinks.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
package main
import (
"context"
"fmt"
"log"
"net/http"
"time"
redisutil "github.com/eth-library/dataset-dj/redisutil"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
type singleUseLink struct {
// ID string `bson:"_id"` // will use the _id returned from Mongo to create a link
Used bool `bson:"used"` // set to True when visited. Then delete this document
ExpireAt time.Time `bson:"expireAt"` //after this time MongoDB will expire this document
Permission string `bson:"permission"` //purpose that this link has e.g. create createAPIToken
OwnerID int `bson:"ownerID"`
Email string `bson:"email"`
}
// EmailRequestBody for binding email field in a a json body
type EmailRequestBody struct {
Email string `json:"email"`
}
// EmailParts required by the mailHandler to send an email
type EmailParts struct {
To string
Subject string
BodyType string // e.g.: text/plain
Body string
}
func handleCreateLink(c *gin.Context) {
var emailRequestBody EmailRequestBody
if err := c.BindJSON(&emailRequestBody); err != nil {
c.IndentedJSON(http.StatusBadRequest, "recipient email required")
return
}
email := emailRequestBody.Email
email, err := emailIsValid(email)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "email format not valid")
return
}
//to prevent duplicates
err = deleteExistingLinks(runfig.MongoCtx, runfig.MongoClient, email)
if err != nil {
log.Println("error deleting existing links: ", err)
}
//create link
linkID := createSingleUseLink(runfig.MongoCtx, runfig.MongoClient, email)
url := c.Request.Host + "/key/claim/" + linkID
//TO DO: send email to recipient instead of return link
err = publishAPILinkEmailTask(url, email)
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, "error encountered while sending email")
return
}
c.IndentedJSON(http.StatusCreated, "email with token link sent")
}
func publishAPILinkEmailTask(url string, recipientEmail string) error {
content := fmt.Sprintf(`
<h1>Welcome to the Data DJ</h1>
<p>Thanks for joining the Data DJ!</p>
<p>
Below is a <em>single-use</em> link that returns an API Key.
This API Key allows your application to authentic with the Data DJ API.
<br/>
<br/>
The Key can only be viewed once. It should be saved somewhere securely (e.g. password manager), not disclosed to users or client side code, and should not be hardcoded or checked into repositories.</p>
</p>
<p>
<a href="%v" target="_">%v</a> <br/>
(click or copy & paste into your browser)
</p>
In case of issues, please contact us at contact[at]librarylab.ethz.ch
`, url, url)
emailparts := EmailParts{
To: recipientEmail,
Subject: "DataDJ - Link to claim API Key",
BodyType: "text/html",
Body: content,
}
err := redisutil.PublishTask(runfig.RdbClient, emailparts, "emails")
if err != nil {
log.Println("ERROR while publishing email task:", err.Error())
return err
}
return nil
}
func createSingleUseLink(ctx context.Context, client *mongo.Client, email string) string {
var signUpLink singleUseLink
signUpLink = singleUseLink{
Used: false,
ExpireAt: time.Now().Add(time.Second * 120), //change this to time.hours in production
Permission: "createAPIToken",
Email: email,
}
collection := client.Database(config.DbName).Collection("temporaryLinks")
result, err := collection.InsertOne(ctx, signUpLink)
if err != nil {
fmt.Println(err)
}
oid, ok := result.InsertedID.(primitive.ObjectID)
if ok {
return oid.Hex()
}
return ""
}
func validateTokenLink(ctx context.Context, client *mongo.Client, linkID string) (bool, error) {
id, _ := primitive.ObjectIDFromHex(linkID)
var link singleUseLink
collection := client.Database(config.DbName).Collection("temporaryLinks")
result := collection.FindOne(
ctx,
bson.M{"_id": id},
)
result.Decode(&link)
if result.Err() != nil {
fmt.Println(result.Err().Error())
if result.Err().Error() == "mongo: no documents in result" {
return false, nil
}
return false, result.Err()
}
if link.Used == true {
fmt.Println("link already used")
return false, nil
}
return true, nil
}
// deleteExistingLinks deletes any existing temporaryLinks assosciated with `email`
// prevents possibility of individual users having multiple valid links
func deleteExistingLinks(ctx context.Context, client *mongo.Client, email string) error {
collection := client.Database(config.DbName).Collection("temporaryLinks")
result, err := collection.DeleteMany(
ctx,
bson.M{"email": email},
// bson.D{
// {"$set", bson.M{"used": true, "expireAt": time.Now()}},
// },
)
fmt.Printf("deleted %v existing temp links\n", result.DeletedCount)
return err
}
func expireLink(ctx context.Context, client *mongo.Client, linkID string) error {
id, _ := primitive.ObjectIDFromHex(linkID)
collection := client.Database(config.DbName).Collection("temporaryLinks")
result, err := collection.UpdateOne(
ctx,
bson.M{"_id": id},
bson.D{
{"$set", bson.M{"used": true, "expireAt": time.Now()}},
},
)
fmt.Println(result)
return err
}