-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nextcloud): add http.Client that handles the CSRF token for webd…
…av requests Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
71 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import 'package:dynamite_runtime/http_client.dart'; | ||
import 'package:http/http.dart' as http; | ||
|
||
// ignore: do_not_use_environment | ||
const bool _kIsWeb = bool.fromEnvironment('dart.library.js_util'); | ||
|
||
/// A [http.Client] that sends the Nextcloud CSRF token. | ||
/// | ||
/// {@template WebDavCSRFClient} | ||
/// On web we need to send a CSRF token because we also send the cookies. In theory this should not be required as | ||
/// long as we send the OCS-APIRequest header, but the server has a bug that only triggers when you also send the | ||
/// cookies. On non-web platforms we don't send the cookies so we are fine, but on web the browser always does it | ||
/// and therefore we need this workaround. | ||
/// {@endtemplate} | ||
final class WebDavCSRFClient with http.BaseClient { | ||
/// Creates a new CSRF client that executes requests through the given [DynamiteClient]. | ||
WebDavCSRFClient(this._inner); | ||
|
||
final DynamiteClient _inner; | ||
|
||
/// The request token sent by the [WebDavCSRFClient]. | ||
String? _token; | ||
|
||
@override | ||
Future<http.StreamedResponse> send(http.BaseRequest request) async { | ||
if (_kIsWeb) { | ||
if (_token == null) { | ||
final response = await _inner.get(Uri.parse('${_inner.baseURL}/index.php')); | ||
if (response.statusCode >= 300) { | ||
throw DynamiteStatusCodeException( | ||
response.statusCode, | ||
); | ||
} | ||
|
||
_token = RegExp('data-requesttoken="([^"]*)"').firstMatch(response.body)!.group(1); | ||
} | ||
|
||
request.headers.addAll({ | ||
'OCS-APIRequest': 'true', | ||
'requesttoken': _token!, | ||
}); | ||
} | ||
|
||
final response = await _inner.send(request); | ||
|
||
if (response.statusCode >= 300) { | ||
throw DynamiteStatusCodeException( | ||
response.statusCode, | ||
); | ||
} | ||
|
||
return response; | ||
} | ||
} |