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.

141 lines
5.2 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. }
  76. // Session contains the complete auth session information for a given user on a given realm.
  77. // They are created for use with ConfigFile.
  78. type Session struct {
  79. SessionID string
  80. RealmID string
  81. UserID id.UserID
  82. Config json.RawMessage
  83. }
  84. // ConfigFile represents config.sample.yaml
  85. type ConfigFile struct {
  86. Clients []ClientConfig
  87. Realms []ConfigureAuthRealmRequest
  88. Services []ConfigureServiceRequest
  89. Sessions []Session
  90. }
  91. // Check validates the /configureService request
  92. func (c *ConfigureServiceRequest) Check() error {
  93. if c.ID == "" || c.Type == "" || c.UserID == "" || c.Config == nil {
  94. return errors.New(`Must supply an "ID", a "Type", a "UserID" and a "Config"`)
  95. }
  96. return nil
  97. }
  98. // Check validates the /configureAuthRealm request
  99. func (c *ConfigureAuthRealmRequest) Check() error {
  100. if c.ID == "" || c.Type == "" || c.Config == nil {
  101. return errors.New(`Must supply a "ID", a "Type" and a "Config"`)
  102. }
  103. return nil
  104. }
  105. // Check validates the session config request
  106. func (c *Session) Check() error {
  107. if c.SessionID == "" || c.UserID == "" || c.RealmID == "" || c.Config == nil {
  108. return errors.New(`Must supply a "SessionID", a "RealmID", a "UserID" and a "Config"`)
  109. }
  110. return nil
  111. }
  112. // Check that the client has supplied the correct fields.
  113. func (c *ClientConfig) Check() error {
  114. if c.UserID == "" || c.HomeserverURL == "" || c.AccessToken == "" {
  115. return errors.New(`Must supply a "UserID", a "HomeserverURL", and an "AccessToken"`)
  116. }
  117. if _, err := url.Parse(c.HomeserverURL); err != nil {
  118. return err
  119. }
  120. return nil
  121. }
  122. // Check that the request is valid.
  123. func (r *RequestAuthSessionRequest) Check() error {
  124. if r.UserID == "" || r.RealmID == "" || r.Config == nil {
  125. return errors.New(`Must supply a "UserID", a "RealmID" and a "Config"`)
  126. }
  127. return nil
  128. }