apps.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package mastodon
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "net/url"
  7. "path"
  8. "strings"
  9. )
  10. // AppConfig is a setting for registering applications.
  11. type AppConfig struct {
  12. http.Client
  13. Server string
  14. ClientName string
  15. // Where the user should be redirected after authorization (for no redirect, use urn:ietf:wg:oauth:2.0:oob)
  16. RedirectURIs string
  17. // This can be a space-separated list of items listed on the /settings/applications/new page of any Mastodon
  18. // instance. "read", "write", and "follow" are top-level scopes that include all the permissions of the more
  19. // specific scopes like "read:favourites", "write:statuses", and "write:follows".
  20. Scopes string
  21. // Optional.
  22. Website string
  23. }
  24. // Application is mastodon application.
  25. type Application struct {
  26. ID string `json:"id"`
  27. RedirectURI string `json:"redirect_uri"`
  28. ClientID string `json:"client_id"`
  29. ClientSecret string `json:"client_secret"`
  30. // AuthURI is not part of the Mastodon API; it is generated by go-mastodon.
  31. AuthURI string `json:"auth_uri,omitempty"`
  32. }
  33. // RegisterApp returns the mastodon application.
  34. func RegisterApp(ctx context.Context, appConfig *AppConfig) (*Application, error) {
  35. params := url.Values{}
  36. params.Set("client_name", appConfig.ClientName)
  37. if appConfig.RedirectURIs == "" {
  38. params.Set("redirect_uris", "urn:ietf:wg:oauth:2.0:oob")
  39. } else {
  40. params.Set("redirect_uris", appConfig.RedirectURIs)
  41. }
  42. params.Set("scopes", appConfig.Scopes)
  43. params.Set("website", appConfig.Website)
  44. u, err := url.Parse(appConfig.Server)
  45. if err != nil {
  46. return nil, err
  47. }
  48. u.Path = path.Join(u.Path, "/api/v1/apps")
  49. req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(params.Encode()))
  50. if err != nil {
  51. return nil, err
  52. }
  53. req = req.WithContext(ctx)
  54. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  55. resp, err := appConfig.Do(req)
  56. if err != nil {
  57. return nil, err
  58. }
  59. defer resp.Body.Close()
  60. if resp.StatusCode != http.StatusOK {
  61. return nil, parseAPIError("bad request", resp)
  62. }
  63. var app Application
  64. err = json.NewDecoder(resp.Body).Decode(&app)
  65. if err != nil {
  66. return nil, err
  67. }
  68. u, err = url.Parse(appConfig.Server)
  69. if err != nil {
  70. return nil, err
  71. }
  72. u.Path = path.Join(u.Path, "/oauth/authorize")
  73. u.RawQuery = url.Values{
  74. "scope": {appConfig.Scopes},
  75. "response_type": {"code"},
  76. "redirect_uri": {app.RedirectURI},
  77. "client_id": {app.ClientID},
  78. }.Encode()
  79. app.AuthURI = u.String()
  80. return &app, nil
  81. }