-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot_code
More file actions
194 lines (157 loc) · 4.47 KB
/
Copy pathcopilot_code
File metadata and controls
194 lines (157 loc) · 4.47 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//package main
//import (
//"net/http"
//"github.com/gin-gonic/gin"
//swaggerfiles "github.com/swaggo/files"
//ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware
//)
//// @title Swagger Example API
//// @version 1.0
//// @description This is a sample server Petstore server.
//// @termsOfService http://swagger.io/terms/
//// @contact.name API Support
//// @contact.url http://www.swagger.io/support
//// @contact.email support@swagger.io
//// @license.name Apache 2.0
//// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
//// @host petstore.swagger.io:8080
//// @BasePath /v2
//func main() {
//router := gin.Default()
//router.GET("/", Home)
//apiV1 := router.Group("/v1")
//apiV1.POST("/v1/login")
//router.Run(":8080")
//}
//func Home(c *gin.Context) {
//c.String(http.StatusOK, "Hin hin hin you didn't say the magic word !")
//}
//func decodetoken
// //
package main
import (
"database/sql"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
_ "github.com/mattn/go-sqlite3"
)
// User represents the user model
type User struct {
ID int `json:"id"`
Username string `json:"username"`
Password string `json:"password"`
}
// TokenDetails contains the JWT token information
type TokenDetails struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
AccessUuid string `json:"access_uuid"`
RefreshUuid string `json:"refresh_uuid"`
AtExpires time.Time `json:"at_expires"`
RtExpires time.Time `json:"rt_expires"`
}
// Create the database and user table
func initDB() *sql.DB {
db, err := sql.Open("sqlite3", "./auth.db")
if err != nil {
log.Fatal(err)
}
// Create users table if it doesn't exist
createTableSQL := `CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
);`
_, err = db.Exec(createTableSQL)
if err != nil {
log.Fatal(err)
}
return db
}
// JWT secret key - in production, use environment variables
var jwtSecret = []byte("your-256-bit-secret")
func main() {
r := gin.Default()
db := initDB()
defer db.Close()
// Login endpoint
r.POST("/login", func(c *gin.Context) {
var user User
if err := c.ShouldBindJSON(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input"})
return
}
// Check user credentials
var dbUser User
err := db.QueryRow("SELECT id, username, password FROM users WHERE username = ?",
user.Username).Scan(&dbUser.ID, &dbUser.Username, &dbUser.Password)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
return
}
if user.Password != dbUser.Password { // In production, use proper password hashing
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
return
}
// Generate JWT Token
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"user_id": dbUser.ID,
"username": dbUser.Username,
"exp": time.Now().Add(time.Hour * 24).Unix(), // Token expires in 24 hours
})
tokenString, err := token.SignedString(jwtSecret)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not generate token"})
return
}
c.JSON(http.StatusOK, gin.H{
"token": tokenString,
})
})
// Middleware to check for valid JWT token
authMiddleware := func() gin.HandlerFunc {
return func(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header is required"})
c.Abort()
return
}
// Remove 'Bearer ' prefix from token
tokenString := authHeader[7:]
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return jwtSecret, nil
})
if err != nil || !token.Valid {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
c.Abort()
return
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token claims"})
c.Abort()
return
}
c.Set("user_id", claims["user_id"])
c.Set("username", claims["username"])
c.Next()
}
}
// Protected route example
protected := r.Group("/api")
protected.Use(authMiddleware())
{
protected.GET("/profile", func(c *gin.Context) {
username := c.MustGet("username").(string)
c.JSON(http.StatusOK, gin.H{
"message": "Welcome to your profile",
"username": username,
})
})
}
r.Run(":8080")
}