Skip to content

Commit 3b69065

Browse files
committed
CLI token reads should ignore whitespace
1 parent fc25175 commit 3b69065

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

extra/Lamdera/CLI/Login.hs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,23 @@
33

44
module Lamdera.CLI.Login where
55

6-
import Prelude hiding (init)
7-
86
import qualified Data.UUID as UUID
97
import qualified Data.UUID.V4 as UUID
10-
import Control.Monad
11-
import Data.Maybe (fromMaybe)
128
import System.Exit (exitFailure)
13-
import qualified System.IO as IO
9+
import System.FilePath ((</>))
1410

15-
-- HTTP
1611
import qualified Data.Text as T
17-
import qualified Data.ByteString.Builder as BS
18-
import qualified Network.HTTP.Types as Http
19-
import qualified Network.HTTP.Client as Client
20-
import qualified Reporting.Task as Task
2112
import qualified Json.Decode as D
2213
import qualified Json.Encode as E
2314
import qualified Json.String
24-
import qualified Reporting.Exit
2515

2616
import qualified Stuff as PerUserCache
27-
import System.FilePath ((</>))
2817
import qualified Reporting.Doc as D
2918

3019
import Lamdera
3120
import qualified Lamdera.Http
3221
import qualified Lamdera.Project
3322
import qualified Lamdera.Progress as Progress
34-
import StandaloneInstances
3523

3624

3725
run :: () -> () -> IO ()
@@ -43,21 +31,20 @@ run () () = do
4331

4432
token <- do
4533
elmHome <- PerUserCache.getElmHome
46-
existingToken <- readUtf8Text (elmHome </> ".lamdera-cli")
34+
existingToken <- getToken
4735
newToken <- UUID.toText <$> UUID.nextRandom
4836
case existingToken of
4937
Just token -> do
50-
5138
apiSession <- fetchApiSession appName token
5239

5340
case apiSession of
5441
Right "success" -> do
55-
writeUtf8 (elmHome </> ".lamdera-cli") token
42+
writeToken token
5643
Progress.report $ D.fillSep ["───>", D.dullgreen "Logged in!"]
5744

5845
Right _ -> do
5946
Progress.report $ D.fillSep ["───>", D.red "Unexpected response, starting again: ", D.fromChars $ show apiSession ]
60-
remove (elmHome </> ".lamdera-cli")
47+
removeToken
6148
checkApiLoop inProduction appName newToken
6249

6350
Left err -> do
@@ -67,7 +54,7 @@ run () () = do
6754
exitFailure
6855
else do
6956
Progress.report $ D.fillSep ["───>", D.red "Existing token invalid, starting again"]
70-
remove (elmHome </> ".lamdera-cli")
57+
removeToken
7158
checkApiLoop inProduction appName newToken
7259

7360
Nothing -> do
@@ -109,7 +96,7 @@ checkApiLoop inProduction appName token =
10996

11097
"success" -> do
11198
elmHome <- PerUserCache.getElmHome
112-
writeUtf8 (elmHome </> ".lamdera-cli") token
99+
writeToken token
113100
Progress.report $ D.fillSep ["───>", D.dullgreen "Logged in!"]
114101
pure True
115102

@@ -123,15 +110,37 @@ checkApiLoop inProduction appName token =
123110
pure True
124111

125112

113+
tokenFile :: FilePath
114+
tokenFile = ".lamdera-cli"
115+
116+
117+
getToken :: IO (Maybe Text)
118+
getToken = do
119+
elmHome <- PerUserCache.getElmHome
120+
tokenM <- readUtf8Text (elmHome </> tokenFile)
121+
pure $ fmap T.strip tokenM
122+
123+
124+
writeToken :: Text -> IO ()
125+
writeToken token = do
126+
elmHome <- PerUserCache.getElmHome
127+
writeUtf8 (elmHome </> tokenFile) token
128+
129+
130+
removeToken :: IO ()
131+
removeToken = do
132+
elmHome <- PerUserCache.getElmHome
133+
remove (elmHome </> tokenFile)
134+
135+
126136
validateCliToken :: IO Text
127137
validateCliToken = do
128138
appName <- Lamdera.Project.appNameOrThrow
129139
elmHome <- PerUserCache.getElmHome
130-
existingToken <- readUtf8Text (elmHome </> ".lamdera-cli")
140+
existingToken <- getToken
131141

132142
case existingToken of
133143
Just token -> do
134-
135144
apiSession <- fetchApiSession appName token
136145

137146
case apiSession of
@@ -146,12 +155,12 @@ validateCliToken = do
146155

147156
_ -> do
148157
Progress.report $ D.fillSep ["───>", D.red "Invalid CLI auth, please re-run `lamdera login`"]
149-
remove (elmHome </> ".lamdera-cli")
158+
removeToken
150159
exitFailure
151160

152161
_ -> do
153162
Progress.report $ D.fillSep ["───>", D.red "Invalid CLI auth, please re-run `lamdera login`"]
154-
remove (elmHome </> ".lamdera-cli")
163+
removeToken
155164
exitFailure
156165

157166

@@ -161,7 +170,6 @@ validateCliToken = do
161170
exitFailure
162171

163172

164-
165173
fetchApiSession :: Text -> Text -> IO (Either Lamdera.Http.Error Text)
166174
fetchApiSession appName token =
167175
let

extra/Lamdera/CLI/Reset.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import qualified Reporting.Doc as D
1313

1414
import Lamdera
1515
import Lamdera.Progress
16+
import qualified Lamdera.CLI.Login
1617
import qualified Lamdera.Version
1718

1819

@@ -26,7 +27,7 @@ run () () = do
2627
let
2728
elmStuff = (root & withDefault "./") </> "elm-stuff"
2829
lamderaLegacy = (root & withDefault "./") </> "lamdera-stuff"
29-
lamderaCliLogin = elmHome </> ".lamdera-cli"
30+
lamderaCliLogin = elmHome </> Lamdera.CLI.Login.tokenFile
3031

3132
progress "Here is the plan:\n"
3233

0 commit comments

Comments
 (0)