A music player that connects to your cloud/distributed storage. diffuse.sh
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114module User.Layer.Methods.RemoteStorage exposing (Attributes, oauthAddress, parseUserAddress, userAddressError, webfingerAddress, webfingerDecoder, webfingerError, webfingerRequest)
import Base64import Httpimport Json.Decode as Decode exposing (Decoder)import Urlimport UrlBase64
-- 🌳
type alias Attributes = { host : String , username : String }
userAddressError = "Please provide a valid RemoteStorage address, the format is **user@server**"
webfingerError = "**Failed to connect** to the given RemoteStorage server, maybe a typo?"
-- 🔱
parseUserAddress : String -> Maybe AttributesparseUserAddress str = case String.split "@" str of [ u, h ] -> Just { host = h, username = u }
_ -> Nothing
oauthAddress : { oauthOrigin : String, origin : String } -> Attributes -> StringoauthAddress { oauthOrigin, origin } { host, username } = let hostWithoutProtocol = host |> String.split "://" |> List.drop 1 |> List.head |> Maybe.withDefault host
ua = (username ++ "@" ++ hostWithoutProtocol) |> UrlBase64.encode (Base64.encode >> Ok) |> Result.withDefault "BASE64_ENCODING_FAILED" in String.concat [ oauthOrigin , "?redirect_uri=" ++ Url.percentEncode (origin ++ "?action=authenticate/remotestorage/" ++ ua) , "&client_id=" ++ Url.percentEncode origin , "&scope=" ++ Url.percentEncode "diffuse:rw" , "&response_type=token" ]
webfingerAddress : Url.Protocol -> Attributes -> StringwebfingerAddress originProtocol { host, username } = let fallbackProtocol = case originProtocol of Url.Http -> "http"
Url.Https -> "https"
protocol = if String.contains "://" host then host |> String.split "://" |> List.head |> Maybe.withDefault fallbackProtocol
else fallbackProtocol
hostWithoutProtocol = host |> String.split "://" |> List.drop 1 |> List.head |> Maybe.withDefault host in protocol ++ "://" ++ hostWithoutProtocol ++ "/.well-known/webfinger?resource=acct:" ++ Url.percentEncode (username ++ "@" ++ hostWithoutProtocol)
webfingerDecoder : Decoder StringwebfingerDecoder = Decode.at [ "links" , "0" , "properties" , "http://tools.ietf.org/html/rfc6749#section-4.2" ] Decode.string
webfingerRequest : (Attributes -> Result Http.Error String -> msg) -> Url.Protocol -> Attributes -> Cmd msgwebfingerRequest toMsg originProtocol rs = Http.get { url = webfingerAddress originProtocol rs , expect = Http.expectJson (toMsg rs) webfingerDecoder }