-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (168 loc) · 5.08 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
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
package main
import (
"crypto/ed25519"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"database/sql"
)
var db *sql.DB
func initDB() {
var err error
connStr := os.Getenv("POSTGRES_URL")
db, err = sql.Open("postgres", connStr)
if err != nil {
log.Fatalf("Failed to connect to PostgreSQL: %v", err)
}
if err = db.Ping(); err != nil {
log.Fatalf("Failed to ping PostgreSQL: %v", err)
}
log.Println("Connected to PostgreSQL")
}
func migrateSchema() {
queries := []string{
`CREATE TABLE IF NOT EXISTS dids (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
did TEXT UNIQUE NOT NULL,
public_key TEXT NOT NULL,
private_key TEXT NOT NULL,
doc JSONB DEFAULT '{}'::jsonb NOT NULL,
alias TEXT UNIQUE,
password_hash TEXT NOT NULL,
name TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)`,
}
for _, query := range queries {
if _, err := db.Exec(query); err != nil {
log.Fatalf("Failed to execute migration query: %v", err)
}
}
log.Println("Database schema migration complete")
}
func generateUUIDv5(namespace, input string) string {
namespaceBytes, _ := hex.DecodeString(namespace)
inputBytes := []byte(input)
hash := sha256.Sum256(append(namespaceBytes, inputBytes...))
uuid := hash[:16]
uuid[6] = (uuid[6] & 0x0f) | 0x50
uuid[8] = (uuid[8] & 0x3f) | 0x80
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x",
uuid[0:4],
uuid[4:6],
uuid[6:8],
uuid[8:10],
uuid[10:])
}
func registerUser(c *gin.Context) {
var req struct {
Name string `json:"name"`
Alias string `json:"alias"`
Password string `json:"password"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
if req.Password == "" || req.Alias == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Alias and password are required"})
return
}
passwordHash, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
if err != nil {
log.Printf("Error hashing password: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to hash password"})
return
}
pub, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
log.Printf("Error generating keys: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate keys"})
return
}
pubKeyHex := hex.EncodeToString(pub)
uuid := generateUUIDv5("5b6e0c88-6867-598c-a7e1-c4c5d10e9c4a", pubKeyHex)
did := fmt.Sprintf("did:nice:%s", uuid)
_, err = db.Exec(`
INSERT INTO dids (did, public_key, private_key, alias, password_hash, name, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
did, pubKeyHex, hex.EncodeToString(priv), req.Alias, string(passwordHash), req.Name, time.Now())
if err != nil {
log.Printf("Error inserting DID into database: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save registration data"})
return
}
log.Printf("User registered successfully: %s", did)
c.JSON(http.StatusOK, gin.H{
"did": did,
"public_key": pubKeyHex,
})
}
func userLogin(c *gin.Context) {
var req struct {
Alias string `json:"alias"`
Password string `json:"password"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
var storedHash, did string
err := db.QueryRow(`SELECT password_hash, did FROM dids WHERE alias = $1`, req.Alias).Scan(&storedHash, &did)
if err != nil || bcrypt.CompareHashAndPassword([]byte(storedHash), []byte(req.Password)) != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
return
}
session := sessions.Default(c)
session.Set("did", did)
session.Save()
c.JSON(http.StatusOK, gin.H{"message": "Login successful", "did": did})
}
func sessionMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
session := sessions.Default(c)
did := session.Get("did")
if did == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "You are not logged in"})
c.Abort()
return
}
c.Next()
}
}
func main() {
_ = godotenv.Load()
initDB()
migrateSchema()
r := gin.Default()
store := cookie.NewStore([]byte("secret"))
r.Use(cors.Default(), sessions.Sessions("my-session", store))
// Static files
r.StaticFile("/", "./static/index.html")
r.StaticFile("/login.html", "./static/login.html")
r.StaticFile("/registration.html", "./static/registration.html")
r.StaticFile("/dashboard.html", "./static/dashboard.html") // Added this route
// API routes
r.POST("/register", registerUser)
r.POST("/login", userLogin)
// Secure routes
secure := r.Group("/secure")
secure.Use(sessionMiddleware())
secure.GET("/dashboard", func(c *gin.Context) {
c.File("./static/dashboard.html") // Serve the dashboard only for logged-in users
})
log.Println("Server running on https://nivenly.nym.st:8080")
r.RunTLS(":8080", os.Getenv("TLS_CERT_PATH"), os.Getenv("TLS_KEY_PATH"))
}