Decode SSH key content field value from base64

This commit is contained in:
fr33domlover 2016-03-08 03:38:32 +00:00
parent 4a6853e7e7
commit 20fb5181cd

View file

@ -23,6 +23,7 @@ where
import Prelude
import Data.ByteString (ByteString)
import Data.ByteString.Base64 (decode)
import Data.Char (isDigit)
import Data.Maybe (isNothing)
import Data.Text (Text)
@ -30,10 +31,11 @@ import Data.Text.Encoding (encodeUtf8, decodeUtf8With)
import Data.Text.Encoding.Error (lenientDecode)
import Database.Persist (checkUnique)
import Yesod.Form.Fields (textField)
import Yesod.Form.Functions (checkBool, checkM, convertField)
import Yesod.Form.Functions (check, checkBool, checkM, convertField)
import Yesod.Form.Types (Field)
import Yesod.Persist.Core (runDB)
import qualified Data.ByteString.Char8 as BC
import qualified Data.Text as T
import Data.Char.Local (isAsciiLetter)
@ -73,14 +75,20 @@ checkAlgoSupported =
algoField :: Field Handler ByteString
algoField = checkAlgoSupported bsField
checkContent :: Field Handler Text -> Field Handler Text
checkContent :: Field Handler ByteString -> Field Handler ByteString
checkContent =
let lasts = (== '=')
{-let lasts = (== '=')
rest c = isAsciiLetter c || isDigit c || c == '+' || c == '/'
ok t = T.all rest $ T.dropWhileEnd lasts t
ok b = BC.all rest $ BC.dropWhileEnd lasts b
msg :: Text
msg = "Must be a base64-encoded public SSH key"
in checkBool ok msg
msg = "Must be a base64-encoded public SSH key"-}
check $ \ t ->
case decode t of
Left s -> Left $ T.pack s
Right b -> Right b
--TODO make the above work over ByteString and when passes the check, apply
--base64 conversion. delete my rel4 key from the DB and re-insert...
contentField :: Field Handler ByteString
contentField = mkBsField $ checkContent textField
contentField = checkContent bsField