Skip to content

Commit 787a7c9

Browse files
committed
add code for loading in secret
1 parent 4554978 commit 787a7c9

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

worker/elm.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ Executable worker
174174
ansi-terminal >= 0.8 && < 0.9,
175175
ansi-wl-pprint >= 0.6.8 && < 0.7,
176176
base >=4.11 && <5,
177+
base64-bytestring,
177178
binary >= 0.8 && < 0.9,
178179
bytestring >= 0.9 && < 0.11,
179180
containers >= 0.5.8.2 && < 0.6,

worker/src/Endpoint/Donate.hs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Data.Aeson ((.:))
1313
import qualified Data.Aeson as Json
1414
import qualified Data.ByteString as BS
1515
import qualified Data.ByteString.Builder as B
16+
import qualified Data.ByteString.Base64 as Base64
1617
import qualified Data.ByteString.Char8 as BSC
1718
import qualified Data.ByteString.Lazy as LBS
1819
import qualified Data.Text as T
@@ -41,21 +42,25 @@ allowedOrigins =
4142
-- GET MANAGER
4243

4344

44-
newtype Manager =
45-
Manager Http.Manager
45+
data Manager =
46+
Manager
47+
{ _manager :: Http.Manager
48+
, _authToken :: BS.ByteString
49+
}
4650

4751

48-
getManager :: IO Manager
49-
getManager =
50-
Manager <$> Http.newManager Http.tlsManagerSettings
52+
getManager :: String -> IO Manager
53+
getManager secret =
54+
do manager <- Http.newManager Http.tlsManagerSettings
55+
return (Manager manager ("Basic " <> Base64.encode (BSC.pack secret)))
5156

5257

5358

5459
-- ENDPOINT
5560

5661

5762
endpoint :: Manager -> Snap ()
58-
endpoint (Manager manager) =
63+
endpoint manager =
5964
Cors.allow POST allowedOrigins $
6065
do amount <- requireParameter "amount" toAmount
6166
frequency <- requireParameter "frequency" toFrequency
@@ -102,16 +107,31 @@ newtype StripeCheckoutSession =
102107
StripeCheckoutSession { _id :: T.Text }
103108

104109

105-
getStripeCheckoutSessionID :: Http.Manager -> Int -> IO (Maybe StripeCheckoutSession)
106-
getStripeCheckoutSessionID manager amount =
110+
getStripeCheckoutSessionID :: Manager -> Int -> IO (Maybe StripeCheckoutSession)
111+
getStripeCheckoutSessionID (Manager manager authToken) amount =
107112
E.handle handleSomeException $
108113
do req0 <- Http.parseRequest "https://api.stripe.com/v1/checkout/sessions"
109-
req1 <- Multi.formDataBody (toOneTimeParts amount) $ req0 { Http.method = Http.methodPost }
114+
req1 <- Multi.formDataBody (toOneTimeParts amount) (configureRequest authToken req0)
110115
Http.withResponse req1 manager $ \response ->
111116
do chunks <- Http.brConsume (Http.responseBody response)
112117
return $ Json.decode $ LBS.fromChunks chunks
113118

114119

120+
-- The "Authorization" header is set based on combining these instructions:
121+
--
122+
-- https://stripe.com/docs/payments/checkout/one-time
123+
-- https://stackoverflow.com/a/35442984
124+
--
125+
-- Setting the -u flag appears to add a base64 encoded "Authorization" header.
126+
--
127+
configureRequest :: BS.ByteString -> Http.Request -> Http.Request
128+
configureRequest authToken req =
129+
req
130+
{ Http.method = Http.methodPost
131+
, Http.requestHeaders = ("Authorization", authToken) : Http.requestHeaders req
132+
}
133+
134+
115135
toOneTimeParts :: Int -> [Multi.Part]
116136
toOneTimeParts amount =
117137
[ Multi.partBS "payment_method_types[]" "card"

worker/src/Main.hs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import Control.Monad (msum)
1010
import qualified Data.ByteString as BS
1111
import Snap.Core
1212
import Snap.Http.Server
13+
import qualified System.Environment as Env
14+
import qualified System.Exit as Exit
15+
import qualified System.IO as IO
1316

1417
import qualified Artifacts
1518
import qualified Cors
@@ -27,7 +30,7 @@ main =
2730
do rArtifacts <- Artifacts.loadRepl
2831
cArtifacts <- Artifacts.loadCompile
2932
errorJS <- Compile.loadErrorJS
30-
manager <- Donate.getManager
33+
manager <- Donate.getManager =<< getSecret
3134
let depsInfo = Artifacts.toDepsInfo cArtifacts
3235

3336
httpServe config $ msum $
@@ -70,3 +73,24 @@ serveDepsInfo json =
7073
Cors.allow GET ["https://elm-lang.org"] $
7174
do modifyResponse $ setContentType "application/json"
7275
writeBS json
76+
77+
78+
79+
-- GET SECRET
80+
81+
82+
getSecret :: IO String
83+
getSecret =
84+
do args <- Env.getArgs
85+
case args of
86+
[secret] ->
87+
return secret
88+
89+
_ ->
90+
do IO.hPutStrLn IO.stderr
91+
"Expecting a secret for /donate page:\n\
92+
\\n\
93+
\ ./run-worker sk_test_abcdefghijklmnopqrstuvwxyz\n\
94+
\\n\
95+
\Needed for handling donations with Stripe."
96+
Exit.exitFailure

0 commit comments

Comments
 (0)