Google OAuth authentication #40

This commit is contained in:
Michael Snoyman 2014-11-27 14:08:18 +02:00
parent a2697b1e27
commit b0e2fbf782
3 changed files with 24 additions and 3 deletions

View File

@ -6,7 +6,7 @@ import Data.Slug (safeMakeSlug, HasGenIO (getGenIO), randomSlug, Slug,
import qualified Database.Persist import qualified Database.Persist
import Model import Model
import qualified Settings import qualified Settings
import Settings (widgetFile, Extra (..)) import Settings (widgetFile, Extra (..), GoogleAuth (..))
import Settings.Development (development) import Settings.Development (development)
import Settings.StaticFiles import Settings.StaticFiles
import qualified System.Random.MWC as MWC import qualified System.Random.MWC as MWC
@ -15,7 +15,7 @@ import Text.Jasmine (minifym)
import Types import Types
import Yesod.Auth import Yesod.Auth
import Yesod.Auth.BrowserId import Yesod.Auth.BrowserId
import Yesod.Auth.GoogleEmail import Yesod.Auth.GoogleEmail2
import Yesod.Core.Types (Logger, GWData) import Yesod.Core.Types (Logger, GWData)
import Yesod.Default.Config import Yesod.Default.Config
import Yesod.Default.Util (addStaticContentExternal) import Yesod.Default.Util (addStaticContentExternal)
@ -218,7 +218,13 @@ instance YesodAuth App where
Just _ -> getHandle (cnt + 1) Just _ -> getHandle (cnt + 1)
-- You can add other plugins like BrowserID, email or OAuth here -- You can add other plugins like BrowserID, email or OAuth here
authPlugins _ = [authBrowserId def, authGoogleEmail] authPlugins app =
authBrowserId def : google
where
google =
case googleAuth $ appExtra $ settings app of
Nothing -> []
Just GoogleAuth {..} -> [authGoogleEmail gaClientId gaClientSecret]
authHttpManager = httpManager authHttpManager = httpManager
instance YesodAuthPersist App instance YesodAuthPersist App

View File

@ -68,6 +68,7 @@ data Extra = Extra
{ storeConfig :: !BlobStoreConfig { storeConfig :: !BlobStoreConfig
, hackageRoot :: !HackageRoot , hackageRoot :: !HackageRoot
, adminUsers :: !(HashSet Text) , adminUsers :: !(HashSet Text)
, googleAuth :: !(Maybe GoogleAuth)
} }
deriving Show deriving Show
@ -76,6 +77,7 @@ parseExtra _ o = Extra
<$> o .: "blob-store" <$> o .: "blob-store"
<*> (HackageRoot <$> o .: "hackage-root") <*> (HackageRoot <$> o .: "hackage-root")
<*> o .:? "admin-users" .!= mempty <*> o .:? "admin-users" .!= mempty
<*> o .:? "google-auth"
data BlobStoreConfig = BSCFile !FilePath data BlobStoreConfig = BSCFile !FilePath
| BSCAWS !FilePath !Text !Text !Text !Text | BSCAWS !FilePath !Text !Text !Text !Text
@ -95,3 +97,13 @@ instance FromJSON BlobStoreConfig where
<*> o .: "secret" <*> o .: "secret"
<*> o .: "bucket" <*> o .: "bucket"
<*> o .:? "prefix" .!= "" <*> o .:? "prefix" .!= ""
data GoogleAuth = GoogleAuth
{ gaClientId :: !Text
, gaClientSecret :: !Text
}
deriving Show
instance FromJSON GoogleAuth where
parseJSON = withObject "GoogleAuth" $ \o -> GoogleAuth
<$> o .: "client-id"
<*> o .: "client-secret"

View File

@ -5,6 +5,9 @@ Default: &defaults
hackage-root: http://hackage.fpcomplete.com hackage-root: http://hackage.fpcomplete.com
admin-users: admin-users:
- fpcomplete - fpcomplete
# google-auth:
# client-id: foo
# client-secret: bar
Development: Development:
<<: *defaults <<: *defaults