feat(http): add new middleware feature
This commit is contained in:
@@ -17,14 +17,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:wyatt_http_client/src/authentication/basic_authentication_client.dart';
|
||||
import 'package:wyatt_http_client/src/authentication/bearer_authentication_client.dart';
|
||||
import 'package:wyatt_http_client/src/authentication/digest_authentication_client.dart';
|
||||
import 'package:wyatt_http_client/src/authentication/refresh_token_client.dart';
|
||||
import 'package:wyatt_http_client/src/authentication/unsafe_authentication_client.dart';
|
||||
import 'package:wyatt_http_client/src/rest_client.dart';
|
||||
import 'package:wyatt_http_client/src/utils/header_keys.dart';
|
||||
import 'package:wyatt_http_client/src/utils/protocols.dart';
|
||||
import 'package:wyatt_http_client/wyatt_http_client.dart';
|
||||
|
||||
String lastToken = '';
|
||||
int token = 0;
|
||||
@@ -42,7 +35,7 @@ Future<void> handleBasic(HttpRequest req) async {
|
||||
|
||||
Future<void> handleBasicNegotiate(HttpRequest req) async {
|
||||
if (req.headers.value('Authorization') == null) {
|
||||
req.response.statusCode = HttpStatus.unauthorized;
|
||||
req.response.statusCode = HttpStatus.unauthorized.statusCode;
|
||||
req.response.headers.set(HeaderKeys.wwwAuthenticate, 'Basic realm="Wyatt"');
|
||||
print(req.response.headers.value('WWW-Authenticate'));
|
||||
return req.response.close();
|
||||
@@ -56,7 +49,7 @@ Future<void> handleBearer(HttpRequest req) async {
|
||||
|
||||
Future<void> handleDigest(HttpRequest req) async {
|
||||
if (req.headers.value('Authorization') == null) {
|
||||
req.response.statusCode = HttpStatus.unauthorized;
|
||||
req.response.statusCode = HttpStatus.unauthorized.statusCode;
|
||||
req.response.headers.set(
|
||||
'WWW-Authenticate',
|
||||
'Digest realm="Wyatt", '
|
||||
@@ -110,7 +103,7 @@ Future<void> handleOauth2RefreshToken(HttpRequest req) async {
|
||||
return req.response.close();
|
||||
} else {
|
||||
lastToken = receivedToken;
|
||||
req.response.statusCode = HttpStatus.unauthorized;
|
||||
req.response.statusCode = HttpStatus.unauthorized.statusCode;
|
||||
return req.response.close();
|
||||
}
|
||||
default:
|
||||
@@ -160,13 +153,13 @@ Future<void> server() async {
|
||||
print('Authorized');
|
||||
error = 0;
|
||||
} else {
|
||||
request.response.statusCode = HttpStatus.unauthorized;
|
||||
request.response.statusCode = HttpStatus.unauthorized.statusCode;
|
||||
}
|
||||
break;
|
||||
case '/test/oauth2-test-timeout':
|
||||
error++;
|
||||
print('Error $error');
|
||||
request.response.statusCode = HttpStatus.unauthorized;
|
||||
request.response.statusCode = HttpStatus.unauthorized.statusCode;
|
||||
break;
|
||||
case '/test/oauth2-login':
|
||||
if (request.method == 'POST') {
|
||||
@@ -189,12 +182,12 @@ Future<void> server() async {
|
||||
}
|
||||
break;
|
||||
case '/test/oauth2-refresh-error':
|
||||
request.response.statusCode = HttpStatus.unauthorized;
|
||||
request.response.statusCode = HttpStatus.unauthorized.statusCode;
|
||||
break;
|
||||
|
||||
default:
|
||||
print(' => Unknown path or method');
|
||||
request.response.statusCode = HttpStatus.notFound;
|
||||
request.response.statusCode = HttpStatus.notFound.statusCode;
|
||||
}
|
||||
request.response.close();
|
||||
print('====================');
|
||||
@@ -204,73 +197,98 @@ Future<void> server() async {
|
||||
Future<void> main() async {
|
||||
unawaited(server());
|
||||
final base = 'localhost:8080';
|
||||
final restClient = RestClient(protocol: Protocols.http, authority: base);
|
||||
final uriPrefix = UriPrefixMiddleware(
|
||||
protocol: Protocols.http,
|
||||
authority: base,
|
||||
);
|
||||
final jsonEncoder = BodyToJsonMiddleware();
|
||||
final logger = SimpleLoggerMiddleware();
|
||||
|
||||
// Basic
|
||||
final basic = BasicAuthenticationClient(
|
||||
final basicAuth = BasicAuthMiddleware(
|
||||
username: 'username',
|
||||
password: 'password',
|
||||
inner: restClient,
|
||||
);
|
||||
final basic = MiddlewareClient(
|
||||
pipeline: Pipeline.fromIterable([
|
||||
uriPrefix,
|
||||
basicAuth,
|
||||
logger,
|
||||
]),
|
||||
);
|
||||
await basic.get(Uri.parse('/test/basic-test'));
|
||||
|
||||
// Basic with negotiate
|
||||
final basicWithNegotiate = BasicAuthenticationClient(
|
||||
username: 'username',
|
||||
password: 'password',
|
||||
preemptive: false,
|
||||
inner: restClient,
|
||||
);
|
||||
await basicWithNegotiate.get(Uri.parse('/test/basic-test-with-negotiate'));
|
||||
|
||||
// Digest
|
||||
final digest = DigestAuthenticationClient(
|
||||
final digestAuth = DigestAuthMiddleware(
|
||||
username: 'Mufasa',
|
||||
password: 'Circle Of Life',
|
||||
inner: restClient,
|
||||
);
|
||||
final digest = MiddlewareClient(
|
||||
pipeline: Pipeline.fromIterable([
|
||||
uriPrefix,
|
||||
digestAuth,
|
||||
logger,
|
||||
]),
|
||||
);
|
||||
await digest.get(Uri.parse('/test/digest-test'));
|
||||
|
||||
// Bearer
|
||||
final bearer = BearerAuthenticationClient(
|
||||
token: 'access-token-test',
|
||||
inner: restClient,
|
||||
);
|
||||
await bearer.get(Uri.parse('/test/bearer-test'));
|
||||
// // Bearer
|
||||
// final bearer = BearerAuthenticationClient(
|
||||
// token: 'access-token-test',
|
||||
// inner: restClient,
|
||||
// );
|
||||
// await bearer.get(Uri.parse('/test/bearer-test'));
|
||||
|
||||
// API Key
|
||||
final apiKey = BearerAuthenticationClient(
|
||||
token: 'awesome-api-key',
|
||||
authenticationMethod: 'ApiKey',
|
||||
inner: restClient,
|
||||
);
|
||||
await apiKey.get(Uri.parse('/test/apikey-test'));
|
||||
// // API Key
|
||||
// final apiKey = BearerAuthenticationClient(
|
||||
// token: 'awesome-api-key',
|
||||
// authenticationMethod: 'ApiKey',
|
||||
// inner: restClient,
|
||||
// );
|
||||
// await apiKey.get(Uri.parse('/test/apikey-test'));
|
||||
|
||||
// Unsafe URL
|
||||
final unsafe = UnsafeAuthenticationClient(
|
||||
final unsafeAuth = UnsafeAuthMiddleware(
|
||||
username: 'Mufasa',
|
||||
password: 'Circle Of Life',
|
||||
inner: restClient,
|
||||
);
|
||||
final unsafe = MiddlewareClient(
|
||||
pipeline: Pipeline.fromIterable([
|
||||
uriPrefix,
|
||||
unsafeAuth,
|
||||
logger,
|
||||
]),
|
||||
);
|
||||
await unsafe.get(Uri.parse('/test/unsafe-test'));
|
||||
|
||||
// OAuth2
|
||||
final refreshToken = RefreshTokenClient(
|
||||
final refreshTokenAuth = RefreshTokenAuthMiddleware(
|
||||
authorizationEndpoint: '/test/oauth2-test?action=login',
|
||||
tokenEndpoint: '/test/oauth2-test?action=refresh',
|
||||
accessTokenParser: (body) => body['accessToken']! as String,
|
||||
refreshTokenParser: (body) => body['refreshToken']! as String,
|
||||
inner: restClient,
|
||||
);
|
||||
final refreshToken = MiddlewareClient(
|
||||
pipeline: Pipeline.fromIterable([
|
||||
uriPrefix,
|
||||
jsonEncoder,
|
||||
refreshTokenAuth,
|
||||
logger,
|
||||
]),
|
||||
);
|
||||
await refreshToken.get(Uri.parse('/test/oauth2-test'));
|
||||
await refreshToken.authorize(<String, String>{
|
||||
'username': 'username',
|
||||
'password': 'password',
|
||||
});
|
||||
// Login
|
||||
await refreshToken.post(
|
||||
Uri.parse('/test/oauth2-test'),
|
||||
body: <String, String>{
|
||||
'username': 'username',
|
||||
'password': 'password',
|
||||
},
|
||||
);
|
||||
await refreshToken.get(Uri.parse('/test/oauth2-test'));
|
||||
await refreshToken.refresh();
|
||||
await refreshToken.get(Uri.parse('/test/oauth2-test'));
|
||||
await refreshToken.get(Uri.parse('/test/oauth2-test?action=access-denied'));
|
||||
// await refreshToken.refresh();
|
||||
// await refreshToken.get(Uri.parse('/test/oauth2-test'));
|
||||
// await refreshToken.get(Uri.parse('/test/oauth2-test?action=access-denied'));
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ import 'dart:convert';
|
||||
|
||||
import 'package:wyatt_http_client/src/middleware_client.dart';
|
||||
import 'package:wyatt_http_client/src/middlewares/body_to_json_middleware.dart';
|
||||
import 'package:wyatt_http_client/src/middlewares/refresh_token_middleware.dart';
|
||||
import 'package:wyatt_http_client/src/middlewares/refresh_token_auth_middleware.dart';
|
||||
import 'package:wyatt_http_client/src/middlewares/simple_logger_middleware.dart';
|
||||
import 'package:wyatt_http_client/src/middlewares/uri_prefix_middleware.dart';
|
||||
import 'package:wyatt_http_client/src/pipeline.dart';
|
||||
@@ -354,7 +354,6 @@ class FastAPI {
|
||||
|
||||
void main(List<String> args) async {
|
||||
final Pipeline pipeline = Pipeline()
|
||||
.addMiddleware(SimpleLoggerMiddleware())
|
||||
.addMiddleware(
|
||||
UriPrefixMiddleware(
|
||||
protocol: Protocols.http,
|
||||
@@ -363,39 +362,37 @@ void main(List<String> args) async {
|
||||
)
|
||||
.addMiddleware(BodyToJsonMiddleware())
|
||||
.addMiddleware(
|
||||
RefreshTokenMiddleware(
|
||||
RefreshTokenAuthMiddleware(
|
||||
authorizationEndpoint: '/api/v1/auth/sign-in-with-password',
|
||||
tokenEndpoint: '/api/v1/auth/refresh',
|
||||
accessTokenParser: (body) => body['access_token']! as String,
|
||||
refreshTokenParser: (body) => body['refresh_token']! as String,
|
||||
unauthorized: HttpStatus.forbidden,
|
||||
),
|
||||
);
|
||||
)
|
||||
.addMiddleware(SimpleLoggerMiddleware());
|
||||
|
||||
print(pipeline.getLogic());
|
||||
print(pipeline);
|
||||
final client = MiddlewareClient(pipeline: pipeline);
|
||||
|
||||
final api = FastAPI(
|
||||
client: client,
|
||||
);
|
||||
|
||||
// await api.sendSignUpCode('git@pcl.ovh');
|
||||
// final verifiedAccount = await api.verifyCode(
|
||||
// VerifyCode(
|
||||
// email: 'git@pcl.ovh',
|
||||
// verificationCode: '000000000',
|
||||
// action: EmailVerificationAction.signUp,
|
||||
// ),
|
||||
// );
|
||||
// print(verifiedAccount);
|
||||
// final registeredAccount = await api.signUp(
|
||||
// SignUp(sessionId: verifiedAccount.sessionId ?? '', password: 'password'),
|
||||
// );
|
||||
// print(registeredAccount);
|
||||
await api.sendSignUpCode('git@pcl.ovh');
|
||||
final verifiedAccount = await api.verifyCode(
|
||||
VerifyCode(
|
||||
email: 'git@pcl.ovh',
|
||||
verificationCode: '000000000',
|
||||
action: EmailVerificationAction.signUp,
|
||||
),
|
||||
);
|
||||
final registeredAccount = await api.signUp(
|
||||
SignUp(sessionId: verifiedAccount.sessionId ?? '', password: 'password'),
|
||||
);
|
||||
final signedInAccount = await api.signInWithPassword(
|
||||
Login(email: 'git@pcl.ovh', password: 'password'),
|
||||
);
|
||||
// print(signedInAccount);
|
||||
final accountList = await api.getAccountList();
|
||||
print(accountList);
|
||||
}
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
Future<void> main(List<String> args) async {
|
||||
// final client = Oauth2Client(
|
||||
// accessToken: 'test-token',
|
||||
// inner: RestClient(protocol: Protocols.http, authority: 'localhost:80'),
|
||||
// );
|
||||
// final client = RestClient(
|
||||
// protocol: Protocols.http,
|
||||
// authority: 'localhost:80',
|
||||
// inner: Oauth2Client(
|
||||
// authorizationEndpoint: '/api/v1/account/test',
|
||||
// tokenEndpoint: '/api/v1/account/test',
|
||||
// accessToken: 'test-token',
|
||||
// refreshToken: 'refresh-token',
|
||||
// ),
|
||||
// );
|
||||
// final client = RestClient(protocol: Protocols.http, authority: 'localhost:80');
|
||||
// final client =AwesomeRestClient(protocol: Protocols.http, authority: 'localhost:80');
|
||||
// var r = await client.post(
|
||||
// Uri.parse('/api/v1/account/test'),
|
||||
// body: <String, String>{
|
||||
// 'email': 'test@test.fr',
|
||||
// },
|
||||
// );
|
||||
}
|
||||
@@ -17,8 +17,8 @@
|
||||
|
||||
import 'package:wyatt_http_client/src/middleware_client.dart';
|
||||
import 'package:wyatt_http_client/src/middlewares/body_to_json_middleware.dart';
|
||||
import 'package:wyatt_http_client/src/middlewares/refresh_token_middleware.dart';
|
||||
import 'package:wyatt_http_client/src/middlewares/simple_logger_middleware.dart';
|
||||
import 'package:wyatt_http_client/src/middlewares/unsafe_auth_middleware.dart';
|
||||
import 'package:wyatt_http_client/src/middlewares/uri_prefix_middleware.dart';
|
||||
import 'package:wyatt_http_client/src/pipeline.dart';
|
||||
import 'package:wyatt_http_client/src/utils/protocols.dart';
|
||||
@@ -117,8 +117,8 @@ import 'package:wyatt_http_client/src/utils/protocols.dart';
|
||||
// }
|
||||
|
||||
Future<void> main(List<String> args) async {
|
||||
final UnsafeAuthMiddleware auth = UnsafeAuthMiddleware();
|
||||
final Pipeline pipeline = Pipeline()
|
||||
.addMiddleware(SimpleLoggerMiddleware())
|
||||
.addMiddleware(
|
||||
UriPrefixMiddleware(
|
||||
protocol: Protocols.http,
|
||||
@@ -127,17 +127,33 @@ Future<void> main(List<String> args) async {
|
||||
)
|
||||
.addMiddleware(BodyToJsonMiddleware())
|
||||
.addMiddleware(
|
||||
RefreshTokenMiddleware(
|
||||
authorizationEndpoint: '/api/v1/account/test?action=authorize',
|
||||
tokenEndpoint: '/api/v1/account/test?action=refresh',
|
||||
accessTokenParser: (body) => body['access_token']! as String,
|
||||
refreshTokenParser: (body) => body['refresh_token']! as String,
|
||||
UnsafeAuthMiddleware(
|
||||
username: 'wyatt',
|
||||
password: 'motdepasse',
|
||||
),
|
||||
);
|
||||
)
|
||||
.addMiddleware(SimpleLoggerMiddleware());
|
||||
// .addMiddleware(
|
||||
// RefreshTokenMiddleware(
|
||||
// authorizationEndpoint: '/api/v1/account/test?action=authorize',
|
||||
// tokenEndpoint: '/api/v1/account/test?action=refresh',
|
||||
// accessTokenParser: (body) => body['access_token']! as String,
|
||||
// refreshTokenParser: (body) => body['refresh_token']! as String,
|
||||
// ),
|
||||
// );
|
||||
|
||||
print(pipeline.getLogic());
|
||||
print(pipeline);
|
||||
final client = MiddlewareClient(pipeline: pipeline);
|
||||
final r = await client.post(
|
||||
await client.post(
|
||||
Uri.parse('/api/v1/account/test'),
|
||||
body: <String, String>{
|
||||
'email': 'test@test.fr',
|
||||
},
|
||||
);
|
||||
auth
|
||||
..username = 'username'
|
||||
..password = 'password';
|
||||
await client.post(
|
||||
Uri.parse('/api/v1/account/test'),
|
||||
body: <String, String>{
|
||||
'email': 'test@test.fr',
|
||||
|
||||
Reference in New Issue
Block a user