|
@@ -1,9 +1,33 @@
|
|
package main
|
|
package main
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "fmt"
|
|
|
|
+ "net/http"
|
|
|
|
+
|
|
|
|
+ jwt "github.com/dgrijalva/jwt-go"
|
|
|
|
+ "github.com/dgrijalva/jwt-go/request"
|
|
"github.com/go-martini/martini"
|
|
"github.com/go-martini/martini"
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+func ValidateTokenMiddleware(w http.ResponseWriter, r *http.Request, c martini.Context) {
|
|
|
|
+
|
|
|
|
+ token, err := request.ParseFromRequest(r, request.AuthorizationHeaderExtractor,
|
|
|
|
+ func(token *jwt.Token) (interface{}, error) {
|
|
|
|
+ return []byte(SignedString), nil
|
|
|
|
+ })
|
|
|
|
+ if err == nil {
|
|
|
|
+ if token.Valid {
|
|
|
|
+ c.Next()
|
|
|
|
+ } else {
|
|
|
|
+ w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
+ fmt.Fprint(w, "Token is not valid")
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
+ fmt.Fprint(w, "Unauthorized access to this resource")
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
// martini router
|
|
// martini router
|
|
func route(m *martini.ClassicMartini) {
|
|
func route(m *martini.ClassicMartini) {
|
|
|
|
|
|
@@ -15,36 +39,41 @@ func route(m *martini.ClassicMartini) {
|
|
// SigningMethod: jwt.SigningMethodHS256,
|
|
// SigningMethod: jwt.SigningMethodHS256,
|
|
// })
|
|
// })
|
|
|
|
|
|
- // find a device by key
|
|
|
|
- m.Get("/application/v1/device/info", GetDeviceInfoByKey)
|
|
|
|
|
|
+ m.Group("/application/v1", func(r martini.Router) {
|
|
|
|
+ // find a device by key
|
|
|
|
+ r.Get("/device/info", GetDeviceInfoByKey)
|
|
|
|
+
|
|
|
|
+ // find a device by identifier
|
|
|
|
+ r.Get("/devices/:identifier/info", ApplicationAuthOnDeviceIdentifer, GetDeviceInfoByIdentifier)
|
|
|
|
+
|
|
|
|
+ // get devie current status
|
|
|
|
+ r.Get("/devices/:identifier/status/current",
|
|
|
|
+ ApplicationAuthOnDeviceIdentifer, CheckDeviceOnline, CheckProductConfig,
|
|
|
|
+ GetDeviceCurrentStatus)
|
|
|
|
|
|
- // find a device by identifier
|
|
|
|
- m.Get("/application/v1/devices/:identifier/info", ApplicationAuthOnDeviceIdentifer, GetDeviceInfoByIdentifier)
|
|
|
|
|
|
+ // get devie latest status
|
|
|
|
+ r.Get("/devices/:identifier/status/latest",
|
|
|
|
+ ApplicationAuthOnDeviceIdentifer, CheckDeviceOnline, CheckProductConfig,
|
|
|
|
+ GetDeviceLatestStatus)
|
|
|
|
|
|
- // get devie current status
|
|
|
|
- m.Get("/application/v1/devices/:identifier/status/current",
|
|
|
|
- ApplicationAuthOnDeviceIdentifer, CheckDeviceOnline, CheckProductConfig,
|
|
|
|
- GetDeviceCurrentStatus)
|
|
|
|
|
|
+ // set device status
|
|
|
|
+ r.Put("/devices/:identifier/status",
|
|
|
|
+ ApplicationAuthOnDeviceIdentifer, CheckDeviceOnline, CheckProductConfig,
|
|
|
|
+ SetDeviceStatus)
|
|
|
|
|
|
- // get devie latest status
|
|
|
|
- m.Get("/application/v1/devices/:identifier/status/latest",
|
|
|
|
- ApplicationAuthOnDeviceIdentifer, CheckDeviceOnline, CheckProductConfig,
|
|
|
|
- GetDeviceLatestStatus)
|
|
|
|
|
|
+ // send a command to device
|
|
|
|
+ r.Post("/devices/:identifier/commands",
|
|
|
|
+ ApplicationAuthOnDeviceIdentifer, CheckDeviceOnline, CheckProductConfig,
|
|
|
|
+ SendCommandToDevice)
|
|
|
|
|
|
- // set device status
|
|
|
|
- m.Put("/application/v1/devices/:identifier/status",
|
|
|
|
- ApplicationAuthOnDeviceIdentifer, CheckDeviceOnline, CheckProductConfig,
|
|
|
|
- SetDeviceStatus)
|
|
|
|
|
|
+ // and a rule to device
|
|
|
|
+ r.Post("/devices/:identifier/rules",
|
|
|
|
+ ApplicationAuthOnDeviceIdentifer, CheckDeviceIdentifier,
|
|
|
|
+ AddRule)
|
|
|
|
|
|
- // send a command to device
|
|
|
|
- m.Post("/application/v1/devices/:identifier/commands",
|
|
|
|
- ApplicationAuthOnDeviceIdentifer, CheckDeviceOnline, CheckProductConfig,
|
|
|
|
- SendCommandToDevice)
|
|
|
|
|
|
+ }, ValidateTokenMiddleware)
|
|
|
|
|
|
- // and a rule to device
|
|
|
|
- m.Post("/application/v1/devices/:identifier/rules",
|
|
|
|
- ApplicationAuthOnDeviceIdentifer, CheckDeviceIdentifier,
|
|
|
|
- AddRule)
|
|
|
|
|
|
+ m.Post("/application/auth", AppAuth)
|
|
// // user login
|
|
// // user login
|
|
// m.Post("/api/v1/login", binding.Bind(models.LoginRequest{}),
|
|
// m.Post("/api/v1/login", binding.Bind(models.LoginRequest{}),
|
|
// UserLogin)
|
|
// UserLogin)
|