1
1
function account_name ( r ) {
2
+ // Get the variable "auth_entity"
2
3
const auth_entity = r . variables [ "auth_entity" ] ;
3
- const b64decoded = Buffer . from ( auth_entity , 'base64' ) ;
4
- const json = JSON . parse ( b64decoded ) ;
5
- const account_name = json . authenticatedEntity . activeAccount . name ;
4
+ if ( ! auth_entity ) {
5
+ r . error ( "account_name: auth_entity variable is missing" ) ;
6
+ return "default" ;
7
+ }
8
+ r . error ( "account_name: auth_entity = " + auth_entity ) ;
6
9
7
- return account_name ;
10
+ // Attempt to base64-decode it
11
+ var decoded ;
12
+ try {
13
+ decoded = Buffer . from ( auth_entity , 'base64' ) . toString ( 'utf8' ) ;
14
+ r . error ( "account_name: decoded auth_entity = " + decoded ) ;
15
+ } catch ( e ) {
16
+ r . error ( "account_name: error decoding auth_entity: " + e ) ;
17
+ return "default" ;
18
+ }
19
+
20
+ // Try to parse the decoded value as JSON
21
+ var json ;
22
+ try {
23
+ json = JSON . parse ( decoded ) ;
24
+ r . error ( "account_name: parsed JSON = " + JSON . stringify ( json ) ) ;
25
+ } catch ( e ) {
26
+ r . error ( "account_name: JSON parse error: " + e ) ;
27
+ return "default" ;
28
+ }
29
+
30
+ // Extract the account name
31
+ if ( json . activeAccount && json . activeAccount . name ) {
32
+ r . error ( "account_name: extracted account name = " + json . activeAccount . name ) ;
33
+ return json . activeAccount . name ;
34
+ } else {
35
+ r . error ( "account_name: activeAccount.name not found in JSON" ) ;
36
+ return "default" ;
37
+ }
38
+ }
39
+
40
+ function setAuthHeader ( r ) {
41
+ let auth = r . headersIn [ 'authorization' ] ;
42
+ if ( auth ) {
43
+ // Look for the pattern: Credential=<value>/...
44
+ let matches = auth . match ( / C r e d e n t i a l = ( [ ^ \/ ] + ) \/ / ) ;
45
+ if ( matches && matches . length > 1 ) {
46
+ return matches [ 1 ] ;
47
+ }
48
+ }
49
+ return "" ;
8
50
}
9
51
10
- export default { account_name} ;
52
+ export default { account_name, setAuthHeader } ;
0 commit comments