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.

137 lines
5.1 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. )
  15. // ConfigureAuthRealmRequest is a request to /configureAuthRealm
  16. type ConfigureAuthRealmRequest struct {
  17. // An arbitrary unique identifier for this auth realm. This can be anything.
  18. // Using an existing ID will REPLACE the entire existing AuthRealm with the new information.
  19. ID string
  20. // The type of auth realm. This determines which code is loaded to execute the
  21. // auth realm. It must be a known type. E.g. "github".
  22. Type string
  23. // AuthRealm specific config information. See the docs for the auth realm you're interested in.
  24. Config json.RawMessage
  25. }
  26. // RequestAuthSessionRequest is a request to /requestAuthSession
  27. type RequestAuthSessionRequest struct {
  28. // The realm ID to request a new auth session on. The realm MUST already exist.
  29. RealmID string
  30. // The Matrix user ID requesting the auth session. If the auth is successful,
  31. // this user ID will be associated with the third-party credentials obtained.
  32. UserID string
  33. // AuthRealm specific config information. See the docs for the auth realm you're interested in.
  34. Config json.RawMessage
  35. }
  36. // ConfigureServiceRequest is a request to /configureService
  37. type ConfigureServiceRequest struct {
  38. // An arbitrary unique identifier for this service. This can be anything.
  39. // Using an existing ID will REPLACE the entire Service with the new information.
  40. ID string
  41. // The type of service. This determines which code is loaded to execute the
  42. // service. It must be a known type, e.g. "github".
  43. Type string
  44. // The user ID of the configured client that this service will use to communicate with Matrix.
  45. // The user MUST already be configured.
  46. UserID string
  47. // Service-specific config information. See the docs for the service you're interested in.
  48. Config json.RawMessage
  49. }
  50. // A ClientConfig contains the configuration information for a matrix client so that
  51. // Go-NEB can drive it. It forms the HTTP body to /configureClient requests.
  52. type ClientConfig struct {
  53. // The matrix User ID to connect with. E.g. @alice:matrix.org
  54. UserID string
  55. // A URL with the host and port of the matrix server. E.g. https://matrix.org:8448
  56. HomeserverURL string
  57. // The matrix access token to authenticate the requests with.
  58. AccessToken string
  59. // True to start a sync stream for this user, making this a "syncing client". If false, no
  60. // /sync goroutine will be created and this client won't listen for new events from Matrix. For services
  61. // which only SEND events into Matrix, it may be desirable to set Sync to false to reduce the
  62. // number of goroutines Go-NEB has to maintain. For services which respond to !commands,
  63. // Sync MUST be set to true in order to receive those commands.
  64. Sync bool
  65. // True to automatically join every room this client is invited to.
  66. // This is desirable for services which have !commands as that means anyone can pull the bot
  67. // into the room. It is up to the service to decide which, if any, users to respond to however.
  68. AutoJoinRooms bool
  69. // The desired display name for this client.
  70. // This does not automatically set the display name for this client. See /configureClient.
  71. DisplayName string
  72. }
  73. // Session contains the complete auth session information for a given user on a given realm.
  74. // They are created for use with ConfigFile.
  75. type Session struct {
  76. SessionID string
  77. RealmID string
  78. UserID string
  79. Config json.RawMessage
  80. }
  81. // ConfigFile represents config.sample.yaml
  82. type ConfigFile struct {
  83. Clients []ClientConfig
  84. Realms []ConfigureAuthRealmRequest
  85. Services []ConfigureServiceRequest
  86. Sessions []Session
  87. }
  88. // Check validates the /configureService request
  89. func (c *ConfigureServiceRequest) Check() error {
  90. if c.ID == "" || c.Type == "" || c.UserID == "" || c.Config == nil {
  91. return errors.New(`Must supply an "ID", a "Type", a "UserID" and a "Config"`)
  92. }
  93. return nil
  94. }
  95. // Check validates the /configureAuthRealm request
  96. func (c *ConfigureAuthRealmRequest) Check() error {
  97. if c.ID == "" || c.Type == "" || c.Config == nil {
  98. return errors.New(`Must supply a "ID", a "Type" and a "Config"`)
  99. }
  100. return nil
  101. }
  102. // Check validates the session config request
  103. func (c *Session) Check() error {
  104. if c.SessionID == "" || c.UserID == "" || c.RealmID == "" || c.Config == nil {
  105. return errors.New(`Must supply a "SessionID", a "RealmID", a "UserID" and a "Config"`)
  106. }
  107. return nil
  108. }
  109. // Check that the client has supplied the correct fields.
  110. func (c *ClientConfig) Check() error {
  111. if c.UserID == "" || c.HomeserverURL == "" || c.AccessToken == "" {
  112. return errors.New(`Must supply a "UserID", a "HomeserverURL", and an "AccessToken"`)
  113. }
  114. if _, err := url.Parse(c.HomeserverURL); err != nil {
  115. return err
  116. }
  117. return nil
  118. }
  119. // Check that the request is valid.
  120. func (r *RequestAuthSessionRequest) Check() error {
  121. if r.UserID == "" || r.RealmID == "" || r.Config == nil {
  122. return errors.New(`Must supply a "UserID", a "RealmID" and a "Config"`)
  123. }
  124. return nil
  125. }