Login providers
Poly Community logs members in with Google, Microsoft, Apple, Facebook and GitHub, and with any provider an owner adds over OpenID Connect without code (see Your own OpenID Connect provider). Any other provider, one that speaks plain OAuth 2 such as Discord, or an OpenID Connect provider you want ready-made with its own name and button, is a plugin of its own that adds a provider to the single sign-on part of the WordPlus SDK.
Facebook and GitHub are written this way too, with nothing your plugin cannot use.
Once added, the provider is a row under Settings → Services → Single sign-on with its mark, set up in its own dialog, and off until the owner turns it on under Settings → Joining → Logging in. Its button then shows on the login forms of the community and on the WordPress login screen, and on the sign-up forms while Let new people join with it is on.
What the part does for you
Your provider says where its endpoints are and who the member is. Everything else belongs to the part and never reaches your code:
- the start, with its state bound to the browser and PKCE,
- the way back to
/wp-json/wordplus/v1/sso/backand the exchange of the code for an access token, - finding the member by their account at the provider, or by an email address the provider vouched for and the member confirmed here, or handing someone new to the sign-up form,
- Connected accounts on the account settings of a member and in wp-admin, and disconnecting,
- the refusals and the words the login page shows for them,
- the dialog of the owner with the redirect address, and the client secret kept sealed.
Registering
function my_plugin_sso_providers( $providers ) {
if ( ! class_exists( 'WordPlus_SDK_SSO_Providers', false ) || WordPlus_SDK_SSO_Providers::API < 1 ) {
return $providers; // a copy of the SDK older than this provider: leave it out
}
require_once __DIR__ . '/class-discord-provider.php';
$providers['discord'] = new My_Discord_Provider(); // the id: [a-z0-9-], up to 40 characters, never changed
return $providers;
}
add_filter( 'wordplus_sso_providers', 'my_plugin_sso_providers' );
- Require the class inside the filter. The part loads its base class,
WordPlus_SDK_SSO_Provider, just before it runs the filter. A file that extends it at the top of your plugin would fail on a site where the part is not loaded yet. - The id is for good. The link between an account here and the account at the provider is kept under it (
wordplus_sso_discordin user meta), and so are the keys the owner enters. - Several plugins may add providers. Each adds its own id to the array and returns the array.
The class
A whole provider for Discord:
The endpoints, scopes and fields below follow the OAuth2 and user docs of Discord. The example has not been run against Discord itself. The same class, with a stand-in server, is what the tests of Poly Community log in through.
class My_Discord_Provider extends WordPlus_SDK_SSO_Provider {
public function label() {
return 'Discord';
}
public function authorize_url() {
return 'https://discord.com/oauth2/authorize';
}
public function token_url() {
return 'https://discord.com/api/oauth2/token';
}
public function scopes() {
return 'identify email';
}
public function console() {
return 'https://discord.com/developers/applications';
}
public function mark() {
// Its logo as the brand gives it, from its press kit: one SVG with a viewBox.
return '<svg viewBox="0 0 24 24"><path fill="currentColor" d="…"/></svg>';
}
public function colors() {
return array( 'background' => '#5865F2', 'border' => '#5865F2', 'text' => '#ffffff' );
}
public function dialog() {
return array(
'app' => 'Make an application in the Discord Developer Portal, add the redirect below under OAuth2, then copy its keys here',
'addresses' => array(
array( 'label' => 'Redirect', 'description' => 'Add it under OAuth2, exactly as here' ),
),
'client' => array( 'label' => 'Client ID', 'description' => 'Under OAuth2 in the Developer Portal' ),
'secret' => array( 'label' => 'Client Secret', 'description' => 'Under OAuth2: reset it and copy it at once' ),
);
}
public function identity( $token, array $tokens, $secret ) {
$me = $this->read( 'https://discord.com/api/users/@me', array( 'Authorization' => 'Bearer ' . $token ) );
if ( ! $me || empty( $me['id'] ) ) {
return new WP_Error( 'sso_profile', 'Discord did not say who you are.' );
}
$avatar = ! empty( $me['avatar'] )
? 'https://cdn.discordapp.com/avatars/' . $me['id'] . '/' . $me['avatar'] . '.png?size=512'
: '';
return array(
'sub' => $me['id'],
'email' => $me['email'] ?? '',
'verified' => true === ( $me['verified'] ?? null ),
'name' => $me['global_name'] ?? $me['username'] ?? '',
'username' => $me['username'] ?? '',
'picture' => $avatar,
);
}
}
The owner then makes the application at Discord, enters the redirect address the dialog shows, copies its client ID and secret into the dialog, and turns Discord on.
What each method says
| Method | What it returns |
|---|---|
label() | Its name, on its button (Continue with and the name) and in the list of providers. Required. |
kind() | oauth2 (the default), or oidc for a provider that speaks OpenID Connect at a fixed issuer. |
authorize_url(), token_url() | OAuth 2: where the member logs in, and where the code is exchanged. Both https. |
issuer() | OpenID Connect: the issuer, whose /.well-known/openid-configuration names the rest. https. |
scopes() | The scopes asked for, separated by spaces. openid email profile for OpenID Connect unless you say otherwise. |
pkce() | Whether the start carries PKCE (S256). True by default: leave it on unless the provider refuses it. |
token_by_get() | True for a provider that exchanges the code by GET with its fields in the address, as Facebook does. False by default. |
console() | Where the owner makes the app: the Make the app button of the dialog. |
mark() | An SVG with a viewBox, drawn at 18 px on the button and in the list. The part keeps its shapes alone: no script, style, link or event. |
colors() | background, border and text of its button, each #rgb or #rrggbb. Any one missing or wrong, and the button looks like that of a provider an owner adds. |
dialog() | The dialog in the words of the console of the provider: app, the line beside Make the app. addresses, each a label, a description and a value to copy: the redirect address when value is left out, home for the address of the site, or an address of your own. client and secret, each a label and a description. Anything left out reads as for any provider. |
identity() | OAuth 2: who the member is, read with the access token (below). |
needs() | The level of the API it was written for, 1 today. |
Who the member is
identity( $token, $tokens, $secret ) gets the access token, the whole answer of the token endpoint (a refresh token or an id it carries, for example) and the client secret, for a provider that signs its calls with it. $this->read( $url, $headers ) reads a JSON object by GET and answers null when it cannot. Answer an array, or a WP_Error when the profile cannot be read:
sub: the id of the account at the provider, the same at every login and never an email address. Required: without it there is no login.email: the address the provider gives, if any.verified:trueonly when the provider vouches that the address belongs to the account. Anything else, a string'true'included, counts as not verified.name,given,family: the names, as plain text.picture: an https address of their picture.username: offered as their username here, unless a member already has it.
The part cleans each before anything reads it. An address that is not verified never links an existing account: the member logs in another way once and connects the provider in their account settings. Someone new confirms such an address with a code on the sign-up form.
A provider over OpenID Connect
A provider that publishes /.well-known/openid-configuration needs no identity(). The part reads its ID token and checks its signature (RS256), issuer, audience, time and nonce, asks its UserInfo when the ID token leaves the address out, offers its preferred_username as the username (the part before the @ when it is an address), and sends the client secret as the provider takes it:
class My_GitLab_Provider extends WordPlus_SDK_SSO_Provider {
public function label() { return 'GitLab'; }
public function kind() { return 'oidc'; }
public function issuer() { return 'https://gitlab.com'; }
public function console() { return 'https://gitlab.com/-/user_settings/applications'; }
// mark(), colors() and dialog() as above
}
The rules it is checked by
A provider is left out, and why is kept, when:
- reserved: its id is one of the five the part carries (
google,microsoft,apple,facebook,github). - taken: an owner already added an OpenID Connect provider with that id. The links of its members are kept by the id, so the id stays with that provider.
- needs: its
needs()is higher thanWordPlus_SDK_SSO_Providers::APIon this site. - invalid: its id is not letters, numbers and hyphens, it is not a
WordPlus_SDK_SSO_Provider, it has no name, or an endpoint or issuer is not https.
WordPlus_SDK_SSO_Providers::refused() lists them while you build, by id.
When your plugin is switched off, its provider leaves the list and its button goes, and a login started at its address goes back to the login page. The keys the owner entered are kept, and switched on again, the provider is as it was.
What stays stable
The filter wordplus_sso_providers, the methods of WordPlus_SDK_SSO_Provider above and what identity() answers. A change to them comes with a new level of WordPlus_SDK_SSO_Providers::API, and a provider that asks for a level the site does not have waits for its update rather than half working.