]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Prelude/Crypto/Pass/User.hs
[FEAT] gargPassUser improved
[gargantext.git] / src / Gargantext / Prelude / Crypto / Pass / User.hs
1 {-|
2 Module : Gargantext.Prelude.Crypto.Pass.User
3 Description :
4 Copyright : (c) CNRS, 2017-Present
5 License : Public Domain
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 Easy password manager for User (easy to memorize).
11
12 -}
13
14
15 module Gargantext.Prelude.Crypto.Pass.User
16 where
17
18 import Data.List ((!!))
19 import Gargantext.Prelude
20 import Gargantext.Prelude.Utils (shuffle)
21 import System.Random
22
23 -- TODO add this as parameter to gargantext.ini
24 gargPassUser :: (Num a, Enum a, Integral a) => a -> [b] -> IO [b]
25 gargPassUser n = gargPassUser' (100 * fromIntegral n) n
26
27 gargPassUser' :: (Num a, Enum a) => Int -> a -> [b] -> IO [b]
28 gargPassUser' threshold size wlist
29 | length wlist > threshold = generatePassword size wlist
30 | otherwise = panic "List to short"
31
32 generatePassword :: (Num a, Enum a) => a -> [b] -> IO [b]
33 generatePassword size wlist = shuffle wlist
34 >>= \wlist' -> mapM (\_ -> getRandomElement wlist') [1..size]
35
36 getRandomIndex :: Foldable t => t a -> IO Int
37 getRandomIndex list = randomRIO (0, (length list - 1))
38
39 getRandomElement :: [b] -> IO b
40 getRandomElement list = do
41 index <- (getRandomIndex list)
42 pure (list !! index)
43