on master help sign in
Bring the codebase closer to github upstream

* Use http-client instead of req * Provide `executeAppRequest` * Make MVar handling exception-safe

commitd97b97c67a77a76aeb15e76db7a6689dea21f542 authorKirill Elagin <kirelagin@gmail.com> dateFri 10 Aug 2018, 05:10 (8 years ago) parent260138f
4 files changed+224−133
package.yaml+6−2 src/GitHub/App.hs−131 src/GitHub/App/Auth.hs+180 src/GitHub/App/Request.hs+38
package.yaml+6−2
@@ -24,6 +24,7 @@ default-extensions:
2424 - NamedFieldPuns
2525 - OverloadedStrings
2626 - RankNTypes
27+ - RecordWildCards
2728 - ScopedTypeVariables
2829 - StandaloneDeriving
2930 - TemplateHaskell
@@ -39,9 +40,12 @@ library:
3940 - aeson
4041 - bytestring
4142 - crypto-pubkey-types
42− - data-default-class
4343 - github
44+ - http-client
45+ - http-client-tls
46+ - http-types
4447 - jwt
45− - req
48+ - mtl
49+ - safe-exceptions
4650 - text
4751 - time
src/GitHub/App.hsdeleted−131
@@ -1,131 +0,0 @@
1−{- This Source Code Form is subject to the terms of the Mozilla Public
2− - License, v. 2.0. If a copy of the MPL was not distributed with this
3− - file, You can obtain one at http://mozilla.org/MPL/2.0/.
4− -}
5−
6−module GitHub.App
7− ( InstallationAuth
8− , mkInstallationAuth
9−
10− , authenticateInstallation
11− ) where
12−
13−import Prelude hiding (exp)
14−
15−import Control.Concurrent (MVar, newEmptyMVar, putMVar, readMVar, takeMVar)
16−import Control.Monad (void)
17−import Crypto.Types.PubKey.RSA (PrivateKey (..))
18−import Data.Aeson (FromJSON (..), withObject, (.:))
19−import Data.ByteString (ByteString)
20−import Data.Default.Class (def)
21−import Data.Semigroup ((<>))
22−import Data.Text (Text)
23−import qualified Data.Text as T
24−import Data.Text.Encoding (encodeUtf8)
25−import Data.Time (NominalDiffTime, UTCTime, addUTCTime, defaultTimeLocale, diffUTCTime,
26− getCurrentTime, iso8601DateFormat, parseTimeM)
27−import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
28−import GitHub.Auth (Auth (..))
29−import Network.HTTP.Req (NoReqBody (..), Option, POST (..), Url, header, https, jsonResponse, req,
30− responseBody, runReq, (/:))
31−import Web.JWT (JSON, JWTClaimsSet (..), Signer (..), encodeSigned, numericDate, stringOrURI)
32−
33−
34−-- | JWT expiration time. Maximum accepted by GitHub is 10 minutes
35−jwtExpTime :: NominalDiffTime
36−jwtExpTime = 600
37−
38−-- | Installation access token expiration time. It is fixed by GitHub and is equal to 1 hour
39−instKeyExpTime :: NominalDiffTime
40−instKeyExpTime = 3600
41−
42−-- | When to renew the installation access token
43−--
44−-- We renew the access token when it is valid for less than 'bufferTime'
45−-- just to be on the safe side.
46−bufferTime :: NominalDiffTime
47−bufferTime = instKeyExpTime * 0.25
48−
49−
50−-- | Base URL of the GitHub API
51−baseURL :: Text
52−baseURL = "api.github.com"
53−
54−
55−type InstallationId = Text
56−
57−
58−-- | GitHub installation access token
59−data InstallationToken = InstallationToken
60− { itToken :: ByteString
61− , itExpirationTime :: UTCTime
62− } deriving (Show)
63−
64−instance FromJSON InstallationToken where
65− parseJSON = withObject "installation token" $ \o -> InstallationToken
66− <$> (encodeUtf8 <$> o .: "token")
67− <*> (either fail pure =<< parseExpiresAt <$> o .: "expires_at")
68− where
69− parseExpiresAt :: String -> Either String UTCTime
70− parseExpiresAt = parseTimeM True defaultTimeLocale (iso8601DateFormat (Just "%H:%M:%SZ"))
71−
72−-- | Credentials required for an app to authenticate as an installation
73−data InstallationAuth = InstallationAuth
74− { iaAppId :: !Int -- ^ Application id
75− , iaAppPrivateKey :: !PrivateKey -- ^ Private key to sign token requests
76− , iaInstallationId :: !Text -- ^ Installation id
77− , iaToken :: !(MVar InstallationToken) -- ^ Installation Auth token
78− }
79−
80−-- | Return a valid App access token
81−--
82−-- Checks if the cached token is expired and renews it if needed.
83−authenticateInstallation :: InstallationAuth -> IO Auth
84−authenticateInstallation ia@InstallationAuth{iaToken} = do
85− InstallationToken{itToken, itExpirationTime} <- readMVar iaToken
86− currentTime <- getCurrentTime
87− if itExpirationTime `diffUTCTime` currentTime >= bufferTime
88− then return $ OAuth itToken
89− else do
90− void $ takeMVar iaToken
91− renewInstAuthToken ia
92− InstallationToken{itToken = newToken} <- readMVar iaToken
93− return $ OAuth newToken
94−
95−-- | Smart constructor for 'InstallationAuth'
96−mkInstallationAuth :: Int -> PrivateKey -> InstallationId -> IO InstallationAuth
97−mkInstallationAuth applicationId key instId = do
98− tokenVar <- newEmptyMVar
99− let instAuth = InstallationAuth applicationId key instId tokenVar
100− renewInstAuthToken instAuth
101− return instAuth
102−
103−-- | Get a new token from GitHub and cache it in 'InstallationAuth'
104−--
105−-- Assumes that the MVar in 'InstallationAuth' is empty. Otherwise will block.
106−renewInstAuthToken :: InstallationAuth -> IO ()
107−renewInstAuthToken InstallationAuth{iaAppId, iaInstallationId, iaAppPrivateKey, iaToken} = do
108− time <- getCurrentTime
109− let jwt = makeJWT time
110− t <- request (https baseURL /: "installations" /: iaInstallationId /: "access_tokens") mempty jwt
111− putMVar iaToken t
112− where
113− -- | Create a JSON Web Token for the given application id using application's private key
114− makeJWT :: UTCTime -> JSON
115− makeJWT currentTime =
116− let currDate = numericDate . utcTimeToPOSIXSeconds $ currentTime
117− expDate = numericDate . utcTimeToPOSIXSeconds $ jwtExpTime `addUTCTime` currentTime
118− issuer = stringOrURI . T.pack . show $ iaAppId
119− jwtClaimsSet = mempty {iss = issuer, iat = currDate, exp = expDate}
120− in encodeSigned (RSAPrivateKey iaAppPrivateKey) jwtClaimsSet
121−
122− -- | Make a request to GitHub to get an installation Auth token
123− request :: FromJSON m => Url scheme -> Option scheme -> JSON -> IO m
124− request url opts jwt = runReq def $ responseBody <$>
125− req POST url NoReqBody
126− jsonResponse -- specify how to interpret response
127− ( header "Authorization" ("Bearer " <> encodeUtf8 jwt)
128− <> header "Accept" "application/vnd.github.machine-man-preview+json"
129− <> header "user-agent" "Haskell/github-app (Haskell/req)"
130− <> opts
131− )
src/GitHub/App/Auth.hsnew file+180
@@ -0,0 +1,180 @@
1+{- This Source Code Form is subject to the terms of the Mozilla Public
2+ - License, v. 2.0. If a copy of the MPL was not distributed with this
3+ - file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+ -}
5+
6+{-# LANGUAGE CPP #-}
7+{-# LANGUAGE DataKinds #-}
8+
9+module GitHub.App.Auth
10+ ( InstallationAuth
11+ , mkInstallationAuth
12+
13+ , obtainAccessToken
14+
15+ , AppId
16+ , InstallationId
17+ ) where
18+
19+import Prelude hiding (exp)
20+
21+import Control.Concurrent (MVar, newMVar, putMVar, readMVar, takeMVar)
22+import Control.Exception.Safe (bracketOnError, catch)
23+import Control.Monad.Except (MonadError (throwError), ExceptT, runExceptT)
24+import Data.Functor (($>))
25+import Crypto.Types.PubKey.RSA (PrivateKey)
26+import Data.Aeson (FromJSON (..), withObject, (.:))
27+import Data.Maybe (fromMaybe)
28+import Data.Semigroup ((<>))
29+import Data.Text (Text)
30+import qualified Data.Text as T
31+import Data.Text.Encoding (encodeUtf8)
32+import Data.Time (NominalDiffTime, UTCTime, diffUTCTime, getCurrentTime)
33+import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
34+import Control.Monad.Trans (lift)
35+import Data.Word (Word64)
36+import GitHub.Auth (Auth (OAuth))
37+import GitHub.Data.Definitions (Error (HTTPError))
38+import GitHub.Data.Request (StatusMap)
39+import GitHub.Request (parseResponse)
40+import Web.JWT (JWTClaimsSet (exp, iat, iss), Signer (RSAPrivateKey), encodeSigned,
41+ numericDate, stringOrURI)
42+import Network.HTTP.Client as HTTP
43+import qualified Network.HTTP.Client.Internal as HTTP
44+import qualified Network.HTTP.Types as HTTP
45+import qualified Data.ByteString.Lazy as LBS
46+
47+
48+-- | JWT expiration time. Maximum accepted by GitHub is 10 minutes
49+jwtExpTime :: NominalDiffTime
50+jwtExpTime = 600
51+
52+-- | Installation access token expiration time. It is fixed by GitHub and is equal to 1 hour
53+instKeyExpTime :: NominalDiffTime
54+instKeyExpTime = 3600
55+
56+-- | When to renew the installation access token
57+--
58+-- We renew the access token when it is valid for less than 'bufferTime'
59+-- just to be on the safe side.
60+bufferTime :: NominalDiffTime
61+bufferTime = instKeyExpTime * 0.25
62+
63+baseUrl :: Text
64+baseUrl = "https://api.github.com"
65+
66+
67+-- | Cached GitHub installation access token
68+data InstallationToken = InstallationToken
69+ { itToken :: Auth
70+ , itExpirationTime :: UTCTime
71+ } deriving (Show)
72+
73+instance FromJSON InstallationToken where
74+ parseJSON = withObject "Installation access token" $ \o ->
75+ InstallationToken
76+ <$> (OAuth . encodeUtf8 <$> o .: "token")
77+ <*> o .: "expires_at"
78+
79+
80+-- | Credentials required for an app to authenticate as an installation
81+data InstallationAuth = InstallationAuth
82+ { iaClaimsSet :: !JWTClaimsSet -- ^ Prefilled claims set
83+ , iaAppPrivateKey :: !PrivateKey -- ^ Private key to sign token requests
84+ , iaInstallationId :: !Text -- ^ Installation id
85+ , iaToken :: !(MVar (Maybe InstallationToken)) -- ^ Installation Auth token
86+ }
87+
88+
89+type AppId = Word64
90+type InstallationId = Word64
91+
92+-- | Smart constructor for 'InstallationAuth'
93+mkInstallationAuth
94+ :: AppId -- ^ GitHub App ID
95+ -> PrivateKey -- ^ GitHub App Private Key
96+ -> InstallationId -- ^ Installation ID
97+ -> IO InstallationAuth
98+mkInstallationAuth appId pk instId = do
99+ varToken <- newMVar Nothing
100+ let issuer = fromMaybe (error "impossible") . stringOrURI . T.pack . show $ appId
101+ claimsSet = mempty { iss = Just issuer }
102+ pure $ InstallationAuth claimsSet pk (T.pack . show $ instId) varToken
103+
104+-- | Create a request which, when executed, will obtain a new access token
105+createAccessTokenR :: InstallationAuth -> IO HTTP.Request
106+createAccessTokenR InstallationAuth{..} = do
107+ currentTime <- utcTimeToPOSIXSeconds <$> getCurrentTime
108+ let expiryTime = currentTime + jwtExpTime
109+ claims = iaClaimsSet
110+ { iat = Just $ toJsonTime currentTime
111+ , exp = Just $ toJsonTime expiryTime
112+ }
113+ jwt = encodeSigned (RSAPrivateKey iaAppPrivateKey) claims
114+
115+ req <- HTTP.parseRequest . T.unpack $ url
116+ pure req
117+ { HTTP.requestHeaders = [("Authorization", "Bearer " <> encodeUtf8 jwt)]
118+ , HTTP.checkResponse = successOrMissing Nothing
119+ , HTTP.method = "POST"
120+ }
121+ where
122+ toJsonTime = fromMaybe (error "impossible") . numericDate
123+ url = baseUrl <> "/installations/" <> iaInstallationId <> "/access_tokens"
124+
125+-- | Get a valid access token
126+--
127+-- Tries to use the cached one. If it is invalid, requests a new one
128+-- and caches it for future use.
129+obtainAccessToken
130+ :: Manager
131+ -> InstallationAuth
132+ -> IO (Either Error Auth)
133+obtainAccessToken mgr ia@InstallationAuth{..} = readMVar iaToken >>= \case
134+ Nothing -> renew
135+ Just InstallationToken{itToken, itExpirationTime} -> do
136+ currentTime <- getCurrentTime
137+ if itExpirationTime `diffUTCTime` currentTime < bufferTime
138+ then renew
139+ else pure $ Right itToken
140+ where
141+ renew :: IO (Either Error Auth)
142+ renew = bracketOnError (takeMVar iaToken) (putMVar iaToken) $ \_ -> do
143+ req <- createAccessTokenR ia
144+ result <- runExceptT $ httpLbs' req >>= parseResponse
145+ case result of
146+ Right newToken -> putMVar iaToken (Just newToken) $> Right (itToken newToken)
147+ Left err -> putMVar iaToken Nothing $> Left err
148+
149+ httpLbs' :: HTTP.Request -> ExceptT Error IO (Response LBS.ByteString)
150+ httpLbs' req' = lift (httpLbs req' mgr) `catch` onHttpException
151+
152+
153+
154+---------------------------------------
155+-- Copy-pasted from the github package
156+---------------------------------------
157+
158+#if MIN_VERSION_http_client(0,5,0)
159+successOrMissing :: Maybe (StatusMap a) -> HTTP.Request -> HTTP.Response HTTP.BodyReader -> IO ()
160+successOrMissing sm _req res
161+ | check = pure ()
162+ | otherwise = do
163+ chunk <- HTTP.brReadSome (HTTP.responseBody res) 1024
164+ let res' = fmap (const ()) res
165+ HTTP.throwHttp $ HTTP.StatusCodeException res' (LBS.toStrict chunk)
166+ where
167+ HTTP.Status sci _ = HTTP.responseStatus res
168+#else
169+successOrMissing :: Maybe (StatusMap a) -> Status -> ResponseHeaders -> HTTP.CookieJar -> Maybe E.SomeException
170+successOrMissing sm s@(Status sci _) hs cookiejar
171+ | check = Nothing
172+ | otherwise = Just $ E.toException $ StatusCodeException s hs cookiejar
173+ where
174+#endif
175+ check = case sm of
176+ Nothing -> 200 <= sci && sci < 300
177+ Just sm' -> sci `elem` map fst sm'
178+
179+onHttpException :: MonadError Error m => HttpException -> m a
180+onHttpException = throwError . HTTPError
src/GitHub/App/Request.hsnew file+38
@@ -0,0 +1,38 @@
1+{- This Source Code Form is subject to the terms of the Mozilla Public
2+ - License, v. 2.0. If a copy of the MPL was not distributed with this
3+ - file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+ -}
5+
6+{-# LANGUAGE CPP #-}
7+
8+module GitHub.App.Request
9+ ( executeAppRequest
10+ , executeAppRequestWithMgr
11+ ) where
12+
13+import GitHub.Data (Error (..))
14+import GitHub.Data.Request (Request)
15+import GitHub.Request (executeRequestWithMgr)
16+import Network.HTTP.Client (Manager)
17+import Network.HTTP.Client.TLS (newTlsManager)
18+
19+import GitHub.App.Auth (InstallationAuth, obtainAccessToken)
20+
21+
22+executeAppRequest :: InstallationAuth -> Request k a -> IO (Either Error a)
23+executeAppRequest instAuth req = do
24+ manager <- newTlsManager
25+ x <- executeAppRequestWithMgr manager instAuth req
26+#if !MIN_VERSION_http_client(0, 4, 18)
27+ closeManager manager
28+#endif
29+ pure x
30+
31+executeAppRequestWithMgr
32+ :: Manager
33+ -> InstallationAuth
34+ -> Request k a
35+ -> IO (Either Error a)
36+executeAppRequestWithMgr mgr instAuth req = obtainAccessToken mgr instAuth >>= \case
37+ Right auth -> executeRequestWithMgr mgr auth req
38+ Left err -> pure $ Left err