mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-15 20:17:22 +00:00
Merge pull request #322 from bknd-io/fix/auth-strategy-respect-register-setting
fix(auth): `allow_register` was not respected in strategy controllers
This commit is contained in:
@@ -199,7 +199,12 @@ export class AuthController extends Controller {
|
|||||||
for (const [name, strategy] of Object.entries(strategies)) {
|
for (const [name, strategy] of Object.entries(strategies)) {
|
||||||
if (!this.auth.isStrategyEnabled(strategy)) continue;
|
if (!this.auth.isStrategyEnabled(strategy)) continue;
|
||||||
|
|
||||||
hono.route(`/${name}`, strategy.getController(this.auth.authenticator));
|
hono.route(
|
||||||
|
`/${name}`,
|
||||||
|
strategy.getController(this.auth.authenticator, {
|
||||||
|
allow_register: this.auth.config.allow_register,
|
||||||
|
}),
|
||||||
|
);
|
||||||
this.registerStrategyActions(strategy, hono);
|
this.registerStrategyActions(strategy, hono);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -305,7 +310,9 @@ export class AuthController extends Controller {
|
|||||||
await c.context.ctx().helper.granted(c, AuthPermissions.testPassword);
|
await c.context.ctx().helper.granted(c, AuthPermissions.testPassword);
|
||||||
|
|
||||||
const pw = this.auth.authenticator.strategy("password") as PasswordStrategy;
|
const pw = this.auth.authenticator.strategy("password") as PasswordStrategy;
|
||||||
const controller = pw.getController(this.auth.authenticator);
|
const controller = pw.getController(this.auth.authenticator, {
|
||||||
|
allow_register: this.auth.config.allow_register,
|
||||||
|
});
|
||||||
|
|
||||||
const res = await controller.request(
|
const res = await controller.request(
|
||||||
new Request("https://localhost/login", {
|
new Request("https://localhost/login", {
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ export class PasswordStrategy extends AuthStrategy<typeof schema> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
getController(authenticator: Authenticator): Hono<any> {
|
getController(authenticator: Authenticator, opts: { allow_register?: boolean }): Hono<any> {
|
||||||
const hono = new Hono();
|
const hono = new Hono();
|
||||||
const redirectQuerySchema = s.object({
|
const redirectQuerySchema = s.object({
|
||||||
redirect: s.string().optional(),
|
redirect: s.string().optional(),
|
||||||
@@ -120,41 +120,43 @@ export class PasswordStrategy extends AuthStrategy<typeof schema> {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
hono.post(
|
if (opts.allow_register) {
|
||||||
"/register",
|
hono.post(
|
||||||
describeRoute({
|
"/register",
|
||||||
summary: "Register a new user with email and password",
|
describeRoute({
|
||||||
tags: ["auth"],
|
summary: "Register a new user with email and password",
|
||||||
}),
|
tags: ["auth"],
|
||||||
jsc("query", redirectQuerySchema),
|
}),
|
||||||
async (c) => {
|
jsc("query", redirectQuerySchema),
|
||||||
try {
|
async (c) => {
|
||||||
const { redirect } = c.req.valid("query");
|
try {
|
||||||
const { password, email, ...body } = parse(
|
const { redirect } = c.req.valid("query");
|
||||||
payloadSchema,
|
const { password, email, ...body } = parse(
|
||||||
await authenticator.getBody(c),
|
payloadSchema,
|
||||||
{
|
await authenticator.getBody(c),
|
||||||
onError: (errors) => {
|
{
|
||||||
$console.error("Invalid register payload", [...errors]);
|
onError: (errors) => {
|
||||||
new InvalidCredentialsException();
|
$console.error("Invalid register payload", [...errors]);
|
||||||
|
new InvalidCredentialsException();
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
);
|
||||||
);
|
|
||||||
|
|
||||||
const profile = {
|
const profile = {
|
||||||
...body,
|
...body,
|
||||||
email,
|
email,
|
||||||
strategy_value: await this.hash(password),
|
strategy_value: await this.hash(password),
|
||||||
};
|
};
|
||||||
|
|
||||||
return await authenticator.resolveRegister(c, this, profile, async () => void 0, {
|
return await authenticator.resolveRegister(c, this, profile, async () => void 0, {
|
||||||
redirect,
|
redirect,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return authenticator.respondWithError(c, e as any);
|
return authenticator.respondWithError(c, e as any);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return hono;
|
return hono;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ export abstract class AuthStrategy<Schema extends s.Schema = s.Schema> {
|
|||||||
|
|
||||||
protected abstract getSchema(): Schema;
|
protected abstract getSchema(): Schema;
|
||||||
|
|
||||||
abstract getController(auth: Authenticator): Hono;
|
abstract getController(auth: Authenticator, opts: { allow_register?: boolean }): Hono;
|
||||||
|
|
||||||
getType(): string {
|
getType(): string {
|
||||||
return this.type;
|
return this.type;
|
||||||
|
|||||||
@@ -284,7 +284,7 @@ export class OAuthStrategy extends AuthStrategy<typeof schemaProvided> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getController(auth: Authenticator): Hono<any> {
|
getController(auth: Authenticator, opts: { allow_register?: boolean }): Hono<any> {
|
||||||
const hono = new Hono();
|
const hono = new Hono();
|
||||||
const secret = "secret";
|
const secret = "secret";
|
||||||
const cookie_name = "_challenge";
|
const cookie_name = "_challenge";
|
||||||
@@ -379,6 +379,10 @@ export class OAuthStrategy extends AuthStrategy<typeof schemaProvided> {
|
|||||||
return c.notFound();
|
return c.notFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (action === "register" && !opts.allow_register) {
|
||||||
|
return c.notFound();
|
||||||
|
}
|
||||||
|
|
||||||
const url = new URL(c.req.url);
|
const url = new URL(c.req.url);
|
||||||
const path = url.pathname.replace(`/${action}`, "");
|
const path = url.pathname.replace(`/${action}`, "");
|
||||||
const redirect_uri = url.origin + path + "/callback";
|
const redirect_uri = url.origin + path + "/callback";
|
||||||
|
|||||||
Reference in New Issue
Block a user