@@ -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' ;
117import {
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' ;
2214import { analyticsDashboardService } from '../openops-analytics/analytics-dashboard-service' ;
2315import { resolveOrganizationIdForAuthnRequest } from '../organization/organization-utils' ;
2416import { userService } from '../user/user-service' ;
2517import { analyticsAuthenticationService } from './analytics-authentication-service' ;
2618import { authenticationService } from './authentication-service' ;
2719import { Provider } from './authentication-service/hooks/authentication-service-hooks' ;
20+ import {
21+ removeAuthCookiesAndReply ,
22+ setAuthCookiesAndReply ,
23+ } from './context/authentication-cookies' ;
2824
2925const edition = system . getEdition ( ) ;
3026const 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- }
0 commit comments