middleware.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package main
  2. import (
  3. "errors"
  4. "github.com/go-martini/martini"
  5. "github.com/martini-contrib/render"
  6. "net/http"
  7. "sparrow/pkg/models"
  8. "sparrow/pkg/productconfig"
  9. "sparrow/pkg/rpcs"
  10. "sparrow/pkg/server"
  11. )
  12. func checkAppDomain(domain string, identifier string) error {
  13. //domainPieces := strings.Split(domain, "/")
  14. //identifierPieces := strings.Split(identifier, "-")
  15. //if len(domainPieces) == 0 {
  16. // return errors.New("wrong app domain format.")
  17. //}
  18. //if len(identifierPieces) != 3 {
  19. // return errors.New("wrong identifier format.")
  20. //}
  21. //devvendorid, err := strconv.ParseUint(identifierPieces[0], 16, 64)
  22. //if err != nil {
  23. // return errors.New("wrong vendor format.")
  24. //}
  25. //devproductid, err := strconv.ParseUint(identifierPieces[1], 16, 64)
  26. //if err != nil {
  27. // return errors.New("wrong product format.")
  28. //}
  29. //
  30. //if len(domainPieces) == 1 {
  31. // if domainPieces[0] != "*" {
  32. // return errors.New("wrong app domain " + domainPieces[0])
  33. // }
  34. // return nil
  35. //}
  36. //
  37. //if len(domainPieces) == 2 {
  38. // id, err := strconv.ParseUint(domainPieces[1], 10, 64)
  39. // if err != nil {
  40. // return errors.New("wrong app domain format..")
  41. // }
  42. // if domainPieces[0] == "vendor" {
  43. // if id != devvendorid {
  44. // return errors.New("app has no access right on device.")
  45. // }
  46. // } else if domainPieces[0] == "product" {
  47. // if id != devproductid {
  48. // return errors.New("app has no access right on device.")
  49. // }
  50. // } else {
  51. // return errors.New("wrong app domain" + domain)
  52. // }
  53. //}
  54. //
  55. //if len(domainPieces) > 2 {
  56. // return errors.New("wrong app domain" + domainPieces[0])
  57. //}
  58. return nil
  59. }
  60. // check if app has access right on device of given identifier( in url params )
  61. func ApplicationAuthOnDeviceIdentifer(context martini.Context, params martini.Params, req *http.Request, r render.Render) {
  62. identifier := params["identifier"]
  63. key := req.Header.Get("App-Key")
  64. if identifier == "" || key == "" {
  65. r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, errors.New("missing device identifier or app key.")))
  66. return
  67. }
  68. app := &models.Application{}
  69. err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.ValidateApplication", key, app)
  70. if err != nil {
  71. r.JSON(http.StatusOK, renderError(ErrAccessDenied, err))
  72. return
  73. }
  74. err = checkAppDomain(app.AppDomain, identifier)
  75. if err != nil {
  76. r.JSON(http.StatusOK, renderError(ErrAccessDenied, err))
  77. return
  78. }
  79. }
  80. func ApplicationAuth(context martini.Context, params martini.Params, req *http.Request, r render.Render) {
  81. identifier := params["identifier"]
  82. key := req.Header.Get("App-Key")
  83. if identifier == "" || key == "" {
  84. r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, errors.New("missing device identifier or app key.")))
  85. return
  86. }
  87. app := &models.Application{}
  88. err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.ValidateApplication", key, app)
  89. if err != nil {
  90. r.JSON(http.StatusOK, renderError(ErrAccessDenied, err))
  91. return
  92. }
  93. err = checkAppDomain(app.AppDomain, identifier)
  94. if err != nil {
  95. r.JSON(http.StatusOK, renderError(ErrAccessDenied, err))
  96. return
  97. }
  98. }
  99. // check if device is online.
  100. func CheckDeviceOnline(context martini.Context, params martini.Params, req *http.Request, r render.Render) {
  101. identifier := params["identifier"]
  102. device := &models.Device{}
  103. err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.FindDeviceByIdentifier", identifier, device)
  104. if err != nil {
  105. r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
  106. return
  107. }
  108. onlineargs := rpcs.ArgsGetDeviceOnlineStatus{
  109. Id: device.DeviceIdentifier,
  110. }
  111. onlinereply := rpcs.ReplyGetDeviceOnlineStatus{}
  112. err = server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceOnlineStatus", onlineargs, &onlinereply)
  113. if err != nil || onlinereply.ClientIP == "" {
  114. server.Log.Errorf("get device online status error: %v", err)
  115. r.JSON(http.StatusOK, renderError(ErrDeviceNotOnline, errors.New("设备不在线")))
  116. return
  117. }
  118. context.Map(device)
  119. }
  120. // get device identifier
  121. func CheckDeviceIdentifier(context martini.Context, params martini.Params, req *http.Request, r render.Render) {
  122. identifier := params["identifier"]
  123. device := &models.Device{}
  124. err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.FindDeviceByIdentifier", identifier, device)
  125. if err != nil {
  126. r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
  127. return
  128. }
  129. context.Map(device)
  130. }
  131. // check if proudct is ok and map a product config to context, must by called after CheckDevice
  132. func CheckProductConfig(context martini.Context, device *models.Device,
  133. params martini.Params, req *http.Request, r render.Render) {
  134. product := &models.Product{}
  135. err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.FindProduct", device.ProductID, product)
  136. if err != nil {
  137. r.JSON(http.StatusOK, renderError(ErrProductNotFound, err))
  138. return
  139. }
  140. c, err := productconfig.New(product.ProductConfig)
  141. if err != nil {
  142. r.JSON(http.StatusOK, renderError(ErrWrongProductConfig, err))
  143. return
  144. }
  145. context.Map(c)
  146. }