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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226module Sources.Services.Azure.Authorization exposing (Computation(..), SignatureDependencies, StorageMethod(..), makeSignature, presignedUrl)
{-| Resources:
- <https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-an-account-sas#account-sas-example> - <https://docs.microsoft.com/en-us/rest/api/storageservices/Constructing-a-Service-SAS?redirectedfrom=MSDN>
-}
import Binaryimport BinaryBase64import Commonimport Cryptography.Hmac as Hmacimport DateFormat as Dateimport Dict.Ext as Dictimport SHAimport Sources exposing (SourceData)import Sources.Processing exposing (HttpMethod)import String.Ext as Stringimport Timeimport Url
-- Types
type Computation = List | Read
type StorageMethod = Blob | File
-- Public functions
presignedUrl : StorageMethod -> Computation -> HttpMethod -> Int -> Time.Posix -> SourceData -> String -> List ( String, String ) -> StringpresignedUrl storageMethod computation _ _ currentTime srcData pathToFile params = let azure = srcData
accountName = Dict.fetchUnknown "accountName" azure
accountKey = Dict.fetchUnknown "accountKey" azure
container = Dict.fetchUnknown "container" azure
-- {var} Time (y-MM-ddTHH:mmZ) expiryTime = Date.format [ Date.yearNumber , Date.text "-" , Date.monthFixed , Date.text "-" , Date.dayOfMonthFixed , Date.text "T" , Date.hourMilitaryFixed , Date.text ":" , Date.minuteFixed , Date.text "Z" ] Time.utc (currentTime |> Time.posixToMillis |> (+) 3600000 |> Time.millisToPosix )
-- {var} Other permissions = case computation of List -> "l"
Read -> "r"
resourceType = case storageMethod of Blob -> "blob"
File -> "file"
resType = case storageMethod of Blob -> "container"
File -> "directory"
-- Signature signatureStuff = { accountKey = accountKey , accountName = accountName , expiryTime = expiryTime , permissions = permissions , protocol = "https" , resources = "co" , services = "bf" , startTime = "" , version = "2017-04-17" } in String.concat [ "https://" , Url.percentEncode accountName , "." , Url.percentEncode resourceType , ".core.windows.net/" , Url.percentEncode container , "/" , Url.percentEncode (String.chopStart "/" pathToFile)
-- Start query params , case Common.queryString params of "" -> "?"
qs -> qs
-- Query params for certain requests , case computation of List -> "&restype=" ++ resType ++ "&comp=list"
_ -> ""
-- Signature things , "&sv=" , Url.percentEncode signatureStuff.version , "&ss=" , Url.percentEncode signatureStuff.services , "&srt=" , Url.percentEncode signatureStuff.resources , "&sp=" , Url.percentEncode signatureStuff.permissions , "&se=" , Url.percentEncode signatureStuff.expiryTime , "&spr=" , Url.percentEncode signatureStuff.protocol , "&sig=" , Url.percentEncode (makeSignature signatureStuff) ]
-- Signature
type alias SignatureDependencies = { accountKey : String , accountName : String , expiryTime : String , permissions : String , protocol : String , resources : String , services : String , startTime : String , version : String }
{-| Make a signature.
>>> makeSignature { accountKey = "93K17Co74T2lDHk2rA+wmb/avIAS6u6lPnZrk2hyT+9+aov82qNhrcXSNGZCzm9mjd4d75/oxxOr6r1JVpgTLA==", accountName = "tsmatsuzsttest0001", expiryTime = "2016-07-08T04:41:20Z", permissions = "rwdlacup", protocol = "https", resources = "sco", services = "bfqt", startTime = "2016-06-29T04:41:20Z", version = "2015-04-05" } "+XuDjuLE1Sv/FrJTLz8YjsaDukWNTKX7e8G8Ew+5aps="
-}makeSignature : SignatureDependencies -> StringmakeSignature { accountKey, accountName, expiryTime, permissions, protocol, resources, services, startTime, version } = let message = -- accountname + "\n" + -- signedpermissions + "\n" + -- signedservice + "\n" + -- signedresourcetype + "\n" + -- signedstart + "\n" + -- signedexpiry + "\n" + -- signedIP + "\n" + -- signedProtocol + "\n" + -- signedversion + "\n" String.join "\n" [ accountName , permissions , services , resources , startTime , expiryTime , "" , protocol , version ++ "\n" ] in accountKey |> BinaryBase64.decode |> Result.withDefault [] |> List.map (Binary.fromDecimal >> Binary.ensureSize 8) |> Binary.concat |> Hmac.encrypt64 SHA.sha256 message |> Binary.chunksOf 8 |> List.map Binary.toDecimal |> BinaryBase64.encode