From 20fb5181cd92fb61e13f33d31aa1bc5fddacdf44 Mon Sep 17 00:00:00 2001 From: fr33domlover Date: Tue, 8 Mar 2016 03:38:32 +0000 Subject: [PATCH] Decode SSH key content field value from base64 --- src/Vervis/Field/Key.hs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Vervis/Field/Key.hs b/src/Vervis/Field/Key.hs index 23a922b..1b0ffc4 100644 --- a/src/Vervis/Field/Key.hs +++ b/src/Vervis/Field/Key.hs @@ -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