logo

pleroma

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

o_auth_app_operation.ex (7121B)


  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.Admin.OAuthAppOperation do
  5. alias OpenApiSpex.Operation
  6. alias OpenApiSpex.Schema
  7. alias Pleroma.Web.ApiSpec.Schemas.ApiError
  8. import Pleroma.Web.ApiSpec.Helpers
  9. def open_api_operation(action) do
  10. operation = String.to_existing_atom("#{action}_operation")
  11. apply(__MODULE__, operation, [])
  12. end
  13. def index_operation do
  14. %Operation{
  15. summary: "Retrieve a list of OAuth applications",
  16. tags: ["OAuth application management"],
  17. operationId: "AdminAPI.OAuthAppController.index",
  18. security: [%{"oAuth" => ["admin:write"]}],
  19. parameters: [
  20. Operation.parameter(:name, :query, %Schema{type: :string}, "App name"),
  21. Operation.parameter(:client_id, :query, %Schema{type: :string}, "Client ID"),
  22. Operation.parameter(:page, :query, %Schema{type: :integer, default: 1}, "Page"),
  23. Operation.parameter(
  24. :trusted,
  25. :query,
  26. %Schema{type: :boolean, default: false},
  27. "Trusted apps"
  28. ),
  29. Operation.parameter(
  30. :page_size,
  31. :query,
  32. %Schema{type: :integer, default: 50},
  33. "Number of apps to return"
  34. )
  35. | admin_api_params()
  36. ],
  37. responses: %{
  38. 200 =>
  39. Operation.response("List of apps", "application/json", %Schema{
  40. type: :object,
  41. properties: %{
  42. apps: %Schema{type: :array, items: oauth_app()},
  43. count: %Schema{type: :integer},
  44. page_size: %Schema{type: :integer}
  45. },
  46. example: %{
  47. "apps" => [
  48. %{
  49. "id" => 1,
  50. "name" => "App name",
  51. "client_id" => "yHoDSiWYp5mPV6AfsaVOWjdOyt5PhWRiafi6MRd1lSk",
  52. "client_secret" => "nLmis486Vqrv2o65eM9mLQx_m_4gH-Q6PcDpGIMl6FY",
  53. "redirect_uri" => "https://example.com/oauth-callback",
  54. "website" => "https://example.com",
  55. "trusted" => true
  56. }
  57. ],
  58. "count" => 1,
  59. "page_size" => 50
  60. }
  61. })
  62. }
  63. }
  64. end
  65. def create_operation do
  66. %Operation{
  67. tags: ["OAuth application management"],
  68. summary: "Create an OAuth application",
  69. operationId: "AdminAPI.OAuthAppController.create",
  70. requestBody: request_body("Parameters", create_request()),
  71. parameters: admin_api_params(),
  72. security: [%{"oAuth" => ["admin:write"]}],
  73. responses: %{
  74. 200 => Operation.response("App", "application/json", oauth_app()),
  75. 400 => Operation.response("Bad Request", "application/json", ApiError)
  76. }
  77. }
  78. end
  79. def update_operation do
  80. %Operation{
  81. tags: ["OAuth application management"],
  82. summary: "Update OAuth application",
  83. operationId: "AdminAPI.OAuthAppController.update",
  84. parameters: [id_param() | admin_api_params()],
  85. security: [%{"oAuth" => ["admin:write"]}],
  86. requestBody: request_body("Parameters", update_request()),
  87. responses: %{
  88. 200 => Operation.response("App", "application/json", oauth_app()),
  89. 400 =>
  90. Operation.response("Bad Request", "application/json", %Schema{
  91. oneOf: [ApiError, %Schema{type: :string}]
  92. })
  93. }
  94. }
  95. end
  96. def delete_operation do
  97. %Operation{
  98. tags: ["OAuth application management"],
  99. summary: "Delete OAuth application",
  100. operationId: "AdminAPI.OAuthAppController.delete",
  101. parameters: [id_param() | admin_api_params()],
  102. security: [%{"oAuth" => ["admin:write"]}],
  103. responses: %{
  104. 204 => no_content_response(),
  105. 400 => no_content_response()
  106. }
  107. }
  108. end
  109. defp create_request do
  110. %Schema{
  111. title: "oAuthAppCreateRequest",
  112. type: :object,
  113. required: [:name, :redirect_uris],
  114. properties: %{
  115. name: %Schema{type: :string, description: "Application Name"},
  116. scopes: %Schema{type: :array, items: %Schema{type: :string}, description: "oAuth scopes"},
  117. redirect_uris: %Schema{
  118. type: :string,
  119. description:
  120. "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."
  121. },
  122. website: %Schema{
  123. type: :string,
  124. nullable: true,
  125. description: "A URL to the homepage of the app"
  126. },
  127. trusted: %Schema{
  128. type: :boolean,
  129. nullable: true,
  130. default: false,
  131. description: "Is the app trusted?"
  132. }
  133. },
  134. example: %{
  135. "name" => "My App",
  136. "redirect_uris" => "https://myapp.com/auth/callback",
  137. "website" => "https://myapp.com/",
  138. "scopes" => ["read", "write"],
  139. "trusted" => true
  140. }
  141. }
  142. end
  143. defp update_request do
  144. %Schema{
  145. title: "oAuthAppUpdateRequest",
  146. type: :object,
  147. properties: %{
  148. name: %Schema{type: :string, description: "Application Name"},
  149. scopes: %Schema{type: :array, items: %Schema{type: :string}, description: "oAuth scopes"},
  150. redirect_uris: %Schema{
  151. type: :string,
  152. description:
  153. "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."
  154. },
  155. website: %Schema{
  156. type: :string,
  157. nullable: true,
  158. description: "A URL to the homepage of the app"
  159. },
  160. trusted: %Schema{
  161. type: :boolean,
  162. nullable: true,
  163. default: false,
  164. description: "Is the app trusted?"
  165. }
  166. },
  167. example: %{
  168. "name" => "My App",
  169. "redirect_uris" => "https://myapp.com/auth/callback",
  170. "website" => "https://myapp.com/",
  171. "scopes" => ["read", "write"],
  172. "trusted" => true
  173. }
  174. }
  175. end
  176. defp oauth_app do
  177. %Schema{
  178. title: "oAuthApp",
  179. type: :object,
  180. properties: %{
  181. id: %Schema{type: :integer},
  182. name: %Schema{type: :string},
  183. client_id: %Schema{type: :string},
  184. client_secret: %Schema{type: :string},
  185. redirect_uri: %Schema{type: :string},
  186. website: %Schema{type: :string, nullable: true},
  187. trusted: %Schema{type: :boolean}
  188. },
  189. example: %{
  190. "id" => 123,
  191. "name" => "My App",
  192. "client_id" => "TWhM-tNSuncnqN7DBJmoyeLnk6K3iJJ71KKXxgL1hPM",
  193. "client_secret" => "ZEaFUFmF0umgBX1qKJDjaU99Q31lDkOU8NutzTOoliw",
  194. "redirect_uri" => "https://myapp.com/oauth-callback",
  195. "website" => "https://myapp.com/",
  196. "trusted" => false
  197. }
  198. }
  199. end
  200. def id_param do
  201. Operation.parameter(:id, :path, :integer, "App ID",
  202. example: 1337,
  203. required: true
  204. )
  205. end
  206. end