@@ -90,57 +90,63 @@ Relation of REST and OCS
90
90
There is a close relationship between REST APIs and :ref: `OCS <ocscontroller >`.
91
91
Both provide a way to transmit data between the backend of the app in the Nextcloud server and some frontend.
92
92
93
- The following combinations of attributes might be useful for various scenarios:
93
+ The following combinations of attributes might be relevant for various scenarios:
94
94
95
95
#. 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
98
97
#. 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.
99
103
100
104
There are different ways a clients might interact with your APIs.
101
105
These ways depend on your API configuration (what you allow) and on which route the request is finally made.
102
106
103
107
- *Access from web frontend * means the user is browses the Nextcloud web frontend with a browser.
104
108
- *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).
108
110
109
- .. list-table :: Comparision of different API types
111
+ .. list-table :: Comparison of different API types
110
112
:header-rows: 1
111
113
:align: center
112
114
113
115
* - Description
114
116
- 1 (plain)
115
- - 2 (no CSRF)
116
- - 3 (CORS)
117
+ - 2 (w/o CSRF)
117
118
- 4 (OCS)
119
+ - 4 (OCS w/o CSRF)
118
120
* - URL prefix (relative to server)
119
- - ``/apps/<appid>/ ``
120
121
- ``/apps/<appid>/ ``
121
122
- ``/apps/<appid>/ ``
122
123
- ``/ocs/v2.php/apps/<appid>/ ``
124
+ - ``/ocs/v2.php/apps/<appid>/ ``
123
125
* - Access from web frontend
124
126
- yes
125
127
- yes (CSRF risk)
126
- - yes (CSRF risk)
127
128
- yes
129
+ - yes (CSRF risk)
128
130
* - Access from external app
129
- - --- (CSRF protection blocks)
130
- - yes
131
- - yes
132
- - yes
133
- * - Access from external web page
134
- - ---
135
131
- ---
136
132
- yes
133
+ - yes (with header [# ]_)
134
+ - yes
135
+ * - Encapsulated data
137
136
- 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.
143
147
144
148
As a rule of thumb one can conclude that OCS provides a good way to handle most use cases including sufficient security checks.
145
149
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.
146
150
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