-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/


-- | Hackage security library
--   
--   The hackage security library provides both server and client utilities
--   for securing the Hackage package server
--   (<a>http://hackage.haskell.org/</a>). It is based on The Update
--   Framework (<a>http://theupdateframework.com/</a>), a set of
--   recommendations developed by security researchers at various
--   universities in the US as well as developers on the Tor project
--   (<a>https://www.torproject.org/</a>).
--   
--   The current implementation supports only index signing, thereby
--   enabling untrusted mirrors. It does not yet provide facilities for
--   author package signing.
--   
--   The library has two main entry points: <a>Hackage.Security.Client</a>
--   is the main entry point for clients (the typical example being
--   <tt>cabal</tt>), and <a>Hackage.Security.Server</a> is the main entry
--   point for servers (the typical example being <tt>hackage-server</tt>).
@package hackage-security
@version 0.5.2.2


-- | Checked exceptions
module Hackage.Security.Util.Checked

-- | Checked exceptions
class Throws e
unthrow :: forall a e proxy. proxy e -> (Throws e => a) -> a

-- | Throw a checked exception
throwChecked :: (Exception e, Throws e) => e -> IO a

-- | Catch a checked exception
catchChecked :: forall a e. Exception e => (Throws e => IO a) -> (e -> IO a) -> IO a

-- | <a>catchChecked</a> with the arguments reversed
handleChecked :: Exception e => (e -> IO a) -> (Throws e => IO a) -> IO a

-- | Like <tt>try</tt>, but for checked exceptions
tryChecked :: Exception e => (Throws e => IO a) -> IO (Either e a)

-- | Rethrow IO exceptions as checked exceptions
checkIO :: Throws IOException => IO a -> IO a

-- | Throw an unchecked exception
--   
--   This is just an alias for <tt>throw</tt>, but makes it evident that
--   this is a very intentional use of an unchecked exception.
throwUnchecked :: Exception e => e -> IO a

-- | Variation on <a>throwUnchecked</a> for internal errors
internalError :: String -> IO a
instance Hackage.Security.Util.Checked.Throws (Hackage.Security.Util.Checked.Catch e)


-- | Producing human-reaadable strings
module Hackage.Security.Util.Pretty

-- | Produce a human-readable string
class Pretty a
pretty :: Pretty a => a -> String


-- | A more type-safe version of file paths
--   
--   This module is intended to replace imports of System.FilePath, and
--   additionally exports thin wrappers around common IO functions. To
--   facilitate importing this module unqualified we also re-export some
--   definitions from System.IO (importing both would likely lead to name
--   clashes).
--   
--   Note that his module does not import any other modules from
--   Hackage.Security; everywhere else we use Path instead of FilePath
--   directly.
module Hackage.Security.Util.Path

-- | Paths
--   
--   A <a>Path</a> is simply a <a>FilePath</a> with a type-level tag
--   indicating where this path is rooted (relative to the current
--   directory, absolute path, relative to a web domain, whatever). Most
--   operations on <a>Path</a> are just lifted versions of the operations
--   on the underlying <a>FilePath</a>. The tag however allows us to give a
--   lot of operations a more meaningful type. For instance, it does not
--   make sense to append two absolute paths together; instead, we can only
--   append an unrooted path to another path. It also means we avoid bugs
--   where we use one kind of path where we expect another.
newtype Path a
Path :: FilePath -> Path a

-- | Reinterpret the root of a path
--   
--   This literally just changes the type-level tag; use with caution!
castRoot :: Path root -> Path root'
takeDirectory :: Path a -> Path a
takeFileName :: Path a -> String
(<.>) :: Path a -> String -> Path a
splitExtension :: Path a -> (Path a, String)
takeExtension :: Path a -> String

-- | Type-level tag for unrooted paths
--   
--   Unrooted paths need a root before they can be interpreted.
data Unrooted
(</>) :: Path a -> Path Unrooted -> Path a

-- | Reinterpret an unrooted path
--   
--   This is an alias for <a>castRoot</a>; see comments there.
rootPath :: Path Unrooted -> Path root

-- | Forget a path's root
--   
--   This is an alias for <a>castRoot</a>; see comments there.
unrootPath :: Path root -> Path Unrooted

-- | Convert a relative/unrooted Path to a FilePath (using POSIX style
--   directory separators).
--   
--   See also <a>toAbsoluteFilePath</a>
toUnrootedFilePath :: Path Unrooted -> FilePath

-- | Convert from a relative/unrooted FilePath (using POSIX style directory
--   separators).
fromUnrootedFilePath :: FilePath -> Path Unrooted

-- | A path fragment (like a single directory or filename)
fragment :: String -> Path Unrooted
joinFragments :: [String] -> Path Unrooted
splitFragments :: Path Unrooted -> [String]
isPathPrefixOf :: Path Unrooted -> Path Unrooted -> Bool
data Relative
data Absolute
data HomeDir

-- | A file system root can be interpreted as an (absolute) FilePath
class FsRoot root

-- | Convert a Path to an absolute FilePath (using native style directory
--   separators).
toAbsoluteFilePath :: FsRoot root => Path root -> IO FilePath

-- | Abstract over a file system root
--   
--   see <a>fromFilePath</a>
data FsPath
FsPath :: (Path root) -> FsPath
toFilePath :: Path Absolute -> FilePath
fromFilePath :: FilePath -> FsPath
makeAbsolute :: FsPath -> IO (Path Absolute)
fromAbsoluteFilePath :: FilePath -> Path Absolute

-- | Wrapper around <a>withFile</a>
withFile :: FsRoot root => Path root -> IOMode -> (Handle -> IO r) -> IO r

-- | Wrapper around <tt>openBinaryTempFileWithDefaultPermissions</tt>
--   
--   NOTE: The caller is responsible for cleaning up the temporary file.
openTempFile' :: FsRoot root => Path root -> String -> IO (Path Absolute, Handle)
readLazyByteString :: FsRoot root => Path root -> IO ByteString
readStrictByteString :: FsRoot root => Path root -> IO ByteString
writeLazyByteString :: FsRoot root => Path root -> ByteString -> IO ()
writeStrictByteString :: FsRoot root => Path root -> ByteString -> IO ()
copyFile :: (FsRoot root, FsRoot root') => Path root -> Path root' -> IO ()
createDirectory :: FsRoot root => Path root -> IO ()
createDirectoryIfMissing :: FsRoot root => Bool -> Path root -> IO ()
removeDirectory :: FsRoot root => Path root -> IO ()
doesFileExist :: FsRoot root => Path root -> IO Bool
doesDirectoryExist :: FsRoot root => Path root -> IO Bool
getModificationTime :: FsRoot root => Path root -> IO UTCTime
removeFile :: FsRoot root => Path root -> IO ()
getTemporaryDirectory :: IO (Path Absolute)

-- | Return the immediate children of a directory
--   
--   Filters out <tt>"."</tt> and <tt>".."</tt>.
getDirectoryContents :: FsRoot root => Path root -> IO [Path Unrooted]

-- | Recursive traverse a directory structure
--   
--   Returns a set of paths relative to the directory specified. The list
--   is lazily constructed, so that directories are only read when
--   required. (This is also essential to ensure that this function does
--   not build the entire result in memory before returning, potentially
--   running out of heap.)
getRecursiveContents :: FsRoot root => Path root -> IO [Path Unrooted]
renameFile :: (FsRoot root, FsRoot root') => Path root -> Path root' -> IO ()
getCurrentDirectory :: IO (Path Absolute)
data Tar
tarIndexLookup :: TarIndex -> Path Tar -> Maybe TarIndexEntry
tarAppend :: (FsRoot root, FsRoot root') => Path root -> Path root' -> [Path Tar] -> IO ()
data Web
toURIPath :: FilePath -> Path Web
fromURIPath :: Path Web -> FilePath
uriPath :: URI -> Path Web
modifyUriPath :: URI -> (Path Web -> Path Web) -> URI

-- | See <a>openFile</a>
data IOMode :: *
ReadMode :: IOMode
WriteMode :: IOMode
AppendMode :: IOMode
ReadWriteMode :: IOMode

-- | Three kinds of buffering are supported: line-buffering,
--   block-buffering or no-buffering. These modes have the following
--   effects. For output, items are written out, or <i>flushed</i>, from
--   the internal buffer according to the buffer mode:
--   
--   <ul>
--   <li><i>line-buffering</i>: the entire output buffer is flushed
--   whenever a newline is output, the buffer overflows, a <a>hFlush</a> is
--   issued, or the handle is closed.</li>
--   <li><i>block-buffering</i>: the entire buffer is written out whenever
--   it overflows, a <a>hFlush</a> is issued, or the handle is closed.</li>
--   <li><i>no-buffering</i>: output is written immediately, and never
--   stored in the buffer.</li>
--   </ul>
--   
--   An implementation is free to flush the buffer more frequently, but not
--   less frequently, than specified above. The output buffer is emptied as
--   soon as it has been written out.
--   
--   Similarly, input occurs according to the buffer mode for the handle:
--   
--   <ul>
--   <li><i>line-buffering</i>: when the buffer for the handle is not
--   empty, the next item is obtained from the buffer; otherwise, when the
--   buffer is empty, characters up to and including the next newline
--   character are read into the buffer. No characters are available until
--   the newline character is available or the buffer is full.</li>
--   <li><i>block-buffering</i>: when the buffer for the handle becomes
--   empty, the next block of data is read into the buffer.</li>
--   <li><i>no-buffering</i>: the next input item is read and returned. The
--   <a>hLookAhead</a> operation implies that even a no-buffered handle may
--   require a one-character buffer.</li>
--   </ul>
--   
--   The default buffering mode when a handle is opened is
--   implementation-dependent and may depend on the file system object
--   which is attached to that handle. For most implementations, physical
--   files will normally be block-buffered and terminals will normally be
--   line-buffered.
data BufferMode :: *

-- | buffering is disabled if possible.
NoBuffering :: BufferMode

-- | line-buffering should be enabled if possible.
LineBuffering :: BufferMode

-- | block-buffering should be enabled if possible. The size of the buffer
--   is <tt>n</tt> items if the argument is <a>Just</a> <tt>n</tt> and is
--   otherwise implementation-dependent.
BlockBuffering :: Maybe Int -> BufferMode

-- | Haskell defines operations to read and write characters from and to
--   files, represented by values of type <tt>Handle</tt>. Each value of
--   this type is a <i>handle</i>: a record used by the Haskell run-time
--   system to <i>manage</i> I/O with file system objects. A handle has at
--   least the following properties:
--   
--   <ul>
--   <li>whether it manages input or output or both;</li>
--   <li>whether it is <i>open</i>, <i>closed</i> or
--   <i>semi-closed</i>;</li>
--   <li>whether the object is seekable;</li>
--   <li>whether buffering is disabled, or enabled on a line or block
--   basis;</li>
--   <li>a buffer (whose length may be zero).</li>
--   </ul>
--   
--   Most handles will also have a current I/O position indicating where
--   the next input or output operation will occur. A handle is
--   <i>readable</i> if it manages only input or both input and output;
--   likewise, it is <i>writable</i> if it manages only output or both
--   input and output. A handle is <i>open</i> when first allocated. Once
--   it is closed it can no longer be used for either input or output,
--   though an implementation cannot re-use its storage while references
--   remain to it. Handles are in the <a>Show</a> and <a>Eq</a> classes.
--   The string produced by showing a handle is system dependent; it should
--   include enough information to identify the handle for debugging. A
--   handle is equal according to <a>==</a> only to itself; no attempt is
--   made to compare the internal state of different handles for equality.
data Handle :: *

-- | A mode that determines the effect of <tt>hSeek</tt> <tt>hdl mode
--   i</tt>.
data SeekMode :: *

-- | the position of <tt>hdl</tt> is set to <tt>i</tt>.
AbsoluteSeek :: SeekMode

-- | the position of <tt>hdl</tt> is set to offset <tt>i</tt> from the
--   current position.
RelativeSeek :: SeekMode

-- | the position of <tt>hdl</tt> is set to offset <tt>i</tt> from the end
--   of the file.
SeekFromEnd :: SeekMode

-- | Computation <a>hSetBuffering</a> <tt>hdl mode</tt> sets the mode of
--   buffering for handle <tt>hdl</tt> on subsequent reads and writes.
--   
--   If the buffer mode is changed from <a>BlockBuffering</a> or
--   <a>LineBuffering</a> to <a>NoBuffering</a>, then
--   
--   <ul>
--   <li>if <tt>hdl</tt> is writable, the buffer is flushed as for
--   <a>hFlush</a>;</li>
--   <li>if <tt>hdl</tt> is not writable, the contents of the buffer is
--   discarded.</li>
--   </ul>
--   
--   This operation may fail with:
--   
--   <ul>
--   <li><tt>isPermissionError</tt> if the handle has already been used for
--   reading or writing and the implementation does not allow the buffering
--   mode to be changed.</li>
--   </ul>
hSetBuffering :: Handle -> BufferMode -> IO ()

-- | Computation <a>hClose</a> <tt>hdl</tt> makes handle <tt>hdl</tt>
--   closed. Before the computation finishes, if <tt>hdl</tt> is writable
--   its buffer is flushed as for <a>hFlush</a>. Performing <a>hClose</a>
--   on a handle that has already been closed has no effect; doing so is
--   not an error. All other operations on a closed handle will fail. If
--   <a>hClose</a> fails for any reason, any further operations (apart from
--   <a>hClose</a>) on the handle will still fail as if <tt>hdl</tt> had
--   been successfully closed.
hClose :: Handle -> IO ()

-- | For a handle <tt>hdl</tt> which attached to a physical file,
--   <a>hFileSize</a> <tt>hdl</tt> returns the size of that file in 8-bit
--   bytes.
hFileSize :: Handle -> IO Integer

-- | Computation <a>hSeek</a> <tt>hdl mode i</tt> sets the position of
--   handle <tt>hdl</tt> depending on <tt>mode</tt>. The offset <tt>i</tt>
--   is given in terms of 8-bit bytes.
--   
--   If <tt>hdl</tt> is block- or line-buffered, then seeking to a position
--   which is not in the current buffer will first cause any items in the
--   output buffer to be written to the device, and then cause the input
--   buffer to be discarded. Some handles may not be seekable (see
--   <a>hIsSeekable</a>), or only support a subset of the possible
--   positioning operations (for instance, it may only be possible to seek
--   to the end of a tape, or to a positive offset from the beginning or
--   current position). It is not possible to set a negative I/O position,
--   or for a physical file, an I/O position beyond the current
--   end-of-file.
--   
--   This operation may fail with:
--   
--   <ul>
--   <li><tt>isIllegalOperationError</tt> if the Handle is not seekable, or
--   does not support the requested seek mode.</li>
--   <li><tt>isPermissionError</tt> if a system resource limit would be
--   exceeded.</li>
--   </ul>
hSeek :: Handle -> SeekMode -> Integer -> IO ()
instance GHC.Classes.Ord (Hackage.Security.Util.Path.Path a)
instance GHC.Classes.Eq (Hackage.Security.Util.Path.Path a)
instance GHC.Show.Show (Hackage.Security.Util.Path.Path a)
instance Hackage.Security.Util.Pretty.Pretty (Hackage.Security.Util.Path.Path Hackage.Security.Util.Path.Unrooted)
instance Hackage.Security.Util.Pretty.Pretty (Hackage.Security.Util.Path.Path Hackage.Security.Util.Path.Absolute)
instance Hackage.Security.Util.Pretty.Pretty (Hackage.Security.Util.Path.Path Hackage.Security.Util.Path.Relative)
instance Hackage.Security.Util.Pretty.Pretty (Hackage.Security.Util.Path.Path Hackage.Security.Util.Path.HomeDir)
instance Hackage.Security.Util.Path.FsRoot Hackage.Security.Util.Path.Relative
instance Hackage.Security.Util.Path.FsRoot Hackage.Security.Util.Path.Absolute
instance Hackage.Security.Util.Path.FsRoot Hackage.Security.Util.Path.HomeDir
instance Hackage.Security.Util.Pretty.Pretty (Hackage.Security.Util.Path.Path Hackage.Security.Util.Path.Tar)

module Hackage.Security.Client.Formats
data FormatUn
data FormatGz

-- | Format is a singleton type (reflection type to term level)
--   
--   NOTE: In the future we might add further compression formats.
data Format :: * -> *
[FUn] :: Format FormatUn
[FGz] :: Format FormatGz

-- | Available formats
--   
--   Rather than having a general list here, we enumerate all
--   possibilities. This means we are very precise about what we expect,
--   and we avoid any runtime errors about unexpect format definitions.
--   
--   NOTE: If we add additional cases here (for dealing with additional
--   formats) all calls to <tt>error "inaccessible"</tt> need to be
--   reevaluated.
data Formats :: * -> * -> *
[FsNone] :: Formats () a
[FsUn] :: a -> Formats (FormatUn :- ()) a
[FsGz] :: a -> Formats (FormatGz :- ()) a
[FsUnGz] :: a -> a -> Formats (FormatUn :- (FormatGz :- ())) a

-- | <tt>HasFormat fs f</tt> is a proof that <tt>f</tt> is a key in
--   <tt>fs</tt>.
--   
--   See <a>formatsMember</a> and <a>formatsLookup</a> for typical usage.
data HasFormat :: * -> * -> *
[HFZ] :: Format f -> HasFormat (f :- fs) f
[HFS] :: HasFormat fs f -> HasFormat (f' :- fs) f
hasFormatAbsurd :: HasFormat () f -> a
hasFormatGet :: HasFormat fs f -> Format f
formatsMap :: (forall f. Format f -> a -> b) -> Formats fs a -> Formats fs b
formatsMember :: Format f -> Formats fs a -> Maybe (HasFormat fs f)
formatsLookup :: HasFormat fs f -> Formats fs a -> a
instance GHC.Show.Show (Hackage.Security.Client.Formats.Format f)
instance GHC.Classes.Eq (Hackage.Security.Client.Formats.Format f)
instance GHC.Classes.Eq a => GHC.Classes.Eq (Hackage.Security.Client.Formats.Formats fs a)
instance GHC.Show.Show a => GHC.Show.Show (Hackage.Security.Client.Formats.Formats fs a)
instance GHC.Classes.Eq (Hackage.Security.Client.Formats.HasFormat fs f)
instance GHC.Show.Show (Hackage.Security.Client.Formats.HasFormat fs f)
instance Hackage.Security.Util.TypedEmbedded.Unify Hackage.Security.Client.Formats.Format
instance GHC.Base.Functor (Hackage.Security.Client.Formats.Formats fs)


-- | Hiding existentials
module Hackage.Security.Util.Some
data Some f
Some :: (f a) -> Some f
data DictEq a
[DictEq] :: Eq a => DictEq a

-- | Type <tt>f</tt> satisfies <tt>SomeEq f</tt> if <tt>f a</tt> satisfies
--   <tt>Eq</tt> independent of <tt>a</tt>
class SomeEq f
someEq :: SomeEq f => DictEq (f a)
data DictShow a
[DictShow] :: Show a => DictShow a

-- | Type <tt>f</tt> satisfies <tt>SomeShow f</tt> if <tt>f a</tt>
--   satisfies <tt>Show</tt> independent of <tt>a</tt>
class SomeShow f
someShow :: SomeShow f => DictShow (f a)
data DictPretty a
[DictPretty] :: Pretty a => DictPretty a

-- | Type <tt>f</tt> satisfies <tt>SomeShow f</tt> if <tt>f a</tt>
--   satisfies <tt>Show</tt> independent of <tt>a</tt>
class SomePretty f
somePretty :: SomePretty f => DictPretty (f a)
typecheckSome :: Typed f => Some f -> Some (TypeOf f) -> Bool
instance (Hackage.Security.Util.TypedEmbedded.Typed f, Hackage.Security.Util.Some.SomeEq f) => GHC.Classes.Eq (Hackage.Security.Util.Some.Some f)
instance Hackage.Security.Util.Some.SomeShow f => GHC.Show.Show (Hackage.Security.Util.Some.Some f)
instance Hackage.Security.Util.Some.SomePretty f => Hackage.Security.Util.Pretty.Pretty (Hackage.Security.Util.Some.Some f)

module Hackage.Security.Util.IO
getFileSize :: (Num a, FsRoot root) => Path root -> IO a
handleDoesNotExist :: IO a -> IO (Maybe a)

-- | Attempt to create a filesystem lock in the specified directory
--   
--   Given a file <tt><i>path</i>to</tt>, we do this by attempting to
--   create the directory <tt>/<i>path</i>to/hackage-security-lock</tt>,
--   and deleting the directory again afterwards. Creating a directory that
--   already exists will throw an exception on most OSs (certainly Linux,
--   OSX and Windows) and is a reasonably common way to implement a lock
--   file.
withDirLock :: Path Absolute -> IO a -> IO a
timedIO :: String -> IO a -> IO a

module Hackage.Security.Client.Verify

-- | Verification monad
--   
--   The verification monad is similar to <tt>ResourceT</tt> in intent, in
--   that we can register handlers to be run to release resources. Unlike
--   <tt>ResourceT</tt>, however, we maintain _two_ handlers: a cleanup
--   handler which is run whether or not verification succeeds, and a
--   finalisation handler which is run only if verification succeeds.
--   
--   <ul>
--   <li>Cleanup handlers are registered using <a>acquire</a>, and are
--   guaranteed to run just before the computation terminates (after the
--   finalisation handler).</li>
--   <li>The finalisation handlers are run only when verification succeeds,
--   and can be registered with <a>ifVerified</a>. Finalisation can be used
--   for instance to update the local cache (which should only happen if
--   verification is successful).</li>
--   </ul>
data Verify a

-- | Run an action in the <a>Verify</a> monad
runVerify :: (Finaliser -> Finaliser) -> Verify a -> IO a

-- | Acquire a resource and register the corresponding cleanup handler
--   
--   NOTE: Resource acquisition happens with exceptions masked. If it is
--   important that the resource acquistion can be timed out (or receive
--   other kinds of asynchronous exceptions), you will need to use an
--   interruptible operation. See
--   <a>http://www.well-typed.com/blog/2014/08/asynchronous-exceptions/</a>
--   for details.
acquire :: IO a -> (a -> IO ()) -> Verify a

-- | Register an action to be run only if verification succeeds
ifVerified :: IO () -> Verify ()

-- | Create a short-lived temporary file
--   
--   Creates the directory where the temp file should live if it does not
--   exist.
openTempFile :: FsRoot root => Path root -> String -> Verify (Path Absolute, Handle)

-- | Lift a computation from the <a>IO</a> monad.
liftIO :: MonadIO m => forall a. IO a -> m a
instance Control.Monad.IO.Class.MonadIO Hackage.Security.Client.Verify.Verify
instance GHC.Base.Monad Hackage.Security.Client.Verify.Verify
instance GHC.Base.Applicative Hackage.Security.Client.Verify.Verify
instance GHC.Base.Functor Hackage.Security.Client.Verify.Verify


-- | Minimal implementation of Canonical JSON.
--   
--   <a>http://wiki.laptop.org/go/Canonical_JSON</a>
--   
--   A "canonical JSON" format is provided in order to provide meaningful
--   and repeatable hashes of JSON-encoded data. Canonical JSON is parsable
--   with any full JSON parser, but security-conscious applications will
--   want to verify that input is in canonical form before authenticating
--   any hash or signature on that input.
--   
--   This implementation is derived from the json parser from the json
--   package, with simplifications to meet the Canonical JSON grammar.
--   
--   TODO: Known bugs/limitations:
--   
--   <ul>
--   <li>Decoding/encoding Unicode code-points beyond <tt>U+00ff</tt> is
--   currently broken</li>
--   </ul>
module Text.JSON.Canonical
data JSValue
JSNull :: JSValue
JSBool :: !Bool -> JSValue
JSNum :: !Int54 -> JSValue
JSString :: String -> JSValue
JSArray :: [JSValue] -> JSValue
JSObject :: [(String, JSValue)] -> JSValue

-- | 54-bit integer values
--   
--   JavaScript can only safely represent numbers between <tt>-(2^53 -
--   1)</tt> and <tt>2^53 - 1</tt>.
--   
--   TODO: Although we introduce the type here, we don't actually do any
--   bounds checking and just inherit all type class instance from Int64.
--   We should probably define <a>fromInteger</a> to do bounds checking,
--   give different instances for type classes such as <a>Bounded</a> and
--   <a>FiniteBits</a>, etc.
data Int54

-- | Parse a canonical JSON format string as a JSON value. The input string
--   does not have to be in canonical form, just in the "canonical JSON"
--   format.
--   
--   Use <a>renderCanonicalJSON</a> to convert into canonical form.
parseCanonicalJSON :: ByteString -> Either String JSValue

-- | Render a JSON value in canonical form. This rendered form is canonical
--   and so allows repeatable hashes.
--   
--   For pretty printing, see prettyCanonicalJSON.
--   
--   NB: Canonical JSON's string escaping rules deviate from RFC 7159 JSON
--   which requires
--   
--   "All Unicode characters may be placed within the quotation marks,
--   except for the characters that must be escaped: quotation mark,
--   reverse solidus, and the control characters (<tt>U+0000</tt> through
--   <tt>U+001F</tt>)."
--   
--   Whereas the current specification of Canonical JSON explicitly
--   requires to violate this by only escaping the quotation mark and the
--   reverse solidus. This, however, contradicts Canonical JSON's statement
--   that "Canonical JSON is parsable with any full JSON parser"
--   
--   Consequently, Canonical JSON is not a proper subset of RFC 7159.
renderCanonicalJSON :: JSValue -> ByteString

-- | Render a JSON value in a reasonable human-readable form. This rendered
--   form is <i>not the canonical form</i> used for repeatable hashes, use
--   <a>renderCanonicalJSON</a> for that.
prettyCanonicalJSON :: JSValue -> String
instance GHC.Classes.Ord Text.JSON.Canonical.JSValue
instance GHC.Classes.Eq Text.JSON.Canonical.JSValue
instance GHC.Read.Read Text.JSON.Canonical.JSValue
instance GHC.Show.Show Text.JSON.Canonical.JSValue
instance Text.Printf.PrintfArg Text.JSON.Canonical.Int54
instance Foreign.Storable.Storable Text.JSON.Canonical.Int54
instance Data.Bits.Bits Text.JSON.Canonical.Int54
instance Data.Bits.FiniteBits Text.JSON.Canonical.Int54
instance GHC.Arr.Ix Text.JSON.Canonical.Int54
instance GHC.Real.Real Text.JSON.Canonical.Int54
instance GHC.Classes.Ord Text.JSON.Canonical.Int54
instance GHC.Num.Num Text.JSON.Canonical.Int54
instance Data.Data.Data Text.JSON.Canonical.Int54
instance GHC.Real.Integral Text.JSON.Canonical.Int54
instance GHC.Classes.Eq Text.JSON.Canonical.Int54
instance GHC.Enum.Enum Text.JSON.Canonical.Int54
instance GHC.Enum.Bounded Text.JSON.Canonical.Int54
instance GHC.Show.Show Text.JSON.Canonical.Int54
instance GHC.Read.Read Text.JSON.Canonical.Int54


-- | Some very simple lens definitions (to avoid further dependencies)
--   
--   Intended to be double-imported &gt; import Hackage.Security.Util.Lens
--   (Lens) &gt; import qualified Hackage.Security.Util.Lens as Lens
module Hackage.Security.Util.Lens

-- | Polymorphic lens
type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t

-- | Monomorphic lens
type Lens' s a = Lens s s a a
get :: Lens' s a -> s -> a
modify :: Lens s t a b -> (a -> b) -> s -> t
set :: Lens s t a b -> b -> s -> t
lookupM :: forall a b. (Eq a, Monoid b) => a -> Lens' [(a, b)] b

module Hackage.Security.Key.Env

-- | A key environment is a mapping from key IDs to the corresponding keys.
--   
--   It should satisfy the invariant that these key IDs actually match the
--   keys; see <a>checkKeyEnvInvariant</a>.
data KeyEnv
keyEnvMap :: KeyEnv -> Map KeyId (Some PublicKey)
fromPublicKeys :: [Some PublicKey] -> KeyEnv
fromKeys :: [Some Key] -> KeyEnv
empty :: KeyEnv
null :: KeyEnv -> Bool
insert :: Some PublicKey -> KeyEnv -> KeyEnv
lookup :: KeyId -> KeyEnv -> Maybe (Some PublicKey)
union :: KeyEnv -> KeyEnv -> KeyEnv
instance GHC.Show.Show Hackage.Security.Key.Env.KeyEnv
instance GHC.Base.Monad m => Hackage.Security.Util.JSON.ToJSON m Hackage.Security.Key.Env.KeyEnv
instance Hackage.Security.Util.JSON.ReportSchemaErrors m => Hackage.Security.Util.JSON.FromJSON m Hackage.Security.Key.Env.KeyEnv


-- | Hackage-specific wrappers around the Util.JSON module
module Hackage.Security.JSON
data DeserializationError

-- | Malformed JSON has syntax errors in the JSON itself (i.e., we cannot
--   even parse it to a JSValue)
DeserializationErrorMalformed :: String -> DeserializationError

-- | Invalid JSON has valid syntax but invalid structure
--   
--   The string gives a hint about what we expected instead
DeserializationErrorSchema :: String -> DeserializationError

-- | The JSON file contains a key ID of an unknown key
DeserializationErrorUnknownKey :: KeyId -> DeserializationError

-- | Some verification step failed
DeserializationErrorValidation :: String -> DeserializationError

-- | Wrong file type
--   
--   Records actual and expected types.
DeserializationErrorFileType :: String -> String -> DeserializationError
validate :: MonadError DeserializationError m => String -> Bool -> m ()
verifyType :: (ReportSchemaErrors m, MonadError DeserializationError m) => JSValue -> String -> m ()

-- | MonadReader-like monad, specialized to key environments
class (ReportSchemaErrors m, MonadError DeserializationError m) => MonadKeys m
localKeys :: MonadKeys m => (KeyEnv -> KeyEnv) -> m a -> m a
askKeys :: MonadKeys m => m KeyEnv
addKeys :: MonadKeys m => KeyEnv -> m a -> m a
withKeys :: MonadKeys m => KeyEnv -> m a -> m a
lookupKey :: MonadKeys m => KeyId -> m (Some PublicKey)
readKeyAsId :: MonadKeys m => JSValue -> m (Some PublicKey)
data ReadJSON_Keys_Layout a
data ReadJSON_Keys_NoLayout a
data ReadJSON_NoKeys_NoLayout a
runReadJSON_Keys_Layout :: KeyEnv -> RepoLayout -> ReadJSON_Keys_Layout a -> Either DeserializationError a
runReadJSON_Keys_NoLayout :: KeyEnv -> ReadJSON_Keys_NoLayout a -> Either DeserializationError a
runReadJSON_NoKeys_NoLayout :: ReadJSON_NoKeys_NoLayout a -> Either DeserializationError a
parseJSON_Keys_Layout :: FromJSON ReadJSON_Keys_Layout a => KeyEnv -> RepoLayout -> ByteString -> Either DeserializationError a
parseJSON_Keys_NoLayout :: FromJSON ReadJSON_Keys_NoLayout a => KeyEnv -> ByteString -> Either DeserializationError a
parseJSON_NoKeys_NoLayout :: FromJSON ReadJSON_NoKeys_NoLayout a => ByteString -> Either DeserializationError a
readJSON_Keys_Layout :: (FsRoot root, FromJSON ReadJSON_Keys_Layout a) => KeyEnv -> RepoLayout -> Path root -> IO (Either DeserializationError a)
readJSON_Keys_NoLayout :: (FsRoot root, FromJSON ReadJSON_Keys_NoLayout a) => KeyEnv -> Path root -> IO (Either DeserializationError a)
readJSON_NoKeys_NoLayout :: (FsRoot root, FromJSON ReadJSON_NoKeys_NoLayout a) => Path root -> IO (Either DeserializationError a)
data WriteJSON a
runWriteJSON :: RepoLayout -> WriteJSON a -> a

-- | Render to canonical JSON format
renderJSON :: ToJSON WriteJSON a => RepoLayout -> a -> ByteString

-- | Variation on <a>renderJSON</a> for files that don't require the repo
--   layout
renderJSON_NoLayout :: ToJSON Identity a => a -> ByteString
writeJSON :: ToJSON WriteJSON a => RepoLayout -> Path Absolute -> a -> IO ()
writeJSON_NoLayout :: ToJSON Identity a => Path Absolute -> a -> IO ()
writeKeyAsId :: Some PublicKey -> JSValue
class ToJSON m a
toJSON :: ToJSON m a => a -> m JSValue
class FromJSON m a
fromJSON :: FromJSON m a => JSValue -> m a

-- | Used in the <a>ToJSON</a> instance for <a>Map</a>
class ToObjectKey m a
toObjectKey :: ToObjectKey m a => a -> m String

-- | Used in the <a>FromJSON</a> instance for <a>Map</a>
class FromObjectKey m a
fromObjectKey :: FromObjectKey m a => String -> m (Maybe a)

-- | Monads in which we can report schema errors
class (Applicative m, Monad m) => ReportSchemaErrors m
expected :: ReportSchemaErrors m => Expected -> Maybe Got -> m a
type Expected = String
type Got = String
expected' :: ReportSchemaErrors m => Expected -> JSValue -> m a
fromJSObject :: ReportSchemaErrors m => JSValue -> m [(String, JSValue)]

-- | Extract a field from a JSON object
fromJSField :: (ReportSchemaErrors m, FromJSON m a) => JSValue -> String -> m a
fromJSOptField :: (ReportSchemaErrors m, FromJSON m a) => JSValue -> String -> m (Maybe a)
mkObject :: forall m. Monad m => [(String, m JSValue)] -> m JSValue
data JSValue
JSNull :: JSValue
JSBool :: !Bool -> JSValue
JSNum :: !Int54 -> JSValue
JSString :: String -> JSValue
JSArray :: [JSValue] -> JSValue
JSObject :: [(String, JSValue)] -> JSValue

-- | 54-bit integer values
--   
--   JavaScript can only safely represent numbers between <tt>-(2^53 -
--   1)</tt> and <tt>2^53 - 1</tt>.
--   
--   TODO: Although we introduce the type here, we don't actually do any
--   bounds checking and just inherit all type class instance from Int64.
--   We should probably define <a>fromInteger</a> to do bounds checking,
--   give different instances for type classes such as <a>Bounded</a> and
--   <a>FiniteBits</a>, etc.
data Int54
instance Control.Monad.Reader.Class.MonadReader Hackage.Security.TUF.Layout.Repo.RepoLayout Hackage.Security.JSON.WriteJSON
instance GHC.Base.Monad Hackage.Security.JSON.WriteJSON
instance GHC.Base.Applicative Hackage.Security.JSON.WriteJSON
instance GHC.Base.Functor Hackage.Security.JSON.WriteJSON
instance Control.Monad.Error.Class.MonadError Hackage.Security.JSON.DeserializationError Hackage.Security.JSON.ReadJSON_NoKeys_NoLayout
instance GHC.Base.Monad Hackage.Security.JSON.ReadJSON_NoKeys_NoLayout
instance GHC.Base.Applicative Hackage.Security.JSON.ReadJSON_NoKeys_NoLayout
instance GHC.Base.Functor Hackage.Security.JSON.ReadJSON_NoKeys_NoLayout
instance Control.Monad.Error.Class.MonadError Hackage.Security.JSON.DeserializationError Hackage.Security.JSON.ReadJSON_Keys_NoLayout
instance GHC.Base.Monad Hackage.Security.JSON.ReadJSON_Keys_NoLayout
instance GHC.Base.Applicative Hackage.Security.JSON.ReadJSON_Keys_NoLayout
instance GHC.Base.Functor Hackage.Security.JSON.ReadJSON_Keys_NoLayout
instance Control.Monad.Error.Class.MonadError Hackage.Security.JSON.DeserializationError Hackage.Security.JSON.ReadJSON_Keys_Layout
instance GHC.Base.Monad Hackage.Security.JSON.ReadJSON_Keys_Layout
instance GHC.Base.Applicative Hackage.Security.JSON.ReadJSON_Keys_Layout
instance GHC.Base.Functor Hackage.Security.JSON.ReadJSON_Keys_Layout
instance GHC.Show.Show Hackage.Security.JSON.DeserializationError
instance GHC.Exception.Exception Hackage.Security.JSON.DeserializationError
instance Hackage.Security.Util.Pretty.Pretty Hackage.Security.JSON.DeserializationError
instance Hackage.Security.Util.JSON.ReportSchemaErrors Hackage.Security.JSON.ReadJSON_Keys_Layout
instance Hackage.Security.Util.JSON.ReportSchemaErrors Hackage.Security.JSON.ReadJSON_Keys_NoLayout
instance Hackage.Security.Util.JSON.ReportSchemaErrors Hackage.Security.JSON.ReadJSON_NoKeys_NoLayout
instance Control.Monad.Reader.Class.MonadReader Hackage.Security.TUF.Layout.Repo.RepoLayout Hackage.Security.JSON.ReadJSON_Keys_Layout
instance Hackage.Security.JSON.MonadKeys Hackage.Security.JSON.ReadJSON_Keys_Layout
instance Hackage.Security.JSON.MonadKeys Hackage.Security.JSON.ReadJSON_Keys_NoLayout


-- | Information about files
--   
--   Intended to be double imported
--   
--   <pre>
--   import Hackage.Security.TUF.FileMap (FileMap)
--   import qualified Hackage.Security.TUF.FileMap as FileMap
--   </pre>
module Hackage.Security.TUF.FileMap

-- | Mapping from paths to file info
--   
--   File maps are used in target files; the paths are relative to the
--   location of the target files containing the file map.
data FileMap

-- | Entries in <a>FileMap</a> either talk about the repository or the
--   index
data TargetPath
TargetPathRepo :: RepoPath -> TargetPath
TargetPathIndex :: IndexPath -> TargetPath
empty :: FileMap
lookup :: TargetPath -> FileMap -> Maybe FileInfo
(!) :: FileMap -> TargetPath -> FileInfo
insert :: TargetPath -> FileInfo -> FileMap -> FileMap
fromList :: [(TargetPath, FileInfo)] -> FileMap
lookupM :: Monad m => FileMap -> TargetPath -> m FileInfo
data FileChange

-- | File got added or modified; we record the new file info
FileChanged :: FileInfo -> FileChange

-- | File got deleted
FileDeleted :: FileChange
fileMapChanges :: FileMap -> FileMap -> Map TargetPath FileChange
instance GHC.Show.Show Hackage.Security.TUF.FileMap.FileChange
instance GHC.Show.Show Hackage.Security.TUF.FileMap.FileMap
instance GHC.Classes.Ord Hackage.Security.TUF.FileMap.TargetPath
instance GHC.Classes.Eq Hackage.Security.TUF.FileMap.TargetPath
instance GHC.Show.Show Hackage.Security.TUF.FileMap.TargetPath
instance Hackage.Security.Util.Pretty.Pretty Hackage.Security.TUF.FileMap.TargetPath
instance GHC.Base.Monad m => Hackage.Security.Util.JSON.ToJSON m Hackage.Security.TUF.FileMap.FileMap
instance Hackage.Security.Util.JSON.ReportSchemaErrors m => Hackage.Security.Util.JSON.FromJSON m Hackage.Security.TUF.FileMap.FileMap
instance GHC.Base.Monad m => Hackage.Security.Util.JSON.ToObjectKey m Hackage.Security.TUF.FileMap.TargetPath
instance Hackage.Security.Util.JSON.ReportSchemaErrors m => Hackage.Security.Util.JSON.FromObjectKey m Hackage.Security.TUF.FileMap.TargetPath

module Hackage.Security.Trusted

-- | Trusted values
--   
--   Trusted values originate in only two ways:
--   
--   <ul>
--   <li>Anything that is statically known is trusted
--   (<a>trustStatic</a>)</li>
--   <li>If we have "dynamic" data we can trust it once we have verified
--   the the signatures (trustSigned).</li>
--   </ul>
--   
--   NOTE: Trusted is NOT a functor. If it was we could define
--   
--   <pre>
--   trustAnything :: a -&gt; Trusted a
--   trustAnything a = fmap (const a) (trustStatic (static ()))
--   </pre>
--   
--   Consequently, it is neither a monad nor a comonad. However, we _can_
--   apply trusted functions to trusted arguments (<a>trustApply</a>).
--   
--   The <a>DeclareTrusted</a> constructor is exported, but any use of it
--   should be verified.
newtype Trusted a
DeclareTrusted :: a -> Trusted a
[trusted] :: Trusted a -> a
trustStatic :: StaticPtr a -> Trusted a
trustVerified :: SignaturesVerified a -> Trusted a

-- | Equivalent of <a>&lt;*&gt;</a>
--   
--   Trusted isn't quite applicative (no pure, not a functor), but it is
--   somehow Applicative-like: we have the equivalent of <a>&lt;*&gt;</a>
trustApply :: Trusted (a -> b) -> Trusted a -> Trusted b

-- | Trust all elements of some trusted (traversable) container
--   
--   If we have, say, a trusted list of values, we should be able to get a
--   list of trusted values out of it.
--   
--   <pre>
--   trustElems :: Trusted [a] -&gt; [Trusted a]
--   </pre>
--   
--   NOTE. It might appear that the more natural primitive to offer is a
--   <a>sequenceA</a>-like operator such as
--   
--   <pre>
--   trustSeq :: Applicative f =&gt; Trusted (f a) -&gt; f (Trusted a)
--   </pre>
--   
--   However, this is unsound. To see this, consider that <tt>((-&gt;)
--   a)</tt> is <a>Applicative</a> (it's the reader monad); hence, we can
--   instantiate <tt>trustSeq</tt> at
--   
--   <pre>
--   trustSeq :: Trusted (a -&gt; a) -&gt; a -&gt; Trusted a
--   </pre>
--   
--   and by passing <tt>trustStatic (static id)</tt> make <a>Trusted</a> a
--   functor, which we certainly don't want to do (see comments for
--   <a>Trusted</a>).
--   
--   So why is it okay when we insist on <a>Traversable</a> rather than
--   <a>Applicative</a>? To see this, it's instructive to consider how we
--   might make a <tt>((-&gt;) a)</tt> an instance of <a>Traversable</a>.
--   If we define the domain of enumerable types as
--   
--   <pre>
--   class Eq a =&gt; Enumerable a where
--     enumerate :: [a]
--   </pre>
--   
--   then we can make <tt>((-&gt;) r)</tt> traversable by
--   
--   <pre>
--   instance Enumerable r =&gt; Traversable ((-&gt;) r) where
--     sequenceA f = rebuild &lt;$&gt; sequenceA ((\r -&gt; (r,) &lt;$&gt; f r) &lt;$&gt; enumerate)
--       where
--         rebuild :: [(r, a)] -&gt; r -&gt; a
--         rebuild fun arg = fromJust (lookup arg fun)
--   </pre>
--   
--   The idea is that if the domain of a function is enumerable, we can
--   apply the function to each possible input, collect the outputs, and
--   construct a new function by pairing the inputs with the outputs. I.e.,
--   if we had something of type
--   
--   <pre>
--   a -&gt; IO b
--   </pre>
--   
--   and <tt>a</tt> is enumerable, we just run the <tt>IO</tt> action on
--   each possible <tt>a</tt> and collect all <tt>b</tt>s to get a pure
--   function <tt>a -&gt; b</tt>. Of course, you probably don't want to be
--   doing that, but the point is that as far as the type system is
--   concerned you could.
--   
--   In the context of <a>Trusted</a>, this means that we can derive
--   
--   <pre>
--   enumPure :: Enumerable a =&gt; a -&gt; Trusted a
--   </pre>
--   
--   but in a way this this makes sense anyway. If a domain is enumerable,
--   it would not be unreasonable to change <tt>Enumerable</tt> to
--   
--   <pre>
--   class Eq a =&gt; Enumerable a where
--     enumerate :: [StaticPtr a]
--   </pre>
--   
--   so we could define <tt>enumPure</tt> as
--   
--   <pre>
--   enumPure :: Enumerable a =&gt; a -&gt; Trusted a
--   enumPure x = trustStatic
--              $ fromJust (find ((== x) . deRefStaticPtr) enumerate)
--   </pre>
--   
--   In other words, we just enumerate the entire domain as trusted values
--   (because we defined them locally) and then return the one that matched
--   the untrusted value.
--   
--   The conclusion from all of this is that the types of untrusted input
--   (like the types of the TUF files we download from the server) should
--   probably not be considered enumerable.
trustElems :: Traversable f => Trusted (f a) -> f (Trusted a)

-- | Errors thrown during role validation
data VerificationError

-- | Not enough signatures signed with the appropriate keys
VerificationErrorSignatures :: TargetPath -> VerificationError

-- | The file is expired
VerificationErrorExpired :: TargetPath -> VerificationError

-- | The file version is less than the previous version
VerificationErrorVersion :: TargetPath -> VerificationError

-- | File information mismatch
VerificationErrorFileInfo :: TargetPath -> VerificationError

-- | We tried to lookup file information about a particular target file,
--   but the information wasn't in the corresponding <tt>targets.json</tt>
--   file.
VerificationErrorUnknownTarget :: TargetPath -> VerificationError

-- | The metadata for the specified target is missing a SHA256
VerificationErrorMissingSHA256 :: TargetPath -> VerificationError

-- | Some verification errors materialize as deserialization errors
--   
--   For example: if we try to deserialize a timestamp file but the
--   timestamp key has been rolled over, deserialization of the file will
--   fail with <a>DeserializationErrorUnknownKey</a>.
VerificationErrorDeserialization :: TargetPath -> DeserializationError -> VerificationError

-- | The spec stipulates that if a verification error occurs during the
--   check for updates, we must download new root information and start
--   over. However, we limit how often we attempt this.
--   
--   We record all verification errors that occurred before we gave up.
VerificationErrorLoop :: VerificationHistory -> VerificationError

-- | Root metadata updated (as part of the normal update process)
data RootUpdated
RootUpdated :: RootUpdated
type VerificationHistory = [Either RootUpdated VerificationError]
data SignaturesVerified a
signaturesVerified :: SignaturesVerified a -> a

-- | Role verification
--   
--   NOTE: We throw an error when the version number _decreases_, but allow
--   it to be the same. This is sufficient: the file number is there so
--   that attackers cannot replay old files. It cannot protect against
--   freeze attacks (that's what the expiry date is for), so "replaying"
--   the same file is not a problem. If an attacker changes the contents of
--   the file but not the version number we have an inconsistent situation,
--   but this is not something we need to worry about: in this case the
--   attacker will need to resign the file or otherwise the signature won't
--   match, and if the attacker has compromised the key then he might just
--   as well increase the version number and resign.
--   
--   NOTE 2: We are not actually verifying the signatures _themselves_ here
--   (we did that when we parsed the JSON). We are merely verifying the
--   provenance of the keys.
verifyRole' :: forall a. HasHeader a => Trusted (RoleSpec a) -> TargetPath -> Maybe FileVersion -> Maybe UTCTime -> Signed a -> Either VerificationError (SignaturesVerified a)

-- | Variation on <tt>verifyRole</tt> that uses key IDs rather than keys
--   
--   This is used during the bootstrap process.
--   
--   See <a>http://en.wikipedia.org/wiki/Public_key_fingerprint</a>.
verifyFingerprints :: [KeyId] -> KeyThreshold -> TargetPath -> Signed Root -> Either VerificationError (SignaturesVerified Root)

-- | A reference to a value of type <tt>a</tt>.
data StaticPtr a :: * -> *

-- | Apply a static function to a trusted argument
(<$$>) :: StaticPtr (a -> b) -> Trusted a -> Trusted b
class VerifyRole a
verifyRole :: VerifyRole a => Trusted Root -> TargetPath -> Maybe FileVersion -> Maybe UTCTime -> Signed a -> Either VerificationError (SignaturesVerified a)

-- | Variation on <a>knownFileInfoEqual</a> for <a>Trusted</a>
--   <a>FileInfo</a>
trustedFileInfoEqual :: Trusted FileInfo -> Trusted FileInfo -> Bool
instance Hackage.Security.Trusted.VerifyRole Hackage.Security.TUF.Root.Root
instance Hackage.Security.Trusted.VerifyRole Hackage.Security.TUF.Timestamp.Timestamp
instance Hackage.Security.Trusted.VerifyRole Hackage.Security.TUF.Snapshot.Snapshot
instance Hackage.Security.Trusted.VerifyRole Hackage.Security.TUF.Mirrors.Mirrors


-- | Abstract definition of a Repository
--   
--   Most clients should only need to import this module if they wish to
--   define their own Repository implementations.
module Hackage.Security.Client.Repository
data Metadata
data Binary

-- | Abstract definition of files we might have to download
--   
--   <a>RemoteFile</a> is parametrized by the type of the formats that we
--   can accept from the remote repository, as well as with information on
--   whether this file is metadata actual binary content.
--   
--   NOTE: Haddock lacks GADT support so constructors have only regular
--   comments.
data RemoteFile :: * -> * -> *
[RemoteTimestamp] :: RemoteFile (FormatUn :- ()) Metadata
[RemoteRoot] :: Maybe (Trusted FileInfo) -> RemoteFile (FormatUn :- ()) Metadata
[RemoteSnapshot] :: Trusted FileInfo -> RemoteFile (FormatUn :- ()) Metadata
[RemoteMirrors] :: Trusted FileInfo -> RemoteFile (FormatUn :- ()) Metadata
[RemoteIndex] :: HasFormat fs FormatGz -> Formats fs (Trusted FileInfo) -> RemoteFile fs Binary
[RemotePkgTarGz] :: PackageIdentifier -> Trusted FileInfo -> RemoteFile (FormatGz :- ()) Binary

-- | Files that we might request from the local cache
data CachedFile

-- | Timestamp metadata (<tt>timestamp.json</tt>)
CachedTimestamp :: CachedFile

-- | Root metadata (<tt>root.json</tt>)
CachedRoot :: CachedFile

-- | Snapshot metadata (<tt>snapshot.json</tt>)
CachedSnapshot :: CachedFile

-- | Mirrors list (<tt>mirrors.json</tt>)
CachedMirrors :: CachedFile

-- | Files that we might request from the index
--   
--   The type index tells us the type of the decoded file, if any. For
--   files for which the library does not support decoding this will be
--   <tt>()</tt>. NOTE: Clients should NOT rely on this type index being
--   <tt>()</tt>, or they might break if we add support for parsing
--   additional file formats in the future.
--   
--   TODO: If we wanted to support legacy Hackage, we should also have a
--   case for the global preferred-versions file. But supporting legacy
--   Hackage will probably require more work anyway..
data IndexFile :: * -> *
[IndexPkgMetadata] :: PackageIdentifier -> IndexFile (Signed Targets)
[IndexPkgCabal] :: PackageIdentifier -> IndexFile ()
[IndexPkgPrefs] :: PackageName -> IndexFile ()

-- | Default format for each file type
--   
--   For most file types we don't have a choice; for the index the
--   repository is only required to offer the GZip-compressed format so
--   that is the default.
remoteFileDefaultFormat :: RemoteFile fs typ -> Some (HasFormat fs)

-- | Default file info (see also <a>remoteFileDefaultFormat</a>)
remoteFileDefaultInfo :: RemoteFile fs typ -> Maybe (Trusted FileInfo)

-- | Repository
--   
--   This is an abstract representation of a repository. It simply provides
--   a way to download metafiles and target files, without specifying how
--   this is done. For instance, for a local repository this could just be
--   doing a file read, whereas for remote repositories this could be using
--   any kind of HTTP client.
data Repository down
Repository :: (forall fs typ. Throws SomeRemoteError => AttemptNr -> RemoteFile fs typ -> Verify (Some (HasFormat fs), down typ)) -> (CachedFile -> IO (Maybe (Path Absolute))) -> IO (Path Absolute) -> IO () -> (forall a. (Handle -> IO a) -> IO a) -> IO TarIndex -> (IO () -> IO ()) -> (forall a. Maybe [Mirror] -> IO a -> IO a) -> (LogMessage -> IO ()) -> RepoLayout -> IndexLayout -> String -> Repository down

-- | Get a file from the server
--   
--   Responsibilies of <a>repGetRemote</a>:
--   
--   <ul>
--   <li>Download the file from the repository and make it available at a
--   temporary location</li>
--   <li>Use the provided file length to protect against endless data
--   attacks. (Repositories such as local repositories that are not
--   suspectible to endless data attacks can safely ignore this
--   argument.)</li>
--   <li>Move the file from its temporary location to its permanent
--   location if verification succeeds.</li>
--   </ul>
--   
--   NOTE: Calls to <a>repGetRemote</a> should _always_ be in the scope of
--   <a>repWithMirror</a>.
[repGetRemote] :: Repository down -> forall fs typ. Throws SomeRemoteError => AttemptNr -> RemoteFile fs typ -> Verify (Some (HasFormat fs), down typ)

-- | Get a cached file (if available)
[repGetCached] :: Repository down -> CachedFile -> IO (Maybe (Path Absolute))

-- | Get the cached root
--   
--   This is a separate method only because clients must ALWAYS have root
--   information available.
[repGetCachedRoot] :: Repository down -> IO (Path Absolute)

-- | Clear all cached data
--   
--   In particular, this should remove the snapshot and the timestamp. It
--   would also be okay, but not required, to delete the index.
[repClearCache] :: Repository down -> IO ()

-- | Open the tarball for reading
--   
--   This function has this shape so that:
--   
--   <ul>
--   <li>We can read multiple files from the tarball without having to open
--   and close the handle each time</li>
--   <li>We can close the handle immediately when done.</li>
--   </ul>
[repWithIndex] :: Repository down -> forall a. (Handle -> IO a) -> IO a

-- | Read the index index
[repGetIndexIdx] :: Repository down -> IO TarIndex

-- | Lock the cache (during updates)
[repLockCache] :: Repository down -> IO () -> IO ()

-- | Mirror selection
--   
--   The purpose of <a>repWithMirror</a> is to scope mirror selection. The
--   idea is that if we have
--   
--   <pre>
--   repWithMirror mirrorList $
--     someCallback
--   </pre>
--   
--   then the repository may pick a mirror before calling
--   <tt>someCallback</tt>, catch exceptions thrown by
--   <tt>someCallback</tt>, and potentially try the callback again with a
--   different mirror.
--   
--   The list of mirrors may be <tt>Nothing</tt> if we haven't yet
--   downloaded the list of mirrors from the repository, or when our cached
--   list of mirrors is invalid. Of course, if we did download it, then the
--   list of mirrors may still be empty. In this case the repository must
--   fall back to its primary download mechanism.
--   
--   Mirrors as currently defined (in terms of a "base URL") are inherently
--   a HTTP (or related) concept, so in repository implementations such as
--   the local-repo <tt>repWithMirrors</tt> is probably just an identity
--   operation (see <tt>ignoreMirrors</tt>). Conversely, HTTP
--   implementations of repositories may have other, out-of-band
--   information (for example, coming from a cabal config file) that they
--   may use to influence mirror selection.
[repWithMirror] :: Repository down -> forall a. Maybe [Mirror] -> IO a -> IO a

-- | Logging
[repLog] :: Repository down -> LogMessage -> IO ()

-- | Layout of this repository
[repLayout] :: Repository down -> RepoLayout

-- | Layout of the index
--   
--   Since the repository hosts the index, the layout of the index is not
--   independent of the layout of the repository.
[repIndexLayout] :: Repository down -> IndexLayout

-- | Description of the repository (used in the show instance)
[repDescription] :: Repository down -> String

-- | Are we requesting this information because of a previous validation
--   error?
--   
--   Clients can take advantage of this to tell caches to revalidate files.
newtype AttemptNr
AttemptNr :: Int -> AttemptNr

-- | Log messages
--   
--   We use a <a>RemoteFile</a> rather than a <a>RepoPath</a> here because
--   we might not have a <a>RepoPath</a> for the file that we were trying
--   to download (that is, for example if the server does not provide an
--   uncompressed tarball, it doesn't make much sense to list the path to
--   that non-existing uncompressed tarball).
data LogMessage

-- | Root information was updated
--   
--   This message is issued when the root information is updated as part of
--   the normal check for updates procedure. If the root information is
--   updated because of a verification error WarningVerificationError is
--   issued instead.
LogRootUpdated :: LogMessage

-- | A verification error
--   
--   Verification errors can be temporary, and may be resolved later; hence
--   these are just warnings. (Verification errors that cannot be resolved
--   are thrown as exceptions.)
LogVerificationError :: VerificationError -> LogMessage

-- | Download a file from a repository
LogDownloading :: (RemoteFile fs typ) -> LogMessage

-- | Incrementally updating a file from a repository
LogUpdating :: (RemoteFile fs Binary) -> LogMessage

-- | Selected a particular mirror
LogSelectedMirror :: MirrorDescription -> LogMessage

-- | Updating a file failed (we will instead download it whole)
LogCannotUpdate :: (RemoteFile fs Binary) -> UpdateFailure -> LogMessage

-- | We got an exception with a particular mirror (we will try with a
--   different mirror if any are available)
LogMirrorFailed :: MirrorDescription -> SomeException -> LogMessage

-- | Records why we are downloading a file rather than updating it.
data UpdateFailure

-- | Server does not support incremental downloads
UpdateImpossibleUnsupported :: UpdateFailure

-- | We don't have a local copy of the file to update
UpdateImpossibleNoLocalCopy :: UpdateFailure

-- | Update failed twice
--   
--   If we attempt an incremental update the first time, and it fails, we
--   let it go round the loop, update local security information, and try
--   again. But if an incremental update then fails _again_, we instead
--   attempt a regular download.
UpdateFailedTwice :: UpdateFailure

-- | Update failed (for example: perhaps the local file got corrupted)
UpdateFailed :: SomeException -> UpdateFailure

-- | Repository-specific exceptions
--   
--   For instance, for repositories using HTTP this might correspond to a
--   404; for local repositories this might correspond to file-not-found,
--   etc.
data SomeRemoteError :: *
[SomeRemoteError] :: Exception e => e -> SomeRemoteError
class DownloadedFile (down :: * -> *)

-- | Verify a download file
downloadedVerify :: DownloadedFile down => down a -> Trusted FileInfo -> IO Bool

-- | Read the file we just downloaded into memory
--   
--   We never read binary data, only metadata.
downloadedRead :: DownloadedFile down => down Metadata -> IO ByteString

-- | Copy a downloaded file to its destination
downloadedCopyTo :: DownloadedFile down => down a -> Path Absolute -> IO ()

-- | Helper function to implement <tt>repWithMirrors</tt>.
mirrorsUnsupported :: Maybe [Mirror] -> IO a -> IO a
remoteRepoPath :: RepoLayout -> RemoteFile fs typ -> Formats fs RepoPath
remoteRepoPath' :: RepoLayout -> RemoteFile fs typ -> HasFormat fs f -> RepoPath

-- | Is a particular remote file cached?
data IsCached :: * -> *
[CacheAs] :: CachedFile -> IsCached Metadata
[DontCache] :: IsCached Binary
[CacheIndex] :: IsCached Binary

-- | Which remote files should we cache locally?
mustCache :: RemoteFile fs typ -> IsCached typ
instance GHC.Num.Num Hackage.Security.Client.Repository.AttemptNr
instance GHC.Classes.Ord Hackage.Security.Client.Repository.AttemptNr
instance GHC.Classes.Eq Hackage.Security.Client.Repository.AttemptNr
instance GHC.Show.Show Hackage.Security.Client.Repository.CachedFile
instance GHC.Classes.Ord Hackage.Security.Client.Repository.CachedFile
instance GHC.Classes.Eq Hackage.Security.Client.Repository.CachedFile
instance GHC.Show.Show (Hackage.Security.Client.Repository.RemoteFile fs typ)
instance GHC.Show.Show Hackage.Security.Client.Repository.SomeRemoteError
instance GHC.Classes.Eq (Hackage.Security.Client.Repository.IsCached typ)
instance GHC.Show.Show (Hackage.Security.Client.Repository.IsCached typ)
instance Hackage.Security.Util.Pretty.Pretty (Hackage.Security.Client.Repository.RemoteFile fs typ)
instance Hackage.Security.Util.Pretty.Pretty Hackage.Security.Client.Repository.CachedFile
instance GHC.Show.Show (Hackage.Security.Client.Repository.Repository down)
instance GHC.Exception.Exception Hackage.Security.Client.Repository.SomeRemoteError
instance Hackage.Security.Util.Pretty.Pretty Hackage.Security.Client.Repository.SomeRemoteError
instance Hackage.Security.Util.Pretty.Pretty Hackage.Security.Client.Repository.LogMessage
instance Hackage.Security.Util.Pretty.Pretty Hackage.Security.Client.Repository.UpdateFailure


-- | Main entry point into the Hackage Security framework for clients
module Hackage.Security.Client

-- | Generic logic for checking if there are updates
--   
--   This implements the logic described in Section 5.1, "The client
--   application", of the TUF spec. It checks which of the server metadata
--   has changed, and downloads all changed metadata to the local cache.
--   (Metadata here refers both to the TUF security metadata as well as the
--   Hackage packge index.)
--   
--   You should pass <tt>Nothing</tt> for the UTCTime _only_ under
--   exceptional circumstances (such as when the main server is down for
--   longer than the expiry dates used in the timestamp files on mirrors).
checkForUpdates :: (Throws VerificationError, Throws SomeRemoteError) => Repository down -> Maybe UTCTime -> IO HasUpdates
data HasUpdates
HasUpdates :: HasUpdates
NoUpdates :: HasUpdates

-- | Download a package
downloadPackage :: (Throws SomeRemoteError, Throws VerificationError, Throws InvalidPackageException) => Repository down -> PackageIdentifier -> Path Absolute -> IO ()

-- | Variation on <a>downloadPackage</a> that takes a FilePath instead.
downloadPackage' :: (Throws SomeRemoteError, Throws VerificationError, Throws InvalidPackageException) => Repository down -> PackageIdentifier -> FilePath -> IO ()

-- | Index directory
data Directory
Directory :: DirectoryEntry -> DirectoryEntry -> (forall dec. IndexFile dec -> Maybe DirectoryEntry) -> [(DirectoryEntry, IndexPath, Maybe (Some IndexFile))] -> Directory

-- | The first entry in the dictionary
[directoryFirst] :: Directory -> DirectoryEntry

-- | The next available (i.e., one after last) directory entry
[directoryNext] :: Directory -> DirectoryEntry

-- | Lookup an entry in the dictionary
--   
--   This is an efficient operation.
[directoryLookup] :: Directory -> forall dec. IndexFile dec -> Maybe DirectoryEntry

-- | An enumeration of all entries
--   
--   This field is lazily constructed, so if you don't need it, it does not
--   incur a performance overhead. Moreover, the <a>IndexFile</a> is also
--   created lazily so if you only need the raw <a>IndexPath</a> there is
--   no parsing overhead.
--   
--   The entries are ordered by <a>DirectoryEntry</a> so that the entries
--   can efficiently be read in sequence.
--   
--   NOTE: This means that there are two ways to enumerate all entries in
--   the tar file, since when lookup an entry using <a>indexLookupEntry</a>
--   the <a>DirectoryEntry</a> of the next entry is also returned. However,
--   this involves reading through the entire <tt>tar</tt> file. If you
--   only need to read <i>some</i> files, it is significantly more
--   efficient to enumerate the tar entries using <a>directoryEntries</a>
--   instead and only call <a>indexLookupEntry</a> when required.
[directoryEntries] :: Directory -> [(DirectoryEntry, IndexPath, Maybe (Some IndexFile))]

-- | Entry into the Hackage index.
newtype DirectoryEntry
DirectoryEntry :: TarEntryOffset -> DirectoryEntry

-- | (Low-level) block number of the tar index entry
--   
--   Exposed for the benefit of clients who read the <tt>.tar</tt> file
--   directly. For this reason also the <a>Show</a> and <a>Read</a>
--   instances for <a>DirectoryEntry</a> just print and parse the
--   underlying <tt>TarEntryOffset</tt>.
[directoryEntryBlockNo] :: DirectoryEntry -> TarEntryOffset

-- | Read the Hackage index directory
--   
--   Should only be called after <a>checkForUpdates</a>.
getDirectory :: Repository down -> IO Directory

-- | Files that we might request from the index
--   
--   The type index tells us the type of the decoded file, if any. For
--   files for which the library does not support decoding this will be
--   <tt>()</tt>. NOTE: Clients should NOT rely on this type index being
--   <tt>()</tt>, or they might break if we add support for parsing
--   additional file formats in the future.
--   
--   TODO: If we wanted to support legacy Hackage, we should also have a
--   case for the global preferred-versions file. But supporting legacy
--   Hackage will probably require more work anyway..
data IndexFile :: * -> *
[IndexPkgMetadata] :: PackageIdentifier -> IndexFile (Signed Targets)
[IndexPkgCabal] :: PackageIdentifier -> IndexFile ()
[IndexPkgPrefs] :: PackageName -> IndexFile ()

-- | Entry from the Hackage index; see <a>withIndex</a>.
data IndexEntry dec
IndexEntry :: IndexPath -> Maybe (IndexFile dec) -> ByteString -> Either SomeException dec -> EpochTime -> IndexEntry dec

-- | The raw path in the tarfile
[indexEntryPath] :: IndexEntry dec -> IndexPath

-- | The parsed file (if recognised)
[indexEntryPathParsed] :: IndexEntry dec -> Maybe (IndexFile dec)

-- | The raw contents
--   
--   Although this is a lazy bytestring, this is actually read into memory
--   strictly (i.e., it can safely be used outside the scope of withIndex
--   and friends).
[indexEntryContent] :: IndexEntry dec -> ByteString

-- | The parsed contents
--   
--   This field is lazily constructed; the parser is not unless you do a
--   pattern match on this value.
[indexEntryContentParsed] :: IndexEntry dec -> Either SomeException dec

-- | The time of the entry in the tarfile.
[indexEntryTime] :: IndexEntry dec -> EpochTime

-- | Various operations that we can perform on the index once its open
--   
--   Note that <a>IndexEntry</a> contains a fields both for the raw file
--   contents and the parsed file contents; clients can choose which to
--   use.
--   
--   In principle these callbacks will do verification (once we have
--   implemented author signing). Right now they don't need to do that,
--   because the index as a whole will have been verified.
data IndexCallbacks
IndexCallbacks :: (DirectoryEntry -> IO (Some IndexEntry, Maybe DirectoryEntry)) -> (forall dec. IndexFile dec -> IO (Maybe (IndexEntry dec))) -> (forall dec. DirectoryEntry -> IndexFile dec -> IO (IndexEntry dec)) -> (Throws InvalidPackageException => PackageIdentifier -> IO (Trusted ByteString)) -> (Throws InvalidPackageException => PackageIdentifier -> IO (Trusted Targets)) -> ((Throws InvalidPackageException, Throws VerificationError) => PackageIdentifier -> IO (Trusted FileInfo)) -> ((Throws InvalidPackageException, Throws VerificationError) => PackageIdentifier -> IO (Trusted Hash)) -> Directory -> IndexCallbacks

-- | Look up an entry by <a>DirectoryEntry</a>
--   
--   Since these <a>DirectoryEntry</a>s must come from somewhere (probably
--   from the <a>Directory</a>), it is assumed that they are valid; if they
--   are not, an (unchecked) exception will be thrown.
--   
--   This function also returns the <a>DirectoryEntry</a> of the
--   <i>next</i> file in the index (if any) for the benefit of clients who
--   wish to walk through the entire index.
[indexLookupEntry] :: IndexCallbacks -> DirectoryEntry -> IO (Some IndexEntry, Maybe DirectoryEntry)

-- | Look up an entry by <a>IndexFile</a>
--   
--   Returns <a>Nothing</a> if the <a>IndexFile</a> does not refer to an
--   existing file.
[indexLookupFile] :: IndexCallbacks -> forall dec. IndexFile dec -> IO (Maybe (IndexEntry dec))

-- | Variation if both the <a>DirectoryEntry</a> and the <a>IndexFile</a>
--   are known
--   
--   You might use this when scanning the index using
--   <a>directoryEntries</a>.
[indexLookupFileEntry] :: IndexCallbacks -> forall dec. DirectoryEntry -> IndexFile dec -> IO (IndexEntry dec)

-- | Get (raw) cabal file (wrapper around <a>indexLookupFile</a>)
[indexLookupCabal] :: IndexCallbacks -> Throws InvalidPackageException => PackageIdentifier -> IO (Trusted ByteString)

-- | Lookup package metadata (wrapper around <a>indexLookupFile</a>)
--   
--   This will throw an (unchecked) exception if the <tt>targets.json</tt>
--   file could not be parsed.
[indexLookupMetadata] :: IndexCallbacks -> Throws InvalidPackageException => PackageIdentifier -> IO (Trusted Targets)

-- | Get file info (including hash) (wrapper around <a>indexLookupFile</a>)
[indexLookupFileInfo] :: IndexCallbacks -> (Throws InvalidPackageException, Throws VerificationError) => PackageIdentifier -> IO (Trusted FileInfo)

-- | Get the SHA256 hash for a package (wrapper around
--   <tt>indexLookupInfo</tt>)
--   
--   In addition to the exceptions thrown by <tt>indexLookupInfo</tt>, this
--   will also throw an exception if the SHA256 is not listed in the
--   <a>FileMap</a> (again, this will not happen with a well-formed Hackage
--   index.)
[indexLookupHash] :: IndexCallbacks -> (Throws InvalidPackageException, Throws VerificationError) => PackageIdentifier -> IO (Trusted Hash)

-- | The <a>Directory</a> for the index
--   
--   We provide this here because <a>withIndex</a> will have read this
--   anyway.
[indexDirectory] :: IndexCallbacks -> Directory

-- | Look up entries in the Hackage index
--   
--   This is in <a>withFile</a> style so that clients can efficiently look
--   up multiple files from the index.
--   
--   Should only be called after <a>checkForUpdates</a>.
withIndex :: Repository down -> (IndexCallbacks -> IO a) -> IO a

-- | Check if we need to bootstrap (i.e., if we have root info)
requiresBootstrap :: Repository down -> IO Bool

-- | Bootstrap the chain of trust
--   
--   New clients might need to obtain a copy of the root metadata. This
--   however represents a chicken-and-egg problem: how can we verify the
--   root metadata we downloaded? The only possibility is to be provided
--   with a set of an out-of-band set of root keys and an appropriate
--   threshold.
--   
--   Clients who provide a threshold of 0 can do an initial "unsafe" update
--   of the root information, if they wish.
--   
--   The downloaded root information will _only_ be verified against the
--   provided keys, and _not_ against previously downloaded root info (if
--   any). It is the responsibility of the client to call <a>bootstrap</a>
--   only when this is the desired behaviour.
bootstrap :: (Throws SomeRemoteError, Throws VerificationError) => Repository down -> [KeyId] -> KeyThreshold -> IO ()

-- | File length
--   
--   Having verified file length information means we can protect against
--   endless data attacks and similar.
newtype FileLength
FileLength :: Int54 -> FileLength
[fileLength] :: FileLength -> Int54

-- | File hash
newtype Hash
Hash :: String -> Hash

-- | Key threshold
--   
--   The key threshold is the minimum number of keys a document must be
--   signed with. Key thresholds are specified in <tt>RoleSpec</tt> or
--   <tt>DelegationsSpec</tt>.
newtype KeyThreshold
KeyThreshold :: Int54 -> KeyThreshold

-- | File information
--   
--   This intentionally does not have an <a>Eq</a> instance; see
--   <a>knownFileInfoEqual</a> and <tt>verifyFileInfo</tt> instead.
--   
--   NOTE: Throughout we compute file information always over the raw
--   bytes. For example, when <tt>timestamp.json</tt> lists the hash of
--   <tt>snapshot.json</tt>, this hash is computed over the actual
--   <tt>snapshot.json</tt> file (as opposed to the canonical form of the
--   embedded JSON). This brings it in line with the hash computed over
--   target files, where that is the only choice available.
data FileInfo
FileInfo :: FileLength -> Map HashFn Hash -> FileInfo
[fileInfoLength] :: FileInfo -> FileLength
[fileInfoHashes] :: FileInfo -> Map HashFn Hash
data HashFn
HashFnSHA256 :: HashFn
HashFnMD5 :: HashFn

-- | File hash
newtype Hash
Hash :: String -> Hash

-- | Compute <a>FileInfo</a>
--   
--   TODO: Currently this will load the entire input bytestring into
--   memory. We need to make this incremental, by computing the length and
--   all hashes in a single traversal over the input.
fileInfo :: ByteString -> FileInfo

-- | Compute <a>FileInfo</a>
computeFileInfo :: FsRoot root => Path root -> IO FileInfo

-- | Compare the expected trusted file info against the actual file info of
--   a target file.
--   
--   This should be used only when the <a>FileInfo</a> is already known. If
--   we want to compare known <a>FileInfo</a> against a file on disk we
--   should delay until we have confirmed that the file lengths match (see
--   <tt>downloadedVerify</tt>).
compareTrustedFileInfo :: FileInfo -> FileInfo -> Bool
knownFileInfoEqual :: FileInfo -> FileInfo -> Bool

-- | Extract SHA256 hash from <a>FileInfo</a> (if present)
fileInfoSHA256 :: FileInfo -> Maybe Hash

-- | 54-bit integer values
--   
--   JavaScript can only safely represent numbers between <tt>-(2^53 -
--   1)</tt> and <tt>2^53 - 1</tt>.
--   
--   TODO: Although we introduce the type here, we don't actually do any
--   bounds checking and just inherit all type class instance from Int64.
--   We should probably define <a>fromInteger</a> to do bounds checking,
--   give different instances for type classes such as <a>Bounded</a> and
--   <a>FiniteBits</a>, etc.
data Int54
class HasHeader a

-- | File expiry date
fileExpires :: HasHeader a => Lens' a FileExpires

-- | File version (monotonically increasing counter)
fileVersion :: HasHeader a => Lens' a FileVersion

-- | File version
--   
--   The file version is a flat integer which must monotonically increase
--   on every file update.
--   
--   <a>Show</a> and <a>Read</a> instance are defined in terms of the
--   underlying <a>Int</a> (this is use for example by hackage during the
--   backup process).
newtype FileVersion
FileVersion :: Int54 -> FileVersion

-- | File expiry date
--   
--   A <a>Nothing</a> value here means no expiry. That makes it possible to
--   set some files to never expire. (Note that not having the Maybe in the
--   type here still allows that, because you could set an expiry date 2000
--   years into the future. By having the Maybe here we avoid the _need_
--   for such encoding issues.)
newtype FileExpires
FileExpires :: (Maybe UTCTime) -> FileExpires

-- | Occassionally it is useful to read only a header from a file.
--   
--   <tt>HeaderOnly</tt> intentionally only has a <a>FromJSON</a> instance
--   (no <a>ToJSON</a>).
data Header
Header :: FileExpires -> FileVersion -> Header
[headerExpires] :: Header -> FileExpires
[headerVersion] :: Header -> FileVersion
expiresInDays :: UTCTime -> Integer -> FileExpires
expiresNever :: FileExpires
isExpired :: UTCTime -> FileExpires -> Bool
versionInitial :: FileVersion
versionIncrement :: FileVersion -> FileVersion

-- | Location of the various files we cache
--   
--   Although the generic TUF algorithms do not care how we organize the
--   cache, we nonetheless specity this here because as long as there are
--   tools which access files in the cache directly we need to define the
--   cache layout. See also comments for <tt>defaultCacheLayout</tt>.
data CacheLayout
CacheLayout :: CachePath -> CachePath -> CachePath -> CachePath -> CachePath -> CachePath -> CachePath -> CacheLayout

-- | TUF root metadata
[cacheLayoutRoot] :: CacheLayout -> CachePath

-- | TUF timestamp
[cacheLayoutTimestamp] :: CacheLayout -> CachePath

-- | TUF snapshot
[cacheLayoutSnapshot] :: CacheLayout -> CachePath

-- | TUF mirrors list
[cacheLayoutMirrors] :: CacheLayout -> CachePath

-- | Uncompressed index tarball
[cacheLayoutIndexTar] :: CacheLayout -> CachePath

-- | Index to the uncompressed index tarball
[cacheLayoutIndexIdx] :: CacheLayout -> CachePath

-- | Compressed index tarball
--   
--   We cache both the compressed and the uncompressed tarballs, because
--   incremental updates happen through the compressed tarball, but reads
--   happen through the uncompressed one (with the help of the tarball
--   index).
[cacheLayoutIndexTarGz] :: CacheLayout -> CachePath

-- | The cache layout cabal-install uses
--   
--   We cache the index as <tt><a>cache</a>/00-index.tar</tt>; this is
--   important because `cabal-install` expects to find it there (and does
--   not currently go through the hackage-security library to get files
--   from the index).
cabalCacheLayout :: CacheLayout

-- | Layout of the files within the index tarball
data IndexLayout
IndexLayout :: (forall dec. IndexFile dec -> IndexPath) -> (IndexPath -> Maybe (Some IndexFile)) -> IndexLayout

-- | Translate an <a>IndexFile</a> to a path
[indexFileToPath] :: IndexLayout -> forall dec. IndexFile dec -> IndexPath

-- | Parse an <a>FilePath</a>
[indexFileFromPath] :: IndexLayout -> IndexPath -> Maybe (Some IndexFile)

-- | Files that we might request from the index
--   
--   The type index tells us the type of the decoded file, if any. For
--   files for which the library does not support decoding this will be
--   <tt>()</tt>. NOTE: Clients should NOT rely on this type index being
--   <tt>()</tt>, or they might break if we add support for parsing
--   additional file formats in the future.
--   
--   TODO: If we wanted to support legacy Hackage, we should also have a
--   case for the global preferred-versions file. But supporting legacy
--   Hackage will probably require more work anyway..
data IndexFile :: * -> *
[IndexPkgMetadata] :: PackageIdentifier -> IndexFile (Signed Targets)
[IndexPkgCabal] :: PackageIdentifier -> IndexFile ()
[IndexPkgPrefs] :: PackageName -> IndexFile ()

-- | The layout of the index as maintained on Hackage
hackageIndexLayout :: IndexLayout
indexLayoutPkgMetadata :: IndexLayout -> PackageIdentifier -> IndexPath
indexLayoutPkgCabal :: IndexLayout -> PackageIdentifier -> IndexPath
indexLayoutPkgPrefs :: IndexLayout -> PackageName -> IndexPath

-- | Layout of a repository
data RepoLayout
RepoLayout :: RepoPath -> RepoPath -> RepoPath -> RepoPath -> RepoPath -> RepoPath -> (PackageIdentifier -> RepoPath) -> RepoLayout

-- | TUF root metadata
[repoLayoutRoot] :: RepoLayout -> RepoPath

-- | TUF timestamp
[repoLayoutTimestamp] :: RepoLayout -> RepoPath

-- | TUF snapshot
[repoLayoutSnapshot] :: RepoLayout -> RepoPath

-- | TUF mirrors list
[repoLayoutMirrors] :: RepoLayout -> RepoPath

-- | Compressed index tarball
[repoLayoutIndexTarGz] :: RepoLayout -> RepoPath

-- | Uncompressed index tarball
[repoLayoutIndexTar] :: RepoLayout -> RepoPath

-- | Path to the package tarball
[repoLayoutPkgTarGz] :: RepoLayout -> PackageIdentifier -> RepoPath

-- | The layout used on Hackage
hackageRepoLayout :: RepoLayout

-- | Layout used by cabal for ("legacy") local repos
--   
--   Obviously, such repos do not normally contain any of the TUF files, so
--   their location is more or less arbitrary here.
cabalLocalRepoLayout :: RepoLayout
data Mirrors
Mirrors :: FileVersion -> FileExpires -> [Mirror] -> Mirrors
[mirrorsVersion] :: Mirrors -> FileVersion
[mirrorsExpires] :: Mirrors -> FileExpires
[mirrorsMirrors] :: Mirrors -> [Mirror]

-- | Definition of a mirror
--   
--   NOTE: Unlike the TUF specification, we require that all mirrors must
--   have the same format. That is, we omit <tt>metapath</tt> and
--   <tt>targetspath</tt>.
data Mirror
Mirror :: URI -> MirrorContent -> Mirror
[mirrorUrlBase] :: Mirror -> URI
[mirrorContent] :: Mirror -> MirrorContent

-- | Full versus partial mirrors
--   
--   The TUF spec explicitly allows for partial mirrors, with the mirrors
--   file specifying (through patterns) what is available from partial
--   mirrors.
--   
--   For now we only support full mirrors; if we wanted to add partial
--   mirrors, we would add a second <tt>MirrorPartial</tt> constructor here
--   with arguments corresponding to TUF's <tt>metacontent</tt> and
--   <tt>targetscontent</tt> fields.
data MirrorContent
MirrorFull :: MirrorContent
type MirrorDescription = String

-- | Give a human-readable description of a particular mirror
--   
--   (for use in error messages)
describeMirror :: Mirror -> MirrorDescription

-- | The root of the repository
--   
--   Repository roots can be anchored at a remote URL or a local directory.
--   
--   Note that even for remote repos <a>RepoRoot</a> is (potentially)
--   different from <a>Web</a> -- for a repository located at, say,
--   <tt><a>http://hackage.haskell.org</a></tt> they happen to coincide,
--   but for one location at
--   <tt><a>http://example.com/some/subdirectory</a></tt> they do not.
data RepoRoot

-- | Paths relative to the root of the repository
type RepoPath = Path RepoRoot
anchorRepoPathLocally :: Path root -> RepoPath -> Path root
anchorRepoPathRemotely :: Path Web -> RepoPath -> Path Web

-- | The root of the index tarball
data IndexRoot

-- | Paths relative to the root of the index tarball
type IndexPath = Path IndexRoot

-- | The cache directory
data CacheRoot
type CachePath = Path CacheRoot

-- | Anchor a cache path to the location of the cache
anchorCachePath :: Path root -> CachePath -> Path root

-- | The root metadata
--   
--   NOTE: We must have the invariant that ALL keys (apart from delegation
--   keys) must be listed in <a>rootKeys</a>. (Delegation keys satisfy a
--   similar invariant, see Targets.)
data Root
Root :: FileVersion -> FileExpires -> KeyEnv -> RootRoles -> Root
[rootVersion] :: Root -> FileVersion
[rootExpires] :: Root -> FileExpires
[rootKeys] :: Root -> KeyEnv
[rootRoles] :: Root -> RootRoles
data RootRoles
RootRoles :: RoleSpec Root -> RoleSpec Snapshot -> RoleSpec Targets -> RoleSpec Timestamp -> RoleSpec Mirrors -> RootRoles
[rootRolesRoot] :: RootRoles -> RoleSpec Root
[rootRolesSnapshot] :: RootRoles -> RoleSpec Snapshot
[rootRolesTargets] :: RootRoles -> RoleSpec Targets
[rootRolesTimestamp] :: RootRoles -> RoleSpec Timestamp
[rootRolesMirrors] :: RootRoles -> RoleSpec Mirrors

-- | Role specification
--   
--   The phantom type indicates what kind of type this role is meant to
--   verify.
data RoleSpec a
RoleSpec :: [Some PublicKey] -> KeyThreshold -> RoleSpec a
[roleSpecKeys] :: RoleSpec a -> [Some PublicKey]
[roleSpecThreshold] :: RoleSpec a -> KeyThreshold
data Signed a
Signed :: a -> Signatures -> Signed a
[signed] :: Signed a -> a
[signatures] :: Signed a -> Signatures

-- | A list of signatures
--   
--   Invariant: each signature must be made with a different key. We
--   enforce this invariant for incoming untrusted data
--   (<a>fromPreSignatures</a>) but not for lists of signatures that we
--   create in code.
newtype Signatures
Signatures :: [Signature] -> Signatures
data Signature
Signature :: ByteString -> Some PublicKey -> Signature
[signature] :: Signature -> ByteString
[signatureKey] :: Signature -> Some PublicKey

-- | Create a new document without any signatures
unsigned :: a -> Signed a

-- | Sign a document
withSignatures :: ToJSON WriteJSON a => RepoLayout -> [Some Key] -> a -> Signed a

-- | Variation on <a>withSignatures</a> that doesn't need the repo layout
withSignatures' :: ToJSON Identity a => [Some Key] -> a -> Signed a

-- | Construct signatures for already rendered value
signRendered :: [Some Key] -> ByteString -> Signatures
verifySignature :: ByteString -> Signature -> Bool

-- | General FromJSON instance for signed datatypes
--   
--   We don't give a general FromJSON instance for Signed because for some
--   datatypes we need to do something special (datatypes where we need to
--   read key environments); for instance, see the "Signed Root" instance.
signedFromJSON :: (MonadKeys m, FromJSON m a) => JSValue -> m (Signed a)

-- | Signature verification
--   
--   NOTES: 1. By definition, the signature must be verified against the
--   canonical JSON format. This means we _must_ parse and then pretty
--   print (as we do here) because the document as stored may or may not be
--   in canonical format. 2. However, it is important that we NOT translate
--   from the JSValue to whatever internal datatype we are using and then
--   back to JSValue, because that may not roundtrip: we must allow for
--   additional fields in the JSValue that we ignore (and would therefore
--   lose when we attempt to roundtrip). 3. We verify that all signatures
--   are valid, but we cannot verify (here) that these signatures are
--   signed with the right key, or that we have a sufficient number of
--   signatures. This will be the responsibility of the calling code.
verifySignatures :: JSValue -> Signatures -> Bool

-- | File with uninterpreted signatures
--   
--   Sometimes we want to be able to read a file without interpreting the
--   signatures (that is, resolving the key IDs) or doing any kind of
--   checks on them. One advantage of this is that this allows us to read
--   many file types without any key environment at all, which is sometimes
--   useful.
data UninterpretedSignatures a
UninterpretedSignatures :: a -> [PreSignature] -> UninterpretedSignatures a
[uninterpretedSigned] :: UninterpretedSignatures a -> a
[uninterpretedSignatures] :: UninterpretedSignatures a -> [PreSignature]

-- | A signature with a key ID (rather than an actual key)
--   
--   This corresponds precisely to the TUF representation of a signature.
data PreSignature
PreSignature :: ByteString -> Some KeyType -> KeyId -> PreSignature
[presignature] :: PreSignature -> ByteString
[presigMethod] :: PreSignature -> Some KeyType
[presigKeyId] :: PreSignature -> KeyId

-- | Convert a pre-signature to a signature
--   
--   Verifies that the key type matches the advertised method.
fromPreSignature :: MonadKeys m => PreSignature -> m Signature

-- | Convert a list of <a>PreSignature</a>s to a list of <a>Signature</a>s
--   
--   This verifies the invariant that all signatures are made with
--   different keys. We do this on the presignatures rather than the
--   signatures so that we can do the check on key IDs, rather than keys
--   (the latter don't have an Ord instance).
fromPreSignatures :: MonadKeys m => [PreSignature] -> m Signatures

-- | Convert signature to pre-signature
toPreSignature :: Signature -> PreSignature

-- | Convert list of pre-signatures to a list of signatures
toPreSignatures :: Signatures -> [PreSignature]
data Snapshot
Snapshot :: FileVersion -> FileExpires -> FileInfo -> FileInfo -> FileInfo -> Maybe FileInfo -> Snapshot
[snapshotVersion] :: Snapshot -> FileVersion
[snapshotExpires] :: Snapshot -> FileExpires

-- | File info for the root metadata
--   
--   We list this explicitly in the snapshot so that we can check if we
--   need to update the root metadata without first having to download the
--   entire index tarball.
[snapshotInfoRoot] :: Snapshot -> FileInfo

-- | File info for the mirror metadata
[snapshotInfoMirrors] :: Snapshot -> FileInfo

-- | Compressed index tarball
[snapshotInfoTarGz] :: Snapshot -> FileInfo

-- | Uncompressed index tarball
--   
--   Repositories are not required to provide this.
[snapshotInfoTar] :: Snapshot -> Maybe FileInfo

-- | Target metadata
--   
--   Most target files do not need expiry dates because they are not
--   subject to change (and hence attacks like freeze attacks are not a
--   concern).
data Targets
Targets :: FileVersion -> FileExpires -> FileMap -> Maybe Delegations -> Targets
[targetsVersion] :: Targets -> FileVersion
[targetsExpires] :: Targets -> FileExpires
[targetsTargets] :: Targets -> FileMap
[targetsDelegations] :: Targets -> Maybe Delegations

-- | Delegations
--   
--   Much like the Root datatype, this must have an invariant that ALL used
--   keys (apart from the global keys, which are in the root key
--   environment) must be listed in <a>delegationsKeys</a>.
data Delegations
Delegations :: KeyEnv -> [DelegationSpec] -> Delegations
[delegationsKeys] :: Delegations -> KeyEnv
[delegationsRoles] :: Delegations -> [DelegationSpec]

-- | Delegation specification
--   
--   NOTE: This is a close analogue of <tt>RoleSpec</tt>.
data DelegationSpec
DelegationSpec :: [Some PublicKey] -> KeyThreshold -> Delegation -> DelegationSpec
[delegationSpecKeys] :: DelegationSpec -> [Some PublicKey]
[delegationSpecThreshold] :: DelegationSpec -> KeyThreshold
[delegation] :: DelegationSpec -> Delegation

-- | A delegation
--   
--   A delegation is a pair of a pattern and a replacement.
--   
--   See <tt>match</tt> for an example.
data Delegation
Delegation :: (Pattern a) -> (Replacement a) -> Delegation
targetsLookup :: TargetPath -> Targets -> Maybe FileInfo
data Timestamp
Timestamp :: FileVersion -> FileExpires -> FileInfo -> Timestamp
[timestampVersion] :: Timestamp -> FileVersion
[timestampExpires] :: Timestamp -> FileExpires
[timestampInfoSnapshot] :: Timestamp -> FileInfo
data Ed25519
data Key a
[KeyEd25519] :: PublicKey -> SecretKey -> Key Ed25519
data PublicKey a
[PublicKeyEd25519] :: PublicKey -> PublicKey Ed25519
data PrivateKey a
[PrivateKeyEd25519] :: SecretKey -> PrivateKey Ed25519
data KeyType typ
[KeyTypeEd25519] :: KeyType Ed25519
somePublicKey :: Some Key -> Some PublicKey
somePublicKeyType :: Some PublicKey -> Some KeyType
someKeyId :: HasKeyId key => Some key -> KeyId
publicKey :: Key a -> PublicKey a
privateKey :: Key a -> PrivateKey a
createKey :: KeyType key -> IO (Key key)
createKey' :: KeyType key -> IO (Some Key)

-- | The key ID of a key, by definition, is the hexdigest of the SHA-256
--   hash of the canonical JSON form of the key where the private object
--   key is excluded.
--   
--   NOTE: The FromJSON and ToJSON instances for KeyId are ntentially
--   omitted. Use writeKeyAsId instead.
newtype KeyId
KeyId :: String -> KeyId
[keyIdString] :: KeyId -> String

-- | Compute the key ID of a key
class HasKeyId key
keyId :: HasKeyId key => key typ -> KeyId

-- | Sign a bytestring and return the signature
--   
--   TODO: It is unfortunate that we have to convert to a strict bytestring
--   for ed25519
sign :: PrivateKey typ -> ByteString -> ByteString
verify :: PublicKey typ -> ByteString -> ByteString -> Bool
trusted :: Trusted a -> a

-- | Repository
--   
--   This is an abstract representation of a repository. It simply provides
--   a way to download metafiles and target files, without specifying how
--   this is done. For instance, for a local repository this could just be
--   doing a file read, whereas for remote repositories this could be using
--   any kind of HTTP client.
data Repository down
class DownloadedFile (down :: * -> *)

-- | Verify a download file
downloadedVerify :: DownloadedFile down => down a -> Trusted FileInfo -> IO Bool

-- | Read the file we just downloaded into memory
--   
--   We never read binary data, only metadata.
downloadedRead :: DownloadedFile down => down Metadata -> IO ByteString

-- | Copy a downloaded file to its destination
downloadedCopyTo :: DownloadedFile down => down a -> Path Absolute -> IO ()

-- | Repository-specific exceptions
--   
--   For instance, for repositories using HTTP this might correspond to a
--   404; for local repositories this might correspond to file-not-found,
--   etc.
data SomeRemoteError :: *
[SomeRemoteError] :: Exception e => e -> SomeRemoteError

-- | Log messages
--   
--   We use a <a>RemoteFile</a> rather than a <a>RepoPath</a> here because
--   we might not have a <a>RepoPath</a> for the file that we were trying
--   to download (that is, for example if the server does not provide an
--   uncompressed tarball, it doesn't make much sense to list the path to
--   that non-existing uncompressed tarball).
data LogMessage

-- | Root information was updated
--   
--   This message is issued when the root information is updated as part of
--   the normal check for updates procedure. If the root information is
--   updated because of a verification error WarningVerificationError is
--   issued instead.
LogRootUpdated :: LogMessage

-- | A verification error
--   
--   Verification errors can be temporary, and may be resolved later; hence
--   these are just warnings. (Verification errors that cannot be resolved
--   are thrown as exceptions.)
LogVerificationError :: VerificationError -> LogMessage

-- | Download a file from a repository
LogDownloading :: (RemoteFile fs typ) -> LogMessage

-- | Incrementally updating a file from a repository
LogUpdating :: (RemoteFile fs Binary) -> LogMessage

-- | Selected a particular mirror
LogSelectedMirror :: MirrorDescription -> LogMessage

-- | Updating a file failed (we will instead download it whole)
LogCannotUpdate :: (RemoteFile fs Binary) -> UpdateFailure -> LogMessage

-- | We got an exception with a particular mirror (we will try with a
--   different mirror if any are available)
LogMirrorFailed :: MirrorDescription -> SomeException -> LogMessage

-- | Re-throw all exceptions thrown by the client API as unchecked
--   exceptions
uncheckClientErrors :: ((Throws VerificationError, Throws SomeRemoteError, Throws InvalidPackageException) => IO a) -> IO a

-- | Errors thrown during role validation
data VerificationError

-- | Not enough signatures signed with the appropriate keys
VerificationErrorSignatures :: TargetPath -> VerificationError

-- | The file is expired
VerificationErrorExpired :: TargetPath -> VerificationError

-- | The file version is less than the previous version
VerificationErrorVersion :: TargetPath -> VerificationError

-- | File information mismatch
VerificationErrorFileInfo :: TargetPath -> VerificationError

-- | We tried to lookup file information about a particular target file,
--   but the information wasn't in the corresponding <tt>targets.json</tt>
--   file.
VerificationErrorUnknownTarget :: TargetPath -> VerificationError

-- | The metadata for the specified target is missing a SHA256
VerificationErrorMissingSHA256 :: TargetPath -> VerificationError

-- | Some verification errors materialize as deserialization errors
--   
--   For example: if we try to deserialize a timestamp file but the
--   timestamp key has been rolled over, deserialization of the file will
--   fail with <a>DeserializationErrorUnknownKey</a>.
VerificationErrorDeserialization :: TargetPath -> DeserializationError -> VerificationError

-- | The spec stipulates that if a verification error occurs during the
--   check for updates, we must download new root information and start
--   over. However, we limit how often we attempt this.
--   
--   We record all verification errors that occurred before we gave up.
VerificationErrorLoop :: VerificationHistory -> VerificationError
type VerificationHistory = [Either RootUpdated VerificationError]

-- | Root metadata updated (as part of the normal update process)
data RootUpdated
RootUpdated :: RootUpdated
data InvalidPackageException
InvalidPackageException :: PackageIdentifier -> InvalidPackageException
data InvalidFileInIndex
InvalidFileInIndex :: IndexFile dec -> ByteString -> DeserializationError -> InvalidFileInIndex
[invalidFileInIndex] :: InvalidFileInIndex -> IndexFile dec
[invalidFileInIndexRaw] :: InvalidFileInIndex -> ByteString
[invalidFileInIndexError] :: InvalidFileInIndex -> DeserializationError
data LocalFileCorrupted
LocalFileCorrupted :: DeserializationError -> LocalFileCorrupted
instance GHC.Classes.Ord Hackage.Security.Client.DirectoryEntry
instance GHC.Classes.Eq Hackage.Security.Client.DirectoryEntry
instance GHC.Classes.Ord Hackage.Security.Client.HasUpdates
instance GHC.Classes.Eq Hackage.Security.Client.HasUpdates
instance GHC.Show.Show Hackage.Security.Client.HasUpdates
instance GHC.Show.Show Hackage.Security.Client.InvalidPackageException
instance GHC.Show.Show Hackage.Security.Client.LocalFileCorrupted
instance GHC.Show.Show Hackage.Security.Client.InvalidFileInIndex
instance GHC.Show.Show Hackage.Security.Client.DirectoryEntry
instance GHC.Read.Read Hackage.Security.Client.DirectoryEntry
instance GHC.Exception.Exception Hackage.Security.Client.InvalidPackageException
instance GHC.Exception.Exception Hackage.Security.Client.LocalFileCorrupted
instance GHC.Exception.Exception Hackage.Security.Client.InvalidFileInIndex
instance Hackage.Security.Util.Pretty.Pretty Hackage.Security.Client.InvalidPackageException
instance Hackage.Security.Util.Pretty.Pretty Hackage.Security.Client.LocalFileCorrupted
instance Hackage.Security.Util.Pretty.Pretty Hackage.Security.Client.InvalidFileInIndex


-- | Abstracting over HTTP libraries
module Hackage.Security.Client.Repository.HttpLib

-- | Abstraction over HTTP clients
--   
--   This avoids insisting on a particular implementation (such as the HTTP
--   package) and allows for other implementations (such as a conduit based
--   one).
--   
--   NOTE: Library-specific exceptions MUST be wrapped in
--   <a>SomeRemoteError</a>.
data HttpLib
HttpLib :: (forall a. Throws SomeRemoteError => [HttpRequestHeader] -> URI -> ([HttpResponseHeader] -> BodyReader -> IO a) -> IO a) -> (forall a. Throws SomeRemoteError => [HttpRequestHeader] -> URI -> (Int, Int) -> (HttpStatus -> [HttpResponseHeader] -> BodyReader -> IO a) -> IO a) -> HttpLib

-- | Download a file
[httpGet] :: HttpLib -> forall a. Throws SomeRemoteError => [HttpRequestHeader] -> URI -> ([HttpResponseHeader] -> BodyReader -> IO a) -> IO a

-- | Download a byte range
--   
--   Range is starting and (exclusive) end offset in bytes.
--   
--   HTTP servers are normally expected to respond to a range request with
--   a "206 Partial Content" response. However, servers can respond with a
--   "200 OK" response, sending the entire file instead (for instance, this
--   may happen for servers that don't actually support range rqeuests, but
--   for which we optimistically assumed they did). Implementations of
--   <a>HttpLib</a> may accept such a response and inform the
--   <tt>hackage-security</tt> library that the whole file is being
--   returned; the security library can then decide to execute the
--   <a>BodyReader</a> anyway (downloading the entire file) or abort the
--   request and try something else. For this reason the security library
--   must be informed whether the server returned the full file or the
--   requested range.
[httpGetRange] :: HttpLib -> forall a. Throws SomeRemoteError => [HttpRequestHeader] -> URI -> (Int, Int) -> (HttpStatus -> [HttpResponseHeader] -> BodyReader -> IO a) -> IO a

-- | Additional request headers
--   
--   Since different libraries represent headers differently, here we just
--   abstract over the few request headers that we might want to set
data HttpRequestHeader

-- | Set <tt>Cache-Control: max-age=0</tt>
HttpRequestMaxAge0 :: HttpRequestHeader

-- | Set <tt>Cache-Control: no-transform</tt>
HttpRequestNoTransform :: HttpRequestHeader

-- | Response headers
--   
--   Since different libraries represent headers differently, here we just
--   abstract over the few response headers that we might want to know
--   about.
data HttpResponseHeader

-- | Server accepts byte-range requests (<tt>Accept-Ranges: bytes</tt>)
HttpResponseAcceptRangesBytes :: HttpResponseHeader

-- | HTTP status code
data HttpStatus

-- | 200 OK
HttpStatus200OK :: HttpStatus

-- | 206 Partial Content
HttpStatus206PartialContent :: HttpStatus

-- | Proxy configuration
--   
--   Although actually setting the proxy is the purview of the
--   initialization function for individual <a>HttpLib</a> implementations
--   and therefore outside the scope of this module, we offer this
--   <tt>ProxyConfiguration</tt> type here as a way to uniformly configure
--   proxies across all <a>HttpLib</a>s.
data ProxyConfig a

-- | Don't use a proxy
ProxyConfigNone :: ProxyConfig a

-- | Use this specific proxy
--   
--   Individual HTTP backends use their own types for specifying proxies.
ProxyConfigUse :: a -> ProxyConfig a

-- | Use automatic proxy settings
--   
--   What precisely automatic means is <a>HttpLib</a> specific, though
--   typically it will involve looking at the <tt>HTTP_PROXY</tt>
--   environment variable or the (Windows) registry.
ProxyConfigAuto :: ProxyConfig a

-- | An <tt>IO</tt> action that represents an incoming response body coming
--   from the server.
--   
--   The action gets a single chunk of data from the response body, or an
--   empty bytestring if no more data is available.
--   
--   This definition is copied from the <tt>http-client</tt> package.
type BodyReader = IO ByteString

-- | Construct a <tt>Body</tt> reader from a lazy bytestring
--   
--   This is appropriate if the lazy bytestring is constructed, say, by
--   calling <tt>hGetContents</tt> on a network socket, and the chunks of
--   the bytestring correspond to the chunks as they are returned from the
--   OS network layer.
--   
--   If the lazy bytestring needs to be re-chunked this function is NOT
--   suitable.
bodyReaderFromBS :: ByteString -> IO BodyReader
instance GHC.Show.Show Hackage.Security.Client.Repository.HttpLib.HttpResponseHeader
instance GHC.Classes.Ord Hackage.Security.Client.Repository.HttpLib.HttpResponseHeader
instance GHC.Classes.Eq Hackage.Security.Client.Repository.HttpLib.HttpResponseHeader
instance GHC.Show.Show Hackage.Security.Client.Repository.HttpLib.HttpRequestHeader
instance GHC.Classes.Ord Hackage.Security.Client.Repository.HttpLib.HttpRequestHeader
instance GHC.Classes.Eq Hackage.Security.Client.Repository.HttpLib.HttpRequestHeader


-- | The files we cache from the repository
--   
--   Both the Local and the Remote repositories make use of this module.
module Hackage.Security.Client.Repository.Cache

-- | Location and layout of the local cache
data Cache
Cache :: Path Absolute -> CacheLayout -> Cache
[cacheRoot] :: Cache -> Path Absolute
[cacheLayout] :: Cache -> CacheLayout

-- | Get a cached file (if available)
getCached :: Cache -> CachedFile -> IO (Maybe (Path Absolute))

-- | Get the cached root
--   
--   Calling <a>getCachedRoot</a> without root info available is a
--   programmer error and will result in an unchecked exception. See
--   <tt>requiresBootstrap</tt>.
getCachedRoot :: Cache -> IO (Path Absolute)

-- | Get the cached index (if available)
getCachedIndex :: Cache -> Format f -> IO (Maybe (Path Absolute))

-- | Delete a previously downloaded remote file
clearCache :: Cache -> IO ()
withIndex :: Cache -> (Handle -> IO a) -> IO a
getIndexIdx :: Cache -> IO TarIndex

-- | Cache a previously downloaded remote file
cacheRemoteFile :: forall down typ f. DownloadedFile down => Cache -> down typ -> Format f -> IsCached typ -> IO ()

-- | Lock the cache
--   
--   This avoids two concurrent processes updating the cache at the same
--   time, provided they both take the lock.
lockCache :: Cache -> IO () -> IO ()


-- | Local repository
module Hackage.Security.Client.Repository.Local

-- | Location of the repository
--   
--   Note that we regard the local repository as immutable; we cache files
--   just like we do for remote repositories.
type LocalRepo = Path Absolute
data LocalFile a

-- | Initialize the repository (and cleanup resources afterwards)
--   
--   Like a remote repository, a local repository takes a RepoLayout as
--   argument; but where the remote repository interprets this RepoLayout
--   relative to a URL, the local repository interprets it relative to a
--   local directory.
--   
--   It uses the same cache as the remote repository.
withRepository :: LocalRepo -> Cache -> RepoLayout -> IndexLayout -> (LogMessage -> IO ()) -> (Repository LocalFile -> IO a) -> IO a
instance Hackage.Security.Client.Repository.DownloadedFile Hackage.Security.Client.Repository.Local.LocalFile


-- | An implementation of Repository that talks to repositories over HTTP.
--   
--   This implementation is itself parameterized over a
--   <tt>HttpClient</tt>, so that it it not tied to a specific library; for
--   instance, <tt>HttpClient</tt> can be implemented with the
--   <tt>HTTP</tt> library, the <tt>http-client</tt> libary, or others.
--   
--   It would also be possible to give _other_ Repository implementations
--   that talk to repositories over HTTP, if you want to make other design
--   decisions than we did here, in particular:
--   
--   <ul>
--   <li>We attempt to do incremental downloads of the index when
--   possible.</li>
--   <li>We reuse the <a>Repository.Local</a> to deal with the local
--   cache.</li>
--   <li>We download <tt>timestamp.json</tt> and <tt>snapshot.json</tt>
--   together. This is implemented here because:</li>
--   <li>One level down (HttpClient) we have no access to the local
--   cache</li>
--   <li>One level up (Repository API) would require _all_ Repositories to
--   implement this optimization.</li>
--   </ul>
module Hackage.Security.Client.Repository.Remote

-- | Initialize the repository (and cleanup resources afterwards)
--   
--   We allow to specify multiple mirrors to initialize the repository.
--   These are mirrors that can be found "out of band" (out of the scope of
--   the TUF protocol), for example in a <tt>cabal.config</tt> file. The
--   TUF protocol itself will specify that any of these mirrors can serve a
--   <tt>mirrors.json</tt> file that itself contains mirrors; we consider
--   these as _additional_ mirrors to the ones that are passed here.
--   
--   NOTE: The list of mirrors should be non-empty (and should typically
--   include the primary server).
--   
--   TODO: In the future we could allow finer control over precisely which
--   mirrors we use (which combination of the mirrors that are passed as
--   arguments here and the mirrors that we get from <tt>mirrors.json</tt>)
--   as well as indicating mirror preferences.
withRepository :: HttpLib -> [URI] -> RepoOpts -> Cache -> RepoLayout -> IndexLayout -> (LogMessage -> IO ()) -> (Repository RemoteTemp -> IO a) -> IO a

-- | Repository options with a reasonable default
--   
--   Clients should use <tt>defaultRepositoryOpts</tt> and override
--   required settings.
data RepoOpts
RepoOpts :: Bool -> RepoOpts

-- | Allow additional mirrors?
--   
--   If this is set to True (default), in addition to the (out-of-band)
--   specified mirrors we will also use mirrors reported by those
--   out-of-band mirrors (that is, <tt>mirrors.json</tt>).
[repoAllowAdditionalMirrors] :: RepoOpts -> Bool

-- | Default repository options
defaultRepoOpts :: RepoOpts
data RemoteTemp :: * -> *
data FileSize

-- | For most files we download we know the exact size beforehand (because
--   this information comes from the snapshot or delegated info)
FileSizeExact :: Int54 -> FileSize

-- | For some files we might not know the size beforehand, but we might be
--   able to provide an upper bound (timestamp, root info)
FileSizeBound :: Int54 -> FileSize
fileSizeWithinBounds :: Int54 -> FileSize -> Bool
instance GHC.Show.Show Hackage.Security.Client.Repository.Remote.FileSize
instance GHC.Show.Show Hackage.Security.Client.Repository.Remote.FileTooLarge
instance Hackage.Security.Util.Pretty.Pretty Hackage.Security.Client.Repository.Remote.FileTooLarge
instance GHC.Exception.Exception Hackage.Security.Client.Repository.Remote.FileTooLarge
instance Hackage.Security.Util.Pretty.Pretty (Hackage.Security.Client.Repository.Remote.RemoteTemp typ)
instance Hackage.Security.Client.Repository.DownloadedFile Hackage.Security.Client.Repository.Remote.RemoteTemp


-- | Main entry point into the Hackage Security framework for clients
module Hackage.Security.Server
data Ed25519
data Key a
[KeyEd25519] :: PublicKey -> SecretKey -> Key Ed25519
data PublicKey a
[PublicKeyEd25519] :: PublicKey -> PublicKey Ed25519
data PrivateKey a
[PrivateKeyEd25519] :: SecretKey -> PrivateKey Ed25519
data KeyType typ
[KeyTypeEd25519] :: KeyType Ed25519
somePublicKey :: Some Key -> Some PublicKey
somePublicKeyType :: Some PublicKey -> Some KeyType
someKeyId :: HasKeyId key => Some key -> KeyId
publicKey :: Key a -> PublicKey a
privateKey :: Key a -> PrivateKey a
createKey :: KeyType key -> IO (Key key)
createKey' :: KeyType key -> IO (Some Key)

-- | The key ID of a key, by definition, is the hexdigest of the SHA-256
--   hash of the canonical JSON form of the key where the private object
--   key is excluded.
--   
--   NOTE: The FromJSON and ToJSON instances for KeyId are ntentially
--   omitted. Use writeKeyAsId instead.
newtype KeyId
KeyId :: String -> KeyId
[keyIdString] :: KeyId -> String

-- | Compute the key ID of a key
class HasKeyId key
keyId :: HasKeyId key => key typ -> KeyId

-- | Sign a bytestring and return the signature
--   
--   TODO: It is unfortunate that we have to convert to a strict bytestring
--   for ed25519
sign :: PrivateKey typ -> ByteString -> ByteString
verify :: PublicKey typ -> ByteString -> ByteString -> Bool

-- | File length
--   
--   Having verified file length information means we can protect against
--   endless data attacks and similar.
newtype FileLength
FileLength :: Int54 -> FileLength
[fileLength] :: FileLength -> Int54

-- | File hash
newtype Hash
Hash :: String -> Hash

-- | Key threshold
--   
--   The key threshold is the minimum number of keys a document must be
--   signed with. Key thresholds are specified in <tt>RoleSpec</tt> or
--   <tt>DelegationsSpec</tt>.
newtype KeyThreshold
KeyThreshold :: Int54 -> KeyThreshold

-- | File information
--   
--   This intentionally does not have an <a>Eq</a> instance; see
--   <a>knownFileInfoEqual</a> and <tt>verifyFileInfo</tt> instead.
--   
--   NOTE: Throughout we compute file information always over the raw
--   bytes. For example, when <tt>timestamp.json</tt> lists the hash of
--   <tt>snapshot.json</tt>, this hash is computed over the actual
--   <tt>snapshot.json</tt> file (as opposed to the canonical form of the
--   embedded JSON). This brings it in line with the hash computed over
--   target files, where that is the only choice available.
data FileInfo
FileInfo :: FileLength -> Map HashFn Hash -> FileInfo
[fileInfoLength] :: FileInfo -> FileLength
[fileInfoHashes] :: FileInfo -> Map HashFn Hash
data HashFn
HashFnSHA256 :: HashFn
HashFnMD5 :: HashFn

-- | File hash
newtype Hash
Hash :: String -> Hash

-- | Compute <a>FileInfo</a>
--   
--   TODO: Currently this will load the entire input bytestring into
--   memory. We need to make this incremental, by computing the length and
--   all hashes in a single traversal over the input.
fileInfo :: ByteString -> FileInfo

-- | Compute <a>FileInfo</a>
computeFileInfo :: FsRoot root => Path root -> IO FileInfo

-- | Compare the expected trusted file info against the actual file info of
--   a target file.
--   
--   This should be used only when the <a>FileInfo</a> is already known. If
--   we want to compare known <a>FileInfo</a> against a file on disk we
--   should delay until we have confirmed that the file lengths match (see
--   <tt>downloadedVerify</tt>).
compareTrustedFileInfo :: FileInfo -> FileInfo -> Bool
knownFileInfoEqual :: FileInfo -> FileInfo -> Bool

-- | Extract SHA256 hash from <a>FileInfo</a> (if present)
fileInfoSHA256 :: FileInfo -> Maybe Hash

-- | 54-bit integer values
--   
--   JavaScript can only safely represent numbers between <tt>-(2^53 -
--   1)</tt> and <tt>2^53 - 1</tt>.
--   
--   TODO: Although we introduce the type here, we don't actually do any
--   bounds checking and just inherit all type class instance from Int64.
--   We should probably define <a>fromInteger</a> to do bounds checking,
--   give different instances for type classes such as <a>Bounded</a> and
--   <a>FiniteBits</a>, etc.
data Int54
class HasHeader a

-- | File expiry date
fileExpires :: HasHeader a => Lens' a FileExpires

-- | File version (monotonically increasing counter)
fileVersion :: HasHeader a => Lens' a FileVersion

-- | File version
--   
--   The file version is a flat integer which must monotonically increase
--   on every file update.
--   
--   <a>Show</a> and <a>Read</a> instance are defined in terms of the
--   underlying <a>Int</a> (this is use for example by hackage during the
--   backup process).
newtype FileVersion
FileVersion :: Int54 -> FileVersion

-- | File expiry date
--   
--   A <a>Nothing</a> value here means no expiry. That makes it possible to
--   set some files to never expire. (Note that not having the Maybe in the
--   type here still allows that, because you could set an expiry date 2000
--   years into the future. By having the Maybe here we avoid the _need_
--   for such encoding issues.)
newtype FileExpires
FileExpires :: (Maybe UTCTime) -> FileExpires

-- | Occassionally it is useful to read only a header from a file.
--   
--   <tt>HeaderOnly</tt> intentionally only has a <a>FromJSON</a> instance
--   (no <a>ToJSON</a>).
data Header
Header :: FileExpires -> FileVersion -> Header
[headerExpires] :: Header -> FileExpires
[headerVersion] :: Header -> FileVersion
expiresInDays :: UTCTime -> Integer -> FileExpires
expiresNever :: FileExpires
isExpired :: UTCTime -> FileExpires -> Bool
versionInitial :: FileVersion
versionIncrement :: FileVersion -> FileVersion

-- | Location of the various files we cache
--   
--   Although the generic TUF algorithms do not care how we organize the
--   cache, we nonetheless specity this here because as long as there are
--   tools which access files in the cache directly we need to define the
--   cache layout. See also comments for <tt>defaultCacheLayout</tt>.
data CacheLayout
CacheLayout :: CachePath -> CachePath -> CachePath -> CachePath -> CachePath -> CachePath -> CachePath -> CacheLayout

-- | TUF root metadata
[cacheLayoutRoot] :: CacheLayout -> CachePath

-- | TUF timestamp
[cacheLayoutTimestamp] :: CacheLayout -> CachePath

-- | TUF snapshot
[cacheLayoutSnapshot] :: CacheLayout -> CachePath

-- | TUF mirrors list
[cacheLayoutMirrors] :: CacheLayout -> CachePath

-- | Uncompressed index tarball
[cacheLayoutIndexTar] :: CacheLayout -> CachePath

-- | Index to the uncompressed index tarball
[cacheLayoutIndexIdx] :: CacheLayout -> CachePath

-- | Compressed index tarball
--   
--   We cache both the compressed and the uncompressed tarballs, because
--   incremental updates happen through the compressed tarball, but reads
--   happen through the uncompressed one (with the help of the tarball
--   index).
[cacheLayoutIndexTarGz] :: CacheLayout -> CachePath

-- | The cache layout cabal-install uses
--   
--   We cache the index as <tt><a>cache</a>/00-index.tar</tt>; this is
--   important because `cabal-install` expects to find it there (and does
--   not currently go through the hackage-security library to get files
--   from the index).
cabalCacheLayout :: CacheLayout

-- | Layout of the files within the index tarball
data IndexLayout
IndexLayout :: (forall dec. IndexFile dec -> IndexPath) -> (IndexPath -> Maybe (Some IndexFile)) -> IndexLayout

-- | Translate an <a>IndexFile</a> to a path
[indexFileToPath] :: IndexLayout -> forall dec. IndexFile dec -> IndexPath

-- | Parse an <a>FilePath</a>
[indexFileFromPath] :: IndexLayout -> IndexPath -> Maybe (Some IndexFile)

-- | Files that we might request from the index
--   
--   The type index tells us the type of the decoded file, if any. For
--   files for which the library does not support decoding this will be
--   <tt>()</tt>. NOTE: Clients should NOT rely on this type index being
--   <tt>()</tt>, or they might break if we add support for parsing
--   additional file formats in the future.
--   
--   TODO: If we wanted to support legacy Hackage, we should also have a
--   case for the global preferred-versions file. But supporting legacy
--   Hackage will probably require more work anyway..
data IndexFile :: * -> *
[IndexPkgMetadata] :: PackageIdentifier -> IndexFile (Signed Targets)
[IndexPkgCabal] :: PackageIdentifier -> IndexFile ()
[IndexPkgPrefs] :: PackageName -> IndexFile ()

-- | The layout of the index as maintained on Hackage
hackageIndexLayout :: IndexLayout
indexLayoutPkgMetadata :: IndexLayout -> PackageIdentifier -> IndexPath
indexLayoutPkgCabal :: IndexLayout -> PackageIdentifier -> IndexPath
indexLayoutPkgPrefs :: IndexLayout -> PackageName -> IndexPath

-- | Layout of a repository
data RepoLayout
RepoLayout :: RepoPath -> RepoPath -> RepoPath -> RepoPath -> RepoPath -> RepoPath -> (PackageIdentifier -> RepoPath) -> RepoLayout

-- | TUF root metadata
[repoLayoutRoot] :: RepoLayout -> RepoPath

-- | TUF timestamp
[repoLayoutTimestamp] :: RepoLayout -> RepoPath

-- | TUF snapshot
[repoLayoutSnapshot] :: RepoLayout -> RepoPath

-- | TUF mirrors list
[repoLayoutMirrors] :: RepoLayout -> RepoPath

-- | Compressed index tarball
[repoLayoutIndexTarGz] :: RepoLayout -> RepoPath

-- | Uncompressed index tarball
[repoLayoutIndexTar] :: RepoLayout -> RepoPath

-- | Path to the package tarball
[repoLayoutPkgTarGz] :: RepoLayout -> PackageIdentifier -> RepoPath

-- | The layout used on Hackage
hackageRepoLayout :: RepoLayout

-- | Layout used by cabal for ("legacy") local repos
--   
--   Obviously, such repos do not normally contain any of the TUF files, so
--   their location is more or less arbitrary here.
cabalLocalRepoLayout :: RepoLayout
data Mirrors
Mirrors :: FileVersion -> FileExpires -> [Mirror] -> Mirrors
[mirrorsVersion] :: Mirrors -> FileVersion
[mirrorsExpires] :: Mirrors -> FileExpires
[mirrorsMirrors] :: Mirrors -> [Mirror]

-- | Definition of a mirror
--   
--   NOTE: Unlike the TUF specification, we require that all mirrors must
--   have the same format. That is, we omit <tt>metapath</tt> and
--   <tt>targetspath</tt>.
data Mirror
Mirror :: URI -> MirrorContent -> Mirror
[mirrorUrlBase] :: Mirror -> URI
[mirrorContent] :: Mirror -> MirrorContent

-- | Full versus partial mirrors
--   
--   The TUF spec explicitly allows for partial mirrors, with the mirrors
--   file specifying (through patterns) what is available from partial
--   mirrors.
--   
--   For now we only support full mirrors; if we wanted to add partial
--   mirrors, we would add a second <tt>MirrorPartial</tt> constructor here
--   with arguments corresponding to TUF's <tt>metacontent</tt> and
--   <tt>targetscontent</tt> fields.
data MirrorContent
MirrorFull :: MirrorContent
type MirrorDescription = String

-- | Give a human-readable description of a particular mirror
--   
--   (for use in error messages)
describeMirror :: Mirror -> MirrorDescription

-- | The root of the repository
--   
--   Repository roots can be anchored at a remote URL or a local directory.
--   
--   Note that even for remote repos <a>RepoRoot</a> is (potentially)
--   different from <a>Web</a> -- for a repository located at, say,
--   <tt><a>http://hackage.haskell.org</a></tt> they happen to coincide,
--   but for one location at
--   <tt><a>http://example.com/some/subdirectory</a></tt> they do not.
data RepoRoot

-- | Paths relative to the root of the repository
type RepoPath = Path RepoRoot
anchorRepoPathLocally :: Path root -> RepoPath -> Path root
anchorRepoPathRemotely :: Path Web -> RepoPath -> Path Web

-- | The root of the index tarball
data IndexRoot

-- | Paths relative to the root of the index tarball
type IndexPath = Path IndexRoot

-- | The cache directory
data CacheRoot
type CachePath = Path CacheRoot

-- | Anchor a cache path to the location of the cache
anchorCachePath :: Path root -> CachePath -> Path root

-- | The root metadata
--   
--   NOTE: We must have the invariant that ALL keys (apart from delegation
--   keys) must be listed in <a>rootKeys</a>. (Delegation keys satisfy a
--   similar invariant, see Targets.)
data Root
Root :: FileVersion -> FileExpires -> KeyEnv -> RootRoles -> Root
[rootVersion] :: Root -> FileVersion
[rootExpires] :: Root -> FileExpires
[rootKeys] :: Root -> KeyEnv
[rootRoles] :: Root -> RootRoles
data RootRoles
RootRoles :: RoleSpec Root -> RoleSpec Snapshot -> RoleSpec Targets -> RoleSpec Timestamp -> RoleSpec Mirrors -> RootRoles
[rootRolesRoot] :: RootRoles -> RoleSpec Root
[rootRolesSnapshot] :: RootRoles -> RoleSpec Snapshot
[rootRolesTargets] :: RootRoles -> RoleSpec Targets
[rootRolesTimestamp] :: RootRoles -> RoleSpec Timestamp
[rootRolesMirrors] :: RootRoles -> RoleSpec Mirrors

-- | Role specification
--   
--   The phantom type indicates what kind of type this role is meant to
--   verify.
data RoleSpec a
RoleSpec :: [Some PublicKey] -> KeyThreshold -> RoleSpec a
[roleSpecKeys] :: RoleSpec a -> [Some PublicKey]
[roleSpecThreshold] :: RoleSpec a -> KeyThreshold
data Signed a
Signed :: a -> Signatures -> Signed a
[signed] :: Signed a -> a
[signatures] :: Signed a -> Signatures

-- | A list of signatures
--   
--   Invariant: each signature must be made with a different key. We
--   enforce this invariant for incoming untrusted data
--   (<a>fromPreSignatures</a>) but not for lists of signatures that we
--   create in code.
newtype Signatures
Signatures :: [Signature] -> Signatures
data Signature
Signature :: ByteString -> Some PublicKey -> Signature
[signature] :: Signature -> ByteString
[signatureKey] :: Signature -> Some PublicKey

-- | Create a new document without any signatures
unsigned :: a -> Signed a

-- | Sign a document
withSignatures :: ToJSON WriteJSON a => RepoLayout -> [Some Key] -> a -> Signed a

-- | Variation on <a>withSignatures</a> that doesn't need the repo layout
withSignatures' :: ToJSON Identity a => [Some Key] -> a -> Signed a

-- | Construct signatures for already rendered value
signRendered :: [Some Key] -> ByteString -> Signatures
verifySignature :: ByteString -> Signature -> Bool

-- | General FromJSON instance for signed datatypes
--   
--   We don't give a general FromJSON instance for Signed because for some
--   datatypes we need to do something special (datatypes where we need to
--   read key environments); for instance, see the "Signed Root" instance.
signedFromJSON :: (MonadKeys m, FromJSON m a) => JSValue -> m (Signed a)

-- | Signature verification
--   
--   NOTES: 1. By definition, the signature must be verified against the
--   canonical JSON format. This means we _must_ parse and then pretty
--   print (as we do here) because the document as stored may or may not be
--   in canonical format. 2. However, it is important that we NOT translate
--   from the JSValue to whatever internal datatype we are using and then
--   back to JSValue, because that may not roundtrip: we must allow for
--   additional fields in the JSValue that we ignore (and would therefore
--   lose when we attempt to roundtrip). 3. We verify that all signatures
--   are valid, but we cannot verify (here) that these signatures are
--   signed with the right key, or that we have a sufficient number of
--   signatures. This will be the responsibility of the calling code.
verifySignatures :: JSValue -> Signatures -> Bool

-- | File with uninterpreted signatures
--   
--   Sometimes we want to be able to read a file without interpreting the
--   signatures (that is, resolving the key IDs) or doing any kind of
--   checks on them. One advantage of this is that this allows us to read
--   many file types without any key environment at all, which is sometimes
--   useful.
data UninterpretedSignatures a
UninterpretedSignatures :: a -> [PreSignature] -> UninterpretedSignatures a
[uninterpretedSigned] :: UninterpretedSignatures a -> a
[uninterpretedSignatures] :: UninterpretedSignatures a -> [PreSignature]

-- | A signature with a key ID (rather than an actual key)
--   
--   This corresponds precisely to the TUF representation of a signature.
data PreSignature
PreSignature :: ByteString -> Some KeyType -> KeyId -> PreSignature
[presignature] :: PreSignature -> ByteString
[presigMethod] :: PreSignature -> Some KeyType
[presigKeyId] :: PreSignature -> KeyId

-- | Convert a pre-signature to a signature
--   
--   Verifies that the key type matches the advertised method.
fromPreSignature :: MonadKeys m => PreSignature -> m Signature

-- | Convert a list of <a>PreSignature</a>s to a list of <a>Signature</a>s
--   
--   This verifies the invariant that all signatures are made with
--   different keys. We do this on the presignatures rather than the
--   signatures so that we can do the check on key IDs, rather than keys
--   (the latter don't have an Ord instance).
fromPreSignatures :: MonadKeys m => [PreSignature] -> m Signatures

-- | Convert signature to pre-signature
toPreSignature :: Signature -> PreSignature

-- | Convert list of pre-signatures to a list of signatures
toPreSignatures :: Signatures -> [PreSignature]
data Snapshot
Snapshot :: FileVersion -> FileExpires -> FileInfo -> FileInfo -> FileInfo -> Maybe FileInfo -> Snapshot
[snapshotVersion] :: Snapshot -> FileVersion
[snapshotExpires] :: Snapshot -> FileExpires

-- | File info for the root metadata
--   
--   We list this explicitly in the snapshot so that we can check if we
--   need to update the root metadata without first having to download the
--   entire index tarball.
[snapshotInfoRoot] :: Snapshot -> FileInfo

-- | File info for the mirror metadata
[snapshotInfoMirrors] :: Snapshot -> FileInfo

-- | Compressed index tarball
[snapshotInfoTarGz] :: Snapshot -> FileInfo

-- | Uncompressed index tarball
--   
--   Repositories are not required to provide this.
[snapshotInfoTar] :: Snapshot -> Maybe FileInfo

-- | Target metadata
--   
--   Most target files do not need expiry dates because they are not
--   subject to change (and hence attacks like freeze attacks are not a
--   concern).
data Targets
Targets :: FileVersion -> FileExpires -> FileMap -> Maybe Delegations -> Targets
[targetsVersion] :: Targets -> FileVersion
[targetsExpires] :: Targets -> FileExpires
[targetsTargets] :: Targets -> FileMap
[targetsDelegations] :: Targets -> Maybe Delegations

-- | Delegations
--   
--   Much like the Root datatype, this must have an invariant that ALL used
--   keys (apart from the global keys, which are in the root key
--   environment) must be listed in <a>delegationsKeys</a>.
data Delegations
Delegations :: KeyEnv -> [DelegationSpec] -> Delegations
[delegationsKeys] :: Delegations -> KeyEnv
[delegationsRoles] :: Delegations -> [DelegationSpec]

-- | Delegation specification
--   
--   NOTE: This is a close analogue of <tt>RoleSpec</tt>.
data DelegationSpec
DelegationSpec :: [Some PublicKey] -> KeyThreshold -> Delegation -> DelegationSpec
[delegationSpecKeys] :: DelegationSpec -> [Some PublicKey]
[delegationSpecThreshold] :: DelegationSpec -> KeyThreshold
[delegation] :: DelegationSpec -> Delegation

-- | A delegation
--   
--   A delegation is a pair of a pattern and a replacement.
--   
--   See <tt>match</tt> for an example.
data Delegation
Delegation :: (Pattern a) -> (Replacement a) -> Delegation
targetsLookup :: TargetPath -> Targets -> Maybe FileInfo
data Timestamp
Timestamp :: FileVersion -> FileExpires -> FileInfo -> Timestamp
[timestampVersion] :: Timestamp -> FileVersion
[timestampExpires] :: Timestamp -> FileExpires
[timestampInfoSnapshot] :: Timestamp -> FileInfo
