2019-01-21 16:54:57 +01:00
|
|
|
{- This file is part of Vervis.
|
|
|
|
-
|
|
|
|
- Written in 2019 by fr33domlover <fr33domlover@riseup.net>.
|
|
|
|
-
|
|
|
|
- ♡ Copying is an act of love. Please copy, reuse and share.
|
|
|
|
-
|
|
|
|
- The author(s) have dedicated all copyright and related and neighboring
|
|
|
|
- rights to this software to the public domain worldwide. This software is
|
|
|
|
- distributed without any warranty.
|
|
|
|
-
|
|
|
|
- You should have received a copy of the CC0 Public Domain Dedication along
|
|
|
|
- with this software. If not, see
|
|
|
|
- <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Web.ActivityPub
|
2019-02-22 00:59:53 +01:00
|
|
|
( -- * Type-safe manipulation tools
|
|
|
|
--
|
|
|
|
-- Types and functions that make handling URIs and JSON-LD contexts less
|
|
|
|
-- error-prone and safer by recording safety checks in the type and
|
|
|
|
-- placing the checks in a single clear place.
|
|
|
|
ActivityPub (..)
|
|
|
|
, Doc (..)
|
|
|
|
|
|
|
|
-- * Actor
|
2019-01-21 16:54:57 +01:00
|
|
|
--
|
|
|
|
-- ActivityPub actor document including a public key, with a 'FromJSON'
|
|
|
|
-- instance for fetching and a 'ToJSON' instance for publishing.
|
2019-02-22 00:59:53 +01:00
|
|
|
, ActorType (..)
|
2019-01-21 16:54:57 +01:00
|
|
|
, Algorithm (..)
|
2019-02-22 00:59:53 +01:00
|
|
|
, Owner (..)
|
2019-01-21 16:54:57 +01:00
|
|
|
, PublicKey (..)
|
|
|
|
, Actor (..)
|
|
|
|
|
|
|
|
-- * Activity
|
2019-02-12 12:53:24 +01:00
|
|
|
, Note (..)
|
|
|
|
, Create (..)
|
2019-01-21 16:54:57 +01:00
|
|
|
, Activity (..)
|
|
|
|
|
|
|
|
-- * Utilities
|
2019-02-07 11:34:33 +01:00
|
|
|
, hActivityPubActor
|
2019-01-21 16:54:57 +01:00
|
|
|
, provideAP
|
|
|
|
, APGetError (..)
|
|
|
|
, httpGetAP
|
2019-03-05 09:26:41 +01:00
|
|
|
, APPostError (..)
|
2019-01-21 16:54:57 +01:00
|
|
|
, httpPostAP
|
2019-02-06 03:48:23 +01:00
|
|
|
, Fetched (..)
|
2019-02-22 00:59:53 +01:00
|
|
|
, fetchAPID
|
When verifying HTTP sig with known shared key, verify actor lists the key
Previously, when verifying an HTTP signature and we fetched the key and
discovered it's shared, we'd fetch the actor and make sure it lists the key URI
in the `publicKey` field. But if we already knew the key, had it cached in our
DB, we wouldn't check the actor at all, despite not knowing whether it lists
the key.
With this patch, we now always GET the actor when the key is shared,
determining the actor URI from the `ActivityPub-Actor` request header, and we
verify that the actor lists the key URI. We do that regardless of whether or
not we have the key in the DB, although these two cases and handled in
different parts of the code right now (for a new key, it's in Web.ActivityPub
fetchKey; for a known key, it's in Vervis.Foundation httpVerifySig).
2019-02-18 10:20:13 +01:00
|
|
|
, keyListedByActor
|
2019-02-23 18:17:52 +01:00
|
|
|
, fetchUnknownKey
|
|
|
|
, fetchKnownPersonalKey
|
|
|
|
, fetchKnownSharedKey
|
2019-01-21 16:54:57 +01:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
2019-02-04 00:39:56 +01:00
|
|
|
import Control.Applicative ((<|>), optional)
|
|
|
|
import Control.Exception (Exception, displayException, try)
|
2019-02-24 02:21:42 +01:00
|
|
|
import Control.Monad (when, unless, (<=<), join)
|
2019-01-21 16:54:57 +01:00
|
|
|
import Control.Monad.IO.Class
|
2019-02-04 00:39:56 +01:00
|
|
|
import Control.Monad.Trans.Except
|
2019-01-21 16:54:57 +01:00
|
|
|
import Control.Monad.Trans.Writer (Writer)
|
2019-02-04 00:39:56 +01:00
|
|
|
import Crypto.Error (CryptoFailable (..))
|
2019-01-21 16:54:57 +01:00
|
|
|
import Data.Aeson
|
2019-02-22 00:59:53 +01:00
|
|
|
import Data.Aeson.Encoding (pair)
|
|
|
|
import Data.Aeson.Types (Parser, typeMismatch, listEncoding)
|
|
|
|
import Data.Bifunctor
|
2019-02-04 11:07:25 +01:00
|
|
|
import Data.Bitraversable (bitraverse)
|
2019-01-21 16:54:57 +01:00
|
|
|
import Data.ByteString (ByteString)
|
2019-02-17 01:14:05 +01:00
|
|
|
import Data.Foldable (for_)
|
2019-02-24 02:21:42 +01:00
|
|
|
import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
|
2019-02-22 00:59:53 +01:00
|
|
|
import Data.Proxy
|
2019-01-21 16:54:57 +01:00
|
|
|
import Data.PEM
|
2019-02-24 02:21:42 +01:00
|
|
|
import Data.Semigroup (Endo, First (..))
|
2019-01-21 16:54:57 +01:00
|
|
|
import Data.Text (Text)
|
|
|
|
import Data.Text.Encoding (encodeUtf8, decodeUtf8)
|
2019-02-05 05:05:44 +01:00
|
|
|
import Data.Time.Clock (UTCTime)
|
2019-02-22 00:59:53 +01:00
|
|
|
import Network.HTTP.Client hiding (Proxy, proxy)
|
2019-01-21 16:54:57 +01:00
|
|
|
import Network.HTTP.Client.Conduit.ActivityPub (httpAPEither)
|
|
|
|
import Network.HTTP.Client.Signature (signRequest)
|
2019-03-05 09:26:41 +01:00
|
|
|
import Network.HTTP.Signature (KeyId, Signature, HttpSigGenError)
|
2019-01-21 16:54:57 +01:00
|
|
|
import Network.HTTP.Simple (JSONException)
|
|
|
|
import Network.HTTP.Types.Header (HeaderName, hContentType)
|
|
|
|
import Network.URI
|
|
|
|
import Yesod.Core.Content (ContentType)
|
|
|
|
import Yesod.Core.Handler (ProvidedRep, provideRepType)
|
|
|
|
|
2019-02-04 00:39:56 +01:00
|
|
|
import qualified Crypto.PubKey.Ed25519 as E (PublicKey, publicKey)
|
2019-01-21 16:54:57 +01:00
|
|
|
import qualified Data.HashMap.Strict as M (lookup)
|
2019-02-06 03:48:23 +01:00
|
|
|
import qualified Data.Text as T (pack, unpack)
|
Support remote actors specifying 2 keys, and DB storage of these keys
It's now possible for activities we be attributed to actors that have more than
one key. We allow up to 2 keys. We also store in the DB. Scaling to support any
number of keys is trivial, but I'm limiting to 2 to avoid potential trouble and
because 2 is the actual number we need.
By having 2 keys, and replacing only one of them in each rotation, we avoid
race conditions. With 1 key, the following can happen:
1. We send an activity to another server
2. We rotate our key
3. The server reaches the activity in its processing queue, tries to verify our
request signature, but fails because it can't fetch the key. It's the old
key and we discarded it already, replaced it with the new one
When we use 2 keys, the previous key remains available and other servers have
time to finish processing our requests signed with that key. We can safely
rotate, without worrying about whether the user sent anything right before the
rotation time.
Caveat: With this feature, we allow OTHER servers to rotate freely. It's safe
because it's optional, but it's just Vervis right now. Once Vervis itself
starts using 2 keys, it will be able to rotate freely without race condition
risk, but probably Mastodon etc. won't accept its signatures because of the use
of 2 keys and because they're server-scope keys.
Maybe I can get these features adopted by the fediverse?
2019-02-04 20:38:50 +01:00
|
|
|
import qualified Data.Vector as V (fromList, toList)
|
2019-01-21 16:54:57 +01:00
|
|
|
|
2019-02-08 00:08:28 +01:00
|
|
|
import Network.FedURI
|
|
|
|
|
2019-02-03 12:01:36 +01:00
|
|
|
import Data.Aeson.Local
|
2019-01-21 16:54:57 +01:00
|
|
|
|
2019-02-22 00:59:53 +01:00
|
|
|
proxy :: a -> Proxy a
|
|
|
|
proxy _ = Proxy
|
|
|
|
|
2019-01-21 16:54:57 +01:00
|
|
|
as2context :: Text
|
|
|
|
as2context = "https://www.w3.org/ns/activitystreams"
|
|
|
|
|
2019-02-22 00:59:53 +01:00
|
|
|
secContext :: Text
|
|
|
|
secContext = "https://w3id.org/security/v1"
|
|
|
|
|
|
|
|
actorContext :: [Text]
|
|
|
|
actorContext = [as2context, secContext]
|
|
|
|
|
|
|
|
data Context = ContextAS2 | ContextPKey | ContextActor deriving Eq
|
|
|
|
|
|
|
|
instance FromJSON Context where
|
|
|
|
parseJSON (String t)
|
|
|
|
| t == as2context = return ContextAS2
|
|
|
|
| t == secContext = return ContextPKey
|
|
|
|
parseJSON (Array v)
|
|
|
|
| V.toList v == map String actorContext = return ContextActor
|
|
|
|
parseJSON _ = fail "Unrecognized @context"
|
|
|
|
|
|
|
|
instance ToJSON Context where
|
|
|
|
toJSON = error "toJSON Context"
|
|
|
|
toEncoding ContextAS2 = toEncoding as2context
|
|
|
|
toEncoding ContextPKey = toEncoding secContext
|
|
|
|
toEncoding ContextActor = toEncoding actorContext
|
|
|
|
|
|
|
|
class ActivityPub a where
|
|
|
|
jsonldContext :: Proxy a -> Context
|
|
|
|
parseObject :: Object -> Parser (Text, a)
|
|
|
|
toSeries :: Text -> a -> Series
|
|
|
|
|
|
|
|
data Doc a = Doc
|
|
|
|
{ docHost :: Text
|
|
|
|
, docValue :: a
|
|
|
|
}
|
|
|
|
|
|
|
|
instance ActivityPub a => FromJSON (Doc a) where
|
|
|
|
parseJSON = withObject "Doc" $ \ o -> do
|
|
|
|
(h, v) <- parseObject o
|
|
|
|
ctx <- o .: "@context"
|
|
|
|
if ctx == jsonldContext (proxy v)
|
|
|
|
then return $ Doc h v
|
|
|
|
else fail "@context doesn't match"
|
|
|
|
|
|
|
|
instance ActivityPub a => ToJSON (Doc a) where
|
|
|
|
toJSON = error "toJSON Doc"
|
|
|
|
toEncoding (Doc h v) =
|
|
|
|
pairs
|
|
|
|
$ "@context" .= jsonldContext (proxy v)
|
|
|
|
<> toSeries h v
|
2019-01-21 16:54:57 +01:00
|
|
|
|
|
|
|
data ActorType = ActorTypePerson | ActorTypeOther Text
|
|
|
|
|
|
|
|
instance FromJSON ActorType where
|
|
|
|
parseJSON = withText "ActorType" $ \ t ->
|
|
|
|
pure $ case t of
|
|
|
|
"Person" -> ActorTypePerson
|
|
|
|
_ -> ActorTypeOther t
|
|
|
|
|
|
|
|
instance ToJSON ActorType where
|
|
|
|
toJSON = error "toJSON ActorType"
|
|
|
|
toEncoding at =
|
|
|
|
toEncoding $ case at of
|
|
|
|
ActorTypePerson -> "Person"
|
|
|
|
ActorTypeOther t -> t
|
|
|
|
|
|
|
|
data Algorithm = AlgorithmEd25519 | AlgorithmOther Text
|
|
|
|
|
|
|
|
instance FromJSON Algorithm where
|
|
|
|
parseJSON = withText "Algorithm" $ \ t ->
|
|
|
|
pure $ if t == frg <> "ed25519"
|
|
|
|
then AlgorithmEd25519
|
|
|
|
else AlgorithmOther t
|
|
|
|
|
|
|
|
instance ToJSON Algorithm where
|
|
|
|
toJSON = error "toJSON Algorithm"
|
|
|
|
toEncoding algo =
|
|
|
|
toEncoding $ case algo of
|
|
|
|
AlgorithmEd25519 -> frg <> "ed25519"
|
|
|
|
AlgorithmOther t -> t
|
|
|
|
|
2019-02-22 00:59:53 +01:00
|
|
|
data Owner = OwnerInstance | OwnerActor LocalURI
|
|
|
|
|
|
|
|
ownerShared :: Owner -> Bool
|
|
|
|
ownerShared OwnerInstance = True
|
|
|
|
ownerShared (OwnerActor _) = False
|
|
|
|
|
2019-01-21 16:54:57 +01:00
|
|
|
data PublicKey = PublicKey
|
2019-02-22 00:59:53 +01:00
|
|
|
{ publicKeyId :: LocalURI
|
2019-02-05 05:05:44 +01:00
|
|
|
, publicKeyExpires :: Maybe UTCTime
|
2019-02-22 00:59:53 +01:00
|
|
|
, publicKeyOwner :: Owner
|
2019-02-05 05:05:44 +01:00
|
|
|
, publicKeyPem :: PEM
|
|
|
|
, publicKeyAlgo :: Maybe Algorithm
|
2019-01-21 16:54:57 +01:00
|
|
|
}
|
|
|
|
|
2019-02-22 00:59:53 +01:00
|
|
|
instance ActivityPub PublicKey where
|
|
|
|
jsonldContext _ = ContextPKey
|
|
|
|
parseObject o = do
|
2019-02-04 00:39:56 +01:00
|
|
|
mtyp <- optional $ o .: "@type" <|> o .: "type"
|
2019-02-22 00:59:53 +01:00
|
|
|
for_ mtyp $ \ t ->
|
|
|
|
when (t /= ("Key" :: Text)) $
|
|
|
|
fail "PublicKey @type isn't Key"
|
|
|
|
(host, id_) <- f2l <$> (o .: "@id" <|> o .: "id")
|
|
|
|
shared <- o .: (frg <> "isShared") .!= False
|
|
|
|
fmap (host,) $
|
|
|
|
PublicKey id_
|
|
|
|
<$> o .:? "expires"
|
|
|
|
<*> (mkOwner shared =<< withHost host o "owner")
|
|
|
|
<*> (parsePEM =<< o .: "publicKeyPem")
|
|
|
|
<*> o .:? (frg <> "algorithm")
|
2019-01-21 16:54:57 +01:00
|
|
|
where
|
2019-02-22 00:59:53 +01:00
|
|
|
withHost h o t = do
|
|
|
|
(h', lu) <- f2l <$> o .: t
|
|
|
|
if h == h'
|
|
|
|
then return lu
|
|
|
|
else fail "URI host mismatch"
|
|
|
|
mkOwner True (LocalURI "" "") = return OwnerInstance
|
|
|
|
mkOwner True _ = fail "Shared key but owner isn't instance URI"
|
|
|
|
mkOwner False lu = return $ OwnerActor lu
|
2019-01-21 16:54:57 +01:00
|
|
|
parsePEM t =
|
|
|
|
case pemParseBS $ encodeUtf8 t of
|
|
|
|
Left e -> fail $ "PEM parsing failed: " ++ e
|
|
|
|
Right xs ->
|
|
|
|
case xs of
|
|
|
|
[] -> fail "Empty PEM"
|
|
|
|
[x] -> pure x
|
|
|
|
_ -> fail "Multiple PEM sections"
|
2019-02-22 00:59:53 +01:00
|
|
|
toSeries host (PublicKey id_ mexpires owner pem malgo)
|
|
|
|
= "@id" .= l2f host id_
|
|
|
|
<> "expires" .=? mexpires
|
|
|
|
<> "owner" .= mkOwner host owner
|
|
|
|
<> "publicKeyPem" .= decodeUtf8 (pemWriteBS pem)
|
|
|
|
<> (frg <> "algorithm") .=? malgo
|
|
|
|
<> (frg <> "isShared") .= ownerShared owner
|
|
|
|
where
|
|
|
|
mkOwner h OwnerInstance = FedURI h "" ""
|
|
|
|
mkOwner h (OwnerActor lu) = l2f h lu
|
2019-01-21 16:54:57 +01:00
|
|
|
|
2019-02-24 02:21:42 +01:00
|
|
|
parsePublicKeySet :: Value -> Parser (Text, NonEmpty (Either LocalURI PublicKey))
|
2019-02-22 00:59:53 +01:00
|
|
|
parsePublicKeySet v =
|
|
|
|
case v of
|
|
|
|
Array a ->
|
2019-02-24 02:21:42 +01:00
|
|
|
case nonEmpty $ V.toList a of
|
|
|
|
Nothing -> fail "No public keys"
|
|
|
|
Just (k :| ks) -> do
|
|
|
|
(h, e) <- parseKey k
|
|
|
|
es <- traverse (withHost h . parseKey) ks
|
|
|
|
return (h, e :| es)
|
|
|
|
_ -> second (:| []) <$> parseKey v
|
2019-02-22 00:59:53 +01:00
|
|
|
where
|
|
|
|
parseKey (String t) = second Left . f2l <$> either fail return (parseFedURI t)
|
|
|
|
parseKey (Object o) = second Right <$> parseObject o
|
|
|
|
parseKey v = typeMismatch "PublicKeySet Item" v
|
|
|
|
withHost h a = do
|
|
|
|
(h', v) <- a
|
|
|
|
if h == h'
|
|
|
|
then return v
|
|
|
|
else fail "URI host mismatch"
|
|
|
|
|
2019-02-24 02:21:42 +01:00
|
|
|
encodePublicKeySet :: Text -> NonEmpty (Either LocalURI PublicKey) -> Encoding
|
|
|
|
encodePublicKeySet host (e :| es) =
|
|
|
|
if null es
|
|
|
|
then renderKey e
|
|
|
|
else listEncoding renderKey $ e : es
|
2019-02-22 00:59:53 +01:00
|
|
|
where
|
|
|
|
renderKey (Left lu) = toEncoding $ l2f host lu
|
|
|
|
renderKey (Right pk) = pairs $ toSeries host pk
|
Support remote actors specifying 2 keys, and DB storage of these keys
It's now possible for activities we be attributed to actors that have more than
one key. We allow up to 2 keys. We also store in the DB. Scaling to support any
number of keys is trivial, but I'm limiting to 2 to avoid potential trouble and
because 2 is the actual number we need.
By having 2 keys, and replacing only one of them in each rotation, we avoid
race conditions. With 1 key, the following can happen:
1. We send an activity to another server
2. We rotate our key
3. The server reaches the activity in its processing queue, tries to verify our
request signature, but fails because it can't fetch the key. It's the old
key and we discarded it already, replaced it with the new one
When we use 2 keys, the previous key remains available and other servers have
time to finish processing our requests signed with that key. We can safely
rotate, without worrying about whether the user sent anything right before the
rotation time.
Caveat: With this feature, we allow OTHER servers to rotate freely. It's safe
because it's optional, but it's just Vervis right now. Once Vervis itself
starts using 2 keys, it will be able to rotate freely without race condition
risk, but probably Mastodon etc. won't accept its signatures because of the use
of 2 keys and because they're server-scope keys.
Maybe I can get these features adopted by the fediverse?
2019-02-04 20:38:50 +01:00
|
|
|
|
2019-01-21 16:54:57 +01:00
|
|
|
data Actor = Actor
|
2019-02-22 00:59:53 +01:00
|
|
|
{ actorId :: LocalURI
|
Support remote actors specifying 2 keys, and DB storage of these keys
It's now possible for activities we be attributed to actors that have more than
one key. We allow up to 2 keys. We also store in the DB. Scaling to support any
number of keys is trivial, but I'm limiting to 2 to avoid potential trouble and
because 2 is the actual number we need.
By having 2 keys, and replacing only one of them in each rotation, we avoid
race conditions. With 1 key, the following can happen:
1. We send an activity to another server
2. We rotate our key
3. The server reaches the activity in its processing queue, tries to verify our
request signature, but fails because it can't fetch the key. It's the old
key and we discarded it already, replaced it with the new one
When we use 2 keys, the previous key remains available and other servers have
time to finish processing our requests signed with that key. We can safely
rotate, without worrying about whether the user sent anything right before the
rotation time.
Caveat: With this feature, we allow OTHER servers to rotate freely. It's safe
because it's optional, but it's just Vervis right now. Once Vervis itself
starts using 2 keys, it will be able to rotate freely without race condition
risk, but probably Mastodon etc. won't accept its signatures because of the use
of 2 keys and because they're server-scope keys.
Maybe I can get these features adopted by the fediverse?
2019-02-04 20:38:50 +01:00
|
|
|
, actorType :: ActorType
|
|
|
|
, actorUsername :: Text
|
2019-02-22 00:59:53 +01:00
|
|
|
, actorInbox :: LocalURI
|
2019-02-24 02:21:42 +01:00
|
|
|
, actorPublicKeys :: NonEmpty (Either LocalURI PublicKey)
|
2019-01-21 16:54:57 +01:00
|
|
|
}
|
|
|
|
|
2019-02-22 00:59:53 +01:00
|
|
|
instance ActivityPub Actor where
|
|
|
|
jsonldContext _ = ContextActor
|
|
|
|
parseObject o = do
|
|
|
|
(host, id_) <- f2l <$> o .: "id"
|
|
|
|
fmap (host,) $
|
|
|
|
Actor id_
|
|
|
|
<$> o .: "type"
|
|
|
|
<*> o .: "preferredUsername"
|
|
|
|
<*> withHost host (f2l <$> o .: "inbox")
|
|
|
|
<*> withHost host (parsePublicKeySet =<< o .: "publicKey")
|
|
|
|
where
|
|
|
|
withHost h a = do
|
|
|
|
(h', v) <- a
|
|
|
|
if h == h'
|
|
|
|
then return v
|
|
|
|
else fail "URI host mismatch"
|
|
|
|
toSeries host (Actor id_ typ username inbox pkeys)
|
|
|
|
= "id" .= l2f host id_
|
|
|
|
<> "type" .= typ
|
|
|
|
<> "preferredUsername" .= username
|
|
|
|
<> "inbox" .= l2f host inbox
|
|
|
|
<> "publicKey" `pair` encodePublicKeySet host pkeys
|
2019-03-10 07:42:03 +01:00
|
|
|
|
2019-02-12 12:53:24 +01:00
|
|
|
data Note = Note
|
2019-03-10 07:42:03 +01:00
|
|
|
{ noteId :: LocalURI
|
|
|
|
--, noteAttrib :: LocalURI
|
|
|
|
--, noteTo :: FedURI
|
2019-02-12 12:53:24 +01:00
|
|
|
, noteReplyTo :: Maybe FedURI
|
|
|
|
, noteContent :: Text
|
2019-01-21 16:54:57 +01:00
|
|
|
}
|
|
|
|
|
2019-03-10 07:42:03 +01:00
|
|
|
parseNote :: Value -> Parser (Text, (Note, LocalURI, FedURI))
|
|
|
|
parseNote = withObject "Note" $ \ o -> do
|
|
|
|
typ <- o .: "type"
|
|
|
|
unless (typ == ("Note" :: Text)) $ fail "type isn't Note"
|
|
|
|
(h, id_) <- f2l <$> o .: "id"
|
|
|
|
fmap (h,) $
|
|
|
|
(,,) <$> (Note id_
|
|
|
|
<$> o .:? "inReplyTo"
|
|
|
|
<*> o .: "content"
|
|
|
|
)
|
|
|
|
<*> withHost h (f2l <$> o .: "attributedTo")
|
|
|
|
<*> o .: "to"
|
|
|
|
where
|
|
|
|
withHost h a = do
|
|
|
|
(h', v) <- a
|
|
|
|
if h == h'
|
|
|
|
then return v
|
|
|
|
else fail "URI host mismatch"
|
|
|
|
|
|
|
|
encodeNote :: Text -> Note -> LocalURI -> FedURI -> Encoding
|
|
|
|
encodeNote host (Note id_ mreply content) attrib to =
|
|
|
|
pairs
|
|
|
|
$ "type" .= ("Note" :: Text)
|
|
|
|
<> "id" .= l2f host id_
|
|
|
|
<> "attributedTo" .= l2f host attrib
|
|
|
|
<> "to" .= to
|
|
|
|
<> "inReplyTo" .=? mreply
|
|
|
|
<> "content" .= content
|
2019-02-12 12:53:24 +01:00
|
|
|
|
|
|
|
data Create = Create
|
2019-03-10 07:42:03 +01:00
|
|
|
{ createId :: LocalURI
|
2019-02-12 12:53:24 +01:00
|
|
|
, createTo :: FedURI
|
2019-03-10 07:42:03 +01:00
|
|
|
, createActor :: LocalURI
|
2019-02-12 12:53:24 +01:00
|
|
|
, createObject :: Note
|
|
|
|
}
|
|
|
|
|
2019-03-10 07:42:03 +01:00
|
|
|
parseCreate :: Object -> Parser (Text, Create)
|
|
|
|
parseCreate o = do
|
|
|
|
typ <- o .: "type"
|
|
|
|
unless (typ == ("Create" :: Text)) $ fail "type isn't Create"
|
|
|
|
(h, id_) <- f2l <$> o .: "id"
|
|
|
|
luActor <- withHost h $ f2l <$> o .: "actor"
|
|
|
|
(note, luAttrib, uTo) <- withHost h $ parseNote =<< o .: "object"
|
|
|
|
unless (luActor == luAttrib) $ fail "Create actor != Note attrib"
|
|
|
|
uTo' <- o .: "to"
|
|
|
|
unless (uTo == uTo') $ fail "Create to != Note to"
|
|
|
|
return (h, Create id_ uTo luActor note)
|
|
|
|
where
|
|
|
|
withHost h a = do
|
|
|
|
(h', v) <- a
|
|
|
|
if h == h'
|
|
|
|
then return v
|
|
|
|
else fail "URI host mismatch"
|
|
|
|
|
|
|
|
encodeCreate :: Text -> Create -> Series
|
|
|
|
encodeCreate host (Create id_ to actor obj)
|
|
|
|
= "type" .= ("Create" :: Text)
|
|
|
|
<> "id" .= l2f host id_
|
|
|
|
<> "to" .= to
|
|
|
|
<> "actor" .= l2f host actor
|
|
|
|
<> "object" `pair` encodeNote host obj actor to
|
2019-02-12 12:53:24 +01:00
|
|
|
|
|
|
|
data Activity = CreateActivity Create
|
|
|
|
|
2019-03-10 07:42:03 +01:00
|
|
|
instance ActivityPub Activity where
|
|
|
|
jsonldContext _ = ContextAS2
|
|
|
|
parseObject o = do
|
2019-02-12 12:53:24 +01:00
|
|
|
typ <- o .: "type"
|
|
|
|
case typ of
|
2019-03-10 07:42:03 +01:00
|
|
|
"Create" -> second CreateActivity <$> parseCreate o
|
2019-02-12 12:53:24 +01:00
|
|
|
_ -> fail $ "Unrecognized activity type: " ++ T.unpack typ
|
2019-03-10 07:42:03 +01:00
|
|
|
toSeries host (CreateActivity c) = encodeCreate host c
|
2019-01-21 16:54:57 +01:00
|
|
|
|
|
|
|
typeActivityStreams2 :: ContentType
|
|
|
|
typeActivityStreams2 = "application/activity+json"
|
|
|
|
|
|
|
|
typeActivityStreams2LD :: ContentType
|
|
|
|
typeActivityStreams2LD =
|
|
|
|
"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
|
|
|
|
|
2019-02-07 11:34:33 +01:00
|
|
|
hActivityPubActor :: HeaderName
|
|
|
|
hActivityPubActor = "ActivityPub-Actor"
|
|
|
|
|
2019-01-21 16:54:57 +01:00
|
|
|
provideAP :: (Monad m, ToJSON a) => a -> Writer (Endo [ProvidedRep m]) ()
|
|
|
|
provideAP v = do
|
|
|
|
let enc = toEncoding v
|
|
|
|
-- provideRepType typeActivityStreams2 $ return enc
|
|
|
|
provideRepType typeActivityStreams2LD $ return enc
|
|
|
|
|
|
|
|
data APGetError
|
|
|
|
= APGetErrorHTTP HttpException
|
|
|
|
| APGetErrorJSON JSONException
|
2019-01-21 23:24:09 +01:00
|
|
|
| APGetErrorContentType Text
|
2019-01-21 16:54:57 +01:00
|
|
|
deriving Show
|
|
|
|
|
|
|
|
instance Exception APGetError
|
|
|
|
|
|
|
|
-- | Perform an HTTP GET request to fetch an ActivityPub object.
|
|
|
|
--
|
|
|
|
-- * Verify the URI scheme is _https:_ and authority part is present
|
|
|
|
-- * Set _Accept_ request header
|
|
|
|
-- * Perform the GET request
|
|
|
|
-- * Verify the _Content-Type_ response header
|
|
|
|
-- * Parse the JSON response body
|
|
|
|
httpGetAP
|
|
|
|
:: (MonadIO m, FromJSON a)
|
|
|
|
=> Manager
|
2019-02-08 00:08:28 +01:00
|
|
|
-> FedURI
|
2019-01-21 16:54:57 +01:00
|
|
|
-> m (Either APGetError (Response a))
|
|
|
|
httpGetAP manager uri =
|
2019-02-08 00:08:28 +01:00
|
|
|
liftIO $
|
|
|
|
mkResult <$> try (httpAPEither manager =<< requestFromURI (toURI uri))
|
2019-01-21 16:54:57 +01:00
|
|
|
where
|
|
|
|
lookup' x = map snd . filter ((== x) . fst)
|
|
|
|
mkResult (Left e) = Left $ APGetErrorHTTP e
|
|
|
|
mkResult (Right r) =
|
|
|
|
case lookup' hContentType $ responseHeaders r of
|
|
|
|
[] -> Left $ APGetErrorContentType "No Content-Type"
|
|
|
|
[b] -> if b == typeActivityStreams2LD || b == typeActivityStreams2
|
|
|
|
then case responseBody r of
|
|
|
|
Left e -> Left $ APGetErrorJSON e
|
|
|
|
Right v -> Right $ v <$ r
|
2019-01-21 23:24:09 +01:00
|
|
|
else Left $ APGetErrorContentType $ "Non-AP Content-Type: " <> decodeUtf8 b
|
2019-01-21 16:54:57 +01:00
|
|
|
_ -> Left $ APGetErrorContentType "Multiple Content-Type"
|
|
|
|
|
2019-03-05 09:26:41 +01:00
|
|
|
data APPostError
|
|
|
|
= APPostErrorSig HttpSigGenError
|
|
|
|
| APPostErrorHTTP HttpException
|
|
|
|
deriving Show
|
|
|
|
|
|
|
|
instance Exception APPostError
|
|
|
|
|
2019-01-21 16:54:57 +01:00
|
|
|
-- | Perform an HTTP POST request to submit an ActivityPub object.
|
|
|
|
--
|
|
|
|
-- * Verify the URI scheme is _https:_ and authority part is present
|
|
|
|
-- * Set _Content-Type_ request header
|
2019-02-07 11:34:33 +01:00
|
|
|
-- * Set _ActivityPub-Actor_ request header
|
2019-01-21 16:54:57 +01:00
|
|
|
-- * Compute HTTP signature and add _Signature_ request header
|
|
|
|
-- * Perform the POST request
|
|
|
|
-- * Verify the response status is 2xx
|
|
|
|
httpPostAP
|
|
|
|
:: (MonadIO m, ToJSON a)
|
|
|
|
=> Manager
|
2019-02-08 00:08:28 +01:00
|
|
|
-> FedURI
|
2019-01-21 16:54:57 +01:00
|
|
|
-> NonEmpty HeaderName
|
|
|
|
-> (ByteString -> (KeyId, Signature))
|
2019-02-07 11:34:33 +01:00
|
|
|
-> Text
|
2019-01-21 16:54:57 +01:00
|
|
|
-> a
|
2019-03-05 09:26:41 +01:00
|
|
|
-> m (Either APPostError (Response ()))
|
|
|
|
httpPostAP manager uri headers sign uActor value = liftIO $ do
|
|
|
|
req <- requestFromURI $ toURI uri
|
|
|
|
let req' =
|
|
|
|
setRequestCheckStatus $
|
|
|
|
consHeader hContentType typeActivityStreams2LD $
|
|
|
|
consHeader hActivityPubActor (encodeUtf8 uActor) $
|
|
|
|
req { method = "POST"
|
|
|
|
, requestBody = RequestBodyLBS $ encode value
|
|
|
|
}
|
|
|
|
sign' b =
|
|
|
|
let (k, s) = sign b
|
|
|
|
in (Nothing, k, s)
|
|
|
|
ereq <- try $ signRequest headers sign' Nothing req'
|
|
|
|
case ereq of
|
|
|
|
Left sigErr -> return $ Left $ APPostErrorSig sigErr
|
|
|
|
Right req'' -> first APPostErrorHTTP <$> try (httpNoBody req'' manager)
|
2019-01-21 16:54:57 +01:00
|
|
|
where
|
|
|
|
consHeader n b r = r { requestHeaders = (n, b) : requestHeaders r }
|
2019-02-04 00:39:56 +01:00
|
|
|
|
2019-02-06 03:48:23 +01:00
|
|
|
-- | Result of GETing the keyId URI and processing the JSON document.
|
|
|
|
data Fetched = Fetched
|
|
|
|
{ fetchedPublicKey :: E.PublicKey
|
|
|
|
-- ^ The Ed25519 public key corresponding to the URI we requested.
|
|
|
|
, fetchedKeyExpires :: Maybe UTCTime
|
|
|
|
-- ^ Optional expiration time declared for the key we received.
|
2019-02-22 00:59:53 +01:00
|
|
|
, fetchedActorId :: LocalURI
|
2019-02-06 03:48:23 +01:00
|
|
|
-- ^ The @id URI of the actor for whom the key's signature applies.
|
2019-02-22 00:59:53 +01:00
|
|
|
, fetchedActorInbox :: LocalURI
|
2019-02-15 00:27:40 +01:00
|
|
|
-- ^ The inbox URI of the actor for whom the key's signature applies.
|
2019-02-06 03:48:23 +01:00
|
|
|
, fetchedKeyShared :: Bool
|
|
|
|
-- ^ Whether the key we received is shared. A shared key can sign
|
|
|
|
-- requests for any actor on the same instance, while a personal key is
|
|
|
|
-- only for one actor. Knowing whether the key is shared will allow us
|
|
|
|
-- when receiving more requests, whether to accept signatures made on
|
|
|
|
-- different actors, or allow only a single permanent actor for the key
|
|
|
|
-- we received.
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:59:53 +01:00
|
|
|
fetchAP :: (MonadIO m, FromJSON a) => Manager -> FedURI -> ExceptT String m a
|
|
|
|
fetchAP m u = ExceptT $ bimap displayException responseBody <$> httpGetAP m u
|
|
|
|
|
2019-02-23 18:17:52 +01:00
|
|
|
{-
|
2019-02-22 00:59:53 +01:00
|
|
|
fetchAPH :: (MonadIO m, ActivityPub a) => Manager -> Text -> LocalURI -> ExceptT String m a
|
|
|
|
fetchAPH m h lu = do
|
|
|
|
Doc h' v <- fetchAP m $ l2f h lu
|
|
|
|
if h == h'
|
|
|
|
then return v
|
|
|
|
else throwE "Object @id URI's host doesn't match the URI we fetched"
|
2019-02-23 18:17:52 +01:00
|
|
|
-}
|
2019-02-22 00:59:53 +01:00
|
|
|
|
|
|
|
fetchAPID :: (MonadIO m, ActivityPub a) => Manager -> (a -> LocalURI) -> Text -> LocalURI -> m (Either String a)
|
|
|
|
fetchAPID m getId h lu = runExceptT $ do
|
|
|
|
Doc h' v <- fetchAP m $ l2f h lu
|
|
|
|
if h == h' && getId v == lu
|
|
|
|
then return v
|
|
|
|
else throwE "Object @id doesn't match the URI we fetched"
|
|
|
|
|
|
|
|
fetchAPIDOrH
|
|
|
|
:: (MonadIO m, ActivityPub a, ActivityPub b)
|
|
|
|
=> Manager
|
|
|
|
-> (a -> LocalURI)
|
|
|
|
-> Text
|
|
|
|
-> LocalURI
|
|
|
|
-> ExceptT String m (Either a b)
|
|
|
|
fetchAPIDOrH m getId h lu = do
|
|
|
|
e <- fetchAP m $ l2f h lu
|
|
|
|
case e of
|
|
|
|
Left' (Doc h' x) ->
|
|
|
|
if h == h' && getId x == lu
|
|
|
|
then return $ Left x
|
|
|
|
else throwE "Object @id doesn't match the URI we fetched"
|
|
|
|
Right' (Doc h' y) ->
|
|
|
|
if h == h'
|
|
|
|
then return $ Right y
|
|
|
|
else throwE "Object @id URI's host doesn't match the URI we fetched"
|
|
|
|
|
When verifying HTTP sig with known shared key, verify actor lists the key
Previously, when verifying an HTTP signature and we fetched the key and
discovered it's shared, we'd fetch the actor and make sure it lists the key URI
in the `publicKey` field. But if we already knew the key, had it cached in our
DB, we wouldn't check the actor at all, despite not knowing whether it lists
the key.
With this patch, we now always GET the actor when the key is shared,
determining the actor URI from the `ActivityPub-Actor` request header, and we
verify that the actor lists the key URI. We do that regardless of whether or
not we have the key in the DB, although these two cases and handled in
different parts of the code right now (for a new key, it's in Web.ActivityPub
fetchKey; for a known key, it's in Vervis.Foundation httpVerifySig).
2019-02-18 10:20:13 +01:00
|
|
|
-- | Fetches the given actor and checks whether it lists the given key (as a
|
|
|
|
-- URI, not as an embedded object). If it does, returns 'Right' the fetched
|
|
|
|
-- actor. Otherwise, or if an error occurs during fetching, returns 'Left' an
|
|
|
|
-- error message.
|
2019-02-22 00:59:53 +01:00
|
|
|
keyListedByActor :: MonadIO m => Manager -> Text -> LocalURI -> LocalURI -> m (Either String Actor)
|
|
|
|
keyListedByActor manager host luKey luActor = runExceptT $ do
|
|
|
|
actor <- ExceptT $ fetchAPID manager actorId host luActor
|
|
|
|
if keyUriListed luKey actor
|
When verifying HTTP sig with known shared key, verify actor lists the key
Previously, when verifying an HTTP signature and we fetched the key and
discovered it's shared, we'd fetch the actor and make sure it lists the key URI
in the `publicKey` field. But if we already knew the key, had it cached in our
DB, we wouldn't check the actor at all, despite not knowing whether it lists
the key.
With this patch, we now always GET the actor when the key is shared,
determining the actor URI from the `ActivityPub-Actor` request header, and we
verify that the actor lists the key URI. We do that regardless of whether or
not we have the key in the DB, although these two cases and handled in
different parts of the code right now (for a new key, it's in Web.ActivityPub
fetchKey; for a known key, it's in Vervis.Foundation httpVerifySig).
2019-02-18 10:20:13 +01:00
|
|
|
then return actor
|
|
|
|
else throwE "Actor publicKey has no URI matching pkey @id"
|
|
|
|
where
|
|
|
|
keyUriListed uk a =
|
2019-02-24 02:21:42 +01:00
|
|
|
let match (Left uri) = uri == uk
|
When verifying HTTP sig with known shared key, verify actor lists the key
Previously, when verifying an HTTP signature and we fetched the key and
discovered it's shared, we'd fetch the actor and make sure it lists the key URI
in the `publicKey` field. But if we already knew the key, had it cached in our
DB, we wouldn't check the actor at all, despite not knowing whether it lists
the key.
With this patch, we now always GET the actor when the key is shared,
determining the actor URI from the `ActivityPub-Actor` request header, and we
verify that the actor lists the key URI. We do that regardless of whether or
not we have the key in the DB, although these two cases and handled in
different parts of the code right now (for a new key, it's in Web.ActivityPub
fetchKey; for a known key, it's in Vervis.Foundation httpVerifySig).
2019-02-18 10:20:13 +01:00
|
|
|
match (Right _) = False
|
2019-02-24 02:21:42 +01:00
|
|
|
in any match $ actorPublicKeys a
|
When verifying HTTP sig with known shared key, verify actor lists the key
Previously, when verifying an HTTP signature and we fetched the key and
discovered it's shared, we'd fetch the actor and make sure it lists the key URI
in the `publicKey` field. But if we already knew the key, had it cached in our
DB, we wouldn't check the actor at all, despite not knowing whether it lists
the key.
With this patch, we now always GET the actor when the key is shared,
determining the actor URI from the `ActivityPub-Actor` request header, and we
verify that the actor lists the key URI. We do that regardless of whether or
not we have the key in the DB, although these two cases and handled in
different parts of the code right now (for a new key, it's in Web.ActivityPub
fetchKey; for a known key, it's in Vervis.Foundation httpVerifySig).
2019-02-18 10:20:13 +01:00
|
|
|
|
2019-02-24 02:21:42 +01:00
|
|
|
matchKeyObj :: (Foldable f, Monad m) => LocalURI -> f (Either LocalURI PublicKey) -> ExceptT String m PublicKey
|
|
|
|
matchKeyObj luKey es =
|
|
|
|
case find' (match luKey) es of
|
|
|
|
Nothing -> throwE "keyId resolved to actor which doesn't have a key object with that ID"
|
|
|
|
Just pk -> return pk
|
2019-02-23 18:17:52 +01:00
|
|
|
where
|
2019-02-24 02:21:42 +01:00
|
|
|
find' :: Foldable f => (a -> Maybe b) -> f a -> Maybe b
|
|
|
|
find' p = join . fmap getFirst . foldMap (Just . First . p)
|
2019-02-23 18:17:52 +01:00
|
|
|
match _ (Left _) = Nothing
|
|
|
|
match luk (Right pk) =
|
|
|
|
if publicKeyId pk == luk
|
|
|
|
then Just pk
|
|
|
|
else Nothing
|
|
|
|
|
|
|
|
verifyAlgo sigAlgo Nothing =
|
|
|
|
Left $
|
|
|
|
if sigAlgo
|
|
|
|
then "Algo mismatch, Ed25519 in Sig but none in actor"
|
|
|
|
else "Algo not given in Sig nor actor"
|
|
|
|
verifyAlgo sigAlgo (Just algo) =
|
|
|
|
case algo of
|
|
|
|
AlgorithmEd25519 -> Right ()
|
|
|
|
AlgorithmOther _ ->
|
|
|
|
Left $
|
|
|
|
if sigAlgo
|
|
|
|
then "Algo mismatch, Ed25519 in Sig but unsupported algo in actor"
|
|
|
|
else "No algo in Sig, unsupported algo in actor"
|
|
|
|
|
|
|
|
parseKey pem =
|
|
|
|
case E.publicKey $ pemContent pem of
|
|
|
|
CryptoPassed k -> Right k
|
|
|
|
CryptoFailed _ -> Left "Parsing Ed25519 public key failed"
|
|
|
|
|
|
|
|
-- | Fetch a key we don't have cached locally.
|
|
|
|
fetchUnknownKey
|
2019-02-04 00:39:56 +01:00
|
|
|
:: MonadIO m
|
|
|
|
=> Manager
|
2019-02-23 18:17:52 +01:00
|
|
|
-- ^ Manager for making HTTP requests
|
2019-02-04 00:39:56 +01:00
|
|
|
-> Bool
|
2019-02-23 18:17:52 +01:00
|
|
|
-- ^ Whether the Ed25519 algorithm is specified explicitly in the HTTP
|
|
|
|
-- signature header
|
2019-02-22 00:59:53 +01:00
|
|
|
-> Text
|
2019-02-23 18:17:52 +01:00
|
|
|
-- ^ Instance host
|
2019-02-22 00:59:53 +01:00
|
|
|
-> Maybe LocalURI
|
2019-02-23 18:17:52 +01:00
|
|
|
-- ^ Actor URI possibly provided in the HTTP request's actor header
|
2019-02-22 00:59:53 +01:00
|
|
|
-> LocalURI
|
2019-02-23 18:17:52 +01:00
|
|
|
-- ^ Key URI provided in HTTP signature header
|
|
|
|
-> ExceptT String m Fetched
|
|
|
|
fetchUnknownKey manager sigAlgo host mluActor luKey = do
|
2019-02-22 00:59:53 +01:00
|
|
|
obj <- fetchAPIDOrH manager publicKeyId host luKey
|
2019-02-22 08:20:19 +01:00
|
|
|
(pem, mkFetched, malgo) <-
|
2019-02-04 00:39:56 +01:00
|
|
|
case obj of
|
2019-02-22 00:59:53 +01:00
|
|
|
Left pkey -> do
|
2019-02-22 08:20:19 +01:00
|
|
|
(oi, luActor) <-
|
2019-02-22 00:59:53 +01:00
|
|
|
case publicKeyOwner pkey of
|
|
|
|
OwnerInstance ->
|
|
|
|
case mluActor of
|
|
|
|
Nothing -> throwE "Key is shared but actor header not specified!"
|
2019-02-22 08:20:19 +01:00
|
|
|
Just u -> return (True, u)
|
2019-02-22 00:59:53 +01:00
|
|
|
OwnerActor owner -> do
|
|
|
|
for_ mluActor $ \ lu ->
|
|
|
|
if owner == lu
|
2019-02-17 01:14:05 +01:00
|
|
|
then return ()
|
|
|
|
else throwE "Key's owner doesn't match actor header"
|
2019-02-22 08:20:19 +01:00
|
|
|
return (False, owner)
|
|
|
|
inbox <- actorInbox <$> ExceptT (keyListedByActor manager host luKey luActor)
|
|
|
|
return
|
|
|
|
( publicKeyPem pkey
|
|
|
|
, \ k ->
|
|
|
|
Fetched
|
|
|
|
{ fetchedPublicKey = k
|
|
|
|
, fetchedKeyExpires = publicKeyExpires pkey
|
|
|
|
, fetchedActorId = luActor
|
|
|
|
, fetchedActorInbox = inbox
|
|
|
|
, fetchedKeyShared = oi
|
|
|
|
}
|
|
|
|
, publicKeyAlgo pkey
|
|
|
|
)
|
2019-02-22 00:59:53 +01:00
|
|
|
Right actor -> do
|
|
|
|
if actorId actor == luKey { luriFragment = "" }
|
2019-02-04 11:07:25 +01:00
|
|
|
then return ()
|
2019-02-04 00:39:56 +01:00
|
|
|
else throwE "Actor ID doesn't match the keyid URI we fetched"
|
2019-02-22 00:59:53 +01:00
|
|
|
for_ mluActor $ \ lu ->
|
|
|
|
if actorId actor == lu
|
2019-02-17 01:14:05 +01:00
|
|
|
then return ()
|
|
|
|
else throwE "Key's owner doesn't match actor header"
|
2019-02-22 09:30:43 +01:00
|
|
|
pk <- matchKeyObj luKey $ actorPublicKeys actor
|
|
|
|
owner <- case publicKeyOwner pk of
|
|
|
|
OwnerInstance -> throwE "Actor's publicKey is shared, but embedded in actor document! We allow shared keys only if they're in a separate document"
|
2019-02-24 02:21:42 +01:00
|
|
|
OwnerActor owner ->
|
2019-02-22 09:30:43 +01:00
|
|
|
if owner == actorId actor
|
|
|
|
then return owner
|
|
|
|
else throwE "Actor's publicKey's owner doesn't match the actor's ID"
|
|
|
|
return
|
|
|
|
( publicKeyPem pk
|
|
|
|
, \ k ->
|
|
|
|
Fetched
|
|
|
|
{ fetchedPublicKey = k
|
|
|
|
, fetchedKeyExpires = publicKeyExpires pk
|
|
|
|
, fetchedActorId = owner
|
|
|
|
, fetchedActorInbox = actorInbox actor
|
|
|
|
, fetchedKeyShared = False
|
|
|
|
}
|
|
|
|
, publicKeyAlgo pk
|
|
|
|
)
|
2019-02-04 00:39:56 +01:00
|
|
|
ExceptT . pure $ do
|
2019-02-22 09:30:43 +01:00
|
|
|
verifyAlgo sigAlgo malgo
|
|
|
|
mkFetched <$> parseKey pem
|
2019-02-23 18:17:52 +01:00
|
|
|
|
|
|
|
keyDetail pk = (publicKeyPem pk, publicKeyExpires pk, publicKeyAlgo pk)
|
|
|
|
|
|
|
|
-- | Fetch a personal key we already have cached locally, but we'd like to
|
|
|
|
-- refresh the local copy by fetching the key again from the server.
|
|
|
|
fetchKnownPersonalKey
|
|
|
|
:: MonadIO m
|
|
|
|
=> Manager
|
|
|
|
-- ^ Manager for making HTTP requests
|
|
|
|
-> Bool
|
|
|
|
-- ^ Whether the Ed25519 algorithm is specified explicitly in the HTTP
|
|
|
|
-- signature header
|
|
|
|
-> Text
|
|
|
|
-- ^ Instance host
|
|
|
|
-> LocalURI
|
|
|
|
-- ^ Key owner actor ID URI
|
|
|
|
-> LocalURI
|
|
|
|
-- ^ Key URI
|
|
|
|
-> ExceptT String m (E.PublicKey, Maybe UTCTime)
|
|
|
|
fetchKnownPersonalKey manager sigAlgo host luOwner luKey = do
|
|
|
|
obj <- fetchAPIDOrH manager publicKeyId host luKey
|
|
|
|
(pem, mexpires, malgo) <-
|
|
|
|
case obj of
|
|
|
|
Left pkey -> do
|
|
|
|
case publicKeyOwner pkey of
|
|
|
|
OwnerInstance -> throwE "Personal key became shared"
|
|
|
|
OwnerActor owner ->
|
|
|
|
when (luOwner /= owner) $ throwE "Key owner changed"
|
|
|
|
return $ keyDetail pkey
|
|
|
|
Right actor -> do
|
|
|
|
when (actorId actor /= luKey { luriFragment = "" }) $
|
|
|
|
throwE "Actor ID doesn't match the keyid URI we fetched"
|
|
|
|
when (actorId actor /= luOwner) $
|
|
|
|
throwE "Key owner changed"
|
|
|
|
pk <- matchKeyObj luKey $ actorPublicKeys actor
|
|
|
|
case publicKeyOwner pk of
|
|
|
|
OwnerInstance -> throwE "Personal key became shared"
|
|
|
|
OwnerActor owner ->
|
|
|
|
when (owner /= luOwner) $
|
|
|
|
throwE "Actor's publicKey's owner doesn't match the actor's ID"
|
|
|
|
return $ keyDetail pk
|
|
|
|
ExceptT . pure $ do
|
|
|
|
verifyAlgo sigAlgo malgo
|
|
|
|
(, mexpires) <$> parseKey pem
|
|
|
|
|
|
|
|
-- | Fetch a shared key we already have cached locally, but we'd like to
|
|
|
|
-- refresh the local copy by fetching the key again from the server.
|
|
|
|
fetchKnownSharedKey
|
|
|
|
:: MonadIO m
|
|
|
|
=> Manager
|
|
|
|
-- ^ Manager for making HTTP requests
|
|
|
|
-> ExceptT String m ()
|
|
|
|
-- ^ Action which checks whether the actor from HTTP actor header lists the
|
|
|
|
-- key, potentually updating our local cache if needed.
|
|
|
|
-> Bool
|
|
|
|
-- ^ Whether the Ed25519 algorithm is specified explicitly in the HTTP
|
|
|
|
-- signature header
|
|
|
|
-> Text
|
|
|
|
-- ^ Instance host
|
|
|
|
-> LocalURI
|
|
|
|
-- ^ Actor ID from HTTP actor header
|
|
|
|
-> LocalURI
|
|
|
|
-- ^ Key URI
|
|
|
|
-> ExceptT String m (E.PublicKey, Maybe UTCTime)
|
|
|
|
fetchKnownSharedKey manager listed sigAlgo host luActor luKey = do
|
|
|
|
obj <- fetchAPIDOrH manager publicKeyId host luKey
|
|
|
|
pkey <-
|
|
|
|
case obj :: Either PublicKey Actor of
|
|
|
|
Left pk -> return pk
|
|
|
|
Right _actor -> throwE "Expected stand-alone key, got embedded key"
|
|
|
|
case publicKeyOwner pkey of
|
|
|
|
OwnerInstance -> return ()
|
|
|
|
OwnerActor _owner -> throwE "Shared key became personal"
|
|
|
|
listed
|
|
|
|
let (pem, mexpires, malgo) = keyDetail pkey
|
|
|
|
ExceptT . pure $ do
|
|
|
|
verifyAlgo sigAlgo malgo
|
|
|
|
(, mexpires) <$> parseKey pem
|