mirror of
https://github.com/foss42/apidash.git
synced 2025-12-03 11:27:50 +08:00
refactor: move auth models to better_networking package
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
import 'package:apidash_core/models/auth/auth_jwt_model.dart';
|
||||
import 'package:crypto/crypto.dart';
|
||||
|
||||
String generateJWT(AuthJwtModel jwtAuth) {
|
||||
try {
|
||||
Map<String, dynamic> header;
|
||||
if (jwtAuth.header.isNotEmpty) {
|
||||
try {
|
||||
header = json.decode(jwtAuth.header) as Map<String, dynamic>;
|
||||
} catch (e) {
|
||||
header = {};
|
||||
}
|
||||
} else {
|
||||
header = {};
|
||||
}
|
||||
header['typ'] = header['typ'] ?? 'JWT';
|
||||
header['alg'] = jwtAuth.algorithm;
|
||||
Map<String, dynamic> payload;
|
||||
if (jwtAuth.payload.isNotEmpty) {
|
||||
try {
|
||||
payload = json.decode(jwtAuth.payload) as Map<String, dynamic>;
|
||||
} catch (e) {
|
||||
payload = {};
|
||||
}
|
||||
} else {
|
||||
payload = {};
|
||||
}
|
||||
if (!payload.containsKey('iat')) {
|
||||
payload['iat'] = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
}
|
||||
|
||||
// Encode header and payload
|
||||
final encodedHeader = _base64UrlEncode(utf8.encode(json.encode(header)));
|
||||
final encodedPayload = _base64UrlEncode(utf8.encode(json.encode(payload)));
|
||||
|
||||
// Create signature
|
||||
final signature = _createSignature(
|
||||
'$encodedHeader.$encodedPayload',
|
||||
jwtAuth.secret,
|
||||
jwtAuth.algorithm,
|
||||
jwtAuth.isSecretBase64Encoded,
|
||||
);
|
||||
|
||||
return '$encodedHeader.$encodedPayload.$signature';
|
||||
} catch (e) {
|
||||
throw Exception('Failed to generate JWT: $e');
|
||||
}
|
||||
}
|
||||
|
||||
String _createSignature(
|
||||
String data, String secret, String algorithm, bool isSecretBase64Encoded) {
|
||||
try {
|
||||
Uint8List secretBytes;
|
||||
if (isSecretBase64Encoded) {
|
||||
secretBytes = base64.decode(secret);
|
||||
} else {
|
||||
secretBytes = utf8.encode(secret);
|
||||
}
|
||||
|
||||
final dataBytes = utf8.encode(data);
|
||||
|
||||
switch (algorithm) {
|
||||
case 'HS256':
|
||||
final hmac = Hmac(sha256, secretBytes);
|
||||
final digest = hmac.convert(dataBytes);
|
||||
return _base64UrlEncode(digest.bytes);
|
||||
|
||||
case 'HS384':
|
||||
final hmac = Hmac(sha384, secretBytes);
|
||||
final digest = hmac.convert(dataBytes);
|
||||
return _base64UrlEncode(digest.bytes);
|
||||
|
||||
case 'HS512':
|
||||
final hmac = Hmac(sha512, secretBytes);
|
||||
final digest = hmac.convert(dataBytes);
|
||||
return _base64UrlEncode(digest.bytes);
|
||||
|
||||
default:
|
||||
// Default to HS256
|
||||
final hmac = Hmac(sha256, secretBytes);
|
||||
final digest = hmac.convert(dataBytes);
|
||||
return _base64UrlEncode(digest.bytes);
|
||||
}
|
||||
} catch (e) {
|
||||
// Return placeholder signature if creation fails
|
||||
return _base64UrlEncode(utf8.encode('signature_generation_failed'));
|
||||
}
|
||||
}
|
||||
|
||||
String _base64UrlEncode(List<int> bytes) {
|
||||
return base64Url.encode(bytes).replaceAll('=', '');
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
import 'dart:convert';
|
||||
import 'package:apidash_core/consts.dart';
|
||||
import 'package:apidash_core/models/auth/api_auth_model.dart';
|
||||
import 'package:apidash_core/utils/auth_utils.dart';
|
||||
|
||||
HttpRequestModel handleAuth(HttpRequestModel httpRequestModel,AuthModel? authData) {
|
||||
if (authData == null || authData.type == APIAuthType.none) {
|
||||
return httpRequestModel;
|
||||
}
|
||||
|
||||
List<NameValueModel> updatedHeaders =
|
||||
List.from(httpRequestModel.headers ?? []);
|
||||
List<NameValueModel> updatedParams = List.from(httpRequestModel.params ?? []);
|
||||
List<bool> updatedHeaderEnabledList =
|
||||
List.from(httpRequestModel.isHeaderEnabledList ?? []);
|
||||
List<bool> updatedParamEnabledList =
|
||||
List.from(httpRequestModel.isParamEnabledList ?? []);
|
||||
|
||||
switch (authData.type) {
|
||||
case APIAuthType.basic:
|
||||
if (authData.basic != null) {
|
||||
final basicAuth = authData.basic!;
|
||||
final encoded = base64Encode(
|
||||
utf8.encode('${basicAuth.username}:${basicAuth.password}'));
|
||||
updatedHeaders.add(
|
||||
NameValueModel(name: 'Authorization', value: 'Basic $encoded'));
|
||||
updatedHeaderEnabledList.add(true);
|
||||
}
|
||||
break;
|
||||
|
||||
case APIAuthType.bearer:
|
||||
if (authData.bearer != null) {
|
||||
final bearerAuth = authData.bearer!;
|
||||
updatedHeaders.add(NameValueModel(
|
||||
name: 'Authorization', value: 'Bearer ${bearerAuth.token}'));
|
||||
updatedHeaderEnabledList.add(true);
|
||||
}
|
||||
break;
|
||||
|
||||
case APIAuthType.jwt:
|
||||
if (authData.jwt != null) {
|
||||
final jwtAuth = authData.jwt!;
|
||||
|
||||
// Generate JWT token
|
||||
final jwtToken = generateJWT(jwtAuth);
|
||||
|
||||
if (jwtAuth.addTokenTo == 'header') {
|
||||
// Add to request header with prefix
|
||||
final headerValue = jwtAuth.headerPrefix.isNotEmpty
|
||||
? '${jwtAuth.headerPrefix} $jwtToken'
|
||||
: jwtToken;
|
||||
updatedHeaders
|
||||
.add(NameValueModel(name: 'Authorization', value: headerValue));
|
||||
updatedHeaderEnabledList.add(true);
|
||||
} else if (jwtAuth.addTokenTo == 'query') {
|
||||
// Add to query parameters(if selected)
|
||||
final paramKey = jwtAuth.queryParamKey.isNotEmpty
|
||||
? jwtAuth.queryParamKey
|
||||
: 'token';
|
||||
updatedParams.add(NameValueModel(name: paramKey, value: jwtToken));
|
||||
updatedParamEnabledList.add(true);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case APIAuthType.apiKey:
|
||||
if (authData.apikey != null) {
|
||||
final apiKeyAuth = authData.apikey!;
|
||||
if (apiKeyAuth.location == 'header') {
|
||||
updatedHeaders.add(
|
||||
NameValueModel(name: apiKeyAuth.name, value: apiKeyAuth.key));
|
||||
updatedHeaderEnabledList.add(true);
|
||||
} else if (apiKeyAuth.location == 'query') {
|
||||
updatedParams.add(
|
||||
NameValueModel(name: apiKeyAuth.name, value: apiKeyAuth.key));
|
||||
updatedParamEnabledList.add(true);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case APIAuthType.none:
|
||||
break;
|
||||
case APIAuthType.digest:
|
||||
// TODO: Handle this case.
|
||||
throw UnimplementedError();
|
||||
case APIAuthType.oauth1:
|
||||
// TODO: Handle this case.
|
||||
throw UnimplementedError();
|
||||
case APIAuthType.oauth2:
|
||||
// TODO: Handle this case.
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
return httpRequestModel.copyWith(
|
||||
headers: updatedHeaders,
|
||||
params: updatedParams,
|
||||
isHeaderEnabledList: updatedHeaderEnabledList,
|
||||
isParamEnabledList: updatedParamEnabledList,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user