|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "github.com/gin-gonic/gin" |
| 7 | + |
| 8 | + "github.com/sdslabs/nymeria/internal/database/applications" |
| 9 | + "github.com/sdslabs/nymeria/internal/database/schema" |
| 10 | + "github.com/sdslabs/nymeria/internal/utils" |
| 11 | +) |
| 12 | + |
| 13 | +func HandleGetApplicationFlow(c *gin.Context) { |
| 14 | + csrfToken, err := utils.GenerateCSRFToken(c.GetHeader("X-User-ID")) // TODO: Get user ID from session |
| 15 | + if err != nil { |
| 16 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 17 | + "status": "error", |
| 18 | + "message": "Failed to generate CSRF token", |
| 19 | + }) |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + c.JSON(http.StatusOK, gin.H{ |
| 24 | + "status": "success", |
| 25 | + "message": "create application flow", |
| 26 | + "csrf_token": csrfToken, |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +func HandleFetchAllApplicationsFlow(c *gin.Context) { |
| 31 | + apps, err := applications.GetAllApplications() |
| 32 | + |
| 33 | + if err != nil { |
| 34 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 35 | + "status": "error", |
| 36 | + "message": err.Error(), |
| 37 | + }) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + c.JSON(http.StatusOK, gin.H{ |
| 42 | + "status": "success", |
| 43 | + "message": "applications fetched", |
| 44 | + "data": apps, |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +func HandleFetchApplicationByIDFlow(c *gin.Context) { |
| 49 | + id := c.Param("id") |
| 50 | + |
| 51 | + app, err := applications.GetApplicationByID(id) |
| 52 | + |
| 53 | + if err != nil { |
| 54 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 55 | + "status": "error", |
| 56 | + "message": err.Error(), |
| 57 | + }) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + c.JSON(http.StatusOK, gin.H{ |
| 62 | + "status": "success", |
| 63 | + "message": "application fetched", |
| 64 | + "data": app, |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +func HandleCreateApplicationFlow(c *gin.Context) { |
| 69 | + var req CreateApplicationRequest |
| 70 | + |
| 71 | + err := c.ShouldBindJSON(&req) |
| 72 | + if err != nil { |
| 73 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 74 | + "status": "error", |
| 75 | + "message": err.Error(), |
| 76 | + }) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + clientKey, err := utils.GenerateRandomString(16) |
| 81 | + if err != nil { |
| 82 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 83 | + "status": "error", |
| 84 | + "message": "Failed to generate client key", |
| 85 | + }) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + clientSecret, err := utils.GenerateRandomString(32) |
| 90 | + if err != nil { |
| 91 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 92 | + "status": "error", |
| 93 | + "message": "Failed to generate client secret", |
| 94 | + }) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + app := schema.Application{ |
| 99 | + Name: req.Name, |
| 100 | + AllowedOrigins: req.AllowedOrigins, |
| 101 | + RedirectURIs: req.RedirectURIs, |
| 102 | + ClientKey: clientKey, |
| 103 | + ClientSecret: clientSecret, |
| 104 | + } |
| 105 | + |
| 106 | + err = applications.CreateApplication(app) |
| 107 | + if err != nil { |
| 108 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 109 | + "status": "error", |
| 110 | + "message": err.Error(), |
| 111 | + }) |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + c.JSON(http.StatusOK, gin.H{ |
| 116 | + "status": "success", |
| 117 | + "message": "application created", |
| 118 | + "data": app, |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +func HandleDeleteApplicationFlow(c *gin.Context) { |
| 123 | + id := c.Param("id") |
| 124 | + |
| 125 | + err := applications.DeleteApplication(id) |
| 126 | + if err != nil { |
| 127 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 128 | + "status": "error", |
| 129 | + "message": err.Error(), |
| 130 | + }) |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + c.JSON(http.StatusOK, gin.H{ |
| 135 | + "status": "success", |
| 136 | + "message": "application deleted", |
| 137 | + }) |
| 138 | +} |
| 139 | + |
| 140 | +func HandleUpdateApplicationFlow(c *gin.Context) { |
| 141 | + var req UpdateApplicationRequest |
| 142 | + |
| 143 | + err := c.ShouldBindJSON(&req) |
| 144 | + if err != nil { |
| 145 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 146 | + "status": "error", |
| 147 | + "message": err.Error(), |
| 148 | + }) |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + app, err := applications.GetApplicationByID(req.ApplicationID) |
| 153 | + if err != nil { |
| 154 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 155 | + "status": "error", |
| 156 | + "message": err.Error(), |
| 157 | + }) |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + if req.NewKeyFlag { |
| 162 | + clientKey, err := utils.GenerateRandomString(16) |
| 163 | + if err != nil { |
| 164 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 165 | + "status": "error", |
| 166 | + "message": err.Error(), |
| 167 | + }) |
| 168 | + return |
| 169 | + } |
| 170 | + |
| 171 | + clientSecret, err := utils.GenerateRandomString(32) |
| 172 | + if err != nil { |
| 173 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 174 | + "status": "error", |
| 175 | + "message": err.Error(), |
| 176 | + }) |
| 177 | + return |
| 178 | + } |
| 179 | + |
| 180 | + app.ClientKey = clientKey |
| 181 | + app.ClientSecret = clientSecret |
| 182 | + } |
| 183 | + |
| 184 | + if req.ApplicationURL != "" { |
| 185 | + app.ApplicationURL = req.ApplicationURL |
| 186 | + } |
| 187 | + |
| 188 | + if len(req.AllowedOrigins) > 0 { |
| 189 | + app.AllowedOrigins = req.AllowedOrigins |
| 190 | + } |
| 191 | + |
| 192 | + if len(req.RedirectURIs) > 0 { |
| 193 | + app.RedirectURIs = req.RedirectURIs |
| 194 | + } |
| 195 | + |
| 196 | + err = applications.UpdateApplication(req.ApplicationID, app) |
| 197 | + |
| 198 | + if err != nil { |
| 199 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 200 | + "status": "error", |
| 201 | + "message": err.Error(), |
| 202 | + }) |
| 203 | + return |
| 204 | + } |
| 205 | + |
| 206 | + c.JSON(http.StatusOK, gin.H{ |
| 207 | + "status": "success", |
| 208 | + "message": "application updated", |
| 209 | + "data": gin.H{ |
| 210 | + "application_id": req.ApplicationID, |
| 211 | + "client_key": app.ClientKey, |
| 212 | + "client_secret": app.ClientSecret, |
| 213 | + }, |
| 214 | + }) |
| 215 | +} |
0 commit comments