Prezados estou com dificuldade para Autenticar minha aplicação Delphi 7 indy 10, componente idHttp, para consumir uma Api.
A Autenticação se dá em duas etapas, na primeira tentativa recebo o seguinte:
HTTP/1.1 401 Unauthorized\r\n
WWW-Authenticate: Digest realm="Login to 487e5a44454e904373ec5eb024fa2c69", qop="auth", nonce="1482119029", opaque="18f18dafed9183a0fe929d0d36756a2535547e51"\r\n
Connection: close\r\n
Set-Cookie:secure; HttpOnly\r\n
Através do wireshark, entendi que o sistema deve enviar como resposta o seguinte:
GET /cgi-bin/global.cgi?action=getCurrentTime HTTP/1.1\r\n
User-Agent: PostmanRuntime/7.28.3\r\n
Accept: */*\r\n
Postman-Token: 2c8232b8-3984-424e-b77d-9470a290b16f\r\n
Host: 192.168.0.25\r\n
Accept-Encoding: gzip, deflate, br\r\n
[truncated]Authorization: Digest username="admin", realm="Login to 487e5a44454e904373ec5eb024fa2c69", nonce="1482119029", uri="/cgi-bin/global.cgi?action=getCurrentTime", algorithm="MD5", qop=auth, nc=00000001, cnonce="YvLgEKOV", respons
username="admin"
realm="Login to 487e5a44454e904373ec5eb024fa2c69"
nonce="1482119029"
uri="/cgi-bin/global.cgi?action=getCurrentTime"
algorithm="MD5"
qop=auth
nc=00000001
cnonce="YvLgEKOV"
response="0d4a5a73db732709c6a5bbe956f21f97"
Não estou sabendo a onde eu adiciono estas informações no componente para enviar, segue meu código:
site:='http://'+inIP.Text+'/cgi-bin/global.cgi?action=getCurrentTime';
IdHTTP1.Request.Clear;
IdHTTP1.Request.BasicAuthentication:=False;
IdHTTP1.Request.UserName := frmPrincipal.inUsuario.Text;
IdHTTP1.Request.Password := frmPrincipal.inSenha.Text;
IdHTTP1.Request.ContentType := 'application/json';
IdHTTP1.Request.AcceptEncoding := 'gzip, deflate, br';
IdHTTP1.HandleRedirects := True;
IdHTTP1.AllowCookies := True;
IdHTTP1.ReadTimeout:=3000;
try // Primeira Tentativa
lResponse:=frmPrincipal.IdHTTP1.Get(site);
except
begin
case idHTTP1.ResponseCode of
401 : begin
Memo1.Lines.Add('RawHeaders:');
Memo1.Lines.Add(idHTTP1.Response.RawHeaders.Text);
// Pegar Parâmetros da Resposta para nova Tentativa
vrealm:=TrataString('realm',idHTTP1.Response.RawHeaders.Text);
vqop:=TrataString('qop',idHTTP1.Response.RawHeaders.Text);
vnonce:=TrataString('nonce',idHTTP1.Response.RawHeaders.Text);
vopaque:=TrataString('opaque',idHTTP1.Response.RawHeaders.Text);
IdHTTP1.Request.CustomHeaders.Values['Authorization']:='Digest username="admin",'+
' realm="'+vrealm+'",'+
' nonce="'+vnonce+'",'+
'uri="/cgi-bin/global.cgi?action=getCurrentTime", algorithm="MD5", qop=auth, nc=00000001,'+
'cnonce="2pefWTXe",'+
'response="409244ccc8ae9136798ddb0c661b6558",'+
'opaque="'+vopaque+'"';
try // Segunda Tentativa
lResponse:=frmPrincipal.IdHTTP1.Get(site);
except
end;
Memo1.Lines.Add('Código da Resposta: '+IntToStr(frmPrincipal.idHTTP1.ResponseCode));
end;
end;
end;
end;
result :=lResponse;
function TrataString(campo,linha:String):String;
var n,ref:Integer;
valor:String;
begin
ref:=pos(campo,linha);
if ref<> 0 then
begin
ref:=ref+length(campo)+1;
for n:=ref to length(linha) do
begin
if linha[n]<>',' then
begin
if linha[n]<>'"' then
valor:=valor+linha[n];
end
else
break;
end;
end;
Result:=valor
end;