Skip to content

Commit ca48854

Browse files
authored
Merge pull request #159 from devforth/keycloack-oauth-adapter
docs: Add Keycloak and GitHub OAuth adapters to documentation
2 parents c55bc2c + 9d79cff commit ca48854

File tree

4 files changed

+182
-2
lines changed

4 files changed

+182
-2
lines changed

adminforth/documentation/docs/tutorial/05-Plugins/11-oauth.md

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,116 @@ plugins: [
174174
]
175175
```
176176

177+
### GitHub Adapter
178+
179+
Install Adapter:
180+
181+
```
182+
npm install @adminforth/github-oauth-adapter --save
183+
```
184+
185+
186+
1. Go to the [GitHub Apps](https://github.com/settings/apps)
187+
2. Create a new app or select an existing one
188+
3. Go to the `Permisiions & events` -> `Account permissions` -> `Email addresses` and change to `Read-only`
189+
3. Go to the `General` and click to `Generate a new client secret` button and copy secret
190+
4. Add the credentials to your `.env` file:
191+
192+
```bash
193+
GITHUB_OAUTH_CLIENT_ID=your_facebook_client_id
194+
GITHUB_OAUTH_CLIENT_SECRET=your_facebook_client_secret
195+
```
196+
197+
Add the adapter to your plugin configuration:
198+
199+
```typescript title="./resources/adminuser.ts"
200+
import AdminForthAdapterGithubOauth2 from '@adminforth/github-oauth-adapter';
201+
202+
// ... existing resource configuration ...
203+
plugins: [
204+
new OAuthPlugin({
205+
adapters: [
206+
...
207+
new AdminForthAdapterGithubOauth2({
208+
clientID: process.env.GITHUB_OAUTH_CLIENT_ID,
209+
clientSecret: process.env.GITHUB_OAUTH_CLIENT_SECRET,
210+
}),
211+
],
212+
}),
213+
]
214+
```
215+
216+
### Kaycloack Adapter
217+
218+
Install Adapter:
219+
220+
```
221+
npm install @adminforth/keycloak-oauth-adapter --save
222+
```
223+
224+
1. Update your .env file with the following Keycloak configuration:
225+
226+
```bash
227+
KEYCLOAK_CLIENT_ID=adminforth-client
228+
KEYCLOAK_CLIENT_SECRET=1234567890
229+
KEYCLOAK_URL=http://localhost:8080
230+
KEYCLOAK_REALM=AdminforthRealm
231+
```
232+
233+
2. Start Keycloak with Docker:
234+
235+
```bash
236+
docker compose -p af-dev-demo -f inventory.yml up -d --build --remove-orphans --wait
237+
```
238+
239+
3. Create a new user from the back office with the same email as the test user (by default: ```testuser@example.com```)
240+
241+
Add the adapter to your plugin configuration:
242+
243+
```typescript title="./resources/adminuser.ts"
244+
import AdminForthAdapterKeycloakOauth2 from '@adminforth/keycloak-oauth-adapter';
245+
246+
// ... existing resource configuration ...
247+
plugins: [
248+
new OAuthPlugin({
249+
adapters: [
250+
...
251+
new AdminForthAdapterKeycloakOauth2({
252+
clientID: process.env.KEYCLOAK_CLIENT_ID,
253+
clientSecret: process.env.KEYCLOAK_CLIENT_SECRET,
254+
keycloakUrl: process.env.KEYCLOAK_URL,
255+
realm: process.env.KEYCLOAK_REALM,
256+
useOpenID: true,
257+
}),
258+
],
259+
}),
260+
]
261+
```
262+
263+
If you want to create a new user:
264+
265+
1. Access Keycloak Admin Panel
266+
Open a browser and go to: http://localhost:8080
267+
Log in with the default Keycloak admin credentials:
268+
Username: admin
269+
Password: admin
270+
271+
2. Navigate to the "Users" Section
272+
Click on "Users" in the left-hand menu.
273+
Click "Add user" in the top-right corner.
274+
275+
3. Enter User Information
276+
Username: username
277+
Email: username@example.com
278+
Email Verified: ✅ (Check this box)
279+
Enabled: ✅ (Check this box)
280+
Click "Save"
281+
282+
4. Set a Password for the User
283+
Go to the "Credentials" tab.
284+
Enter a password (e.g., testpassword).
285+
Uncheck "Temporary" (so the user doesn't have to reset the password).
286+
Click "Set Password".
177287

178288
### Microsoft Adapter
179289

@@ -224,4 +334,11 @@ Just fork any existing adapter e.g. [Google](https://github.com/devforth/adminfo
224334

225335
This is really easy, you have to change several then 10 lines of code in this [file](https://github.com/devforth/adminforth-google-oauth-adapter/blob/main/index.ts)
226336

227-
Then just publish it to npm and install it in your project.
337+
Then just publish it to npm and install it in your project.
338+
339+
340+
Links to adapters:
341+
[Google](https://github.com/devforth/adminforth-google-oauth-adapter)
342+
[GitHub](https://github.com/devforth/adminforth-github-oauth-adapter)
343+
[Facebook](https://github.com/devforth/adminforth-facebook-oauth-adapter)
344+
[Keycloak](https://github.com/devforth/adminforth-keycloak-oauth-adapter)

dev-demo/inventory.yml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,34 @@ services:
5555
volumes:
5656
- mysql-data:/var/lib/mysql
5757

58+
keycloak:
59+
image: quay.io/keycloak/keycloak:latest
60+
command: start-dev --import-realm
61+
environment:
62+
- KEYCLOAK_ADMIN=admin
63+
- KEYCLOAK_ADMIN_PASSWORD=admin
64+
- DB_VENDOR=postgres
65+
- DB_ADDR=pg
66+
- DB_DATABASE=demo
67+
- DB_USER=demo
68+
- DB_PASSWORD=demo
69+
- KEYCLOAK_IMPORT=/opt/keycloak/data/import/keycloak-realm.json
70+
- KEYCLOAK_CLIENT_ID=${KEYCLOAK_CLIENT_ID}
71+
- KEYCLOAK_CLIENT_SECRET=${KEYCLOAK_CLIENT_SECRET}
72+
- KEYCLOAK_URL=${KEYCLOAK_URL}
73+
- KEYCLOAK_REALM=${KEYCLOAK_REALM}
74+
ports:
75+
- "8080:8080"
76+
depends_on:
77+
- pg
78+
volumes:
79+
- keycloak-data:/opt/keycloak/data
80+
- ./keycloak-realm.json:/opt/keycloak/data/import/keycloak-realm.json
81+
82+
5883
volumes:
5984
clickhouse-data:
6085
pg-data:
6186
mongo-data1:
62-
mysql-data:
87+
mysql-data:
88+
keycloak-data:

dev-demo/keycloak-realm.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"id": "AdminforthRealm",
3+
"realm": "AdminforthRealm",
4+
"enabled": true,
5+
"users": [
6+
{
7+
"username": "testuser",
8+
"enabled": true,
9+
"credentials": [
10+
{
11+
"type": "password",
12+
"value": "testpassword"
13+
}
14+
],
15+
"email": "testuser@example.com",
16+
"emailVerified": true
17+
}
18+
],
19+
"clients": [
20+
{
21+
"clientId": "adminforth-client",
22+
"enabled": true,
23+
"publicClient": true,
24+
"redirectUris": ["*"],
25+
"defaultClientScopes": ["openid", "email", "profile"]
26+
}
27+
]
28+
}
29+

dev-demo/resources/users.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import AdminForthAdapterGoogleOauth2 from "../../adapters/adminforth-google-oaut
1717
import AdminForthAdapterGithubOauth2 from "../../adapters/adminforth-github-oauth-adapter";
1818

1919
import AdminForthAdapterFacebookOauth2 from "../../adapters/adminforth-facebook-oauth-adapter";
20+
import AdminForthAdapterKeycloakOauth2 from "../../adapters/adminforth-keycloak-oauth-adapter";
2021
import AdminForthAdapterMicrosoftOauth2 from "../../adapters/adminforth-microsoft-oauth-adapter";
22+
2123
export default {
2224
dataSource: "maindb",
2325
table: "users",
@@ -103,6 +105,12 @@ export default {
103105
clientID: process.env.FACEBOOK_CLIENT_ID,
104106
clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
105107
}),
108+
new AdminForthAdapterKeycloakOauth2({
109+
clientID: process.env.KEYCLOAK_CLIENT_ID,
110+
clientSecret: process.env.KEYCLOAK_CLIENT_SECRET,
111+
keycloakUrl: process.env.KEYCLOAK_URL,
112+
realm: process.env.KEYCLOAK_REALM,
113+
}),
106114
new AdminForthAdapterMicrosoftOauth2({
107115
clientID: process.env.MICROSOFT_CLIENT_ID,
108116
clientSecret: process.env.MICROSOFT_CLIENT_SECRET,

0 commit comments

Comments
 (0)