From 6b3c6af8952e935488fdb84c0aefeafaf08cd93a Mon Sep 17 00:00:00 2001 From: patrick brisbin Date: Sat, 27 Jan 2018 11:55:37 -0500 Subject: [PATCH] Remove extras from Spotify --- src/Yesod/Auth/OAuth2/Spotify.hs | 89 +++++++------------------------- 1 file changed, 19 insertions(+), 70 deletions(-) diff --git a/src/Yesod/Auth/OAuth2/Spotify.hs b/src/Yesod/Auth/OAuth2/Spotify.hs index 8c6bf1e..b16829d 100644 --- a/src/Yesod/Auth/OAuth2/Spotify.hs +++ b/src/Yesod/Auth/OAuth2/Spotify.hs @@ -9,84 +9,33 @@ module Yesod.Auth.OAuth2.Spotify import Yesod.Auth.OAuth2.Prelude -import Data.Maybe -import qualified Data.Text as T +newtype User = User Text -data SpotifyUserImage = SpotifyUserImage - { spotifyUserImageHeight :: Maybe Int - , spotifyUserImageWidth :: Maybe Int - , spotifyUserImageUrl :: Text - } +instance FromJSON User where + parseJSON = withObject "User" $ \o -> User + <$> o .: "id" -instance FromJSON SpotifyUserImage where - parseJSON = withObject "SpotifyUserImage" $ \v -> SpotifyUserImage - <$> v .:? "height" - <*> v .:? "width" - <*> v .: "url" +pluginName :: Text +pluginName = "spotify" -data SpotifyUser = SpotifyUser - { spotifyUserId :: Text - , spotifyUserHref :: Text - , spotifyUserUri :: Text - , spotifyUserDisplayName :: Maybe Text - , spotifyUserProduct :: Maybe Text - , spotifyUserCountry :: Maybe Text - , spotifyUserEmail :: Maybe Text - , spotifyUserImages :: Maybe [SpotifyUserImage] - } +oauth2Spotify :: YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m +oauth2Spotify scopes clientId clientSecret = + authOAuth2 pluginName oauth2 $ \manager token -> do + (User userId, userResponseJSON) <- + authGetProfile pluginName manager token "https://api.spotify.com/v1/me" -instance FromJSON SpotifyUser where - parseJSON = withObject "SpotifyUser" $ \v -> SpotifyUser - <$> v .: "id" - <*> v .: "href" - <*> v .: "uri" - <*> v .:? "display_name" - <*> v .:? "product" - <*> v .:? "country" - <*> v .:? "email" - <*> v .:? "images" - -oauth2Spotify :: YesodAuth m - => Text -- ^ Client ID - -> Text -- ^ Client Secret - -> [Text] -- ^ Scopes - -> AuthPlugin m -oauth2Spotify clientId clientSecret scope = authOAuth2 "spotify" - OAuth2 + pure Creds + { credsPlugin = pluginName + , credsIdent = userId + , credsExtra = setExtra token userResponseJSON + } + where + oauth2 = OAuth2 { oauthClientId = clientId , oauthClientSecret = clientSecret , oauthOAuthorizeEndpoint = "https://accounts.spotify.com/authorize" `withQuery` - [ ("scope", encodeUtf8 $ T.intercalate " " scope) + [ scopeParam " " scopes ] , oauthAccessTokenEndpoint = "https://accounts.spotify.com/api/token" , oauthCallback = Nothing } - $ fromProfileURL "spotify" "https://api.spotify.com/v1/me" toCreds - -toCreds :: SpotifyUser -> Creds m -toCreds user = Creds - { credsPlugin = "spotify" - , credsIdent = spotifyUserId user - , credsExtra = mapMaybe getExtra extrasTemplate - } - - where - userImage :: Maybe SpotifyUserImage - userImage = spotifyUserImages user >>= listToMaybe - - userImagePart :: (SpotifyUserImage -> Maybe a) -> Maybe a - userImagePart getter = userImage >>= getter - - extrasTemplate = [ ("href", Just $ spotifyUserHref user) - , ("uri", Just $ spotifyUserUri user) - , ("display_name", spotifyUserDisplayName user) - , ("product", spotifyUserProduct user) - , ("country", spotifyUserCountry user) - , ("email", spotifyUserEmail user) - , ("image_url", spotifyUserImageUrl <$> userImage) - , ("image_height", T.pack . show <$> userImagePart spotifyUserImageHeight) - , ("image_width", T.pack . show <$> userImagePart spotifyUserImageWidth) - ] - - getExtra :: (Text, Maybe Text) -> Maybe (Text, Text) - getExtra (key, val) = fmap ((,) key) val