Skip to content

Commit 8188407

Browse files
authored
Create authentication cookies context file (#1598)
Part of OPS-3003.
1 parent d18592e commit 8188407

2 files changed

Lines changed: 65 additions & 53 deletions

File tree

packages/server/api/src/app/authentication/authentication.controller.ts

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,24 @@ import {
33
FastifyPluginAsyncTypebox,
44
Type,
55
} from '@fastify/type-provider-typebox';
6-
import {
7-
AppSystemProp,
8-
SharedSystemProp,
9-
system,
10-
} from '@openops/server-shared';
6+
import { AppSystemProp, system } from '@openops/server-shared';
117
import {
128
ALL_PRINCIPAL_TYPES,
13-
AuthenticationResponse,
149
OpsEdition,
1510
PrincipalType,
1611
SignInRequest,
1712
SignUpRequest,
1813
} from '@openops/shared';
19-
import { FastifyReply } from 'fastify';
20-
import { jwtDecode } from 'jwt-decode';
21-
import { getSubDomain } from '../helper/sub-domain';
2214
import { analyticsDashboardService } from '../openops-analytics/analytics-dashboard-service';
2315
import { resolveOrganizationIdForAuthnRequest } from '../organization/organization-utils';
2416
import { userService } from '../user/user-service';
2517
import { analyticsAuthenticationService } from './analytics-authentication-service';
2618
import { authenticationService } from './authentication-service';
2719
import { Provider } from './authentication-service/hooks/authentication-service-hooks';
20+
import {
21+
removeAuthCookiesAndReply,
22+
setAuthCookiesAndReply,
23+
} from './context/authentication-cookies';
2824

2925
const edition = system.getEdition();
3026
const adminEmail = system.getOrThrow(AppSystemProp.OPENOPS_ADMIN_EMAIL);
@@ -67,7 +63,7 @@ export const authenticationController: FastifyPluginAsyncTypebox = async (
6763
provider: Provider.EMAIL,
6864
});
6965

70-
return sendResponse(reply, signUpResponse);
66+
return setAuthCookiesAndReply(reply, signUpResponse);
7167
});
7268

7369
app.post('/sign-in', SignInRequestOptions, async (request, reply) => {
@@ -83,8 +79,9 @@ export const authenticationController: FastifyPluginAsyncTypebox = async (
8379
provider: Provider.EMAIL,
8480
});
8581

86-
return sendResponse(reply, signInResponse);
82+
return setAuthCookiesAndReply(reply, signInResponse);
8783
});
84+
8885
app.post(
8986
'/sign-out',
9087
{
@@ -94,17 +91,10 @@ export const authenticationController: FastifyPluginAsyncTypebox = async (
9491
},
9592
},
9693
async (request, reply) => {
97-
return reply
98-
.clearCookie('jwt_token', {
99-
domain: getOpenOpsSubDomain(),
100-
path: '/',
101-
})
102-
.clearCookie('token', {
103-
path: '/',
104-
})
105-
.send('Cookies removed');
94+
return removeAuthCookiesAndReply(reply);
10695
},
10796
);
97+
10898
app.get('/analytics-embed-id', async (request, reply) => {
10999
const { access_token } = await analyticsAuthenticationService.signIn();
110100

@@ -114,6 +104,7 @@ export const authenticationController: FastifyPluginAsyncTypebox = async (
114104

115105
return reply.send(embedId);
116106
});
107+
117108
app.get(
118109
'/analytics-guest-token',
119110
AnalyticsGuestTokenRequestOptions,
@@ -162,36 +153,3 @@ const SignInRequestOptions = {
162153
body: SignInRequest,
163154
},
164155
};
165-
166-
function sendResponse(
167-
reply: FastifyReply,
168-
response: AuthenticationResponse,
169-
): FastifyReply {
170-
const date = jwtDecode<{ exp: number }>(response.tablesRefreshToken);
171-
const cookieExpiryDate = new Date(date.exp * 1000);
172-
173-
return reply
174-
.setCookie('jwt_token', response.tablesRefreshToken, {
175-
domain: getOpenOpsSubDomain(),
176-
path: '/',
177-
signed: true,
178-
httpOnly: false,
179-
expires: cookieExpiryDate,
180-
})
181-
.setCookie('token', response.token, {
182-
path: '/',
183-
signed: true,
184-
httpOnly: false,
185-
expires: cookieExpiryDate,
186-
sameSite: 'lax',
187-
})
188-
.send(response);
189-
}
190-
191-
function getOpenOpsSubDomain(): string {
192-
const frontendUrl = system.getOrThrow(SharedSystemProp.FRONTEND_URL);
193-
194-
const tablesUrl = system.getOrThrow(AppSystemProp.OPENOPS_TABLES_PUBLIC_URL);
195-
196-
return getSubDomain(frontendUrl, tablesUrl);
197-
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {
2+
AppSystemProp,
3+
SharedSystemProp,
4+
system,
5+
} from '@openops/server-shared';
6+
import { AuthenticationResponse } from '@openops/shared';
7+
import { FastifyReply } from 'fastify';
8+
import { jwtDecode } from 'jwt-decode';
9+
import { getSubDomain } from '../../helper/sub-domain';
10+
11+
export function setAuthCookiesAndReply(
12+
reply: FastifyReply,
13+
response: AuthenticationResponse,
14+
): FastifyReply {
15+
const date = jwtDecode<{ exp: number }>(response.tablesRefreshToken);
16+
const cookieExpiryDate = new Date(date.exp * 1000);
17+
18+
return reply
19+
.setCookie('jwt_token', response.tablesRefreshToken, {
20+
domain: getOpenOpsSubDomain(),
21+
path: '/',
22+
signed: true,
23+
httpOnly: false,
24+
expires: cookieExpiryDate,
25+
})
26+
.setCookie('token', response.token, {
27+
path: '/',
28+
signed: true,
29+
httpOnly: false,
30+
expires: cookieExpiryDate,
31+
sameSite: 'lax',
32+
})
33+
.send(response);
34+
}
35+
36+
export function removeAuthCookiesAndReply(reply: FastifyReply): FastifyReply {
37+
return reply
38+
.clearCookie('jwt_token', {
39+
domain: getOpenOpsSubDomain(),
40+
path: '/',
41+
})
42+
.clearCookie('token', {
43+
path: '/',
44+
})
45+
.send('Cookies removed');
46+
}
47+
48+
function getOpenOpsSubDomain(): string {
49+
const frontendUrl = system.getOrThrow(SharedSystemProp.FRONTEND_URL);
50+
51+
const tablesUrl = system.getOrThrow(AppSystemProp.OPENOPS_TABLES_PUBLIC_URL);
52+
53+
return getSubDomain(frontendUrl, tablesUrl);
54+
}

0 commit comments

Comments
 (0)