refactor for digest

This commit is contained in:
Ankit Mahato
2025-07-12 18:04:19 +05:30
parent 33acbc8e5e
commit 447cbd4fa7
4 changed files with 54 additions and 49 deletions

View File

@@ -16,3 +16,22 @@ const kHintUsername = "Username";
const kHintPassword = "Password"; const kHintPassword = "Password";
const kHintToken = "Token"; const kHintToken = "Token";
const kInfoDigestUsername =
"Your username for digest authentication. This will be sent to the server for credential verification.";
const kInfoDigestPassword =
"Your password for digest authentication. This is hashed and not sent in plain text to the server.";
const kHintRealm = "Realm";
const kInfoDigestRealm =
"Authentication realm as specified by the server. This defines the protection space for the credentials.";
const kHintNonce = "Nonce";
const kInfoDigestNonce =
"Server-generated random value used to prevent replay attacks.";
const kAlgorithm = "Algorithm";
const kTooltipAlgorithm = "Algorithm that will be used to produce the digest";
const kHintQop = "QOP";
const kInfoDigestQop =
"Quality of Protection. Typically 'auth' for authentication only, or 'auth-int' for authentication with integrity protection.";
const kHintDataString = "Opaque";
const kInfoDigestDataString =
"Server-specified data string that should be returned unchanged in the authorization header. Usually obtained from server's 401 response.";

View File

