mirror of
https://github.com/freckle/yesod-auth-oauth2.git
synced 2026-09-10 11:34:55 +02:00
Refactor Spotify
- Scopes argument is now in first position - Scopes list is given as Text values
This commit is contained in:
parent
fd08813517
commit
46f6a9118d
@ -10,21 +10,17 @@ module Yesod.Auth.OAuth2.Spotify
|
|||||||
) where
|
) where
|
||||||
|
|
||||||
#if __GLASGOW_HASKELL__ < 710
|
#if __GLASGOW_HASKELL__ < 710
|
||||||
import Control.Applicative ((<$>), (<*>), pure)
|
import Control.Applicative ((<$>), (<*>))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
import Control.Exception.Lifted
|
|
||||||
import Control.Monad (mzero)
|
import Control.Monad (mzero)
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import Data.ByteString (ByteString)
|
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
|
import Data.Monoid ((<>))
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import Data.Text.Encoding (encodeUtf8)
|
|
||||||
import Network.HTTP.Conduit(Manager)
|
|
||||||
import Yesod.Auth
|
import Yesod.Auth
|
||||||
import Yesod.Auth.OAuth2
|
import Yesod.Auth.OAuth2
|
||||||
|
|
||||||
import qualified Data.ByteString as B
|
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
|
||||||
data SpotifyUserImage = SpotifyUserImage
|
data SpotifyUserImage = SpotifyUserImage
|
||||||
@ -66,34 +62,23 @@ instance FromJSON SpotifyUser where
|
|||||||
parseJSON _ = mzero
|
parseJSON _ = mzero
|
||||||
|
|
||||||
oauth2Spotify :: YesodAuth m
|
oauth2Spotify :: YesodAuth m
|
||||||
=> Text -- ^ Client ID
|
=> [Text] -- ^ Scopes
|
||||||
|
-> Text -- ^ Client ID
|
||||||
-> Text -- ^ Client Secret
|
-> Text -- ^ Client Secret
|
||||||
-> [ByteString] -- ^ Scopes
|
|
||||||
-> AuthPlugin m
|
-> AuthPlugin m
|
||||||
oauth2Spotify clientId clientSecret scope = authOAuth2 "spotify"
|
oauth2Spotify scopes = oauth2Plugin OAuth2Plugin
|
||||||
OAuth2
|
{ oapName = "spotify"
|
||||||
{ oauthClientId = encodeUtf8 clientId
|
, oapAuthEndpoint = "https://accounts.spotify.com/authorize?scope=" <> T.intercalate "%20" scopes
|
||||||
, oauthClientSecret = encodeUtf8 clientSecret
|
, oapTokenEndpoint = "https://accounts.spotify.com/api/token"
|
||||||
, oauthOAuthorizeEndpoint = B.append "https://accounts.spotify.com/authorize?scope=" (B.intercalate "%20" scope)
|
, oapFetchProfile = \manager token ->
|
||||||
, oauthAccessTokenEndpoint = "https://accounts.spotify.com/api/token"
|
authGetJSON manager token "https://api.spotify.com/v1/me"
|
||||||
, oauthCallback = Nothing
|
, oapToCredsIdent = spotifyUserId
|
||||||
}
|
, oapToCredsExtra = toCredsExtra
|
||||||
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
|
|
||||||
{ credsPlugin = "spotify"
|
|
||||||
, credsIdent = spotifyUserId user
|
|
||||||
, credsExtra = mapMaybe getExtra extrasTemplate
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toCredsExtra :: SpotifyUser -> [(Text, Text)]
|
||||||
|
toCredsExtra user = mapMaybe getExtra extrasTemplate
|
||||||
|
|
||||||
where
|
where
|
||||||
userImage :: Maybe SpotifyUserImage
|
userImage :: Maybe SpotifyUserImage
|
||||||
userImage = spotifyUserImages user >>= listToMaybe
|
userImage = spotifyUserImages user >>= listToMaybe
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user