-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(neon_http_client): Add AuthenticationInterceptor
Signed-off-by: provokateurin <[email protected]>
- Loading branch information
1 parent
8355052
commit 685a517
Showing
5 changed files
with
198 additions
and
8 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
37 changes: 37 additions & 0 deletions
37
..._framework/packages/neon_http_client/lib/src/interceptors/authentication_interceptor.dart
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,37 @@ | ||
import 'package:http/http.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:neon_http_client/src/interceptors/http_interceptor.dart'; | ||
import 'package:nextcloud/nextcloud.dart'; | ||
|
||
@internal | ||
final class AuthenticationInterceptor implements HttpInterceptor { | ||
var _blocked = false; | ||
|
||
@override | ||
bool shouldInterceptRequest(BaseRequest request) { | ||
final authorization = request.headers['authorization']; | ||
return authorization != null && authorization.isNotEmpty && _blocked; | ||
} | ||
|
||
@override | ||
Never interceptRequest({required BaseRequest request}) { | ||
throw DynamiteStatusCodeException(Response('', 401)); | ||
} | ||
|
||
@override | ||
bool shouldInterceptResponse(StreamedResponse response) { | ||
final authorization = response.request?.headers['authorization']; | ||
if (authorization == null || authorization.isEmpty) { | ||
return false; | ||
} | ||
|
||
return response.statusCode == 401 || (response.request?.url.path.endsWith('/index.php/login/v2/poll') ?? false); | ||
} | ||
|
||
@override | ||
StreamedResponse interceptResponse({required StreamedResponse response, required Uri url}) { | ||
_blocked = response.statusCode == 401; | ||
|
||
return response; | ||
} | ||
} |
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
137 changes: 137 additions & 0 deletions
137
...ramework/packages/neon_http_client/test/interceptors/authentication_interceptor_test.dart
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,137 @@ | ||
import 'package:http/http.dart'; | ||
import 'package:neon_http_client/src/interceptors/authentication_interceptor.dart'; | ||
import 'package:nextcloud/nextcloud.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group(AuthenticationInterceptor, () { | ||
test('blocks after unauthenticated response', () async { | ||
final interceptor = AuthenticationInterceptor(); | ||
|
||
final authorizedRequest = Request('GET', Uri()); | ||
authorizedRequest.headers['authorization'] = 'test'; | ||
final authorizedResponse = StreamedResponse( | ||
const Stream.empty(), | ||
401, | ||
request: authorizedRequest, | ||
); | ||
|
||
expect( | ||
interceptor.shouldInterceptRequest(authorizedRequest), | ||
isFalse, | ||
); | ||
|
||
expect( | ||
interceptor.shouldInterceptResponse(authorizedResponse), | ||
isTrue, | ||
); | ||
expect( | ||
interceptor.interceptResponse(response: authorizedResponse, url: authorizedRequest.url), | ||
isA<StreamedResponse>(), | ||
); | ||
|
||
expect( | ||
interceptor.shouldInterceptRequest(authorizedRequest), | ||
isTrue, | ||
); | ||
await expectLater( | ||
() async => interceptor.interceptRequest(request: authorizedRequest), | ||
throwsA(isA<DynamiteStatusCodeException>().having((e) => e.response.statusCode, 'response.statusCode', 401)), | ||
); | ||
}); | ||
|
||
test('unblocks after successful poll', () async { | ||
final interceptor = AuthenticationInterceptor(); | ||
|
||
final authorizedRequest = Request('GET', Uri()); | ||
authorizedRequest.headers['authorization'] = 'test'; | ||
final authorizedResponse = StreamedResponse( | ||
const Stream.empty(), | ||
401, | ||
request: authorizedRequest, | ||
); | ||
|
||
expect( | ||
interceptor.interceptResponse(response: authorizedResponse, url: authorizedRequest.url), | ||
isA<StreamedResponse>(), | ||
); | ||
expect( | ||
interceptor.shouldInterceptRequest(authorizedRequest), | ||
isTrue, | ||
); | ||
|
||
final pollRequest = Request( | ||
'POST', | ||
Uri.parse('https://cloud.example.com:8443/nextcloud/index.php/login/v2/poll'), | ||
); | ||
final pollResponse = StreamedResponse( | ||
const Stream.empty(), | ||
200, | ||
request: pollRequest, | ||
); | ||
|
||
expect( | ||
interceptor.interceptResponse(response: pollResponse, url: pollRequest.url), | ||
isA<StreamedResponse>(), | ||
); | ||
expect( | ||
interceptor.shouldInterceptRequest(authorizedRequest), | ||
isFalse, | ||
); | ||
}); | ||
|
||
test('never blocks requests without authorization', () async { | ||
final interceptor = AuthenticationInterceptor(); | ||
|
||
final unauthorizedRequest = Request('GET', Uri()); | ||
final unauthorizedResponse = StreamedResponse( | ||
const Stream.empty(), | ||
401, | ||
request: unauthorizedRequest, | ||
); | ||
|
||
expect( | ||
interceptor.shouldInterceptRequest(unauthorizedRequest), | ||
isFalse, | ||
); | ||
expect( | ||
interceptor.shouldInterceptResponse(unauthorizedResponse), | ||
isFalse, | ||
); | ||
|
||
final authorizedRequest = Request('GET', Uri()); | ||
authorizedRequest.headers['authorization'] = 'test'; | ||
final authorizedResponse = StreamedResponse( | ||
const Stream.empty(), | ||
401, | ||
request: authorizedRequest, | ||
); | ||
|
||
expect( | ||
interceptor.shouldInterceptRequest(authorizedRequest), | ||
isFalse, | ||
); | ||
expect( | ||
interceptor.shouldInterceptResponse(authorizedResponse), | ||
isTrue, | ||
); | ||
expect( | ||
interceptor.interceptResponse(response: unauthorizedResponse, url: unauthorizedRequest.url), | ||
isA<StreamedResponse>(), | ||
); | ||
expect( | ||
interceptor.shouldInterceptRequest(authorizedRequest), | ||
isTrue, | ||
); | ||
|
||
expect( | ||
interceptor.shouldInterceptRequest(unauthorizedRequest), | ||
isFalse, | ||
); | ||
expect( | ||
interceptor.shouldInterceptResponse(unauthorizedResponse), | ||
isFalse, | ||
); | ||
}); | ||
}); | ||
} |
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