/
hash.go
226 lines (195 loc) · 5.83 KB
/
hash.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
package peda
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"time"
"aidanwoods.dev/go-paseto"
"github.com/aiteung/atdb"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"golang.org/x/crypto/bcrypt"
)
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
return string(bytes), err
}
func GenerateRandomNumber() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
func CheckPasswordHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
func CreateResponse(status bool, message string, data interface{}) Response {
response := Response{
Status: status,
Message: message,
Data: data,
}
return response
}
// pengecekantoken
func IsTokenValid(publickey, tokenstr string) (payload Payload, err error) {
var token *paseto.Token
var pubKey paseto.V4AsymmetricPublicKey
pubKey, err = paseto.NewV4AsymmetricPublicKeyFromHex(publickey) // this wil fail if given key in an invalid format
if err != nil {
fmt.Println("Decode NewV4AsymmetricPublicKeyFromHex : ", err)
}
parser := paseto.NewParser() // only used because this example token has expired, use NewParser() (which checks expiry by default)
token, err = parser.ParseV4Public(pubKey, tokenstr, nil) // this will fail if parsing failes, cryptographic checks fail, or validation rules fail
if err != nil {
fmt.Println("Decode ParseV4Public : ", err)
} else {
json.Unmarshal(token.ClaimsJSON(), &payload)
}
return payload, err
}
func GenerateKey() (privatekey, publickey string) {
secretKey := paseto.NewV4AsymmetricSecretKey() // don't share this!!!
privatekey = secretKey.ExportHex() // DO share this one
publickey = secretKey.Public().ExportHex()
return privatekey, publickey
}
func Encode(name, username, role, privatekey string) (string, error) {
token := paseto.NewToken()
token.SetIssuedAt(time.Now())
token.SetNotBefore(time.Now())
token.SetExpiration(time.Now().Add(2 * time.Hour))
token.SetString("name", name)
token.SetString("username", username)
token.SetString("role", role)
key, err := paseto.NewV4AsymmetricSecretKeyFromHex(privatekey)
return token.V4Sign(key, nil), err
}
func SetConnection2dsphereTest(mongoenv, dbname string) *mongo.Database {
var DBmongoinfo = atdb.DBInfo{
DBString: os.Getenv(mongoenv),
DBName: dbname,
}
db := atdb.MongoConnect(DBmongoinfo)
// Create a geospatial index if it doesn't exist
indexModel := mongo.IndexModel{
Keys: bson.D{
{Key: "geometry", Value: "2dsphere"},
},
}
_, err := db.Collection("near").Indexes().CreateOne(context.TODO(), indexModel)
if err != nil {
log.Printf("Error creating geospatial index: %v\n", err)
}
return db
}
func SetConnection2dsphereMax(mongoenv, dbname string) *mongo.Database {
var DBmongoinfo = atdb.DBInfo{
DBString: os.Getenv(mongoenv),
DBName: dbname,
}
db := atdb.MongoConnect(DBmongoinfo)
// Create a geospatial index if it doesn't exist
indexModel := mongo.IndexModel{
Keys: bson.D{
{Key: "geometry", Value: "2dsphere"},
},
}
_, err := db.Collection("max").Indexes().CreateOne(context.TODO(), indexModel)
if err != nil {
log.Printf("Error creating geospatial index: %v\n", err)
}
return db
}
func SetConnection2dsphereMix(mongoenv, dbname string) *mongo.Database {
var DBmongoinfo = atdb.DBInfo{
DBString: os.Getenv(mongoenv),
DBName: dbname,
}
db := atdb.MongoConnect(DBmongoinfo)
// Create a geospatial index if it doesn't exist
indexModel := mongo.IndexModel{
Keys: bson.D{
{Key: "geometry", Value: "2dsphere"},
},
}
_, err := db.Collection("min").Indexes().CreateOne(context.TODO(), indexModel)
if err != nil {
log.Printf("Error creating geospatial index: %v\n", err)
}
return db
}
func SetConnection2dsphereTestBox(mongoenv, dbname string) *mongo.Database {
var DBmongoinfo = atdb.DBInfo{
DBString: os.Getenv(mongoenv),
DBName: dbname,
}
db := atdb.MongoConnect(DBmongoinfo)
// Create a geospatial index if it doesn't exist
indexModel := mongo.IndexModel{
Keys: bson.D{
{Key: "geometry", Value: "2dsphere"},
},
}
_, err := db.Collection("boxfix").Indexes().CreateOne(context.TODO(), indexModel)
if err != nil {
fmt.Printf("Error creating 2dsphere index: %v\n", err)
}
return db
}
func SetConnection2dsphereTestPoint(mongoenv, dbname string) *mongo.Database {
var DBmongoinfo = atdb.DBInfo{
DBString: os.Getenv(mongoenv),
DBName: dbname,
}
db := atdb.MongoConnect(DBmongoinfo)
// Create a geospatial index if it doesn't exist
indexModel := mongo.IndexModel{
Keys: bson.D{
{Key: "geometry", Value: "2dsphere"},
},
}
_, err := db.Collection("nearspehere").Indexes().CreateOne(context.TODO(), indexModel)
if err != nil {
log.Printf("Error creating geospatial index: %v\n", err)
}
return db
}
func Polygon(mongoconn *mongo.Database, coordinates [][][]float64) (namalokasi string) {
lokasicollection := mongoconn.Collection("polygon")
// Log coordinates for debugging
fmt.Println("Coordinates:", coordinates)
filter := bson.M{
"location": bson.M{
"$geoWithin": bson.M{
"$geometry": bson.M{
"type": "Polygon",
"coordinates": coordinates,
},
},
},
}
fmt.Println("Filter:", filter)
var lokasi Lokasi
err := lokasicollection.FindOne(context.TODO(), filter).Decode(&lokasi)
if err != nil {
log.Printf("Polygon: %v\n", err)
return ""
}
return lokasi.Properties.Name
}
func GetBoxDoc(db *mongo.Database, collname string, coordinates Polyline) (result string) {
filter := bson.M{
"geometry": bson.M{
"$geoWithin": bson.M{
"$box": coordinates.Coordinates,
},
},
}
var doc FullGeoJson
err := db.Collection(collname).FindOne(context.TODO(), filter).Decode(&doc)
if err != nil {
fmt.Printf("Box: %v\n", err)
}
return "Box anda berada pada " + doc.Properties.Name
}