From 67bf8f70f21ad3fab25b05361e909a0aa868e5dd Mon Sep 17 00:00:00 2001 From: Matthew Welch Date: Thu, 4 Feb 2021 20:46:39 -0800 Subject: [PATCH] Ask for username and password to authenticate (closes #2) --- downloader.pas | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/downloader.pas b/downloader.pas index d34bfb8..65e3821 100644 --- a/downloader.pas +++ b/downloader.pas @@ -2,10 +2,8 @@ program downloader; uses fphttpclient, base64, opensslsockets, fpjson, jsonparser, SysUtils, process, Classes, libtar; -const - authString = '***REMOVED***'; - var + authString: String; gameId: Integer; gameData: TJSONData; client: TFPHTTPClient; @@ -105,22 +103,51 @@ begin tarFile.Destroy; 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 if ParamCount > 0 then begin gameId := StrToInt(ParamStr(1)); client := TFPHTTPClient.Create(nil); - client.AddHeader('Authorization', 'Basic '+base64.EncodeStringBase64(authString)); - if getGameData(client, gameId, gameData) then + if authenticate(client) then begin - tarFile := downloadGameTar(client, gameId); - extractTar(tarFile); - DeleteFile(tarFile); - WriteLn('Download finished'); + if getGameData(client, gameId, gameData) then + begin + tarFile := downloadGameTar(client, gameId); + extractTar(tarFile); + DeleteFile(tarFile); + WriteLn('Download finished'); + end + else + begin + WriteLn('Invalid game id given'); + end; end else begin - WriteLn('Invalid game id given'); + WriteLn('Invalid username or password'); end; end else