middleware.go 4.9 KB

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