Skip to content

Commit 4b76fec

Browse files
Fix OCS description after some research
Signed-off-by: Christian Wolf <[email protected]>
1 parent 0758bc0 commit 4b76fec

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

developer_manual/digging_deeper/rest_apis.rst

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,57 +90,63 @@ Relation of REST and OCS
9090
There is a close relationship between REST APIs and :ref:`OCS <ocscontroller>`.
9191
Both provide a way to transmit data between the backend of the app in the Nextcloud server and some frontend.
9292

93-
The following combinations of attributes might be useful for various scenarios:
93+
The following combinations of attributes might be relevant for various scenarios:
9494

9595
#. Plain frontend route: ``Controller`` class
96-
#. Plain Frontend with CRSF checks disabled: Add the ``#[NoCSRFRequired]`` attribute
97-
#. REST route with CORS enabled: ``Controller`` class and ``#[CORS]`` attribute on the route
96+
#. Plain Frontend with CRSF checks disabled: ``Controller`` class and ``#[NoCSRFRequired]`` attribute on the method
9897
#. OCS-based route: ``OCSController`` class
98+
#. OCS-based route with CSRF disabled: ``OCSController`` class and ``#[NoCSRFRequired]`` attribute on the method
99+
100+
.. warning::
101+
Adding the ``#[NoCRSFRequired]`` attribute imposes a security risk.
102+
You should not add this to your controller methods unless you understand the implications and be sure that you absolutely need the attribute.
99103

100104
There are different ways a clients might interact with your APIs.
101105
These ways depend on your API configuration (what you allow) and on which route the request is finally made.
102106

103107
- *Access from web frontend* means the user is browses the Nextcloud web frontend with a browser.
104108
- *Access from an external app* indicates that the user is not using the normal browser (as logged in) but directly navigates a certain URL.
105-
This can be in a browser tab or an external program (like an Android app or simply a curl command line).
106-
- *Access from external website* means that the user browses some third party web site and *magically* data from your app appears.
107-
Technically, the other website would embed/load/use images, JSON data, or other resources from a URL pointing to the Nextcloud server.
109+
This can be in a new browser tab or an external program (like an Android app or simply a curl command line).
108110

109-
.. list-table:: Comparision of different API types
111+
.. list-table:: Comparison of different API types
110112
:header-rows: 1
111113
:align: center
112114

113115
* - Description
114116
- 1 (plain)
115-
- 2 (no CSRF)
116-
- 3 (CORS)
117+
- 2 (w/o CSRF)
117118
- 4 (OCS)
119+
- 4 (OCS w/o CSRF)
118120
* - URL prefix (relative to server)
119-
- ``/apps/<appid>/``
120121
- ``/apps/<appid>/``
121122
- ``/apps/<appid>/``
122123
- ``/ocs/v2.php/apps/<appid>/``
124+
- ``/ocs/v2.php/apps/<appid>/``
123125
* - Access from web frontend
124126
- yes
125127
- yes (CSRF risk)
126-
- yes (CSRF risk)
127128
- yes
129+
- yes (CSRF risk)
128130
* - Access from external app
129-
- --- (CSRF protection blocks)
130-
- yes
131-
- yes
132-
- yes
133-
* - Access from external web page
134-
- ---
135131
- ---
136132
- yes
133+
- yes (with header [#]_)
134+
- yes
135+
* - Encapsulated data
137136
- no
138-
* - Transmitted data type
139-
- plain data
140-
- plain data
141-
- plain data
142-
- encapsulated data (JSON or XML)
137+
- no
138+
- yes (JSON or XML)
139+
- yes (JSON or XML)
140+
141+
Methods from ``Controller`` classes can return ``DataResponse`` objects similar to ``OCSController`` class methods.
142+
For methods of a ``Controller`` class, the data of this response is sent e.g. as JSON as you provide it.
143+
Basically, the output is very similar to what ``json_encode`` would do.
144+
In contrast, the OCS controller will encapsulate the data in an outer shell that provides some more (meta) information.
145+
For example a status code (similar to the HTTP status code) is transmitted at top level.
146+
The actual data is transmitted in the ``data`` property.
143147

144148
As a rule of thumb one can conclude that OCS provides a good way to handle most use cases including sufficient security checks.
145149
The only exception to this is if you want to provide an API for external usage where you have to comply with an externally defined API scheme.
146150
Here, the encapsulation introduced in OCS and CSRF checks might be in your way.
151+
152+
.. [#] The OCS controller needs the request header ``OCS-APIREQUEST`` to be set to ``true``.

0 commit comments

Comments
 (0)