13
13
* permissions and limitations under the License.
14
14
*/
15
15
16
- /* eslint-disable max-classes-per-file */
17
- // This file has extra classes used for testing
18
-
19
16
import { SecurityPluginConfigType } from '../..' ;
20
17
import { AuthenticationType } from './authentication_type' ;
21
18
import { httpServerMock } from '../../../../../src/core/server/mocks' ;
22
- import {
23
- SessionStorageFactory ,
24
- SessionStorage ,
25
- OpenSearchDashboardsRequest ,
26
- } from '../../../../../src/core/server' ;
19
+ import { OpenSearchDashboardsRequest } from '../../../../../src/core/server' ;
27
20
import { SecuritySessionCookie } from '../../session/security_cookie' ;
28
21
29
22
class DummyAuthType extends AuthenticationType {
@@ -50,43 +43,6 @@ class DummyAuthType extends AuthenticationType {
50
43
}
51
44
}
52
45
53
- // Implementation of SessionStorage using browser's sessionStorage
54
- class BrowserSessionStorage < T > implements SessionStorage < T > {
55
- private readonly storageKey : string ;
56
-
57
- constructor ( storageKey : string ) {
58
- this . storageKey = storageKey ;
59
- }
60
-
61
- async get ( ) : Promise < T | null > {
62
- const storedValue = sessionStorage . getItem ( this . storageKey ) ;
63
- return storedValue ? JSON . parse ( storedValue ) : null ;
64
- }
65
-
66
- set ( sessionValue : T ) : void {
67
- const serializedValue = JSON . stringify ( sessionValue ) ;
68
- sessionStorage . setItem ( this . storageKey , serializedValue ) ;
69
- }
70
-
71
- clear ( ) : void {
72
- sessionStorage . removeItem ( this . storageKey ) ;
73
- }
74
- }
75
-
76
- // Implementation of SessionStorageFactory using the browser's sessionStorage
77
- export class BrowserSessionStorageFactory < T > implements SessionStorageFactory < T > {
78
- private readonly storageKey : string ;
79
-
80
- constructor ( storageKey : string ) {
81
- this . storageKey = storageKey ;
82
- }
83
-
84
- // This method returns a new instance of the browser's SessionStorage for each request
85
- asScoped ( request : OpenSearchDashboardsRequest ) : SessionStorage < T > {
86
- return new BrowserSessionStorage < T > ( this . storageKey ) ;
87
- }
88
- }
89
-
90
46
describe ( 'test tenant header' , ( ) => {
91
47
const config = {
92
48
multitenancy : {
@@ -158,4 +114,50 @@ describe('test tenant header', () => {
158
114
const result = await dummyAuthType . authHandler ( request , response , toolkit ) ;
159
115
expect ( result . requestHeaders . securitytenant ) . toEqual ( 'dummy_tenant' ) ;
160
116
} ) ;
117
+
118
+ it ( `keepalive should not shorten the cookie expiry` , async ( ) => {
119
+ const realDateNow = Date . now . bind ( global . Date ) ;
120
+ const dateNowStub = jest . fn ( ( ) => 0 ) ;
121
+ global . Date . now = dateNowStub ;
122
+
123
+ const keepAliveConfig = {
124
+ multitenancy : {
125
+ enabled : true ,
126
+ } ,
127
+ auth : {
128
+ unauthenticated_routes : [ ] as string [ ] ,
129
+ } ,
130
+ session : {
131
+ keepalive : true ,
132
+ ttl : 1000 ,
133
+ } ,
134
+ } as SecurityPluginConfigType ;
135
+ const keepAliveDummyAuth = new DummyAuthType (
136
+ keepAliveConfig ,
137
+ new BrowserSessionStorageFactory ( 'security_cookie' ) ,
138
+ router ,
139
+ esClient ,
140
+ coreSetup ,
141
+ logger
142
+ ) ;
143
+ const testCookie : SecuritySessionCookie = {
144
+ credentials : {
145
+ authHeaderValueExtra : true ,
146
+ } ,
147
+ expiryTime : 2000 ,
148
+ } ;
149
+ // Set cookie
150
+ sessionStorage . setItem ( 'security_cookie' , JSON . stringify ( testCookie ) ) ;
151
+ const request = httpServerMock . createOpenSearchDashboardsRequest ( {
152
+ path : '/internal/v1' ,
153
+ } ) ;
154
+ const response = jest . fn ( ) ;
155
+ const toolkit = {
156
+ authenticated : jest . fn ( ( value ) => value ) ,
157
+ } ;
158
+ await keepAliveDummyAuth . authHandler ( request , response , toolkit ) ;
159
+ const cookieAfterRequest = sessionStorage . getItem ( 'security_cookie' ) ;
160
+ expect ( JSON . parse ( cookieAfterRequest ! ) . expiryTime ) . toBe ( 2000 ) ;
161
+ global . Date . now = realDateNow ;
162
+ } ) ;
161
163
} ) ;
0 commit comments