1
1
/*******************************************************************************
2
- * Copyright (c) 2019 IBM Corporation and others.
2
+ * Copyright (c) 2019, 2025 IBM Corporation and others.
3
3
* All rights reserved. This program and the accompanying materials
4
4
* are made available under the terms of the Eclipse Public License 2.0
5
5
* which accompanies this distribution, and is available at
6
6
* http://www.eclipse.org/legal/epl-2.0/
7
7
*
8
8
* SPDX-License-Identifier: EPL-2.0
9
- *
10
- * Contributors:
11
- * IBM Corporation - initial API and implementation
12
9
*******************************************************************************/
13
10
window . addEventListener ( "message" , receiveMessage , false ) ;
14
11
12
+ /**
13
+ * Generate a cryptographic hash of a string.
14
+ */
15
+ function hash ( string ) {
16
+ const encodedString = new TextEncoder ( ) . encode ( string ) ;
17
+ return crypto . subtle . digest ( 'SHA-256' , encodedString ) . then ( ( hashBuffer ) => {
18
+ const hashArray = Array . from ( new Uint8Array ( hashBuffer ) ) ;
19
+ const hashHex = hashArray
20
+ . map ( ( bytes ) => bytes . toString ( 16 ) . padStart ( 2 , '0' ) )
21
+ . join ( '' ) ;
22
+ return hashHex ;
23
+ } ) ;
24
+ }
25
+
15
26
/**
16
27
* Get the current browser state value from the browser state cookie.
17
28
*
@@ -41,13 +52,13 @@ function getBrowserState() {
41
52
* @returns A Base64-encoded SHA-256 hash of the concatenation of all of the
42
53
* provided arguments.
43
54
*/
44
- function calculateSessionState ( clientId , browserState , salt ) {
55
+ async function calculateSessionState ( clientId , browserState , salt ) {
45
56
var stringToHash = clientId + '' + browserState ;
46
57
if ( salt ) {
47
58
stringToHash = stringToHash + '' + salt ;
48
59
}
49
- var sessionState = CryptoJS . SHA256 ( stringToHash ) ;
50
- sessionState = sessionState . toString ( CryptoJS . enc . Base64 ) ;
60
+ var sessionState = await hash ( stringToHash ) ;
61
+ sessionState = btoa ( sessionState ) ;
51
62
if ( salt ) {
52
63
sessionState = sessionState + '.' + salt ;
53
64
}
@@ -65,7 +76,7 @@ function calculateSessionState(clientId, browserState, salt) {
65
76
*
66
77
* @param message
67
78
*/
68
- function receiveMessage ( message ) {
79
+ async function receiveMessage ( message ) {
69
80
if ( message . origin !== EXPECTED_ORIGIN ) {
70
81
console . log ( "Unable to complete request from " + message . origin ) ;
71
82
return ;
@@ -94,7 +105,7 @@ function receiveMessage(message) {
94
105
salt = stateAndSalt [ 1 ] ;
95
106
}
96
107
97
- var sessionState = calculateSessionState ( clientId , browserState , salt ) ;
108
+ var sessionState = await calculateSessionState ( clientId , browserState , salt ) ;
98
109
99
110
var msg = "changed" ;
100
111
// Ensure both the type and value of the two session states are equivalent
0 commit comments