Skip to content

Commit 74288b0

Browse files
fix: OptionalApiAuthGuard when only client id is provided (calcom#23783)
1 parent a977fb4 commit 74288b0

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

apps/api/v2/src/modules/auth/guards/optional-api-auth/optional-api-auth.guard.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { ApiAuthGuard } from "@/modules/auth/guards/api-auth/api-auth.guard";
2-
import { NO_AUTH_PROVIDED_MESSAGE } from "@/modules/auth/strategies/api-auth/api-auth.strategy";
2+
import {
3+
NO_AUTH_PROVIDED_MESSAGE,
4+
ONLY_CLIENT_ID_PROVIDED_MESSAGE,
5+
} from "@/modules/auth/strategies/api-auth/api-auth.strategy";
36

47
export class OptionalApiAuthGuard extends ApiAuthGuard {
58
handleRequest(error: Error, user: any) {
69
// note(Lauris): optional means that auth is not required but if it is invalid then still throw error.
710
const noAuthProvided = error && error.message.includes(NO_AUTH_PROVIDED_MESSAGE);
11+
const onlyClientIdProvided = error && error.message.includes(ONLY_CLIENT_ID_PROVIDED_MESSAGE);
12+
13+
if (onlyClientIdProvided) {
14+
return null;
15+
}
16+
817
if (user || noAuthProvided || !error) {
918
return user || null;
1019
} else {

apps/api/v2/src/modules/auth/strategies/api-auth/api-auth.strategy.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export type ApiAuthGuardRequest = Request & {
3232
export const NO_AUTH_PROVIDED_MESSAGE =
3333
"No authentication method provided. Either pass an API key as 'Bearer' header or OAuth client credentials as 'x-cal-secret-key' and 'x-cal-client-id' headers";
3434

35+
export const ONLY_CLIENT_ID_PROVIDED_MESSAGE =
36+
"Only 'x-cal-client-id' header provided. Please also provide 'x-cal-secret-key' header or Auth bearer token as 'Authentication' header";
37+
3538
@Injectable()
3639
export class ApiAuthStrategy extends PassportStrategy(BaseStrategy, "api-auth") {
3740
private readonly logger = new Logger("ApiAuthStrategy");
@@ -117,10 +120,15 @@ export class ApiAuthStrategy extends PassportStrategy(BaseStrategy, "api-auth")
117120
}
118121

119122
const noAuthProvided = !oAuthClientId && !oAuthClientSecret && !bearerToken && !nextAuthToken;
123+
const onlyClientIdProvided = !!oAuthClientId && !oAuthClientSecret && !bearerToken && !nextAuthToken;
120124
if (noAuthProvided) {
121125
throw new UnauthorizedException(`ApiAuthStrategy - ${NO_AUTH_PROVIDED_MESSAGE}`);
122126
}
123127

128+
if (onlyClientIdProvided) {
129+
throw new UnauthorizedException(`ApiAuthStrategy - ${ONLY_CLIENT_ID_PROVIDED_MESSAGE}`);
130+
}
131+
124132
throw new UnauthorizedException(
125133
`ApiAuthStrategy - Invalid authentication method. Please provide one of the allowed methods: ${
126134
allowedMethods && allowedMethods.length > 0 ? allowedMethods.join(", ") : "Any supported method"

0 commit comments

Comments
 (0)