extractCookie ::
     ByteString
  -> Maybe CTime
  -> ByteString
  -> W.Request
  -> Maybe (AuthCookie, Cookies)
extractCookie key now name req = do
  (_, cookies) <- find ((==) hCookie . fst) $ W.requestHeaders req
  (auth, others) <- discriminate cookies
  case cookieDecode key auth of
    Left _ -> Nothing
    Right cookie ->
      if maybe True (acExpiry cookie >) now
        then Just (cookie, others)
        else Nothing
  where
    discriminate cs =
      case partition ((==) name . fst) $ parseCookies cs of
        ((_, x):_, xs) -> Just (x, xs)
        _ -> Nothing
extractCookie ::
     ByteString
  -> Maybe CTime
  -> ByteString
  -> W.Request
  -> Maybe (AuthCookie, Cookies)
extractCookie key now name req = do
  (_, cookies) <- find ((==) hCookie . fst) $ W.requestHeaders req
  (auth, others) <- discriminate cookies
  case cookieDecode key auth of
    Left _ -> Nothing
    Right cookie ->
      if maybe True (acExpiry cookie >) now
        then Just (cookie, others)
        else Nothing
  where
    discriminate cs =
      case partition ((==) name . fst) $ parseCookies cs of
        ((_, x):_, xs) -> Just (x, xs)
        _ -> Nothing
extractCookie key now name req = do
  (_, cookies) <- find ((==) hCookie . fst) $ W.requestHeaders req
  (auth, others) <- discriminate cookies
  case cookieDecode key auth of
    Left _ -> Nothing
    Right cookie ->
      if maybe True (acExpiry cookie >) now
        then Just (cookie, others)
        else Nothing
  where
    discriminate cs =
      case partition ((==) name . fst) $ parseCookies cs of
        ((_, x):_, xs) -> Just (x, xs)
        _ -> Nothing