@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:apidash_core/apidash_core.dart'; import 'package:apidash_core/apidash_core.dart';
import 'package:apidash_design_system/apidash_design_system.dart'; import 'package:apidash_design_system/apidash_design_system.dart';
import 'package:apidash/widgets/widgets.dart'; import 'package:apidash/widgets/widgets.dart';
import 'consts.dart';
class DigestAuthFields extends StatefulWidget { class DigestAuthFields extends StatefulWidget {
final AuthModel? authData; final AuthModel? authData;
@@ -36,8 +37,8 @@ class _DigestAuthFieldsState extends State<DigestAuthFields> {
_passwordController = TextEditingController(text: digest?.password ?? ''); _passwordController = TextEditingController(text: digest?.password ?? '');
_realmController = TextEditingController(text: digest?.realm ?? ''); _realmController = TextEditingController(text: digest?.realm ?? '');
_nonceController = TextEditingController(text: digest?.nonce ?? ''); _nonceController = TextEditingController(text: digest?.nonce ?? '');
_algorithmController = digest?.algorithm ?? 'MD5'; _algorithmController = digest?.algorithm ?? kDigestAlgos[0];
_qopController = TextEditingController(text: digest?.qop ?? 'auth'); _qopController = TextEditingController(text: digest?.qop ?? kQop[0]);
_opaqueController = TextEditingController(text: digest?.opaque ?? ''); _opaqueController = TextEditingController(text: digest?.opaque ?? '');
} }
@@ -50,42 +51,38 @@ class _DigestAuthFieldsState extends State<DigestAuthFields> {
AuthTextField( AuthTextField(
readOnly: widget.readOnly, readOnly: widget.readOnly,
controller: _usernameController, controller: _usernameController,
hintText: "Username", hintText: kHintUsername,
infoText: infoText: kInfoDigestUsername,
"Your username for digest authentication. This will be sent to the server for credential verification.",
onChanged: (_) => _updateDigestAuth(), onChanged: (_) => _updateDigestAuth(),
), ),
const SizedBox(height: 12), const SizedBox(height: 12),
AuthTextField( AuthTextField(
readOnly: widget.readOnly, readOnly: widget.readOnly,
controller: _passwordController, controller: _passwordController,
hintText: "Password", hintText: kHintPassword,
isObscureText: true, isObscureText: true,
infoText: infoText: kInfoDigestPassword,
"Your password for digest authentication. This is hashed and not sent in plain text to the server.",
onChanged: (_) => _updateDigestAuth(), onChanged: (_) => _updateDigestAuth(),
), ),
const SizedBox(height: 12), const SizedBox(height: 12),
AuthTextField( AuthTextField(
readOnly: widget.readOnly, readOnly: widget.readOnly,
controller: _realmController, controller: _realmController,
hintText: "Realm", hintText: kHintRealm,
infoText: infoText: kInfoDigestRealm,
"Authentication realm as specified by the server. This defines the protection space for the credentials.",
onChanged: (_) => _updateDigestAuth(), onChanged: (_) => _updateDigestAuth(),
), ),
const SizedBox(height: 12), const SizedBox(height: 12),
AuthTextField( AuthTextField(
readOnly: widget.readOnly, readOnly: widget.readOnly,
controller: _nonceController, controller: _nonceController,
hintText: "Nonce", hintText: kHintNonce,
infoText: infoText: kInfoDigestNonce,
"Server-generated random value used to prevent replay attacks.",
onChanged: (_) => _updateDigestAuth(), onChanged: (_) => _updateDigestAuth(),
), ),
const SizedBox(height: 12), const SizedBox(height: 12),
Text( Text(
"Algorithm", kAlgorithm,
style: TextStyle( style: TextStyle(
fontWeight: FontWeight.normal, fontWeight: FontWeight.normal,
fontSize: 14, fontSize: 14,
@@ -94,13 +91,8 @@ class _DigestAuthFieldsState extends State<DigestAuthFields> {
SizedBox(height: 4), SizedBox(height: 4),
ADPopupMenu<String>( ADPopupMenu<String>(
value: _algorithmController.trim(), value: _algorithmController.trim(),
values: const [ values: kDigestAlgos.map((i) => (i, null)),
('MD5', 'MD5'), tooltip: kTooltipAlgorithm,
('MD5-sess', 'MD5-sess'),
('SHA-256', 'SHA-256'),
('SHA-256-sess', 'SHA-256-sess'),
],
tooltip: "Algorithm that will be used to produce the digest",
isOutlined: true, isOutlined: true,
onChanged: widget.readOnly onChanged: widget.readOnly
? null ? null
@@ -117,18 +109,16 @@ class _DigestAuthFieldsState extends State<DigestAuthFields> {
AuthTextField( AuthTextField(
readOnly: widget.readOnly, readOnly: widget.readOnly,
controller: _qopController, controller: _qopController,
hintText: "QOP", hintText: kHintQop,
infoText: infoText: kInfoDigestQop,
"Quality of Protection. Typically 'auth' for authentication only, or 'auth-int' for authentication with integrity protection.",
onChanged: (_) => _updateDigestAuth(), onChanged: (_) => _updateDigestAuth(),
), ),
const SizedBox(height: 12), const SizedBox(height: 12),
AuthTextField( AuthTextField(
readOnly: widget.readOnly, readOnly: widget.readOnly,
controller: _opaqueController, controller: _opaqueController,
hintText: "Opaque", hintText: kHintDataString,
infoText: infoText: kInfoDigestDataString,
"Server-specified data string that should be returned unchanged in the authorization header. Usually obtained from server's 401 response.",
onChanged: (_) => _updateDigestAuth(), onChanged: (_) => _updateDigestAuth(),
), ),
], ],
@@ -137,29 +127,22 @@ class _DigestAuthFieldsState extends State<DigestAuthFields> {
} }
void _updateDigestAuth() { void _updateDigestAuth() {
final digest = AuthDigestModel(
username: _usernameController.text.trim(),
password: _passwordController.text.trim(),
realm: _realmController.text.trim(),
nonce: _nonceController.text.trim(),
algorithm: _algorithmController.trim(),
qop: _qopController.text.trim(),
opaque: _opaqueController.text.trim(),
);
widget.updateAuth(widget.authData?.copyWith( widget.updateAuth(widget.authData?.copyWith(
type: APIAuthType.digest, type: APIAuthType.digest,
digest: AuthDigestModel( digest: digest,
username: _usernameController.text.trim(),
password: _passwordController.text.trim(),
realm: _realmController.text.trim(),
nonce: _nonceController.text.trim(),
algorithm: _algorithmController.trim(),
qop: _qopController.text.trim(),
opaque: _opaqueController.text.trim(),
),
) ?? ) ??
AuthModel( AuthModel(
type: APIAuthType.digest, type: APIAuthType.digest,
digest: AuthDigestModel( digest: digest,
username: _usernameController.text.trim(),
password: _passwordController.text.trim(),
realm: _realmController.text.trim(),
nonce: _nonceController.text.trim(),
algorithm: _algorithmController.trim(),
qop: _qopController.text.trim(),
opaque: _opaqueController.text.trim(),
),
)); ));
} }
} }

View File

@@ -23,6 +23,9 @@ enum APIAuthType {
final String displayType; final String displayType;
} }
const kDigestAlgos = ['MD5', 'MD5-sess', 'SHA-256', 'SHA-256-sess'];
const kQop = ['auth', 'auth-int'];
enum HTTPVerb { enum HTTPVerb {
get("GET"), get("GET"),
head("HEAD"), head("HEAD"),

View File

@@ -1,8 +1,8 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:math' as math; import 'dart:math' as math;
import 'package:convert/convert.dart'; import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart' as crypto; import 'package:crypto/crypto.dart' as crypto;
import '../../consts.dart';
import '../../models/models.dart'; import '../../models/models.dart';
Map<String, String>? splitAuthenticateHeader(String header) { Map<String, String>? splitAuthenticateHeader(String header) {
@@ -130,7 +130,7 @@ Map<String, String?> computeResponse(
if (qop == null) { if (qop == null) {
final token3 = '$ha1:$nonce:$ha2'; final token3 = '$ha1:$nonce:$ha2';
ret['response'] = md5Hash(token3); ret['response'] = md5Hash(token3);
} else if (qop == 'auth' || qop == 'auth-int') { } else if (kQop.contains(qop)) {
final token3 = '$ha1:$nonce:$nonceCount:$cnonce:$qop:$ha2'; final token3 = '$ha1:$nonce:$nonceCount:$cnonce:$qop:$ha2';
ret['response'] = md5Hash(token3); ret['response'] = md5Hash(token3);
} }
@@ -138,7 +138,7 @@ Map<String, String?> computeResponse(
if (qop == null) { if (qop == null) {
final token3 = '$ha1:$nonce:$ha2'; final token3 = '$ha1:$nonce:$ha2';
ret['response'] = sha256Hash(token3); ret['response'] = sha256Hash(token3);
} else if (qop == 'auth' || qop == 'auth-int') { } else if (kQop.contains(qop)) {
final token3 = '$ha1:$nonce:$nonceCount:$cnonce:$qop:$ha2'; final token3 = '$ha1:$nonce:$nonceCount:$cnonce:$qop:$ha2';
ret['response'] = sha256Hash(token3); ret['response'] = sha256Hash(token3);
} }