Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] use functions from Utils.String module for to/from List Bits8 #14

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 =>
jarlah marked this conversation as resolved.
Show resolved Hide resolved
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."
Loading