Social login (OAuth)

The plugin oauth (_class('oauth'), yf_oauth) lets visitors log in with an account of another service: GitHub, Google, Facebook, LinkedIn, Microsoft (live), Twitter, Bitbucket, Dropbox and others. The first login creates a site user linked to that account; a visitor who is already logged in links the account to their own user.

Providers

A provider is a driver classes/oauth/yf_oauth_driver_<name>.class.php. The framework ships: bitbucket, bitly, box, disqus, dropbox, facebook, flickr, foursquare, github, google, heroku, instagram, linkedin, live, steamcommunity, twitter, vimeo, wordpress. A plugin or the site adds a driver with a file of the same name in its own classes/oauth/; it extends yf_oauth_driver2 (OAuth 2) or yf_oauth_driver1 (OAuth 1). Whether a provider's API still matches its driver is up to that provider: test the login of each provider you enable.

Configuration

Register an application with the provider, set its callback URL (below), and put its client id and secret in the site config, in the global $oauth_config. Only providers with both values (or openid) are offered:

$oauth_config = [
    'github' => ['client_id' => getenv('GITHUB_CLIENT_ID'), 'client_secret' => getenv('GITHUB_CLIENT_SECRET')],
    'google' => [
        'client_id' => getenv('GOOGLE_CLIENT_ID'),
        'client_secret' => getenv('GOOGLE_CLIENT_SECRET'),
        'redirect_uri' => 'https://example.com/login_form/oauth/google',   // optional: pin the callback URL
    ],
];

Optional keys of an OAuth 2 provider: scope (replaces the driver's scope), redirect_uri (pins the callback URL instead of building it from the current host), pkce (false turns PKCE off for a driver that sends it) and email_scope (asks for the scope that returns the e-mail, overriding REQUEST_EMAIL_SCOPE). Keep the secrets out of the code: the environment or a secrets store.

The login page and the callback

The login runs in the user module login_form: ./?object=login_form&action=oauth&id=github (/login_form/oauth/github with pretty URLs) sends the visitor to the provider, and the provider sends them back to the same URL, which is the callback URL to register. user_profile and profile take the same action for linking an account from the profile.

The login form shows the configured providers as icon links when login_form.OAUTH_ICONS is on (default off: an empty list, as before). OAUTH_ICON_MAP gives a Font Awesome icon per provider name, OAUTH_ICON_DEFAULT (fa-sign-in) the others.

$PROJECT_CONF['login_form']['OAUTH_ICONS'] = true;

Accounts

  • Each provider account is a row of oauth_users (provider, provider user id, the site user).
  • A first login creates an active site user (group 2) with a random password; code that calls _class('oauth')->login($provider, ['set_user_info' => $callback]) creates the user itself (the callback gets the provider's user info and returns the new user id). Without a login from the provider the login is oauth.<provider>.<id>; without an e-mail the e-mail is that login at the site's host, a placeholder.
  • A visitor who is logged in when they come back from the provider gets the account linked to their user.
  • Events: oauth.before_login, oauth.insert, oauth.user_added, and the usual user.registered (source oauth) and user.login (events).

Settings

SettingDefaultWhat it does
oauth.CHECK_STATEtrueAn OAuth 2 callback must carry the random, single-use state this session started (OAuth 1: the request token); otherwise it is refused. Turn it off only when the session cookie does not reach the callback (SameSite=Strict).
oauth.TLS_VERIFYtrueVerify the provider's TLS certificate on the token and user info requests. Off only on a server without a CA bundle.
oauth.EMAIL_VERIFIED_ONLYfalse; 4.0: trueUse the provider's e-mail (the new user, linking) only when the provider marks it verified; otherwise the placeholder. Tools / Settings, Escaping tab.
oauth.REQUEST_EMAIL_SCOPEfalse; 4.0: trueAsk Facebook (email) and WordPress (auth) for the scope that returns the e-mail. Tools / Settings, New features tab.
login_form.OAUTH_ICONSfalseProvider icons on the login form.

Security: an e-mail the provider did not verify can be anyone's. With EMAIL_VERIFIED_ONLY off it is stored on the new user as given, so do not trust it for anything (password resets, matching existing users) until you turn the flag on. Register exact callback URLs with the providers, over HTTPS.