-
Notifications
You must be signed in to change notification settings - Fork 0
/
paseto.go
512 lines (468 loc) · 15.5 KB
/
paseto.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package pasetobackend
import (
"encoding/json"
"net/http"
"os"
"github.com/whatsauth/watoken"
"go.mongodb.org/mongo-driver/bson"
)
// <--- ini Login & Register Admin --->
func Login(Privatekey, MongoEnv, dbname, Colname string, r *http.Request) string {
var resp Credential
mconn := SetConnection(MongoEnv, dbname)
var dataadmin Admin
err := json.NewDecoder(r.Body).Decode(&dataadmin)
if err != nil {
resp.Message = "error parsing application/json: " + err.Error()
} else {
if IsPasswordValid(mconn, Colname, dataadmin) {
tokenstring, err := watoken.Encode(dataadmin.Email, os.Getenv(Privatekey))
if err != nil {
resp.Message = "Gagal Encode Token : " + err.Error()
} else {
resp.Status = true
resp.Message = "Selamat Datang SUPERADMIN"
resp.Token = tokenstring
}
} else {
resp.Message = "Password Salah"
}
}
return GCFReturnStruct(resp)
}
// return struct
func GCFReturnStruct(DataStuct any) string {
jsondata, _ := json.Marshal(DataStuct)
return string(jsondata)
}
func ReturnStringStruct(Data any) string {
jsonee, _ := json.Marshal(Data)
return string(jsonee)
}
func Register(Mongoenv, dbname string, r *http.Request) string {
resp := new(Credential)
admindata := new(Admin)
resp.Status = false
conn := SetConnection(Mongoenv, dbname)
err := json.NewDecoder(r.Body).Decode(&admindata)
if err != nil {
resp.Message = "error parsing application/json: " + err.Error()
} else {
resp.Status = true
hash, err := HashPass(admindata.Password)
if err != nil {
resp.Message = "Gagal Hash Password" + err.Error()
}
InsertAdmindata(conn, admindata.Email, admindata.Role, hash)
resp.Message = "Berhasil Input data"
}
response := ReturnStringStruct(resp)
return response
}
// <--- ini hp --->
// hp post
func GCFInsertHp(publickey, MONGOCONNSTRINGENV, dbname, colladmin, collhp string, r *http.Request) string {
var response Credential
response.Status = false
mconn := SetConnection(MONGOCONNSTRINGENV, dbname)
var admindata Admin
gettoken := r.Header.Get("Login")
if gettoken == "" {
response.Message = "Header Login Not Exist"
} else {
// Process the request with the "Login" token
checktoken := watoken.DecodeGetId(os.Getenv(publickey), gettoken)
admindata.Email = checktoken
if checktoken == "" {
response.Message = "Kamu kayaknya belum punya akun"
} else {
admin2 := FindAdmin(mconn, colladmin, admindata)
if admin2.Role == "admin" {
var datahp Hp
err := json.NewDecoder(r.Body).Decode(&datahp)
if err != nil {
response.Message = "Error parsing application/json: " + err.Error()
} else {
insertHp(mconn, collhp, Hp{
Nomorid: datahp.Nomorid,
Title: datahp.Title,
Description: datahp.Description,
Image: datahp.Image,
Status: datahp.Status,
})
response.Status = true
response.Message = "Berhasil Insert Hp"
}
} else {
response.Message = "Anda tidak dapat Insert data karena bukan admin"
}
}
}
return GCFReturnStruct(response)
}
// delete Hp
func GCFDeleteHp(publickey, MONGOCONNSTRINGENV, dbname, colladmin, collhp string, r *http.Request) string {
var respon Credential
respon.Status = false
mconn := SetConnection(MONGOCONNSTRINGENV, dbname)
var admindata Admin
gettoken := r.Header.Get("Login")
if gettoken == "" {
respon.Message = "Header Login Not Exist"
} else {
// Process the request with the "Login" token
checktoken := watoken.DecodeGetId(os.Getenv(publickey), gettoken)
admindata.Email = checktoken
if checktoken == "" {
respon.Message = "Kamu kayaknya belum punya akun"
} else {
admin2 := FindAdmin(mconn, colladmin, admindata)
if admin2.Role == "admin" {
var datahp Hp
err := json.NewDecoder(r.Body).Decode(&datahp)
if err != nil {
respon.Message = "Error parsing application/json: " + err.Error()
} else {
DeleteHp(mconn, collhp, datahp)
respon.Status = true
respon.Message = "Berhasil Delete Hp"
}
} else {
respon.Message = "Anda tidak dapat Delete data karena bukan admin"
}
}
}
return GCFReturnStruct(respon)
}
// update Hp
func GCFUpdateHp(publickey, MONGOCONNSTRINGENV, dbname, colladmin, collhp string, r *http.Request) string {
var response Credential
response.Status = false
mconn := SetConnection(MONGOCONNSTRINGENV, dbname)
var admindata Admin
gettoken := r.Header.Get("Login")
if gettoken == "" {
response.Message = "Header Login Not Exist"
} else {
checktoken := watoken.DecodeGetId(os.Getenv(publickey), gettoken)
admindata.Email = checktoken
if checktoken == "" {
response.Message = "Kamu kayaknya belum punya akun"
} else {
admin2 := FindAdmin(mconn, colladmin, admindata)
if admin2.Role == "admin" {
var datahp Hp
err := json.NewDecoder(r.Body).Decode(&datahp)
if err != nil {
response.Message = "Error parsing application/json: " + err.Error()
} else {
UpdatedHp(mconn, collhp, bson.M{"id": datahp.ID}, datahp)
response.Status = true
response.Message = "Berhasil Update Hp"
GCFReturnStruct(CreateResponse(true, "Success Update Hp", datahp))
}
} else {
response.Message = "Anda tidak dapat Update data karena bukan admin"
}
}
}
return GCFReturnStruct(response)
}
// get all hp
func GCFGetAllHp(MONGOCONNSTRINGENV, dbname, collectionname string, r *http.Request) string {
mconn := SetConnection(MONGOCONNSTRINGENV, dbname)
datahp := GetAllHp(mconn, collectionname)
if datahp != nil {
return GCFReturnStruct(CreateResponse(true, "success Get All Hp", datahp))
} else {
return GCFReturnStruct(CreateResponse(false, "Failed Get All Hp", datahp))
}
}
func GCFGetAllHpg(publickey, Mongostring, dbname, colname string, r *http.Request) string {
resp := new(Credential)
tokenlogin := r.Header.Get("Login")
if tokenlogin == "" {
resp.Status = false
resp.Message = "Header Login Not Exist"
} else {
existing := IsExist(tokenlogin, os.Getenv(publickey))
if !existing {
resp.Status = false
resp.Message = "Kamu kayaknya belum punya akun"
} else {
koneksyen := SetConnection(Mongostring, dbname)
datahp := GetAllHp(koneksyen, colname)
yas, _ := json.Marshal(datahp)
resp.Status = true
resp.Message = "Data Berhasil diambil"
resp.Token = string(yas)
}
}
return ReturnStringStruct(resp)
}
func GetAllDataHps(PublicKey, MongoEnv, dbname, colname string, r *http.Request) string {
req := new(Response)
conn := SetConnection(MongoEnv, dbname)
tokenlogin := r.Header.Get("Login")
if tokenlogin == "" {
req.Status = false
req.Message = "Header Login Not Found"
} else {
// Dekode token untuk mendapatkan
_, err := DecodeGetHp(os.Getenv(PublicKey), tokenlogin)
if err != nil {
req.Status = false
req.Message = "Tidak ada data " + tokenlogin
} else {
// Langsung ambil data hp
datahp := GetAllHp(conn, colname)
if datahp == nil {
req.Status = false
req.Message = "Data hp tidak ada"
} else {
req.Status = true
req.Message = "Data Hp berhasil diambil"
req.Data = datahp
}
}
}
return ReturnStringStruct(req)
}
// get all hp by id
func GCFGetAllHpID(MONGOCONNSTRINGENV, dbname, collectionname string, r *http.Request) string {
mconn := SetConnection(MONGOCONNSTRINGENV, dbname)
var datahp Hp
err := json.NewDecoder(r.Body).Decode(&datahp)
if err != nil {
return err.Error()
}
hp := GetAllHpID(mconn, collectionname, datahp)
if hp != (Hp{}) {
return GCFReturnStruct(CreateResponse(true, "Success: Get ID Hp", datahp))
} else {
return GCFReturnStruct(CreateResponse(false, "Failed to Get ID Hp", datahp))
}
}
// <--- ini about --->
// about post
// func GCFInsertAbout(publickey, MONGOCONNSTRINGENV, dbname, colladmin, collabout string, r *http.Request) string {
// var response Credential
// response.Status = false
// mconn := SetConnection(MONGOCONNSTRINGENV, dbname)
// var authdata Admin
// gettoken := r.Header.Get("Login")
// if gettoken == "" {
// response.Message = "Header Login Not Exist"
// } else {
// Process the request with the "Login" token
// checktoken := watoken.DecodeGetId(os.Getenv(publickey), gettoken)
// authdata.Email = checktoken
// if checktoken == "" {
// response.Message = "Kamu kayaknya belum punya akun"
// } else {
// auth2 := FindAdmin(mconn, colladmin, authdata)
// if auth2.Role == "admin" {
// var dataabout About
// err := json.NewDecoder(r.Body).Decode(&dataabout)
// if err != nil {
// response.Message = "Error parsing application/json: " + err.Error()
// } else {
// InsertAbout(mconn, collabout, About{
// ID: dataabout.ID,
// Title: dataabout.Title,
// Description: dataabout.Description,
// Image: dataabout.Image,
// Status: dataabout.Status,
// })
// response.Status = true
// response.Message = "Berhasil Insert About"
// }
// } else {
// response.Message = "Anda tidak dapat Insert data karena bukan admin"
// }
// }
// }
// return GCFReturnStruct(response)
// }
// delete about
// func GCFDeleteAbout(publickey, MONGOCONNSTRINGENV, dbname, colladmin, collabout string, r *http.Request) string {
// var response Credential
// response.Status = false
// mconn := SetConnection(MONGOCONNSTRINGENV, dbname)
// var authdata Admin
// gettoken := r.Header.Get("Login")
// if gettoken == "" {
// response.Message = "Header Login Not Exist"
// } else {
// Process the request with the "Login" token
// checktoken := watoken.DecodeGetId(os.Getenv(publickey), gettoken)
// authdata.Email = checktoken
// if checktoken == "" {
// response.Message = "Kamu kayaknya belum punya akun"
// } else {
// auth2 := FindAdmin(mconn, colladmin, authdata)
// if auth2.Role == "admin" {
// var dataabout About
// err := json.NewDecoder(r.Body).Decode(&dataabout)
// if err != nil {
// response.Message = "Error parsing application/json: " + err.Error()
// } else {
// DeleteAbout(mconn, collabout, dataabout)
// response.Status = true
// response.Message = "Berhasil Delete About"
// CreateResponse(true, "Success Delete About", dataabout)
// }
// } else {
// response.Message = "Anda tidak dapat Delete data karena bukan admin"
// }
// }
// }
// return GCFReturnStruct(response)
// }
// update about
// func GCFUpdateAbout(publickey, MONGOCONNSTRINGENV, dbname, colladmin, collabout string, r *http.Request) string {
// var response Credential
// response.Status = false
// mconn := SetConnection(MONGOCONNSTRINGENV, dbname)
// var authdata Admin
// gettoken := r.Header.Get("Login")
// if gettoken == "" {
// response.Message = "Header Login Not Exist"
// } else {
// Process the request with the "Login" token
// checktoken := watoken.DecodeGetId(os.Getenv(publickey), gettoken)
// authdata.Email = checktoken
// if checktoken == "" {
// response.Message = "Kamu kayaknya belum punya akun"
// } else {
// auth2 := FindAdmin(mconn, colladmin, authdata)
// if auth2.Role == "admin" {
// var dataabout About
// err := json.NewDecoder(r.Body).Decode(&dataabout)
// if err != nil {
// response.Message = "Error parsing application/json: " + err.Error()
// } else {
// UpdatedAbout(mconn, collabout, bson.M{"id": dataabout.ID}, dataabout)
// response.Status = true
// response.Message = "Berhasil Update Hp"
// CreateResponse(true, "Success Update About", dataabout)
// }
// } else {
// response.Message = "Anda tidak dapat Update data karena bukan admin"
// }
// }
// }
// return GCFReturnStruct(response)
// }
// get all about
// func GCFGetAllAbout(MONGOCONNSTRINGENV, dbname, collectionname string) string {
// mconn := SetConnection(MONGOCONNSTRINGENV, dbname)
// dataabout := GetAllAbout(mconn, collectionname)
// if dataabout != nil {
// return GCFReturnStruct(CreateResponse(true, "Berhasil Get All About", dataabout))
// } else {
// return GCFReturnStruct(CreateResponse(false, "Gagal Get All About", dataabout))
// }
// }
// func GCFGetAllAboutt(publickey, Mongostring, dbname, colname string, r *http.Request) string {
// resp := new(Credential)
// tokenlogin := r.Header.Get("Login")
// if tokenlogin == "" {
// resp.Status = false
// resp.Message = "Header Login Not Exist"
// } else {
// existing := IsExist(tokenlogin, os.Getenv(publickey))
// if !existing {
// resp.Status = false
// resp.Message = "Kamu kayaknya belum punya akun"
// } else {
// koneksyen := SetConnection(Mongostring, dbname)
// datahp := GetAllAbout(koneksyen, colname)
// yas, _ := json.Marshal(datahp)
// resp.Status = true
// resp.Message = "Data Berhasil diambil"
// resp.Token = string(yas)
// }
// }
// return ReturnStringStruct(resp)
// }
// <--- ini contact --->
// contact post
// func GCFInsertContact(MONGOCONNSTRINGENV, dbname, collectionname string, r *http.Request) string {
// mconn := SetConnection(MONGOCONNSTRINGENV, dbname)
// var datacontact Contact
// err := json.NewDecoder(r.Body).Decode(&datacontact)
// if err != nil {
// return err.Error()
// }
// if err := InsertContact(mconn, collectionname, datacontact); err != nil {
// return GCFReturnStruct(CreateResponse(true, "Success Create Contact", datacontact))
// } else {
// return GCFReturnStruct(CreateResponse(false, "Failed Create Contact", datacontact))
// }
// }
// get all contact
// func GCFGetAllContact(MONGOCONNSTRINGENV, dbname, collectionname string) string {
// mconn := SetConnection(MONGOCONNSTRINGENV, dbname)
// datacontact := GetAllContact(mconn, collectionname)
// if datacontact != nil {
// return GCFReturnStruct(CreateResponse(true, "success Get All Contact", datacontact))
// } else {
// return GCFReturnStruct(CreateResponse(false, "Failed Get All Contact", datacontact))
// }
// }
// func GCFGetAllContactt(publickey, Mongostring, dbname, colname string, r *http.Request) string {
// resp := new(Credential)
// tokenlogin := r.Header.Get("Login")
// if tokenlogin == "" {
// resp.Status = false
// resp.Message = "Header Login Not Exist"
// } else {
// existing := IsExist(tokenlogin, os.Getenv(publickey))
// if !existing {
// resp.Status = false
// resp.Message = "Kamu kayaknya belum punya akun"
// } else {
// koneksyen := SetConnection(Mongostring, dbname)
// datahp := GetAllContact(koneksyen, colname)
// yas, _ := json.Marshal(datahp)
// resp.Status = true
// resp.Message = "Data Berhasil diambil"
// resp.Token = string(yas)
// }
// }
// return ReturnStringStruct(resp)
// }
//crawling
// get all crawling
// func GCFGetAllCrawling(MONGOCONNSTRINGENV, dbname, collectionname string) string {
// mconn := SetConnection(MONGOCONNSTRINGENV, dbname)
// datacrawling := GetAllCrawling(mconn, collectionname)
// if datacrawling != nil {
// return GCFReturnStruct(CreateResponse(true, "success Get All Contact", datacrawling))
// } else {
// return GCFReturnStruct(CreateResponse(false, "Failed Get All Contact", datacrawling))
// }
// }
// func GCFGetAllCrawlingg(publickey, Mongostring, dbname, colname string, r *http.Request) string {
// resp := new(Credential)
// tokenlogin := r.Header.Get("Login")
// if tokenlogin == "" {
// resp.Status = false
// resp.Message = "Header Login Not Exist"
// } else {
// existing := IsExist(tokenlogin, os.Getenv(publickey))
// if !existing {
// resp.Status = false
// resp.Message = "Kamu kayaknya belum punya akun"
// } else {
// koneksyen := SetConnection(Mongostring, dbname)
// datahp := GetAllCrawling(koneksyen, colname)
// yas, _ := json.Marshal(datahp)
// resp.Status = true
// resp.Message = "Data Berhasil diambil"
// resp.Token = string(yas)
// }
// }
// return ReturnStringStruct(resp)
// }