OAuth2 authentication for yesod
Go to file
patrick brisbin 573b7b01a3 Disable 8.0.2 CI job
I just can't get the older LTS to solve after updating for yesod-1.6. I
will not in the CHANGELOG that, from this point, it may work on that
version, but we aren't explicitly testing it anymore.

8.4 is about to release, so it seems OK to start phasing out anyway, if
we're following a reasonable "current and one back" policy.
2018-04-21 10:49:05 -04:00
.circleci Disable 8.0.2 CI job 2018-04-21 10:49:05 -04:00
example Fixup example 2018-04-21 10:49:05 -04:00
src Update LTS and dependencies 2018-04-21 10:49:05 -04:00
test Project setup files 2018-01-23 10:16:22 -05:00
.env.example Bring back example application 2018-02-13 08:59:01 -05:00
.gitignore Bring back example application 2018-02-13 08:59:01 -05:00
.hlint.yaml Address HLint issues 2018-01-23 10:16:22 -05:00
.stylish-haskell.yaml Project setup files 2018-01-23 10:16:22 -05:00
CHANGELOG.md Version bump 2018-03-27 19:12:31 -04:00
LICENSE Correct license information 2018-01-26 13:58:16 -05:00
Makefile Add clean target 2018-03-27 19:14:01 -04:00
package.yaml Fixup example 2018-04-21 10:49:05 -04:00
README.md Make extras accessors safer 2018-02-12 09:10:34 -08:00
Setup.lhs Initial import 2013-07-14 11:11:44 +02:00
stack-lts-9.21.yaml Add newer deps to 8.0.2 build 2018-04-21 10:49:05 -04:00
stack-lts-10.3.yaml Further configure 8.2.2 build 2018-04-21 10:49:05 -04:00
stack.yaml Update LTS and dependencies 2018-04-21 10:49:05 -04:00

Yesod.Auth.OAuth2

OAuth2 AuthPlugins for Yesod.

Usage

import Yesod.Auth
import Yesod.Auth.OAuth2.Github

instance YesodAuth App where
    -- ...

    authPlugins _ = [oauth2Github clientId clientSecret]

clientId :: Text
clientId = "..."

clientSecret :: Text
clientSecret = "..."

Some plugins, such as GitHub and Slack, have scoped functions for requesting additional information:

oauth2SlackScoped [SlackBasicScope, SlackEmailScope] clientId clientSecret

Working with Extra Data

We put the minimal amount of user data possible in credsExtra -- just enough to support you parsing or fetching additional data yourself.

For example, if you work with GitHub and GitHub user profiles, you likely already have a model and a way to parse the /user response. Rather than duplicate all that in our own library, we try to make it easy for you to re-use that code yourself:

authenticate creds = do
    let
        -- You can run your own FromJSON parser on the respose we already have
        eGitHubUser :: Either String GitHubUser
        eGitHubUser = getUserResponseJSON creds

        -- Avert your eyes, simplified example
        Just accessToken = getAccessToken creds
        Right githubUser = eGitHubUser

    -- Or make followup requests using our access token
    runGitHub accessToken $ userRepositories githubUser

    -- Or store it for later
    insert User
        { userIdent = credsIdent creds
        , userAccessToken = accessToken
        }

NOTE: Avoid looking up values in credsExtra yourself; prefer the provided get functions. The data representation itself is no longer considered public API.

Local Providers

If we don't supply a "Provider" (e.g. GitHub, Google, etc) you need. You can write your own within your codebase:

import Yesod.Auth.OAuth2.Prelude

pluginName :: Text
pluginName = "mysite"

oauth2MySite :: YesodAuth m => Text -> Text -> AuthPlugin m
oauth2MySite clientId clientSecret =
    authOAuth2 pluginName oauth2 $ \manager token -> do
        -- Fetch a profile using the manager and token, leave it a ByteString
        userResponse <- -- ...

        -- Parse it to your preferred identifier, e.g. with Data.Aeson
        userId <- -- ...

        -- See authGetProfile for the typical case

        pure Creds
            { credsPlugin = pluginName
            , credsIdent = userId
            , credsExtra = setExtra token userResponse
            }
  where
    oauth2 = OAuth2
        { oauthClientId = clientId
        , oauthClientSecret = clientSecret
        , oauthOAuthorizeEndpoint = "https://mysite.com/oauth/authorize"
        , oauthAccessTokenEndpoint = "https://mysite.com/oauth/token"
        , oauthCallback = Nothing
        }

The Prelude module is considered public API, though we may build something higher-level that is more convenient for this use-case in the future.

Development & Tests

stack setup
stack build --dependencies-only
stack build --pedantic --test

Please also run HLint and Weeder before submitting PRs.


CHANGELOG | LICENSE