middleware.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // check if device is online.
  81. func CheckDeviceOnline(context martini.Context, params martini.Params, req *http.Request, r render.Render) {
  82. identifier := params["identifier"]
  83. device := &models.Device{}
  84. err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.FindDeviceByIdentifier", identifier, device)
  85. if err != nil {
  86. r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
  87. return
  88. }
  89. onlineargs := rpcs.ArgsGetDeviceOnlineStatus{
  90. Id: device.DeviceIdentifier,
  91. }
  92. onlinereply := rpcs.ReplyGetDeviceOnlineStatus{}
  93. err = server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceOnlineStatus", onlineargs, &onlinereply)
  94. if err != nil || onlinereply.ClientIP == "" {
  95. server.Log.Errorf("get device online status error: %v", err)
  96. r.JSON(http.StatusOK, renderError(ErrDeviceNotOnline, errors.New("设备不在线")))
  97. return
  98. }
  99. context.Map(device)
  100. }
  101. // get device identifier
  102. func CheckDeviceIdentifier(context martini.Context, params martini.Params, req *http.Request, r render.Render) {
  103. identifier := params["identifier"]
  104. device := &models.Device{}
  105. err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.FindDeviceByIdentifier", identifier, device)
  106. if err != nil {
  107. r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
  108. return
  109. }
  110. context.Map(device)
  111. }
  112. // check if proudct is ok and map a product config to context, must by called after CheckDevice
  113. func CheckProductConfig(context martini.Context, device *models.Device,
  114. params martini.Params, req *http.Request, r render.Render) {
  115. product := &models.Product{}
  116. err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.FindProduct", device.ProductID, product)
  117. if err != nil {
  118. r.JSON(http.StatusOK, renderError(ErrProductNotFound, err))
  119. return
  120. }
  121. c, err := productconfig.New(product.ProductConfig)
  122. if err != nil {
  123. r.JSON(http.StatusOK, renderError(ErrWrongProductConfig, err))
  124. return
  125. }
  126. context.Map(c)
  127. }