Skip to content

Commit c1aaacb

Browse files
feat: made expiry of jwt token configurable (#174)
* feat: made expiry of jwt token configurable Signed-off-by: Kapish Upadhyay <upadhyaykapish@gmail.com> * chore: go formatting Signed-off-by: Kapish Upadhyay <upadhyaykapish@gmail.com> --------- Signed-off-by: Kapish Upadhyay <upadhyaykapish@gmail.com>
1 parent a0c0401 commit c1aaacb

3 files changed

Lines changed: 22 additions & 11 deletions

File tree

go-dedup/main.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func main() {
124124
router.GET("/nothing", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "Nothing"}) })
125125
router.GET("/somewhere", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "Somewhere"}) })
126126
router.GET("/nowhere", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "Nowhere"}) })
127-
127+
128128
// Group 2: RESTful API for 'products'
129129
router.GET("/products", func(c *gin.Context) {
130130
products := []Product{
@@ -183,7 +183,7 @@ func main() {
183183
filepath := c.Param("filepath")
184184
c.JSON(http.StatusOK, gin.H{"requested_file": filepath})
185185
})
186-
186+
187187
// Group 4: Different response types
188188
router.GET("/html", func(c *gin.Context) {
189189
c.Data(http.StatusOK, "text/html; charset=utf-8", []byte("<h1>This is HTML</h1>"))
@@ -194,7 +194,7 @@ func main() {
194194
router.GET("/redirect", func(c *gin.Context) {
195195
c.Redirect(http.StatusMovedPermanently, "http://google.com")
196196
})
197-
197+
198198
// Group 5: More HTTP methods
199199
router.PATCH("/config", func(c *gin.Context) {
200200
var update ConfigUpdate
@@ -222,8 +222,12 @@ func main() {
222222
v2 := router.Group("/api/v2")
223223
{
224224
v2.GET("/data", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"version": 2, "payload": "new data format"}) })
225-
v2.GET("/users", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"version": 2, "users": []map[string]string{{"name": "gamma"}, {"name": "delta"}}}) })
226-
v2.POST("/users", func(c *gin.Context) { c.JSON(http.StatusCreated, gin.H{"version": 2, "message": "user successfully registered"}) })
225+
v2.GET("/users", func(c *gin.Context) {
226+
c.JSON(http.StatusOK, gin.H{"version": 2, "users": []map[string]string{{"name": "gamma"}, {"name": "delta"}}})
227+
})
228+
v2.POST("/users", func(c *gin.Context) {
229+
c.JSON(http.StatusCreated, gin.H{"version": 2, "message": "user successfully registered"})
230+
})
227231
}
228232

229233
// Group 7: Filler endpoints to reach 63
@@ -241,11 +245,13 @@ func main() {
241245
router.GET("/anybody", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "Anybody"}) })
242246
router.GET("/everybody", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "Everybody"}) })
243247
router.GET("/somebody", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "Somebody"}) })
244-
router.PUT("/user/:id/password", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("password for user %s updated", c.Param("id"))}) })
248+
router.PUT("/user/:id/password", func(c *gin.Context) {
249+
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("password for user %s updated", c.Param("id"))})
250+
})
245251
router.GET("/user/:id/profile", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"user_id": c.Param("id"), "profile": "..."}) })
246252
router.POST("/events", func(c *gin.Context) { c.JSON(http.StatusAccepted, gin.H{"status": "event received"}) })
247253
router.GET("/session/info", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"session_id": "xyz-123", "active": true}) })
248-
254+
249255
// Start the HTTP server on port 8080
250256
router.Run(":8080")
251-
}
257+
}

go-jwt/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ this will return the response.
5858
2. Fetch the Products
5959
```bash
6060
curl --request GET \
61-
--url http://localhost:8000/generate-token \
61+
--url http://localhost:8000/generate-token?expiry=15 \
6262
--header 'Host: localhost:8000' \
6363
--header 'User-Agent: curl/7.81.0' \
6464
--header 'Accept: */*'

go-jwt/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log"
88
"net/http"
99
"os"
10+
"strconv"
1011
"time"
1112

1213
"github.com/dgrijalva/jwt-go"
@@ -57,8 +58,12 @@ func GenerateTokenHandler(c *gin.Context) {
5758
password := "example_password"
5859

5960
// Set token expiration time
60-
expirationTime := time.Now().Add(5 * time.Minute)
61-
61+
expiryMinutesStr := c.DefaultQuery("expiry", "5")
62+
expiryMinutes, err := strconv.Atoi(expiryMinutesStr)
63+
if err != nil || expiryMinutes <= 0 {
64+
expiryMinutes = 5
65+
}
66+
expirationTime := time.Now().Add(time.Duration(expiryMinutes) * time.Minute)
6267
// Create the JWT claims, which includes the username and expiry time
6368
claims := &Claims{
6469
Username: username,

0 commit comments

Comments
 (0)