Simply copy and paste the following command line in your terminal to create your first Strapi project.
npx create-strapi-app
my-project
This article will cover the required steps to add a custom OAuth2/OpenID Connect provider to Strapi v4.
First things first, what are OAuth2 and OpenID Connect? OAuth2 handles authorization whereas OpenID Connect handles authentication. What’s the difference you may ask, well, OAuth2 is built to manage authorization access to APIs with scopes, and OpenID Connect is built on top of OAuth2 to manage authentication of users.
Strapi actually relies on OpenID Connect protocol to retrieve user info and create the internal Strapi user upon provider account connection.
If you want more details on what are those and the differences, I can only recommend the OAuth2 and OpenID Connect: The Professional Guide ebook by the guys behind Auth0 by Okta.
Before you can jump into this content, you need to have a basic understanding of the following.
Strapi is an open-source headless CMS based on Node.js that is used to develop and manage content using a Restful API and/or GraphQL.
With Strapi, we can scaffold our API faster and consume the content via APIs using any HTTP client or GraphQL enabled frontend.
To scaffold a new Strapi project is very simple and works precisely as installing a new frontend framework.
We are going to start by running the following commands and testing them out in our default browser.
npx create-strapi-app strapi-api --quickstart
# OR
yarn create strapi-app strapi-api --quick start
The command above will scaffold a new strapi project in the directory you specified.
Next, run yarn build
to build your app and yarn develop
to run the new project if it doesn't start automatically.
The last command will open a new tab with a page to register your new admin of the system. Go ahead and fill out the form and click on the submit button to create a new Admin.
patch-package
lets app authors instantly make and keep fixes to npm dependencies. It's a vital band-aid for those of us living on the bleeding edge.
Head over to its GitHub repo if you want to know more.
Well, Strapi manages OAuth2/OpenID Connect through a dependency, Grant. It then uses Purest to make some API calls to retrieve User information. In order to have our OAuth2/OpenID Connect Provider to work with Strapi, we then have 2 options:
I guess you figured we will go with the second option ;)
Simply follow its setup instructions
Grant is an OAuth2/OpenID Connect proxy for Node.js with 200+ preconfigured providers. More information in its GitHub repo
First, we need to check if the provider you want to use already exists in grant in the oauth.json file
node_modules/grant/config/oauth.json
file1
2
3
4
5
6
"trackmania": {
"authorize_url": "https://api.trackmania.com/oauth/authorize",
"access_url": "https://api.trackmania.com/api/access_token",
"oauth": 2,
"scope_delimiter": " "
}
node_modules/grant/config/profile.json
file1
2
3
"trackmania": {
"profile_url": "https://api.trackmania.com/api/user"
}
Execute the following command
npm patch-package grant
# OR
yarn patch-package grant
This will create the patches/grant+VERSION.patch
file where VERSION is the one you have currently installed
Purest is a REST API client library with embeded providers, think of it as an axios with preconfigured base urls and endpoints for each provider. More information on its GitHub repo
Same first step than Grant: Is your provider already listed in Purest?
node_modules/purest/config/providers.json
file1
2
3
4
5
6
7
8
9
10
11
12
"trackmania": {
"default": {
"origin": "https://api.trackmania.com",
"path": "api/{path}",
"headers": {
"authorization": "Bearer {auth}"
}
},
"oauth": {
"origin": "https://api.trackmania.com",
"path": "oauth/{path}"
}
Execute the following command
npm patch-package purest
# OR
yarn patch-package purest
This will create the patches/purest+VERSION.patch
file where VERSION is the one you have currently installed
Strapi Plugin Users Permissions
This is the Strapi’s internal way of handling Users and Permissions, in which the supported OAuth2/OpenID Connect providers are implemented
We will need to edit multiple files for the provider to work, just follow along ;)
node_modules/@strapi/plugin-users-permissions/server/bootstrap/grant-config.js
file1
2
3
4
5
6
7
8
9
10
trackmania: {
enabled: true,
icon: 'trackmania',
key: '',
secret: '',
basicHeader: '',
openPlanetSecret: '',
callback: `${baseURL}/trackmania/callback`,
scope:[]
}
node_modules/@strapi/plugin-users-permissions/server/services/providers-registry.js
file1
2
3
4
5
6
7
8
9
10
async trackmania({ accessToken }) {
const trackmania = purest({ provider: 'trackmania' });
const { body: userBody } = await trackmania.get('user').auth(accessToken).request();
// The userBody may differ from provider to provider, refer to the provider documentation for details
return {
username: userBody.displayName,
email: userBody.email
};
}
Execute the following command
npm patch-package @strapi/plugin-users-permissions
# OR
yarn patch-package @strapi/plugin-users-permissions
This will create the patches/@strapi/plugin-users-permissions+VERSION.patch
file where VERSION is the one you have currently installed
This article demonstrated how to add a custom Login Provider to Strapi v4. Here is a demo repo where you’ll find an example implemetation.
Let me know if you have any question/suggestion, I’d love to hear your feedback ;)