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.

167 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. // The minimum required powerlevel to honour an invite request
  80. MinimumPowerLevel int
  81. }
  82. // A IncomingDecimalSAS contains the decimal SAS as displayed on another device. The SAS consists of three numbers.
  83. type IncomingDecimalSAS struct {
  84. // The matrix User ID of the user that Neb uses in the verification process. E.g. @neb:localhost
  85. UserID id.UserID
  86. // The three numbers that the SAS consists of.
  87. SAS [3]uint
  88. // The matrix User ID of the other user whose device is being verified.
  89. OtherUserID id.UserID
  90. // The matrix Device ID of the other device that is being verified.
  91. OtherDeviceID id.DeviceID
  92. }
  93. // Session contains the complete auth session information for a given user on a given realm.
  94. // They are created for use with ConfigFile.
  95. type Session struct {
  96. SessionID string
  97. RealmID string
  98. UserID id.UserID
  99. Config json.RawMessage
  100. }
  101. // ConfigFile represents config.sample.yaml
  102. type ConfigFile struct {
  103. Clients []ClientConfig
  104. Realms []ConfigureAuthRealmRequest
  105. Services []ConfigureServiceRequest
  106. Sessions []Session
  107. }
  108. // Check validates the /configureService request
  109. func (c *ConfigureServiceRequest) Check() error {
  110. if c.ID == "" || c.Type == "" || c.UserID == "" || c.Config == nil {
  111. return errors.New(`Must supply an "ID", a "Type", a "UserID" and a "Config"`)
  112. }
  113. return nil
  114. }
  115. // Check validates the /configureAuthRealm request
  116. func (c *ConfigureAuthRealmRequest) Check() error {
  117. if c.ID == "" || c.Type == "" || c.Config == nil {
  118. return errors.New(`Must supply a "ID", a "Type" and a "Config"`)
  119. }
  120. return nil
  121. }
  122. // Check validates the session config request
  123. func (c *Session) Check() error {
  124. if c.SessionID == "" || c.UserID == "" || c.RealmID == "" || c.Config == nil {
  125. return errors.New(`Must supply a "SessionID", a "RealmID", a "UserID" and a "Config"`)
  126. }
  127. return nil
  128. }
  129. // Check that the client has supplied the correct fields.
  130. func (c *ClientConfig) Check() error {
  131. if c.UserID == "" || c.HomeserverURL == "" || c.AccessToken == "" {
  132. return errors.New(`Must supply a "UserID", a "HomeserverURL", and an "AccessToken"`)
  133. }
  134. if _, err := url.Parse(c.HomeserverURL); err != nil {
  135. return err
  136. }
  137. return nil
  138. }
  139. // Check that the received SAS data contains the correct fields.
  140. func (c *IncomingDecimalSAS) Check() error {
  141. if c.UserID == "" || c.OtherUserID == "" || c.OtherDeviceID == "" {
  142. return errors.New(`Must supply a "UserID", an "OtherUserID", and an "OtherDeviceID"`)
  143. }
  144. return nil
  145. }
  146. // Check that the request is valid.
  147. func (r *RequestAuthSessionRequest) Check() error {
  148. if r.UserID == "" || r.RealmID == "" || r.Config == nil {
  149. return errors.New(`Must supply a "UserID", a "RealmID" and a "Config"`)
  150. }
  151. return nil
  152. }