The example to test how to secure Nest framework API integrate with Keycloak OAuth 2.0.
$ npm install
- Startup Keycloak Server
$ docker-compose -f ./docker/docker-compose.yml up -d
- Change to
nodejs-example
realm. - Regenerate the client
nestjs-app
secret. - Update keycloak.json with new generate the client secret.
Example:
{
"realm": "nodejs-example",
"auth-server-url": "http://localhost:9003/auth",
"resource": "nestjs-app",
"credentials": {
"secret": "%client-secret%"
},
"public-client": false
}
$ npm run start
- Get token from Keycloak.
curl --location --request POST 'http://localhost:9003/auth/realms/nodejs-example/protocol/openid-connect/token' \
--header 'Authorization: Basic %BASIC_AUTH_BASE_ON_ID_SECRET%' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials'
- Test API endpoints.
curl --location --request GET 'http://localhost:3000/users' \
--header 'Authorization: Bearer %TOKEN%'
- Test API endpoints secure with resource scopes.
curl --location --request GET 'http://localhost:3000/users/scope' \
--header 'Authorization: Bearer %TOKEN%'
Nest is MIT licensed.