forked from commercialhaskell/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSig.hs
More file actions
101 lines (83 loc) · 2.94 KB
/
Sig.hs
File metadata and controls
101 lines (83 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-|
Module : Stack.Types.Sig
Description : Signature Types
Copyright : (c) FPComplete.com, 2015
License : BSD3
Maintainer : Tim Dysinger <tim@fpcomplete.com>
Stability : experimental
Portability : POSIX
-}
module Stack.Types.Sig
(Signature(..), Fingerprint, mkFingerprint, SigException(..)) where
import Prelude ()
import Prelude.Compat
import Control.Exception (Exception)
import Data.Aeson (Value(..), ToJSON(..), FromJSON(..))
import Data.ByteString (ByteString)
import qualified Data.ByteString as SB
import Data.Char (isHexDigit)
import Data.Monoid ((<>))
import Data.String (IsString(..))
import Data.Text (Text)
import qualified Data.Text as T
import Data.Typeable (Typeable)
import Stack.Types.PackageName
-- | A GPG signature.
newtype Signature =
Signature ByteString
deriving (Ord,Eq)
instance Show Signature where
show (Signature s) = "Signature " ++
(if SB.length s > 140
then show (SB.take 140 s) ++
"..."
else show (SB.take 140 s))
-- | The GPG fingerprint.
newtype Fingerprint =
Fingerprint Text
deriving (Eq,Ord)
mkFingerprint :: Text -> Fingerprint
mkFingerprint = Fingerprint . hexText
hexText :: Text -> Text
hexText = T.toUpper . T.dropWhile (not . isHexDigit)
instance Show Fingerprint where
show (Fingerprint hex) = T.unpack (hexText hex)
instance FromJSON Fingerprint where
parseJSON j = Fingerprint . hexText <$> parseJSON j
instance ToJSON Fingerprint where
toJSON (Fingerprint hex) = String (hexText hex)
instance IsString Fingerprint where
fromString = Fingerprint . hexText . T.pack
instance FromJSON (Aeson PackageName) where
parseJSON j = do
s <- parseJSON j
case parsePackageName s of
Just name -> return (Aeson name)
Nothing -> fail ("Invalid package name: " <> T.unpack s)
-- | Handy wrapper for orphan instances.
newtype Aeson a = Aeson
{ _unAeson :: a
} deriving (Ord,Eq)
-- | Exceptions
data SigException
= GPGFingerprintException String
| GPGNotFoundException
| GPGSignException String
| GPGVerifyException String
| SigInvalidSDistTarBall
| SigNoProjectRootException
| SigServiceException String
deriving (Typeable)
instance Exception SigException
instance Show SigException where
show (GPGFingerprintException e) =
"Error extracting a GPG fingerprint " <> e
show GPGNotFoundException = "Unable to find gpg2 or gpg executable"
show (GPGSignException e) = "Error signing with GPG " <> e
show (GPGVerifyException e) = "Error verifying with GPG " <> e
show SigNoProjectRootException = "Missing Project Root"
show SigInvalidSDistTarBall = "Invalid sdist tarball"
show (SigServiceException e) = "Error with the Signature Service " <> e