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.

165 lines
6.3 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. // Package api contains the fundamental data types used by Go-NEB.
  2. //
  3. // Most HTTP API calls and/or config file sections are just ways of representing these
  4. // data types.
  5. //
  6. // See also
  7. //
  8. // Package "api.handlers" for information on the HTTP API calls.
  9. package api
  10. import (
  11. "encoding/json"
  12. "errors"
  13. "net/url"
  14. "maunium.net/go/mautrix/id"
  15. )
  16. // ConfigureAuthRealmRequest is a request to /configureAuthRealm
  17. type ConfigureAuthRealmRequest struct {
  18. // An arbitrary unique identifier for this auth realm. This can be anything.
  19. // Using an existing ID will REPLACE the entire existing AuthRealm with the new information.
  20. ID string
  21. // The type of auth realm. This determines which code is loaded to execute the
  22. // auth realm. It must be a known type. E.g. "github".
  23. Type string
  24. // AuthRealm specific config information. See the docs for the auth realm you're interested in.
  25. Config json.RawMessage
  26. }
  27. // RequestAuthSessionRequest is a request to /requestAuthSession
  28. type RequestAuthSessionRequest struct {
  29. // The realm ID to request a new auth session on. The realm MUST already exist.
  30. RealmID string
  31. // The Matrix user ID requesting the auth session. If the auth is successful,
  32. // this user ID will be associated with the third-party credentials obtained.
  33. UserID id.UserID
  34. // AuthRealm specific config information. See the docs for the auth realm you're interested in.
  35. Config json.RawMessage
  36. }
  37. // ConfigureServiceRequest is a request to /configureService
  38. type ConfigureServiceRequest struct {
  39. // An arbitrary unique identifier for this service. This can be anything.
  40. // Using an existing ID will REPLACE the entire Service with the new information.
  41. ID string
  42. // The type of service. This determines which code is loaded to execute the
  43. // service. It must be a known type, e.g. "github".
  44. Type string
  45. // The user ID of the configured client that this service will use to communicate with Matrix.
  46. // The user MUST already be configured.
  47. UserID id.UserID
  48. // Service-specific config information. See the docs for the service you're interested in.
  49. Config json.RawMessage
  50. }
  51. // A ClientConfig contains the configuration information for a matrix client so that
  52. // Go-NEB can drive it. It forms the HTTP body to /configureClient requests.
  53. type ClientConfig struct {
  54. // The matrix User ID to connect with. E.g. @alice:matrix.org
  55. UserID id.UserID
  56. // A URL with the host and port of the matrix server. E.g. https://matrix.org:8448
  57. HomeserverURL string
  58. // The matrix access token to authenticate the requests with.
  59. AccessToken string
  60. // The device ID for this access token.
  61. DeviceID id.DeviceID
  62. // True to start a sync stream for this user, making this a "syncing client". If false, no
  63. // /sync goroutine will be created and this client won't listen for new events from Matrix. For services
  64. // which only SEND events into Matrix, it may be desirable to set Sync to false to reduce the
  65. // number of goroutines Go-NEB has to maintain. For services which respond to !commands,
  66. // Sync MUST be set to true in order to receive those commands.
  67. Sync bool
  68. // True to automatically join every room this client is invited to.
  69. // This is desirable for services which have !commands as that means anyone can pull the bot
  70. // into the room. It is up to the service to decide which, if any, users to respond to however.
  71. AutoJoinRooms bool
  72. // The desired display name for this client.
  73. // This does not automatically set the display name for this client. See /configureClient.
  74. DisplayName string
  75. // A list of regexes that control which users are allowed to start a SAS verification with this client.
  76. // When a user starts a new SAS verification with us, their user ID has to match one of these regexes
  77. // for the verification process to start.
  78. AcceptVerificationFromUsers []string
  79. }
  80. // A IncomingDecimalSAS contains the decimal SAS as displayed on another device. The SAS consists of three numbers.
  81. type IncomingDecimalSAS struct {
  82. // The matrix User ID of the user that Neb uses in the verification process. E.g. @neb:localhost
  83. UserID id.UserID
  84. // The three numbers that the SAS consists of.
  85. SAS [3]uint
  86. // The matrix User ID of the other user whose device is being verified.
  87. OtherUserID id.UserID
  88. // The matrix Device ID of the other device that is being verified.
  89. OtherDeviceID id.DeviceID
  90. }
  91. // Session contains the complete auth session information for a given user on a given realm.
  92. // They are created for use with ConfigFile.
  93. type Session struct {
  94. SessionID string
  95. RealmID string
  96. UserID id.UserID
  97. Config json.RawMessage
  98. }
  99. // ConfigFile represents config.sample.yaml
  100. type ConfigFile struct {
  101. Clients []ClientConfig
  102. Realms []ConfigureAuthRealmRequest
  103. Services []ConfigureServiceRequest
  104. Sessions []Session
  105. }
  106. // Check validates the /configureService request
  107. func (c *ConfigureServiceRequest) Check() error {
  108. if c.ID == "" || c.Type == "" || c.UserID == "" || c.Config == nil {
  109. return errors.New(`Must supply an "ID", a "Type", a "UserID" and a "Config"`)
  110. }
  111. return nil
  112. }
  113. // Check validates the /configureAuthRealm request
  114. func (c *ConfigureAuthRealmRequest) Check() error {
  115. if c.ID == "" || c.Type == "" || c.Config == nil {
  116. return errors.New(`Must supply a "ID", a "Type" and a "Config"`)
  117. }
  118. return nil
  119. }
  120. // Check validates the session config request
  121. func (c *Session) Check() error {
  122. if c.SessionID == "" || c.UserID == "" || c.RealmID == "" || c.Config == nil {
  123. return errors.New(`Must supply a "SessionID", a "RealmID", a "UserID" and a "Config"`)
  124. }
  125. return nil
  126. }
  127. // Check that the client has supplied the correct fields.
  128. func (c *ClientConfig) Check() error {
  129. if c.UserID == "" || c.HomeserverURL == "" || c.AccessToken == "" {
  130. return errors.New(`Must supply a "UserID", a "HomeserverURL", and an "AccessToken"`)
  131. }
  132. if _, err := url.Parse(c.HomeserverURL); err != nil {
  133. return err
  134. }
  135. return nil
  136. }
  137. // Check that the received SAS data contains the correct fields.
  138. func (c *IncomingDecimalSAS) Check() error {
  139. if c.UserID == "" || c.OtherUserID == "" || c.OtherDeviceID == "" {
  140. return errors.New(`Must supply a "UserID", an "OtherUserID", and an "OtherDeviceID"`)
  141. }
  142. return nil
  143. }
  144. // Check that the request is valid.
  145. func (r *RequestAuthSessionRequest) Check() error {
  146. if r.UserID == "" || r.RealmID == "" || r.Config == nil {
  147. return errors.New(`Must supply a "UserID", a "RealmID" and a "Config"`)
  148. }
  149. return nil
  150. }