Skip to content

Commit 61656fa

Browse files
committed
Adds: verification flow
1 parent 70687ec commit 61656fa

File tree

5 files changed

+127
-10
lines changed

5 files changed

+127
-10
lines changed

internal/api/applications.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55

66
"github.com/gin-gonic/gin"
77

8-
"github.com/sdslabs/nymeria/internal/database/applications"
8+
database "github.com/sdslabs/nymeria/internal/database/applications"
99
"github.com/sdslabs/nymeria/internal/database/schema"
1010
"github.com/sdslabs/nymeria/internal/utils"
1111
)
@@ -28,7 +28,7 @@ func HandleGetApplicationFlow(c *gin.Context) {
2828
}
2929

3030
func HandleFetchAllApplicationsFlow(c *gin.Context) {
31-
apps, err := applications.GetAllApplications()
31+
apps, err := database.GetAllApplications()
3232

3333
if err != nil {
3434
c.JSON(http.StatusInternalServerError, gin.H{
@@ -48,7 +48,7 @@ func HandleFetchAllApplicationsFlow(c *gin.Context) {
4848
func HandleFetchApplicationByIDFlow(c *gin.Context) {
4949
id := c.Param("id")
5050

51-
app, err := applications.GetApplicationByID(id)
51+
app, err := database.GetApplicationByID(id)
5252

5353
if err != nil {
5454
c.JSON(http.StatusInternalServerError, gin.H{
@@ -103,7 +103,7 @@ func HandleCreateApplicationFlow(c *gin.Context) {
103103
ClientSecret: clientSecret,
104104
}
105105

106-
err = applications.CreateApplication(app)
106+
err = database.CreateApplication(app)
107107
if err != nil {
108108
c.JSON(http.StatusInternalServerError, gin.H{
109109
"status": "error",
@@ -122,7 +122,7 @@ func HandleCreateApplicationFlow(c *gin.Context) {
122122
func HandleDeleteApplicationFlow(c *gin.Context) {
123123
id := c.Param("id")
124124

125-
err := applications.DeleteApplication(id)
125+
err := database.DeleteApplication(id)
126126
if err != nil {
127127
c.JSON(http.StatusInternalServerError, gin.H{
128128
"status": "error",
@@ -149,7 +149,7 @@ func HandleUpdateApplicationFlow(c *gin.Context) {
149149
return
150150
}
151151

152-
app, err := applications.GetApplicationByID(req.ApplicationID)
152+
app, err := database.GetApplicationByID(req.ApplicationID)
153153
if err != nil {
154154
c.JSON(http.StatusInternalServerError, gin.H{
155155
"status": "error",
@@ -193,7 +193,7 @@ func HandleUpdateApplicationFlow(c *gin.Context) {
193193
app.RedirectURIs = req.RedirectURIs
194194
}
195195

196-
err = applications.UpdateApplication(req.ApplicationID, app)
196+
err = database.UpdateApplication(req.ApplicationID, app)
197197

198198
if err != nil {
199199
c.JSON(http.StatusInternalServerError, gin.H{

internal/api/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ func Start() {
6363
r.GET("/login", HandleGetLoginFlow)
6464
r.POST("/login", middlewares.CSRFMiddleware(), HandlePostLoginFlow)
6565

66+
r.GET("/verification", HandleGetVerificationFlow)
67+
r.POST("/verification", HandlePostVerificationCodeFlow)
68+
r.POST("/verification/code", HandlePostVerifyEmailFlow)
69+
6670
r.GET("/applications", HandleGetApplicationFlow)
6771
r.POST("/applications", HandleFetchAllApplicationsFlow)
6872
r.POST("/applications/:id", HandleFetchApplicationByIDFlow)

internal/api/types.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,24 @@ type LoginRequest struct {
1515
type CreateApplicationRequest struct {
1616
Name string `json:"name" binding:"required"`
1717
ApplicationURL string `json:"application_url" binding:"required"`
18-
CSRFToken string `json:"csrf_token" binding:"required"`
1918
AllowedOrigins []string `json:"allowed_origins" binding:"required"`
2019
RedirectURIs []string `json:"redirect_uris" binding:"required"`
2120
}
2221

2322
type UpdateApplicationRequest struct {
2423
ApplicationID string `json:"application_id" binding:"required"`
25-
CSRFToken string `json:"csrf_token" binding:"required"`
2624
ApplicationURL string `json:"application_url" binding:"omitempty"`
2725
AllowedOrigins []string `json:"allowed_origins" binding:"omitempty"`
2826
RedirectURIs []string `json:"redirect_uris" binding:"omitempty"`
2927
NewKeyFlag bool `json:"new_key_flag" binding:"omitempty"`
3028
}
29+
30+
type VerifyEmailRequest struct {
31+
Email string `json:"email" binding:"required"`
32+
IsIITRCheck bool `json:"is_iitr_check" binding:"omitempty"`
33+
}
34+
35+
type VerifyEmailCodeRequest struct {
36+
Email string `json:"email" binding:"required"`
37+
Code string `json:"code" binding:"required"`
38+
}

internal/api/verification.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
8+
"github.com/sdslabs/nymeria/internal/smtp"
9+
"github.com/sdslabs/nymeria/internal/utils"
10+
)
11+
12+
func HandleGetVerificationFlow(c *gin.Context) {
13+
csrfToken, err := utils.GenerateCSRFToken(c.GetHeader("X-User-ID")) // TODO: Get user ID from session
14+
if err != nil {
15+
c.JSON(http.StatusInternalServerError, gin.H{
16+
"status": "error",
17+
"message": "Failed to generate CSRF token",
18+
})
19+
return
20+
}
21+
22+
c.JSON(http.StatusOK, gin.H{
23+
"status": "success",
24+
"message": "Email verification flow initiated",
25+
"csrf_token": csrfToken,
26+
})
27+
}
28+
29+
func HandlePostVerificationCodeFlow(c *gin.Context) {
30+
var req VerifyEmailRequest
31+
err := c.ShouldBindJSON(&req)
32+
if err != nil {
33+
c.JSON(http.StatusBadRequest, gin.H{
34+
"status": "error",
35+
"message": "Invalid request body",
36+
})
37+
return
38+
}
39+
40+
if req.Email == "" {
41+
c.JSON(http.StatusBadRequest, gin.H{
42+
"status": "error",
43+
"message": "Email is required",
44+
})
45+
return
46+
}
47+
48+
otp, err := smtp.SendOTPHandler(req.Email, req.IsIITRCheck)
49+
if err != nil {
50+
c.JSON(http.StatusInternalServerError, gin.H{
51+
"status": "error",
52+
"message": err.Error(),
53+
})
54+
return
55+
}
56+
57+
c.JSON(http.StatusOK, gin.H{
58+
"status": "success",
59+
"message": "Email verification code sent",
60+
"data": gin.H{
61+
"otp": otp,
62+
},
63+
})
64+
}
65+
66+
func HandlePostVerifyEmailFlow(c *gin.Context) {
67+
var req VerifyEmailCodeRequest
68+
err := c.ShouldBindJSON(&req)
69+
if err != nil {
70+
c.JSON(http.StatusBadRequest, gin.H{
71+
"status": "error",
72+
"message": "Invalid request body",
73+
})
74+
return
75+
}
76+
77+
if req.Email == "" {
78+
c.JSON(http.StatusBadRequest, gin.H{
79+
"status": "error",
80+
"message": "Email is required",
81+
})
82+
return
83+
}
84+
85+
if req.Code == "" {
86+
c.JSON(http.StatusBadRequest, gin.H{
87+
"status": "error",
88+
"message": "Code is required",
89+
})
90+
return
91+
}
92+
93+
err = smtp.VerifyOTPHandler(req.Email, req.Code)
94+
if err != nil {
95+
c.JSON(http.StatusBadRequest, gin.H{
96+
"status": "error",
97+
"message": err.Error(),
98+
})
99+
}
100+
101+
c.JSON(http.StatusOK, gin.H{
102+
"status": "success",
103+
"message": "Email verified",
104+
})
105+
}

internal/database/applications/applications.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package applications
1+
package database
22

33
import (
44
"github.com/sdslabs/nymeria/internal/database"

0 commit comments

Comments
 (0)