mirror of
https://github.com/freckle/yesod-auth-oauth2.git
synced 2026-01-27 19:30:25 +01:00
Added Spotify as a supported OAuth2 backend
This commit is contained in:
parent
46b0d1f4a9
commit
a1ff864b4e
61
Yesod/Auth/OAuth2/Spotify.hs
Normal file
61
Yesod/Auth/OAuth2/Spotify.hs
Normal file
@ -0,0 +1,61 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
module Yesod.Auth.OAuth2.Spotify
|
||||
( oauth2Spotify
|
||||
, module Yesod.Auth.OAuth2
|
||||
) where
|
||||
|
||||
import Control.Applicative ((<$>), (<*>))
|
||||
import Control.Exception.Lifted
|
||||
import Control.Monad (mzero)
|
||||
import Data.Aeson
|
||||
import Data.Text (Text)
|
||||
import Data.ByteString (ByteString)
|
||||
import Data.Text.Encoding (encodeUtf8)
|
||||
import Network.HTTP.Conduit(Manager)
|
||||
import Yesod.Auth
|
||||
import Yesod.Auth.OAuth2
|
||||
|
||||
data SpotifyUser = SpotifyUser
|
||||
{ spotifyUserId :: Text
|
||||
, spotifyUserHref :: Text
|
||||
, spotifyUserDisplayName :: Text
|
||||
, spotifyProduct :: Text
|
||||
}
|
||||
|
||||
instance FromJSON SpotifyUser where
|
||||
parseJSON (Object v) = SpotifyUser <$>
|
||||
v .: "id" <*>
|
||||
v .: "href" <*>
|
||||
v .: "display_name" <*>
|
||||
v .: "product"
|
||||
parseJSON _ = mzero
|
||||
|
||||
oauth2Spotify :: YesodAuth m
|
||||
=> Text -- ^ Client ID
|
||||
-> Text -- ^ Client Secret
|
||||
-> AuthPlugin m
|
||||
oauth2Spotify clientId clientSecret = authOAuth2 "spotify"
|
||||
(OAuth2
|
||||
{ oauthClientId = encodeUtf8 clientId
|
||||
, oauthClientSecret = encodeUtf8 clientSecret
|
||||
, oauthOAuthorizeEndpoint = "https://accounts.spotify.com/authorize"
|
||||
, oauthAccessTokenEndpoint = "https://accounts.spotify.com/api/token"
|
||||
, oauthCallback = Nothing
|
||||
})
|
||||
fetchSpotifyProfile
|
||||
|
||||
fetchSpotifyProfile :: Manager -> AccessToken -> IO (Creds m)
|
||||
fetchSpotifyProfile manager token = do
|
||||
result <- authGetJSON manager token "https://api.spotify.com/v1/me"
|
||||
case result of
|
||||
Right user -> return $ toCreds user
|
||||
Left err -> throwIO $ InvalidProfileResponse "spotify" err
|
||||
|
||||
toCreds :: SpotifyUser -> Creds m
|
||||
toCreds user = Creds "spotify"
|
||||
(spotifyUserId user)
|
||||
[ ("href" , spotifyUserHref user)
|
||||
, ("display_name", spotifyUserDisplayName user)
|
||||
, ("product" , spotifyProduct user)
|
||||
]
|
||||
@ -48,6 +48,7 @@ library
|
||||
Yesod.Auth.OAuth2.Google
|
||||
Yesod.Auth.OAuth2.Learn
|
||||
Yesod.Auth.OAuth2.Github
|
||||
Yesod.Auth.OAuth2.Spotify
|
||||
|
||||
ghc-options: -Wall
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user