Skip to content

Commit

Permalink
[chore] use functions from Utils.String module to work to/from List B…
Browse files Browse the repository at this point in the history
…its8
  • Loading branch information
jarlah committed Nov 23, 2024
1 parent ed8677b commit 4c9d665
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/Network/HTTP/Authorization.idr
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
module Network.HTTP.Authorization

import Data.String.Base64
import Utils.String

private
buildBasicAuth : String -- username
-> String -- password
-> (String, String)
buildBasicAuth user passwd =
let userandpasswd = user ++ ":" ++ passwd
userandpasswd' = map (\x => the Bits8 (cast $ ord x)) (fastUnpack userandpasswd)
in ("Authorization", "Basic " ++ (base64EncodeString userandpasswd'))
userandpasswd' = utf8_unpack userandpasswd
in ("Authorization", "Basic " ++ base64EncodeString userandpasswd')

public export
applyBasicAuth : String -- username
Expand Down
28 changes: 16 additions & 12 deletions tests/src/AuthorizationTest.idr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Control.Monad.Error.Either
import Data.List1
import Data.String
import Data.String.Base64
import Utils.String

%default partial

Expand All @@ -16,17 +17,20 @@ test_base64_encoding_decoding =
(_, basicauth) = applyBasicAuth user passwd
basicauth' = base64DecodeString $ last $ split ((==) ' ') basicauth
in case basicauth' of
Nothing =>
Nothing =>
idris_crash "couldn't base64 decode string."
Just basicauth'' =>
let basicauth''' = fastPack $ map (\x => chr $ the Int (cast x)) basicauth''
userpasswd = split ((==) ':') basicauth'''
user' = head userpasswd
passwd' = last userpasswd
in case basicauth == "Basic YWxhZGRpbjpvcGVuc2VzYW1l" &&
user == user' &&
passwd == passwd' of
True =>
pure ()
False =>
throwE "base64 encode/decode error: expected \{user} and \{user'}, \{passwd} and \{passwd'} to be equivalent."
case utf8_pack basicauth'' of
Nothing =>
idris_crash "couldn't utf8 pack decoded string."
Just basicauth''' =>
let userpasswd = split ((==) ':') basicauth'''
user' = head userpasswd
passwd' = last userpasswd
in case basicauth == "Basic YWxhZGRpbjpvcGVuc2VzYW1l" &&
user == user' &&
passwd == passwd' of
True =>
pure ()
False =>
throwE "base64 encode/decode error: expected \{user} and \{user'}, \{passwd} and \{passwd'} to be equivalent."

0 comments on commit 4c9d665

Please sign in to comment.