@@ -4,9 +4,11 @@ class SwiftXAccountSharing {
44 // Swift cross account sharing backend client.
55
66 constructor (
7- address
7+ address ,
8+ signatureAddress = "" ,
89 ) {
910 this . address = address ;
11+ this . signatureAddress = signatureAddress ;
1012 }
1113
1214 _parseListString (
@@ -20,11 +22,37 @@ class SwiftXAccountSharing {
2022 return ret . slice ( 0 , ret . length - 1 ) ;
2123 }
2224
25+ async _getSignature (
26+ validFor ,
27+ toSign
28+ ) {
29+ // Get a signature for an API call.
30+ if ( this . signatureAddress != "" ) {
31+ let signatureUrl = new URL ( "/sign/" . concat ( validFor ) , this . signatureAddress ) ;
32+ signatureUrl . searchParams . append ( "path" , toSign ) ;
33+ let signed = await fetch (
34+ signatureUrl , { method : "GET" , credentials : "same-origin" }
35+ ) ;
36+ return signed . json ( ) ;
37+ }
38+ else {
39+ return undefined ;
40+ }
41+ }
42+
2343 async getAccess (
2444 username
2545 ) {
2646 // List the containers the user has been given access to.
2747 let url = new URL ( "/access/" . concat ( username ) , this . address ) ;
48+
49+ let signed = await this . _getSignature (
50+ 60 ,
51+ "/access/" . concat ( username )
52+ ) ;
53+ url . searchParams . append ( "valid" , signed . valid_until ) ;
54+ url . searchParams . append ( "signature" , signed . signature ) ;
55+
2856 let containers = fetch (
2957 url , { method : "GET" }
3058 ) . then (
@@ -42,6 +70,14 @@ class SwiftXAccountSharing {
4270 let url = new URL (
4371 "/access/" . concat ( username , "/" , container ) , this . address
4472 ) ;
73+
74+ let signed = await this . _getSignature (
75+ 60 ,
76+ "/access/" . concat ( username , "/" , container )
77+ ) ;
78+ url . searchParams . append ( "valid" , signed . valid_until ) ;
79+ url . searchParams . append ( "signature" , signed . signature ) ;
80+
4581 url . searchParams . append ( "owner" , owner ) ;
4682 let details = fetch (
4783 url , { method : "GET" }
@@ -56,6 +92,14 @@ class SwiftXAccountSharing {
5692 ) {
5793 // List the containers the user has shared to another user / users.
5894 let url = new URL ( "/share/" . concat ( username ) , this . address ) ;
95+
96+ let signed = await this . _getSignature (
97+ 60 ,
98+ "/share/" . concat ( username )
99+ ) ;
100+ url . searchParams . append ( "valid" , signed . valid_until ) ;
101+ url . searchParams . append ( "signature" , signed . signature ) ;
102+
59103 let shared = fetch (
60104 url , { method : "GET" }
61105 ) . then (
@@ -72,6 +116,14 @@ class SwiftXAccountSharing {
72116 let url = new URL (
73117 "/share/" . concat ( username , "/" , container ) , this . address
74118 ) ;
119+
120+ let signed = await this . _getSignature (
121+ 60 ,
122+ "/share/" . concat ( username , "/" , container )
123+ ) ;
124+ url . searchParams . append ( "valid" , signed . valid_until ) ;
125+ url . searchParams . append ( "signature" , signed . signature ) ;
126+
75127 let details = fetch (
76128 url , { method : "GET" }
77129 ) . then (
@@ -94,6 +146,14 @@ class SwiftXAccountSharing {
94146 url . searchParams . append ( "user" , this . _parseListString ( userlist ) ) ;
95147 url . searchParams . append ( "access" , this . _parseListString ( accesslist ) ) ;
96148 url . searchParams . append ( "address" , address ) ;
149+
150+ let signed = await this . _getSignature (
151+ 60 ,
152+ "/share/" . concat ( username , "/" , container )
153+ ) ;
154+ url . searchParams . append ( "valid" , signed . valid_until ) ;
155+ url . searchParams . append ( "signature" , signed . signature ) ;
156+
97157 let shared = fetch (
98158 url , { method : "POST" }
99159 ) . then (
@@ -114,6 +174,14 @@ class SwiftXAccountSharing {
114174 ) ;
115175 url . searchParams . append ( "user" , this . _parseListString ( userlist ) ) ;
116176 url . searchParams . append ( "access" , this . _parseListString ( accesslist ) ) ;
177+
178+ let signed = await this . _getSignature (
179+ 60 ,
180+ "/share/" . concat ( username , "/" , container )
181+ ) ;
182+ url . searchParams . append ( "valid" , signed . valid_until ) ;
183+ url . searchParams . append ( "signature" , signed . signature ) ;
184+
117185 let shared = fetch (
118186 url , { method : "PATCH" }
119187 ) . then (
@@ -132,6 +200,14 @@ class SwiftXAccountSharing {
132200 "/share/" . concat ( username , "/" , container ) , this . address
133201 ) ;
134202 url . searchParams . append ( "user" , this . _parseListString ( userlist ) ) ;
203+ url . searchParams . append ( "valid" , signed . valid_until ) ;
204+ url . searchParams . append ( "signature" , signed . signature ) ;
205+
206+ let signed = await this . _getSignature (
207+ 60 ,
208+ "/share/" . concat ( username , "/" , container )
209+ ) ;
210+
135211 let deleted = fetch (
136212 url , { method : "DELETE" }
137213 ) . then (
0 commit comments