This repository has been archived by the owner on Aug 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auth.hs
65 lines (51 loc) · 2.19 KB
/
Auth.hs
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
module Web.Zenfolio.Auth (
authenticate,
authenticatePlain,
getChallenge,
getDownloadOriginalKey,
keyringAddKeyPlain,
loadAccessRealm,
updateGroupAccess,
updatePhotoAccess,
updatePhotoSetAccess,
login,
emptyKeyring
) where
import qualified Data.Digest.SHA256 as SHA256 (hash)
import Data.String.UTF8 (fromString, toRep)
import Web.Zenfolio.Monad (ZM)
import Web.Zenfolio.RPC (zfRemote, zfRemoteSsl)
import Web.Zenfolio.Types (Void, LoginName, AuthChallenge(..), Password, AuthToken, RealmID,
AccessDescriptor, PhotoID, AccessUpdater, GroupID, PhotoSetID)
import Web.Zenfolio.Types.Access (DownloadKey, Keyring(..))
authenticate :: AuthChallenge -> Password -> ZM AuthToken
authenticate challenge password = do
zfRemote "Authenticate" challengeBytes roundTwoBytes
where saltBytes = acPasswordSalt challenge
challengeBytes = acChallenge challenge
passwordBytes = toRep $ fromString password
roundOneBytes = SHA256.hash $ saltBytes ++ passwordBytes
roundTwoBytes = SHA256.hash $ challengeBytes ++ roundOneBytes
authenticatePlain :: LoginName -> Password -> ZM AuthToken
authenticatePlain = zfRemoteSsl "AuthenticatePlain"
getChallenge :: LoginName -> ZM AuthChallenge
getChallenge = zfRemote "GetChallenge"
getDownloadOriginalKey :: [PhotoID] -> Password -> ZM DownloadKey
getDownloadOriginalKey = zfRemote "GetDownloadOriginalKey"
keyringAddKeyPlain :: Keyring -> RealmID -> Password -> ZM Keyring
keyringAddKeyPlain = zfRemoteSsl "KeyringAddKeyPlain"
loadAccessRealm :: RealmID -> ZM AccessDescriptor
loadAccessRealm = zfRemote "LoadAccessRealm"
updateGroupAccess :: GroupID -> AccessUpdater -> ZM Void
updateGroupAccess = zfRemote "UpdateGroupAccess"
updatePhotoAccess :: PhotoID -> AccessUpdater -> ZM Void
updatePhotoAccess = zfRemote "UpdatePhotoAccess"
updatePhotoSetAccess :: PhotoSetID -> AccessUpdater -> ZM Void
updatePhotoSetAccess = zfRemote "UpdatePhotoSetAccess"
-- Helper functions
login :: LoginName -> Password -> ZM AuthToken
login username password = do
challenge <- getChallenge username
authenticate challenge password
emptyKeyring :: Keyring
emptyKeyring = Keyring ""