xOf` (fromMaybe "" $ lookup hContentType headers)
     && not ("Bearer" `isInfixOf` (fromMaybe "" $ lookup hAuthorization headers))
     && not ("Bearer" `isInfixOf` (fromMaybe "" $ lookup hCookie headers))
     then do
       (req', body) ← getRequestBody req
       let form = fromMaybe [] $ readForm $ S8.concat body ∷ [(ByteString, ByteString)]
       let token = fromMaybe "" $ lookup "access_token" form
       app req' { Wai.requestHeaders = (hAuthorization, "Bearer " ++ token) : headers } respond
     else app req respond
  where getRequestBody rq = do
          -- https://hackage.haskell.org/package/wai-extra-3.0.16.1/docs/src/Network.Wai.Middleware.rquestLogger.html
          let loop front = do
                 bs ← Wai.requestBody rq
                 if S8.null bs
                     then return $ front []
                     else loop $ front . (bs:)
          body ← loop id
          ichunks ← newIORef body
          let rbody = atomicModifyIORef ichunks $ \case
                     [] → ([], S8.empty)
                     x:y → (y, x)
          let rq' = rq { Wai.requestBody = rbody }
          return (rq', body)

supportFormAuth ∷ Wai.Middleware
supportFormAuth app req respond = do
  let headers = Wai.requestHeaders req
  if "urlencoded" `isInfixOf` (fromMaybe "" $ lookup hContentType headers)
     && not ("Bearer" `isInfixOf` (fromMaybe "" $ lookup hAuthorization headers))
     && not ("Bearer" `isInfixOf` (fromMaybe "" $ lookup hCookie headers))
     then do
       (req', body) ← getRequestBody req
       let form = fromMaybe [] $ readForm $ S8.concat body ∷ [(ByteString, ByteString)]
       let token = fromMaybe "" $ lookup "access_token" form
       app req' { Wai.requestHeaders = (hAuthorization, "Bearer " ++ token) : headers } respond
     else app req respond
  where getRequestBody rq = do
          -- https://hackage.haskell.org/package/wai-extra-3.0.16.1/docs/src/Network.Wai.Middleware.rquestLogger.html
          let loop front = do
                 bs ← Wai.requestBody rq
                 if S8.null bs
                     then return $ front []
                     else loop $ front . (bs:)
          body ← loop id
          ichunks ← newIORef body
          let rbody = atomicModifyIORef ichunks $ \case
                     [] → ([], S8.empty)
                     x:y → (y, x)
          let rq' = rq { Wai.requestBody = rbody }
          return (rq', body)
supportFormAuth app req respond = do
  let headers = Wai.requestHeaders req
  if "urlencoded" `isInfixOf` (fromMaybe "" $ lookup hContentType headers)
     && not ("Bearer" `isInfixOf` (fromMaybe "" $ lookup hAuthorization headers))
     && not ("Bearer" `isInfixOf` (fromMaybe "" $ lookup hCookie headers))
     then do
       (req', body) ← getRequestBody req
       let form = fromMaybe [] $ readForm $ S8.concat body ∷ [(ByteString, ByteString)]
       let token = fromMaybe "" $ lookup "access_token" form
       app req' { Wai.requestHeaders = (hAuthorization, "Bearer " ++ token) : headers } respond
     else app req respond
  where getRequestBody rq = do
          -- https://hackage.haskell.org/package/wai-extra-3.0.16.1/docs/src/Network.Wai.Middleware.rquestLogger.html
          let loop front = do
                 bs ← Wai.requestBody rq
                 if S8.null bs
                     then return $ front []
                     else loop $ front . (bs:)
          body ← loop id
          ichunks ← newIORef body
          let rbody = atomicModifyIORef ichunks $ \case
                     [] → ([], S8.empty)
                     x:y → (y, x)
          let rq' = rq { Wai.requestBody = rbody }
          return (rq', body)