-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1101 from wireapp/release_2020_05_15
- Loading branch information
Showing
130 changed files
with
13,318 additions
and
8,919 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- This file is part of the Wire Server implementation. | ||
-- | ||
-- Copyright (C) 2020 Wire Swiss GmbH <[email protected]> | ||
|
@@ -19,114 +16,21 @@ | |
-- with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
module Brig.Types.Activation | ||
( module Brig.Types.Activation, | ||
( ActivationPair, | ||
|
||
-- * re-exports | ||
module C, | ||
ActivationKey (..), | ||
ActivationCode (..), | ||
Activate (..), | ||
ActivationTarget (..), | ||
ActivationResponse (..), | ||
SendActivationCode (..), | ||
) | ||
where | ||
|
||
import Brig.Types.Common as C | ||
import Data.Aeson | ||
import Data.ByteString.Conversion | ||
import Data.Json.Util ((#)) | ||
import Data.Text.Ascii | ||
import Imports | ||
|
||
-- | An opaque identifier of a 'UserKey' awaiting activation. | ||
newtype ActivationKey = ActivationKey | ||
{fromActivationKey :: AsciiBase64Url} | ||
deriving (Eq, Show, FromByteString, ToByteString, FromJSON, ToJSON, Generic) | ||
|
||
-- | A random code for use with an 'ActivationKey' that is usually transmitted | ||
-- out-of-band, e.g. via email or sms. | ||
newtype ActivationCode = ActivationCode | ||
{fromActivationCode :: AsciiBase64Url} | ||
deriving (Eq, Show, FromByteString, ToByteString, FromJSON, ToJSON, Generic) | ||
import Wire.API.User.Activation | ||
|
||
-- | A pair of 'ActivationKey' and 'ActivationCode' as required for activation. | ||
type ActivationPair = (ActivationKey, ActivationCode) | ||
|
||
-- | Data for an activation request. | ||
data Activate = Activate | ||
{ activateTarget :: !ActivationTarget, | ||
activateCode :: !ActivationCode, | ||
activateDryrun :: !Bool | ||
} | ||
|
||
-- | The target of an activation request. | ||
data ActivationTarget | ||
= -- | An opaque key for some email or phone number awaiting activation. | ||
ActivateKey !ActivationKey | ||
| -- | A known phone number awaiting activation. | ||
ActivatePhone !Phone | ||
| -- | A known email address awaiting activation. | ||
ActivateEmail !Email | ||
|
||
instance ToByteString ActivationTarget where | ||
builder (ActivateKey k) = builder k | ||
builder (ActivateEmail e) = builder e | ||
builder (ActivatePhone p) = builder p | ||
|
||
-- | Information returned as part of a successful activation. | ||
data ActivationResponse = ActivationResponse | ||
{ -- | The activated / verified user identity. | ||
activatedIdentity :: !UserIdentity, | ||
-- | Whether this is the first verified identity of the account. | ||
activatedFirst :: !Bool | ||
} | ||
|
||
-- | Payload for a request to (re-)send an activation code | ||
-- for a phone number or e-mail address. If a phone is used, | ||
-- one can also request a call instead of SMS. | ||
data SendActivationCode = SendActivationCode | ||
{ saUserKey :: !(Either Email Phone), | ||
saLocale :: !(Maybe Locale), | ||
saCall :: !Bool | ||
} | ||
|
||
-- * JSON Instances: | ||
|
||
instance FromJSON SendActivationCode where | ||
parseJSON = withObject "SendActivationCode" $ \o -> do | ||
e <- o .:? "email" | ||
p <- o .:? "phone" | ||
SendActivationCode <$> key e p | ||
<*> o .:? "locale" | ||
<*> o .:? "voice_call" .!= False | ||
where | ||
key (Just _) (Just _) = fail "Only one of 'email' or 'phone' allowed." | ||
key Nothing Nothing = fail "One of 'email' or 'phone' required." | ||
key (Just e) Nothing = return $ Left e | ||
key Nothing (Just p) = return $ Right p | ||
|
||
instance ToJSON ActivationResponse where | ||
toJSON (ActivationResponse ident first) = | ||
object $ | ||
"email" .= emailIdentity ident | ||
# "phone" .= phoneIdentity ident | ||
# "first" .= first | ||
# [] | ||
|
||
instance FromJSON ActivationResponse where | ||
parseJSON = withObject "ActivationResponse" $ \o -> | ||
ActivationResponse <$> parseJSON (Object o) | ||
<*> o .:? "first" .!= False | ||
|
||
instance FromJSON Activate where | ||
parseJSON = withObject "Activation" $ \o -> | ||
Activate <$> key o | ||
<*> o .: "code" | ||
<*> o .:? "dryrun" .!= False | ||
where | ||
key o = | ||
(ActivateKey <$> o .: "key") | ||
<|> (ActivateEmail <$> o .: "email") | ||
<|> (ActivatePhone <$> o .: "phone") | ||
|
||
instance ToJSON Activate where | ||
toJSON (Activate k c d) = | ||
object | ||
[key k, "code" .= c, "dryrun" .= d] | ||
where | ||
key (ActivateKey ak) = "key" .= ak | ||
key (ActivateEmail e) = "email" .= e | ||
key (ActivatePhone p) = "phone" .= p |
Oops, something went wrong.