Something went wrong. Try again.
A music player that connects to your cloud/distributed storage. forked from tokono.ma/diffuse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209module Sources.Services.AmazonS3.Presign exposing (presignedUrl)
import Binary exposing (Bits)import Commonimport Cryptography.Hmac as Hmacimport DateFormat as Dateimport Dict.Ext as Dictimport Heximport Maybe.Extra as Maybeimport Regeximport SHAimport Sources exposing (SourceData)import Sources.Processing exposing (HttpMethod, httpMethod)import String.Ext as Stringimport Timeimport Url
-- 🔱
presignedUrl : HttpMethod -> Int -> List ( String, String ) -> Time.Posix -> SourceData -> String -> StringpresignedUrl method lifeExpectancyInSeconds extraParams currentTime srcData pathToFile = let aws = srcData
region = Dict.fetchUnknown "region" aws
bucketName = Dict.fetchUnknown "bucketName" aws
customHost = case Dict.fetch "host" "" aws of "" -> Nothing
x -> Just x
host = case customHost of Just h -> h |> String.chopStart "http://" |> String.chopStart "https://" |> String.chopEnd "/"
Nothing -> case String.trim region of "" -> bucketName ++ ".s3.amazonaws.com"
r -> bucketName ++ ".s3." ++ r ++ ".amazonaws.com"
protocol = if String.contains "http://" (Maybe.withDefault "" customHost) then "http://"
else "https://"
-- {var} Paths filePathPrefix = if Maybe.isJust customHost then bucketName ++ "/"
else ""
filePath = pathToFile |> String.chopStart "/" |> String.split "/" |> List.map (Url.percentEncode >> encodeAdditionalCharacters) |> String.join "/" |> String.append ("/" ++ filePathPrefix)
-- {var} Time -- timestamp -> 20130721T201207Z -- date -> 20130721 timestamp = Date.format [ Date.yearNumber , Date.monthFixed , Date.dayOfMonthFixed , Date.text "T" , Date.hourMilitaryFixed , Date.minuteFixed , Date.secondFixed , Date.text "Z" ] Time.utc currentTime
date = Date.format [ Date.yearNumber , Date.monthFixed , Date.dayOfMonthFixed ] Time.utc currentTime
-- Request credential = [ Dict.fetchUnknown "accessKey" aws , date , region , "s3" , "aws4_request" ] |> String.join "/"
queryString = [ ( "X-Amz-Algorithm", "AWS4-HMAC-SHA256" ) , ( "X-Amz-Credential", credential ) , ( "X-Amz-Date", timestamp ) , ( "X-Amz-Expires", String.fromInt lifeExpectancyInSeconds ) , ( "X-Amz-SignedHeaders", "host" ) ] |> List.append extraParams |> List.sortBy Tuple.first |> Common.queryString |> String.dropLeft 1 |> encodeAdditionalCharacters
request = String.join "\n" [ httpMethod method , filePath , queryString , "host:" ++ host , "" , "host" , "UNSIGNED-PAYLOAD" ]
-- String to sign stringToSign = String.join "\n" [ "AWS4-HMAC-SHA256" , timestamp , String.join "/" [ date, region, "s3", "aws4_request" ]
-- , request |> Binary.fromStringAsUtf8 |> SHA.sha256 |> Binary.toHex |> String.toLower ]
-- Signature signature = ("AWS4" ++ Dict.fetchUnknown "secretKey" aws) |> Binary.fromStringAsUtf8 |> hmacSha256 date |> hmacSha256 region |> hmacSha256 "s3" |> hmacSha256 "aws4_request" |> hmacSha256 stringToSign |> Binary.toHex |> String.toLower in String.concat [ protocol , host , filePath , "?" , queryString , "&X-Amz-Signature=" , signature ]
-- ⚗️
encodeAdditionalCharacters : String -> StringencodeAdditionalCharacters query = Regex.replace (Maybe.withDefault Regex.never <| Regex.fromString "[!*'()]") (\{ match } -> match |> String.toList |> List.map (Char.toCode >> Hex.toString >> String.toUpper >> (++) "%") |> String.concat ) query
hmacSha256 : String -> Bits -> BitshmacSha256 = Hmac.encrypt64 SHA.sha256