-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeypler.js
More file actions
172 lines (116 loc) · 4.2 KB
/
keypler.js
File metadata and controls
172 lines (116 loc) · 4.2 KB
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
if(Meteor.isServer){
Keypler = function(configObj){
/*configObj's defaults
{
publish: true,
makeLicense: function(userId){
//returns a license based off of a userId, default returns a SHA of the id plus the timestamp multiplied by math.random
},
licenseType: "sha" | "guid"//only to be set if makeLicense is not defined - as a note, the guid makeLicense function does not actually use the userId,
makeVerificationRoute: true//whether or not a route is created
}
*/
var self = this;
for(key in configObj)
this[key] = configObj[key]
if(!this.makeLicense){//configure the makeLicense function, if it hasn't been defined yet
if(!this.licenseType)
this.licenseType = "sha"
if(this.licenseType.toLowerCase() == "guid")
this.makeLicense = function(userId){
return uuid.v4();
}
else
this.makeLicense = function(userId){
return CryptoJS.SHA1(userId + Date.now() * Math.random()).toString();
}
}
this.generateLicense = function(userId){//Keypler function to generate a license - only available serverside, like the rest of Keypler
var user = Meteor.users.findOne({_id: userId})
if(user && user.services && user.services.keypler && user.services.keypler.license)
return false;//returns false if the user has a license
return Meteor.users.update({_id: userId}, {$set:{'services.keypler.license': this.makeLicense(userId)}})
//returns the id of the user who received a license
}
if(this.publish === undefined || this.publish)
Meteor.publish('_keypler_userKey', function(){
return Meteor.users.find({_id: this.userId}, {fields:
{
'services.keypler.license': 1
}
})
})
Accounts.onCreateUser(function(options, user){//give all new users an empty license
if(!user.services)
user.services = {};
user.services.keypler = {};
user.services.keypler.license = null;
if(options.profile)
user.profile = options.profile;
return user;
})
if(this.makeVerificationRoute === undefined || this.makeVerificationRoute)
Router.route('/keypler_verify', function(){
/*
POST request with JSON body
key `email` has user's email
key `license` has license
response:
202 if the key is verified
400 if it's not a POST request
401 if the key doesn't match
*/
var req = this.request;
var res = this.response;
var query = {};
if(req.body.email)
query['emails.0.address'] = req.body.email
if(req.body.id)
query['_id'] = req.body.id
if(Object.keys(query).length == 0 || req.method !== "POST"){
res.writeHead(400, {'Content-Type': 'text/plain'});
res.write("Bad request");
return res.end();
}
query['services.keypler.license'] = req.body.license
// res.status(200).send('A+').end();
var userQuery = Meteor.users.findOne(query);
if(userQuery && userQuery.services && userQuery.services.keypler && userQuery.services.keypler.license){//they have a license
res.writeHead(202, {'Content-Type': 'text/plain'});
res.write("Everything checks out, boss");
return res.end();
}
res.writeHead(401, {'Content-Type': 'text/plain'});
res.write("This user's key is invalid!");
res.end();
},
{where: 'server'})
if(this.makeGumroadRoute)
Router.route('/' + (configObj.gumroadRouteName || "keypler_gumroad"), function(){
/*
POST request with JSON body
https://gumroad.com/webhooks
User matching the user in the `email` field of the post body will get a license
*/
var req = this.request;
var res = this.response;
var userEmail = req.body.email;
var query = {};
query['emails.address'] = userEmail;
var user = Meteor.users.findOne(query);
if(!user){
res.writeHead(400, {'Content-Type': 'text/plain'});
res.write("User with email '" + userEmail + "' not found!");
return res.end();
}
self.generateLicense(user._id)
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(Meteor.absoluteUrl());
return res.end();
},
{where: 'server'})
};
}
if(Meteor.isClient){
Meteor.subscribe('_keypler_userKey')//automatically subscribe to this - only publish the actual data if configured
}