Skip to content

Commit 9b6afa3

Browse files
authored
Accept cloud token as header (#1696)
Part of OPS-3153
1 parent 6529a60 commit 9b6afa3

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

packages/server/api/src/app/helper/allow-all-origins-hook-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const allowAllOriginsHookHandler: onRequestHookHandler = (
1515

1616
void reply.header(
1717
'Access-Control-Allow-Headers',
18-
'Content-Type,Ops-Origin,Authorization',
18+
'Content-Type,Ops-Origin,Authorization,Ops-Cloud-Token',
1919
);
2020

2121
void reply.header('Access-Control-Allow-Credentials', 'true');

packages/server/api/src/app/user-info/cloud-auth.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ import jwt, { JwtPayload } from 'jsonwebtoken';
44
const CLOUD_TOKEN_COOKIE_NAME = 'cloud-token';
55

66
const getCloudToken = (request: FastifyRequest): string | undefined => {
7-
let token = request.headers.authorization?.replace('Bearer ', '');
8-
if (!token) {
9-
token = request.cookies[CLOUD_TOKEN_COOKIE_NAME];
7+
const authorizationHeader = request.headers.authorization;
8+
const cookieToken = request.cookies[CLOUD_TOKEN_COOKIE_NAME];
9+
const headerToken = request.headers['ops-cloud-token'] as string | undefined;
10+
11+
if (authorizationHeader) {
12+
return authorizationHeader.replace('Bearer ', '');
1013
}
11-
return token;
14+
15+
if (cookieToken) {
16+
return cookieToken;
17+
}
18+
19+
return headerToken;
1220
};
1321

1422
export function getVerifiedUser(

packages/server/api/test/integration/cloud/cloud/cloud-auth.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ describe('getVerifiedUser', () => {
8585
expect(result).toEqual(payload);
8686
});
8787

88+
it('should verify token from header when Authorization header and cookie are missing', () => {
89+
const payload = { sub: 'abc' };
90+
(jwt.verify as jest.Mock).mockReturnValue(payload);
91+
const mockRequest = createMockRequest({
92+
headers: { 'ops-cloud-token': 'cookie-header-token' },
93+
cookies: {},
94+
});
95+
96+
const result = getVerifiedUser(mockRequest, publicKey);
97+
98+
expect(jwt.verify).toHaveBeenCalledWith('cookie-header-token', publicKey, {
99+
algorithms: ['RS256'],
100+
});
101+
expect(result).toEqual(payload);
102+
});
103+
88104
it('should return undefined when verification fails (throws)', () => {
89105
(jwt.verify as jest.Mock).mockImplementation(() => {
90106
throw new Error('invalid');

0 commit comments

Comments
 (0)