OAuth2 authentication for yesod
Go to file
patrick brisbin 937ad572a3 Update to LTS-9.5 and hoauth2 1.3.0
The largest changes were around the hoauth2 interface:

The OAuth2 type replaced all of its ByteString fields with either Text
or URI. This is a huge improvement. The fields that are now Text are the
type we had them in anyway. The fields that are now URI are type safe
and easier to manipulate. For example, we were doing very unsafe query
string manipulations looking for raw ? or & values, but now we can work
with tuples in a well-typed property.

Additionally the AccessToken type was upgraded to OAuth2Token with an
accessToken field, and the simple Either ByteString a results were
replaced by a real OAuth2Error type. This required changes to our
InvalidProfileResponse mechanism to support.

To make working with uri-bytestring more convenient, an Extension
library was added with some useful instances and helper functions. This
library may be upstreamed at some point.
2017-10-18 17:21:47 -04:00
example Add example application 2016-02-10 09:50:51 -05:00
test Update to LTS-9.5 and hoauth2 1.3.0 2017-10-18 17:21:47 -04:00
URI/ByteString Update to LTS-9.5 and hoauth2 1.3.0 2017-10-18 17:21:47 -04:00
Yesod/Auth Update to LTS-9.5 and hoauth2 1.3.0 2017-10-18 17:21:47 -04:00
.gitignore Add example application 2016-02-10 09:50:51 -05:00
circle.yml Update stack version for Circle CI. 2017-07-21 17:05:18 -04:00
LICENSE Initial import 2013-07-14 11:11:44 +02:00
README.md Fix code example in Readme 2017-07-17 11:52:38 -04:00
Setup.lhs Initial import 2013-07-14 11:11:44 +02:00
stack.yaml Update to LTS-9.5 and hoauth2 1.3.0 2017-10-18 17:21:47 -04:00
yesod-auth-oauth2.cabal Update to LTS-9.5 and hoauth2 1.3.0 2017-10-18 17:21:47 -04:00

Yesod.Auth.OAuth2

OAuth2 AuthPlugins for Yesod.

Basic Usage

To use one of the supported providers:

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:

import Yesod.Auth
import Yesod.Auth.OAuth2.Slack

instance YesodAuth App where
    -- ...

    authPlugins _ =
        [oauth2SlackScoped clientId clientSecret slackScopes]
      where
        slackScopes = [SlackEmailScope, SlackAvatarScope, SlackTeamScope]

clientId :: Text
clientId = "..."

clientSecret :: Text
clientSecret = "..."

Advanced Usage

To use any other provider:

import Yesod.Auth
import Yesod.Auth.OAuth2

instance YesodAuth App where
    -- ...

    authPlugins _ = [myPlugin]

myPlugin :: AuthPlugin m
myPlugin = authOAuth2 "mysite"
    (OAuth2
        { oauthClientId            = "..."
        , oauthClientSecret        = "..."
        , oauthOAuthorizeEndpoint  = "https://mysite.com/oauth/authorize"
        , oauthAccessTokenEndpoint = "https://mysite.com/oauth/token"
        , oauthCallback            = Nothing
        })
    makeCredentials

makeCredentials :: Manager -> AccessToken -> IO (Creds m)
makeCredentials manager token = do
    result <- authGetJSON manager token "https://mysite.com/api/me.json"
    return $ -- Parse the JSON into (Creds m)

If you write one of these, please consider opening a Pull Request