Skip to content

Commit

Permalink
Make authentication mandatory for uploading files to Airsequel
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed Jan 29, 2024
1 parent d4286e4 commit 9ed384e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
27 changes: 23 additions & 4 deletions app/FileUploader.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import Network.HTTP.Client (
httpLbs,
newManager,
parseRequest,
requestHeaders,
)
import Network.HTTP.Client.MultipartFormData (formDataBody, partFileSource)
import Network.HTTP.Client.TLS (tlsManagerSettings)
Expand Down Expand Up @@ -92,6 +93,7 @@ getFilesSorted path = do
& filterM (doesFileExist . (path </>))
<&> ( P.filter (/= ".DS_Store")
>>> P.sort
>>> P.map (path </>)
)


Expand All @@ -107,8 +109,8 @@ createSQLQuery tableName fileData =
<> "RETURNING rowid"


uploadFiles :: Text -> Text -> Text -> [FilePath] -> IO ()
uploadFiles domain dbId tableName paths = do
uploadFiles :: Text -> Text -> Text -> Text -> [FilePath] -> IO ()
uploadFiles domain airseqWriteToken dbId tableName paths = do
manager <- newManager tlsManagerSettings

fileLists <- P.forM paths $ \filePath -> do
Expand Down Expand Up @@ -136,7 +138,10 @@ uploadFiles domain dbId tableName paths = do
initialRequest
{ method = "POST"
, requestBody = RequestBodyLBS $ encode $ object ["query" .= query]
, requestHeaders = [("Content-Type", "application/json")]
, requestHeaders =
[ ("Content-Type", "application/json")
, ("Authorization", "Bearer " <> airseqWriteToken & P.encodeUtf8)
]
}

sqlResponse <- httpLbs sqlRequest manager
Expand Down Expand Up @@ -199,7 +204,21 @@ uploadFiles domain dbId tableName paths = do

fileRes <-
flip httpLbs manager
=<< (fileRequest <&> (\req -> req{method = methodPut}))
=<< ( fileRequest
<&> ( \req ->
req
{ method = methodPut
, requestHeaders =
requestHeaders req
<> [
( "Authorization"
, "Bearer "
<> airseqWriteToken & P.encodeUtf8
)
]
}
)
)

if (fileRes.responseStatus.statusMessage /= "OK")
|| ("error" `BS.isInfixOf` (fileRes.responseBody & BSL.toStrict))
Expand Down
10 changes: 8 additions & 2 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ import Text.RawString.QQ (r)
import Airsequel (saveReposInAirsequel)
import FileUploader (uploadFiles)
import Types (GqlRepoRes (..), Repo (..), SaveStrategy (..))
import Utils (loadGitHubToken)
import Utils (loadAirsWriteToken, loadGitHubToken)


data CliCmd
Expand Down Expand Up @@ -396,7 +396,13 @@ run :: CliCmd -> IO ()
run cliCmd = do
case cliCmd of
FileUpload{domain, dbId, tableName, paths} -> do
uploadFiles (T.pack domain) (T.pack dbId) (T.pack tableName) paths
airsWriteToken <- loadAirsWriteToken
uploadFiles
(T.pack domain)
airsWriteToken
(T.pack dbId)
(T.pack tableName)
paths
--
GithubUpload repoSlug -> do
ghTokenMb <- loadGitHubToken
Expand Down

0 comments on commit 9ed384e

Please sign in to comment.