@@ -57,18 +57,15 @@ impl Policy for AuthorizationPolicy {
57
57
58
58
let time_nonce = TimeNonce :: new ( ) ;
59
59
60
- let uri_path = request. path_and_query ( ) ;
61
- trace ! ( "uri_path used by AuthorizationPolicy == {:#?}" , uri_path) ;
62
-
63
60
let auth = {
64
- let resource_link = generate_resource_link ( & uri_path ) ;
61
+ let resource_link = generate_resource_link ( request ) ;
65
62
trace ! ( "resource_link == {}" , resource_link) ;
66
63
generate_authorization (
67
64
& self . authorization_token ,
68
65
& request. method ( ) ,
69
66
ctx. get ( )
70
67
. expect ( "ResourceType must be in the Context at this point" ) ,
71
- resource_link,
68
+ & resource_link,
72
69
time_nonce,
73
70
)
74
71
} ;
@@ -95,15 +92,17 @@ impl Policy for AuthorizationPolicy {
95
92
}
96
93
}
97
94
98
- /// This function strips the resource name from the passed uri. It does not alter the uri if a
99
- /// resource name is not present. This is accomplished in three steps (with eager return):
100
- /// 1. Find if the uri ends with a ENDING_STRING. If so, strip it and return. Every ENDING_STRING
95
+ /// This function strips the leading slash and the resource name from the uri of the passed request.
96
+ /// It does not strip the resource name if the resource name is not present. This is accomplished in
97
+ /// four steps (with eager return):
98
+ /// 1. Strip leading slash from the uri of the passed request.
99
+ /// 2. Find if the uri ends with a ENDING_STRING. If so, strip it and return. Every ENDING_STRING
101
100
/// starts with a leading slash so this check will not match uri compsed **only** by the
102
101
/// ENDING_STRING.
103
- /// 2 . Find if the uri **is** the ending string (without the leading slash). If so return an empty
102
+ /// 3 . Find if the uri **is** the ending string (without the leading slash). If so return an empty
104
103
/// string. This covers the exception of the rule above.
105
- /// 3 . Return the received uri unchanged.
106
- fn generate_resource_link ( uri : & str ) -> & str {
104
+ /// 4 . Return the received uri unchanged.
105
+ fn generate_resource_link ( request : & Request ) -> String {
107
106
static ENDING_STRINGS : & [ & str ] = & [
108
107
"/dbs" ,
109
108
"/colls" ,
@@ -117,12 +116,17 @@ fn generate_resource_link(uri: &str) -> &str {
117
116
"/triggers" ,
118
117
] ;
119
118
119
+ // This strips the leading slash from the uri of the passed request.
120
+ let uri_path = request. path_and_query ( ) ;
121
+ let uri = uri_path. trim_start_matches ( '/' ) ;
122
+ trace ! ( "uri used by AuthorizationPolicy == {:#?}" , uri) ;
123
+
120
124
// We find the above resource names. If found, we strip it and eagerly return. Note that the
121
125
// resource names have a leading slash so the suffix will match `test/users` but not
122
126
// `test-users`.
123
127
for ending in ENDING_STRINGS {
124
128
if let Some ( uri_without_ending) = uri. strip_suffix ( ending) {
125
- return uri_without_ending;
129
+ return uri_without_ending. to_string ( ) ;
126
130
}
127
131
}
128
132
@@ -134,9 +138,9 @@ fn generate_resource_link(uri: &str) -> &str {
134
138
. map ( |ending| & ending[ 1 ..] ) // this is safe since every ENDING_STRING starts with a slash
135
139
. any ( |item| uri == item)
136
140
{
137
- ""
141
+ "" . to_string ( )
138
142
} else {
139
- uri
143
+ uri. to_string ( )
140
144
}
141
145
}
142
146
@@ -319,12 +323,37 @@ mon, 01 jan 1900 01:00:00 gmt
319
323
320
324
#[ test]
321
325
fn generate_resource_link_00 ( ) {
322
- assert_eq ! ( generate_resource_link( "dbs/second" ) , "dbs/second" ) ;
323
- assert_eq ! ( generate_resource_link( "dbs" ) , "" ) ;
324
- assert_eq ! (
325
- generate_resource_link( "colls/second/third" ) ,
326
- "colls/second/third"
326
+ let request = Request :: new (
327
+ reqwest:: Url :: parse ( "https://.documents.azure.com/dbs/second" ) . unwrap ( ) ,
328
+ http:: Method :: GET ,
329
+ ) ;
330
+ assert_eq ! ( & generate_resource_link( & request) , "dbs/second" ) ;
331
+ }
332
+
333
+ #[ test]
334
+ fn generate_resource_link_01 ( ) {
335
+ let request = Request :: new (
336
+ reqwest:: Url :: parse ( "https://.documents.azure.com/dbs" ) . unwrap ( ) ,
337
+ http:: Method :: GET ,
338
+ ) ;
339
+ assert_eq ! ( & generate_resource_link( & request) , "" ) ;
340
+ }
341
+
342
+ #[ test]
343
+ fn generate_resource_link_02 ( ) {
344
+ let request = Request :: new (
345
+ reqwest:: Url :: parse ( "https://.documents.azure.com/colls/second/third" ) . unwrap ( ) ,
346
+ http:: Method :: GET ,
347
+ ) ;
348
+ assert_eq ! ( & generate_resource_link( & request) , "colls/second/third" ) ;
349
+ }
350
+
351
+ #[ test]
352
+ fn generate_resource_link_03 ( ) {
353
+ let request = Request :: new (
354
+ reqwest:: Url :: parse ( "https://.documents.azure.com/dbs/test_db/colls" ) . unwrap ( ) ,
355
+ http:: Method :: GET ,
327
356
) ;
328
- assert_eq ! ( generate_resource_link( "dbs/test_db/colls" ) , "dbs/test_db" ) ;
357
+ assert_eq ! ( & generate_resource_link( & request ) , "dbs/test_db" ) ;
329
358
}
330
359
}
0 commit comments