Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keycloak test #612

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft

Keycloak test #612

wants to merge 4 commits into from

Conversation

pozylon
Copy link
Member

@pozylon pozylon commented Dec 13, 2024

No description provided.

@pozylon pozylon marked this pull request as draft December 13, 2024 15:36
@pozylon
Copy link
Member Author

pozylon commented Dec 13, 2024

This is just an experiment connecting unchained with a self-hosted Keycloak server.

Comment on lines +45 to +88
fastify.get('/callback', async function (request, reply) {
const accessToken = await this.keycloak.getAccessTokenFromAuthorizationCodeFlow(request);
try {
const userinfo = await this.keycloak.userinfo(accessToken.token.access_token);
const { sub, email, resource_access, email_verified, name, given_name, family_name } = userinfo;
const roles = resource_access?.['unchained-local']?.roles || [];
const user = await engine.unchainedAPI.modules.users.findUserByUsername(`keycloak:${sub}`);

if (user) {
if (JSON.stringify(user.roles) !== JSON.stringify(roles)) {
await engine.unchainedAPI.modules.users.updateRoles(user._id, roles);
}
request.unchainedContext.login(user);
return reply.redirect('/');
}
// TODO: try to use the preferred_username as the username first
const newUserId = await engine.unchainedAPI.modules.users.createUser(
{
username: `keycloak:${sub}`,
password: null,
email: email_verified ? email : undefined,
profile: {
displayName: name,
address: {
firstName: given_name,
lastName: family_name,
},
},
roles,
},
{ skipMessaging: true, skipPasswordEnrollment: true },
);
const newUser = await engine.unchainedAPI.modules.users.findUserById(newUserId);
request.unchainedContext.login(newUser);
return reply.redirect('/');
} catch (e) {
console.error(e);
}

// if later need to refresh the token this can be used
// const { token: newToken } = await this.getNewAccessTokenUsingRefreshToken(token)

return reply.send({ access_token: token.access_token });
});

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.
This route handler performs
authorization
, but is not rate-limited.

Copilot Autofix AI about 1 month ago

To fix the problem, we need to introduce rate limiting to the Fastify application. We can use the @fastify/rate-limit plugin to achieve this. This plugin allows us to set a maximum number of requests per time window for each IP address, which will help mitigate the risk of denial-of-service attacks.

We will:

  1. Install the @fastify/rate-limit package.
  2. Register the rate limiting plugin with the Fastify instance.
  3. Configure the rate limiting settings to a reasonable default, such as 100 requests per 15 minutes.
Suggested changeset 2
examples/minimal/boot.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/examples/minimal/boot.ts b/examples/minimal/boot.ts
--- a/examples/minimal/boot.ts
+++ b/examples/minimal/boot.ts
@@ -8,2 +8,3 @@
 import FastifyOAuth2 from '@fastify/oauth2';
+import FastifyRateLimit from '@fastify/rate-limit';
 
@@ -25,2 +26,7 @@
   });
+
+  await fastify.register(FastifyRateLimit, {
+    max: 100, // maximum 100 requests per 15 minutes
+    timeWindow: '15 minutes'
+  });
 
EOF
@@ -8,2 +8,3 @@
import FastifyOAuth2 from '@fastify/oauth2';
import FastifyRateLimit from '@fastify/rate-limit';

@@ -25,2 +26,7 @@
});

await fastify.register(FastifyRateLimit, {
max: 100, // maximum 100 requests per 15 minutes
timeWindow: '15 minutes'
});

examples/minimal/package.json
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/examples/minimal/package.json b/examples/minimal/package.json
--- a/examples/minimal/package.json
+++ b/examples/minimal/package.json
@@ -41,3 +41,4 @@
     "@unchainedshop/plugins": "^3.0.0-alpha4",
-    "fastify": "^5.1.0"
+    "fastify": "^5.1.0",
+    "@fastify/rate-limit": "^10.2.1"
   },
EOF
@@ -41,3 +41,4 @@
"@unchainedshop/plugins": "^3.0.0-alpha4",
"fastify": "^5.1.0"
"fastify": "^5.1.0",
"@fastify/rate-limit": "^10.2.1"
},
This fix introduces these dependencies
Package Version Security advisories
@fastify/rate-limit (npm) 10.2.1 None
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant