You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.5 KiB

  1. package handlers
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/matrix-org/go-neb/api"
  6. "github.com/matrix-org/go-neb/clients"
  7. "github.com/matrix-org/util"
  8. "maunium.net/go/mautrix/crypto"
  9. )
  10. // ConfigureClient represents an HTTP handler capable of processing /admin/configureClient requests.
  11. type ConfigureClient struct {
  12. Clients *clients.Clients
  13. }
  14. // OnIncomingRequest handles POST requests to /admin/configureClient. The JSON object provided
  15. // is of type "api.ClientConfig".
  16. //
  17. // If a DisplayName is supplied, this request will set this client's display name
  18. // if the old ClientConfig DisplayName differs from the new ClientConfig DisplayName.
  19. //
  20. // Request:
  21. // POST /admin/configureClient
  22. // {
  23. // "UserID": "@my_bot:localhost",
  24. // "HomeserverURL": "http://localhost:8008",
  25. // "Sync": true,
  26. // "DisplayName": "My Bot"
  27. // }
  28. //
  29. // Response:
  30. // HTTP/1.1 200 OK
  31. // {
  32. // "OldClient": {
  33. // // The old api.ClientConfig
  34. // },
  35. // "NewClient": {
  36. // // The new api.ClientConfig
  37. // }
  38. // }
  39. func (s *ConfigureClient) OnIncomingRequest(req *http.Request) util.JSONResponse {
  40. if req.Method != "POST" {
  41. return util.MessageResponse(405, "Unsupported Method")
  42. }
  43. var body api.ClientConfig
  44. if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
  45. return util.MessageResponse(400, "Error parsing request JSON")
  46. }
  47. if err := body.Check(); err != nil {
  48. return util.MessageResponse(400, "Error parsing client config")
  49. }
  50. oldClient, err := s.Clients.Update(body)
  51. if err != nil {
  52. util.GetLogger(req.Context()).WithError(err).WithField("body", body).Error("Failed to Clients.Update")
  53. return util.MessageResponse(500, "Error storing token")
  54. }
  55. return util.JSONResponse{
  56. Code: 200,
  57. JSON: struct {
  58. OldClient api.ClientConfig
  59. NewClient api.ClientConfig
  60. }{oldClient, body},
  61. }
  62. }
  63. // VerifySAS represents an HTTP handler capable of processing /verifySAS requests.
  64. type VerifySAS struct {
  65. Clients *clients.Clients
  66. }
  67. // OnIncomingRequest handles POST requests to /verifySAS. The JSON object provided
  68. // is of type "api.IncomingDecimalSAS".
  69. //
  70. // The request should contain the three decimal SAS numbers as displayed on the other device that is being verified,
  71. // as well as that device's user and device ID.
  72. // It should also contain the user ID that Go-NEB's client is using.
  73. //
  74. // Request:
  75. // POST /verifySAS
  76. // {
  77. // "UserID": "@my_bot:localhost", // Neb's user ID
  78. // "OtherUserID": "@user:localhost", // User ID of device we're verifying with
  79. // "OtherDeviceID": "ABCDEFG", // Device ID of device we're verifying with
  80. // "SAS": [1111, 2222, 3333] // SAS displayed on device we're verifying with
  81. // }
  82. //
  83. // Response:
  84. // HTTP/1.1 200 OK
  85. // {}
  86. func (s *VerifySAS) OnIncomingRequest(req *http.Request) util.JSONResponse {
  87. if req.Method != "POST" {
  88. return util.MessageResponse(405, "Unsupported Method")
  89. }
  90. var body api.IncomingDecimalSAS
  91. if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
  92. return util.MessageResponse(400, "Error parsing request JSON: "+err.Error())
  93. }
  94. if err := body.Check(); err != nil {
  95. return util.MessageResponse(400, "Request error: "+err.Error())
  96. }
  97. client, err := s.Clients.Client(body.UserID)
  98. if err != nil {
  99. util.GetLogger(req.Context()).WithError(err).WithField("body", body).Error("Failed to load client")
  100. return util.MessageResponse(500, "Error storing SAS")
  101. }
  102. client.SubmitDecimalSAS(body.OtherUserID, body.OtherDeviceID, crypto.DecimalSASData(body.SAS))
  103. return util.JSONResponse{
  104. Code: 200,
  105. JSON: struct{}{},
  106. }
  107. }