Ask for username and password to authenticate (closes #2)

This commit is contained in:
Matthew Welch 2021-02-04 20:46:39 -08:00
parent 7b8594adef
commit 67bf8f70f2

View File

@ -2,10 +2,8 @@ program downloader;
uses fphttpclient, base64, opensslsockets, fpjson, jsonparser, SysUtils, process, Classes, libtar; uses fphttpclient, base64, opensslsockets, fpjson, jsonparser, SysUtils, process, Classes, libtar;
const
authString = '***REMOVED***';
var var
authString: String;
gameId: Integer; gameId: Integer;
gameData: TJSONData; gameData: TJSONData;
client: TFPHTTPClient; client: TFPHTTPClient;
@ -105,22 +103,51 @@ begin
tarFile.Destroy; tarFile.Destroy;
end; end;
function authenticate(client: TFPHTTPClient): Boolean;
const
url = 'https://rpi.narnian.us';
var
username, password: String;
begin
Write('Enter username: ');
ReadLn(username);
Write('Enter password: ');
ReadLn(password);
authString := username + ':' + password;
client.AddHeader('Authorization', 'Basic '+base64.EncodeStringBase64(authString));
client.AllowRedirect := true;
try
client.Get(url);
authenticate := true;
except
authenticate := false;
end;
end;
begin begin
if ParamCount > 0 then if ParamCount > 0 then
begin begin
gameId := StrToInt(ParamStr(1)); gameId := StrToInt(ParamStr(1));
client := TFPHTTPClient.Create(nil); client := TFPHTTPClient.Create(nil);
client.AddHeader('Authorization', 'Basic '+base64.EncodeStringBase64(authString)); if authenticate(client) then
if getGameData(client, gameId, gameData) then
begin begin
tarFile := downloadGameTar(client, gameId); if getGameData(client, gameId, gameData) then
extractTar(tarFile); begin
DeleteFile(tarFile); tarFile := downloadGameTar(client, gameId);
WriteLn('Download finished'); extractTar(tarFile);
DeleteFile(tarFile);
WriteLn('Download finished');
end
else
begin
WriteLn('Invalid game id given');
end;
end end
else else
begin begin
WriteLn('Invalid game id given'); WriteLn('Invalid username or password');
end; end;
end end
else else