A music player that connects to your cloud/distributed storage. diffuse.sh
Something went wrong. Try again.
1.9 kB · 79 lines
Elm
at commit bce88033
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980module Authentication.RemoteStorage exposing (RemoteStorage, oauthAddress, parseUserAddress, userAddressError, webfingerAddress, webfingerDecoder, webfingerError, webfingerRequest)
import Base64import Httpimport Json.Decode as Decode exposing (Decoder)import Url
-- 🌳
type alias RemoteStorage = { 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."
-- 🔱
parseUserAddress : String -> Maybe RemoteStorageparseUserAddress str = case String.split "@" str of [ u, h ] -> Just { host = h, username = u }
_ -> Nothing
oauthAddress : { oauthOrigin : String, origin : String } -> RemoteStorage -> StringoauthAddress { oauthOrigin, origin } { host, username } = let ua = (username ++ "@" ++ host) |> Base64.encode |> Url.percentEncode in String.concat [ oauthOrigin , "?redirect_uri=" ++ Url.percentEncode (origin ++ "/authenticate/remotestorage/" ++ ua) , "&client_id=" ++ Url.percentEncode origin , "&scope=" ++ Url.percentEncode "diffuse-v2:rw" , "&response_type=token" ]
webfingerAddress : RemoteStorage -> StringwebfingerAddress { host, username } = "https://" ++ host ++ "/.well-known/webfinger?resource=acct:" ++ username
webfingerDecoder : Decoder StringwebfingerDecoder = Decode.at [ "links" , "0" , "properties" , "http://tools.ietf.org/html/rfc6749#section-4.2" ] Decode.string
webfingerRequest : (RemoteStorage -> Result Http.Error String -> msg) -> RemoteStorage -> Cmd msgwebfingerRequest toMsg rs = Http.get { url = webfingerAddress rs , expect = Http.expectJson (toMsg rs) webfingerDecoder }