Skip to content

Commit 4762e95

Browse files
committed
add zitadel tests
1 parent 2481a98 commit 4762e95

File tree

5 files changed

+122
-5
lines changed

5 files changed

+122
-5
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Test Google OAuth
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
push:
7+
branches: [main]
8+
workflow_dispatch:
9+
10+
jobs:
11+
test-google-oauth:
12+
runs-on: ubuntu-latest
13+
env:
14+
##########################################################################
15+
# App Config
16+
##########################################################################
17+
NEXT_PUBLIC_URL: "http://localhost:3000"
18+
DATABASE_URI: "file:./payload-oauth2.db"
19+
PAYLOAD_SECRET: "hellohereisasecretforyou"
20+
21+
##########################################################################
22+
# Google OAuth Config
23+
##########################################################################
24+
GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }}
25+
GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }}
26+
27+
##########################################################################
28+
# Test Config
29+
##########################################################################
30+
# Optional: Set to "true" to run test browser in headless mode
31+
HEADLESS: true
32+
33+
##########################################################################
34+
# Google Test Account
35+
##########################################################################
36+
# Required: Google Test Account Email
37+
GOOGLE_TEST_EMAIL: ${{ secrets.GOOGLE_TEST_EMAIL }}
38+
39+
# Required: Google Test Account Password
40+
GOOGLE_TEST_PASSWORD: ${{ secrets.GOOGLE_TEST_PASSWORD }}
41+
42+
steps:
43+
- uses: actions/checkout@v4
44+
- uses: pnpm/action-setup@v2
45+
with:
46+
version: 9
47+
- uses: actions/setup-node@v4
48+
with:
49+
node-version: "20"
50+
cache: "pnpm"
51+
- run: |
52+
pnpx puppeteer browsers install chrome
53+
- run: pnpm install
54+
- run: pnpm test:google
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Test OAuth
1+
name: Test Zitadel OAuth
22

33
on:
44
pull_request:

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 Payload
3+
Copyright (c) 2024 Wilson Le
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121

2222
Technically this plugin should work with all generic OAuth2 providers. Here are the list of providers that have been tested:
2323

24-
| Provider | Status | Example |
25-
| -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------ |
26-
| Google | [![Test Google OAuth](https://github.com/WilsonLe/payload-oauth2/actions/workflows/test-google-oauth.yml/badge.svg)](https://github.com/WilsonLe/payload-oauth2/actions/workflows/test-google-oauth.yml) | [Config](./examples/google.md) |
24+
| Provider | Status | Example |
25+
| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------ |
26+
| Google | [![Test Google OAuth](https://github.com/WilsonLe/payload-oauth2/actions/workflows/test-google-oauth.yml/badge.svg)](https://github.com/WilsonLe/payload-oauth2/actions/workflows/test-google-oauth.yml) | [Config](./examples/google.md) |
27+
| Zitadel | [![Test Zitadel OAuth](https://github.com/WilsonLe/payload-oauth2/actions/workflows/test-zitadel-oauth.yml/badge.svg)](https://github.com/WilsonLe/payload-oauth2/actions/workflows/test-zitadel-oauth.yml) | [Config](./examples/google.md) |
2728

2829
# Installation
2930

examples/zitadel.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Google OAuth2
2+
3+
```ts
4+
export default buildConfig({
5+
// ...
6+
admin: {
7+
importMap: { baseDir: path.resolve(dirname) },
8+
components: {
9+
// A simple button with <a> tag that links to your authorization path
10+
// which defaults to /api/users/oauth/authorize
11+
afterLogin: ["app/components/OAuthLoginButton#OAuthLoginButton"],
12+
},
13+
user: "users", // assuming you already have a users collection with auth enabled
14+
},
15+
// ...
16+
plugins: [
17+
OAuth2Plugin({
18+
enabled:
19+
typeof process.env.ZITADEL_CLIENT_ID === "string" &&
20+
typeof process.env.ZITADEL_CLIENT_SECRET === "string" &&
21+
typeof process.env.ZITADEL_TOKEN_ENDPOINT === "string" &&
22+
typeof process.env.ZITADEL_AUTHORIZATION_URL === "string" &&
23+
typeof process.env.ZITADEL_USERINFO_ENDPOINT === "string",
24+
strategyName: "zitadel",
25+
useEmailAsIdentity: true,
26+
serverURL: process.env.NEXT_PUBLIC_URL || "http://localhost:3000",
27+
clientId: process.env.ZITADEL_CLIENT_ID || "",
28+
clientSecret: process.env.ZITADEL_CLIENT_SECRET || "",
29+
authorizePath: "/oauth/zitadel",
30+
callbackPath: "/oauth/zitadel/callback",
31+
authCollection: "users",
32+
tokenEndpoint: process.env.ZITADEL_TOKEN_ENDPOINT || "",
33+
scopes: [
34+
"openid",
35+
"profile",
36+
"email",
37+
"offline_access",
38+
"urn:zitadel:iam:user:metadata",
39+
],
40+
providerAuthorizationUrl: process.env.ZITADEL_AUTHORIZATION_URL || "",
41+
getUserInfo: async (accessToken: string) => {
42+
const response = await fetch(
43+
process.env.ZITADEL_USERINFO_ENDPOINT || "",
44+
{
45+
headers: { Authorization: `Bearer ${accessToken}` },
46+
},
47+
);
48+
const user = await response.json();
49+
return { email: user.email, sub: user.sub };
50+
},
51+
successRedirect: (req) => {
52+
return "/admin";
53+
},
54+
failureRedirect: (req, err) => {
55+
req.payload.logger.error(err);
56+
return "/admin/login";
57+
},
58+
}),
59+
],
60+
// ...
61+
});
62+
```

0 commit comments

Comments
 (0)