logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

app_operation.ex (4268B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.ApiSpec.AppOperation do
  5. alias OpenApiSpex.Operation
  6. alias OpenApiSpex.Schema
  7. alias Pleroma.Web.ApiSpec.Helpers
  8. alias Pleroma.Web.ApiSpec.Schemas.App
  9. @spec open_api_operation(atom) :: Operation.t()
  10. def open_api_operation(action) do
  11. operation = String.to_existing_atom("#{action}_operation")
  12. apply(__MODULE__, operation, [])
  13. end
  14. @spec create_operation() :: Operation.t()
  15. def create_operation do
  16. %Operation{
  17. tags: ["Applications"],
  18. summary: "Create an application",
  19. description: "Create a new application to obtain OAuth2 credentials",
  20. operationId: "AppController.create",
  21. requestBody: Helpers.request_body("Parameters", create_request(), required: true),
  22. responses: %{
  23. 200 => Operation.response("App", "application/json", App),
  24. 422 =>
  25. Operation.response(
  26. "Unprocessable Entity",
  27. "application/json",
  28. %Schema{
  29. type: :object,
  30. description:
  31. "If a required parameter is missing or improperly formatted, the request will fail.",
  32. properties: %{
  33. error: %Schema{type: :string}
  34. },
  35. example: %{
  36. "error" => "Validation failed: Redirect URI must be an absolute URI."
  37. }
  38. }
  39. )
  40. }
  41. }
  42. end
  43. def verify_credentials_operation do
  44. %Operation{
  45. tags: ["Applications"],
  46. summary: "Verify the application works",
  47. description: "Confirm that the app's OAuth2 credentials work.",
  48. operationId: "AppController.verify_credentials",
  49. security: [%{"oAuth" => ["read"]}],
  50. responses: %{
  51. 200 =>
  52. Operation.response("App", "application/json", %Schema{
  53. type: :object,
  54. description:
  55. "If the Authorization header was provided with a valid token, you should see your app returned as an Application entity.",
  56. properties: %{
  57. name: %Schema{type: :string},
  58. vapid_key: %Schema{type: :string},
  59. website: %Schema{type: :string, nullable: true}
  60. },
  61. example: %{
  62. "name" => "My App",
  63. "vapid_key" =>
  64. "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M=",
  65. "website" => "https://myapp.com/"
  66. }
  67. }),
  68. 422 =>
  69. Operation.response(
  70. "Unauthorized",
  71. "application/json",
  72. %Schema{
  73. type: :object,
  74. description:
  75. "If the Authorization header contains an invalid token, is malformed, or is not present, an error will be returned indicating an authorization failure.",
  76. properties: %{
  77. error: %Schema{type: :string}
  78. },
  79. example: %{
  80. "error" => "The access token is invalid."
  81. }
  82. }
  83. )
  84. }
  85. }
  86. end
  87. defp create_request do
  88. %Schema{
  89. title: "AppCreateRequest",
  90. description: "POST body for creating an app",
  91. type: :object,
  92. properties: %{
  93. client_name: %Schema{type: :string, description: "A name for your application."},
  94. redirect_uris: %Schema{
  95. type: :string,
  96. description:
  97. "Where the user should be redirected after authorization. To display the authorization code to the user instead of redirecting to a web page, use `urn:ietf:wg:oauth:2.0:oob` in this parameter."
  98. },
  99. scopes: %Schema{
  100. type: :string,
  101. description: "Space separated list of scopes",
  102. default: "read"
  103. },
  104. website: %Schema{
  105. type: :string,
  106. nullable: true,
  107. description: "A URL to the homepage of your app"
  108. }
  109. },
  110. required: [:client_name, :redirect_uris],
  111. example: %{
  112. "client_name" => "My App",
  113. "redirect_uris" => "https://myapp.com/auth/callback",
  114. "website" => "https://myapp.com/"
  115. }
  116. }
  117. end
  118. end