@@ -131,39 +131,63 @@ impl fmt::Display for AccountSasPermissions {
131
131
pub struct AccountSharedAccessSignature {
132
132
account : String ,
133
133
key : String ,
134
- signed_version : AccountSasVersion ,
135
- signed_resource : AccountSasResource ,
136
- signed_resource_type : AccountSasResourceType ,
137
- signed_start : Option < DateTime < Utc > > ,
138
- signed_expiry : DateTime < Utc > ,
139
- signed_permissions : AccountSasPermissions ,
140
- signed_ip : Option < String > ,
141
- signed_protocol : Option < SasProtocol > ,
134
+ version : AccountSasVersion ,
135
+ resource : AccountSasResource ,
136
+ resource_type : AccountSasResourceType ,
137
+ expiry : DateTime < Utc > ,
138
+ permissions : AccountSasPermissions ,
139
+ start : Option < DateTime < Utc > > ,
140
+ ip : Option < String > ,
141
+ protocol : Option < SasProtocol > ,
142
142
}
143
143
144
144
impl AccountSharedAccessSignature {
145
- #[ allow( clippy:: new_ret_no_self) ]
146
- pub fn new ( account : String , key : String ) -> AccountSharedAccessSignatureBuilder {
147
- AccountSharedAccessSignatureBuilder :: new ( account, key)
145
+ pub fn new (
146
+ account : String ,
147
+ key : String ,
148
+ resource : AccountSasResource ,
149
+ resource_type : AccountSasResourceType ,
150
+ expiry : DateTime < Utc > ,
151
+ permissions : AccountSasPermissions ,
152
+ ) -> Self {
153
+ Self {
154
+ account,
155
+ key,
156
+ version : AccountSasVersion :: V20181109 ,
157
+ resource,
158
+ resource_type,
159
+ expiry,
160
+ permissions,
161
+ start : None ,
162
+ ip : None ,
163
+ protocol : None ,
164
+ }
165
+ }
166
+
167
+ setters ! {
168
+ version: AccountSasVersion => version,
169
+ start: DateTime <Utc > => Some ( start) ,
170
+ ip: String => Some ( ip) ,
171
+ protocol: SasProtocol => Some ( protocol) ,
148
172
}
149
173
150
174
// Azure documentation: https://docs.microsoft.com/rest/api/storageservices/create-service-sas#constructing-the-signature-string
151
175
fn signature ( & self ) -> String {
152
- match self . signed_version {
176
+ match self . version {
153
177
AccountSasVersion :: V20181109 => {
154
178
let string_to_sign = format ! (
155
179
"{}\n {}\n {}\n {}\n {}\n {}\n {}\n {}\n {}\n " ,
156
180
self . account,
157
- self . signed_permissions ,
158
- self . signed_resource ,
159
- self . signed_resource_type ,
160
- self . signed_start . map_or( "" . to_string( ) , format_date) ,
161
- format_date( self . signed_expiry ) ,
162
- self . signed_ip . clone( ) . unwrap_or_else( || "" . to_string( ) ) ,
163
- self . signed_protocol
181
+ self . permissions ,
182
+ self . resource ,
183
+ self . resource_type ,
184
+ self . start . map_or( "" . to_string( ) , format_date) ,
185
+ format_date( self . expiry ) ,
186
+ self . ip . clone( ) . unwrap_or_else( || "" . to_string( ) ) ,
187
+ self . protocol
164
188
. as_ref( )
165
189
. map_or( "" . to_string( ) , |v| v. to_string( ) ) ,
166
- self . signed_version ,
190
+ self . version ,
167
191
) ;
168
192
169
193
sign ( & string_to_sign, & self . key ) . unwrap ( )
@@ -180,20 +204,20 @@ impl SasToken for AccountSharedAccessSignature {
180
204
/// [Example](https://docs.microsoft.com/rest/api/storageservices/create-service-sas#service-sas-example) from Azure documentation.
181
205
fn token ( & self ) -> String {
182
206
let mut elements: Vec < String > = vec ! [
183
- format!( "sv={}" , self . signed_version ) ,
184
- format!( "ss={}" , self . signed_resource ) ,
185
- format!( "srt={}" , self . signed_resource_type ) ,
186
- format!( "se={}" , format_form( format_date( self . signed_expiry ) ) ) ,
187
- format!( "sp={}" , self . signed_permissions ) ,
207
+ format!( "sv={}" , self . version ) ,
208
+ format!( "ss={}" , self . resource ) ,
209
+ format!( "srt={}" , self . resource_type ) ,
210
+ format!( "se={}" , format_form( format_date( self . expiry ) ) ) ,
211
+ format!( "sp={}" , self . permissions ) ,
188
212
] ;
189
213
190
- if let Some ( start) = & self . signed_start {
214
+ if let Some ( start) = & self . start {
191
215
elements. push ( format ! ( "st={}" , format_form( format_date( * start) ) ) )
192
216
}
193
- if let Some ( ip) = & self . signed_ip {
217
+ if let Some ( ip) = & self . ip {
194
218
elements. push ( format ! ( "sip={}" , ip) )
195
219
}
196
- if let Some ( protocol) = & self . signed_protocol {
220
+ if let Some ( protocol) = & self . protocol {
197
221
elements. push ( format ! ( "spr={}" , protocol) )
198
222
}
199
223
let sig = AccountSharedAccessSignature :: signature ( self ) ;
@@ -214,136 +238,3 @@ impl std::fmt::Debug for AccountSharedAccessSignature {
214
238
write ! ( f, "SharedAccessSignature {{{}}}" , self . signature( ) )
215
239
}
216
240
}
217
-
218
- pub struct AccountSharedAccessSignatureBuilder {
219
- account : String ,
220
- key : String ,
221
- signed_version : AccountSasVersion ,
222
- signed_resource : Option < AccountSasResource > ,
223
- signed_resource_type : Option < AccountSasResourceType > ,
224
- signed_start : Option < DateTime < Utc > > ,
225
- signed_expiry : Option < DateTime < Utc > > ,
226
- signed_permissions : Option < AccountSasPermissions > ,
227
- signed_ip : Option < String > ,
228
- signed_protocol : Option < SasProtocol > ,
229
- }
230
-
231
- impl AccountSharedAccessSignatureBuilder {
232
- pub fn new ( account : String , key : String ) -> Self {
233
- Self {
234
- account,
235
- key,
236
- signed_version : AccountSasVersion :: V20181109 ,
237
- signed_resource : None ,
238
- signed_resource_type : None ,
239
- signed_start : None ,
240
- signed_expiry : None ,
241
- signed_permissions : None ,
242
- signed_ip : None ,
243
- signed_protocol : None ,
244
- }
245
- }
246
-
247
- pub fn finalize ( & self ) -> AccountSharedAccessSignature {
248
- AccountSharedAccessSignature {
249
- account : self . account . to_owned ( ) ,
250
- key : self . key . to_owned ( ) ,
251
- signed_version : self . signed_version ,
252
- signed_resource : self . signed_resource . unwrap ( ) ,
253
- signed_resource_type : self . signed_resource_type . unwrap ( ) ,
254
- signed_start : self . signed_start ,
255
- signed_expiry : self . signed_expiry . unwrap ( ) ,
256
- signed_permissions : self . signed_permissions . unwrap ( ) ,
257
- signed_ip : self . signed_ip . clone ( ) ,
258
- signed_protocol : self . signed_protocol ,
259
- }
260
- }
261
-
262
- pub fn version ( & self ) -> AccountSasVersion {
263
- self . signed_version
264
- }
265
-
266
- pub fn with_version ( self , version : AccountSasVersion ) -> Self {
267
- Self {
268
- signed_version : version,
269
- ..self
270
- }
271
- }
272
-
273
- pub fn resource ( & self ) -> AccountSasResource {
274
- self . signed_resource . unwrap ( )
275
- }
276
-
277
- pub fn with_resource ( self , resource : AccountSasResource ) -> Self {
278
- Self {
279
- signed_resource : Some ( resource) ,
280
- ..self
281
- }
282
- }
283
-
284
- pub fn resource_type_type ( & self ) -> AccountSasResourceType {
285
- self . signed_resource_type . unwrap ( )
286
- }
287
-
288
- pub fn with_resource_type ( self , resource_type : AccountSasResourceType ) -> Self {
289
- Self {
290
- signed_resource_type : Some ( resource_type) ,
291
- ..self
292
- }
293
- }
294
-
295
- pub fn expiry ( & self ) -> DateTime < Utc > {
296
- self . signed_expiry . unwrap ( )
297
- }
298
-
299
- pub fn with_expiry ( self , expiry : DateTime < Utc > ) -> Self {
300
- Self {
301
- signed_expiry : Some ( expiry) ,
302
- ..self
303
- }
304
- }
305
-
306
- pub fn signed_permissions ( & self ) -> AccountSasPermissions {
307
- self . signed_permissions . unwrap ( )
308
- }
309
-
310
- pub fn with_permissions ( self , permissions : AccountSasPermissions ) -> Self {
311
- Self {
312
- signed_permissions : Some ( permissions) ,
313
- ..self
314
- }
315
- }
316
-
317
- pub fn signed_start ( & self ) -> DateTime < Utc > {
318
- self . signed_start . unwrap ( )
319
- }
320
-
321
- pub fn with_start ( self , start : DateTime < Utc > ) -> Self {
322
- Self {
323
- signed_start : Some ( start) ,
324
- ..self
325
- }
326
- }
327
-
328
- pub fn ip ( & self ) -> & str {
329
- self . signed_ip . as_deref ( ) . unwrap ( )
330
- }
331
-
332
- pub fn with_ip ( self , ip : String ) -> Self {
333
- Self {
334
- signed_ip : Some ( ip) ,
335
- ..self
336
- }
337
- }
338
-
339
- pub fn protocol ( & self ) -> SasProtocol {
340
- self . signed_protocol . unwrap ( )
341
- }
342
-
343
- pub fn with_protocol ( self , protocol : SasProtocol ) -> Self {
344
- Self {
345
- signed_protocol : Some ( protocol) ,
346
- ..self
347
- }
348
- }
349
- }
0 commit comments