authenticationRequired ::
     ByteString -> HM.HashMap Text OAuth2Client -> W.Application
authenticationRequired key oa2 req resp = do
  Log.info $ "511 Unauthenticated: " ++ showReq req
  resp $
    W.responseLBS
      networkAuthenticationRequired511
      [(hContentType, "text/html; charset=utf-8")]
      page
  where
    path =
      if W.requestMethod req == methodGet
        then W.rawPathInfo req <> W.rawQueryString req
        else "/"
    state =
      State.encode key $
      "https://" <> fromJust (W.requestHeaderHost req) <> path
    authLink :: Text -> OAuth2Client -> ByteString -> ByteString
    authLink provider oa2c html =
      let u = oauth2AuthorizeURL oa2c state (redirectURL req provider)
          d = pack $ oauth2Description oa2c
       in [qc|{html}<p><a href="{u}">Authenticate with {d}</a></p>|]
    authHtml = HM.foldrWithKey authLink "" oa2
    page =
      fromStrict
        [qc|
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Authentication required</title>
  </head>
  <body style="text-align:center;">
  <h1>Authentication required</h1>
  {authHtml}
  </body>
</html>
|]
authenticationRequired ::
     ByteString -> HM.HashMap Text OAuth2Client -> W.Application
authenticationRequired key oa2 req resp = do
  Log.info $ "511 Unauthenticated: " ++ showReq req
  resp $
    W.responseLBS
      networkAuthenticationRequired511
      [(hContentType, "text/html; charset=utf-8")]
      page
  where
    path =
      if W.requestMethod req == methodGet
        then W.rawPathInfo req <> W.rawQueryString req
        else "/"
    state =
      State.encode key $
      "https://" <> fromJust (W.requestHeaderHost req) <> path
    authLink :: Text -> OAuth2Client -> ByteString -> ByteString
    authLink provider oa2c html =
      let u = oauth2AuthorizeURL oa2c state (redirectURL req provider)
          d = pack $ oauth2Description oa2c
       in [qc|{html}<p><a href="{u}">Authenticate with {d}</a></p>|]
    authHtml = HM.foldrWithKey authLink "" oa2
    page =
      fromStrict
        [qc|
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Authentication required</title>
  </head>
  <body style="text-align:center;">
  <h1>Authentication required</h1>
  {authHtml}
  </body>
</html>
|]
authenticationRequired key oa2 req resp = do
  Log.info $ "511 Unauthenticated: " ++ showReq req
  resp $
    W.responseLBS
      networkAuthenticationRequired511
      [(hContentType, "text/html; charset=utf-8")]
      page
  where
    path =
      if W.requestMethod req == methodGet
        then W.rawPathInfo req <> W.rawQueryString req
        else "/"
    state =
      State.encode key $
      "https://" <> fromJust (W.requestHeaderHost req) <> path
    authLink :: Text -> OAuth2Client -> ByteString -> ByteString
    authLink provider oa2c html =
      let u = oauth2AuthorizeURL oa2c state (redirectURL req provider)
          d = pack $ oauth2Description oa2c
       in [qc|{html}<p><a href="{u}">Authenticate with {d}</a></p>|]
    authHtml = HM.foldrWithKey authLink "" oa2
    page =
      fromStrict
        [qc|
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Authentication required</title>
  </head>
  <body style="text-align:center;">
  <h1>Authentication required</h1>
  {authHtml}
  </body>
</html>
|]