Explorar o código

working on login && auth

Torkel Ödegaard %!s(int64=11) %!d(string=hai) anos
pai
achega
e7ce371ee8
Modificáronse 5 ficheiros con 55 adicións e 26 borrados
  1. 2 19
      backend/api/api.go
  2. 1 1
      backend/api/api_dashboard.go
  3. 46 0
      backend/api/api_login.go
  4. 5 5
      backend/api/api_models.go
  5. 1 1
      grafana

+ 2 - 19
backend/api/api.go

@@ -55,9 +55,8 @@ func (self *HttpServer) ListenAndServe() {
 	}
 
 	// register default route
-	self.router.GET("/", self.AuthMiddleware(), self.index)
-	self.router.GET("/login/*_", self.index)
-	self.router.GET("/dashboard/*_", self.index)
+	self.router.GET("/", self.authMiddleware(), self.index)
+	self.router.GET("/dashboard/*_", self.authMiddleware(), self.index)
 
 	self.router.Run(":" + self.port)
 }
@@ -66,22 +65,6 @@ func (self *HttpServer) index(c *gin.Context) {
 	c.HTML(200, "index.html", &indexViewModel{title: "hello from go"})
 }
 
-func (self *HttpServer) login(c *gin.Context) {
-	c.HTML(200, "login.html", &indexViewModel{title: "hello from go"})
-}
-
-func (self *HttpServer) AuthMiddleware() gin.HandlerFunc {
-	return func(c *gin.Context) {
-		session, _ := sessionStore.Get(c.Request, "grafana-session")
-		// if session.Values["login"] == nil {
-		// 	c.Writer.Header().Set("Location", "/login/login#login")
-		// 	c.Abort(302)
-		// }
-		session.Values["asd"] = 1
-		session.Save(c.Request, c.Writer)
-	}
-}
-
 func CacheHeadersMiddleware() gin.HandlerFunc {
 	return func(c *gin.Context) {
 		c.Writer.Header().Add("Cache-Control", "max-age=0, public, must-revalidate, proxy-revalidate")

+ 1 - 1
backend/api/api_dashboard.go

@@ -41,7 +41,7 @@ func (self *HttpServer) postDashboard(c *gin.Context) {
 	var command saveDashboardCommand
 
 	if c.EnsureBody(&command) {
-		err := self.store.Save(&models.Dashboard{Data: command.dashboard})
+		err := self.store.Save(&models.Dashboard{Data: command.Dashboard})
 		if err == nil {
 			c.JSON(200, gin.H{"status": "saved"})
 			return

+ 46 - 0
backend/api/api_login.go

@@ -0,0 +1,46 @@
+package api
+
+import "github.com/gin-gonic/gin"
+
+func init() {
+	addRoutes(func(self *HttpServer) {
+		self.router.GET("/login/*_", self.index)
+		self.router.POST("/login", self.loginPost)
+	})
+}
+
+type loginJsonModel struct {
+	Email    string `json:"email" binding:"required"`
+	Password string `json:"password" binding:"required"`
+	Remember bool   `json:"remember"`
+}
+
+func (self *HttpServer) loginPost(c *gin.Context) {
+	var loginModel loginJsonModel
+
+	if c.EnsureBody(&loginModel) {
+		if loginModel.Email == "manu" && loginModel.Password == "123" {
+
+			session, _ := sessionStore.Get(c.Request, "grafana-session")
+			session.Values["login"] = true
+			session.Save(c.Request, c.Writer)
+
+			c.JSON(200, gin.H{"status": "you are logged in"})
+		} else {
+			c.JSON(401, gin.H{"status": "unauthorized"})
+		}
+	}
+}
+
+func (self *HttpServer) authMiddleware() gin.HandlerFunc {
+	return func(c *gin.Context) {
+		session, _ := sessionStore.Get(c.Request, "grafana-session")
+
+		if c.Request.URL.Path != "/login" && session.Values["login"] == nil {
+			c.Writer.Header().Set("Location", "/login")
+			c.Abort(302)
+		}
+
+		session.Save(c.Request, c.Writer)
+	}
+}

+ 5 - 5
backend/api/api_models.go

@@ -1,13 +1,13 @@
 package api
 
 type saveDashboardCommand struct {
-	id        string `json:"id"`
-	title     string `json:"title"`
-	dashboard map[string]interface{}
+	Id        string `json:"id"`
+	Title     string `json:"title"`
+	Dashboard map[string]interface{}
 }
 
 type errorResponse struct {
-	message string `json:"message"`
+	Message string `json:"message"`
 }
 
 type indexViewModel struct {
@@ -15,5 +15,5 @@ type indexViewModel struct {
 }
 
 func newErrorResponse(message string) *errorResponse {
-	return &errorResponse{message: message}
+	return &errorResponse{Message: message}
 }

+ 1 - 1
grafana

@@ -1 +1 @@
-Subproject commit c690d4aae84fc39d7c4bf9a0a820a71f69059b78
+Subproject commit bfe1ef07330fcfcdbc3aa9ddd4eef827ee48f316