@@ -86,4 +86,42 @@ public function containerExists(string $name): bool
86
86
throw $ e ;
87
87
}
88
88
}
89
+
90
+ /**
91
+ * Creates a temporary URL to access object in private containers.
92
+ * This method loosely follows swift command's way to generate temporary url: `swift tempurl $METHOD $EXPIRE $PATH $KEY`.
93
+ *
94
+ * @param string $method An HTTP method to allow for this temporary URL. Any of GET, POST, HEAD, PUT, POST, DELETE.
95
+ * @param int $expires Unix timestamp
96
+ * @param string $path The full path or storage URL to the Swift object. Example: '/v1/AUTH_account/c/o' or: 'http://saio:8080/v1/AUTH_account/c/o'
97
+ * For prefix based signature, set path to 'prefix:/v1/AUTH_account/container/pre'
98
+ * @param string $key The secret temporary URL key set on the Swift cluster*
99
+ * @param string $ipRange [OPTIONAL] If present, the temporary URL will be restricted to the given ip or ip range
100
+ * @param string $digest [OPTIONAL] The digest algorithm to be used may be configured by the operator. Default to sha1.
101
+ * Check the tempurl.allowed_digests entry in the cluster's capabilities response to see which algorithms are supported by your
102
+ * deployment;
103
+ *
104
+ * @return string
105
+ *
106
+ * @throws \RuntimeException
107
+ */
108
+ public function tempUrl (string $ method , int $ expires , string $ path , string $ key , string $ ipRange = null , string $ digest = 'sha1 ' ): string
109
+ {
110
+ if (!function_exists ('hash_hmac ' )) {
111
+ throw new \RuntimeException (sprintf ('tempUrl requires hash extension enabled. ' ));
112
+ }
113
+
114
+ if ($ ipRange ) {
115
+ $ message = sprintf ("ip=%s \n%s \n%s \n%s " , $ ipRange , $ method , $ expires , $ path );
116
+ } else {
117
+ $ message = sprintf ("%s \n%s \n%s " , $ method , $ expires , $ path );
118
+ }
119
+
120
+ $ signature = hash_hmac ($ digest , $ message , $ key );
121
+
122
+ // sha512 requires prefixing signature
123
+ $ signature = 'sha512 ' === $ digest ? 'sha512: ' .$ signature : $ signature ;
124
+
125
+ return sprintf ('%s?temp_url_sig=%s&temp_url_expires=%s ' , $ path , $ signature , $ expires );
126
+ }
89
127
}
0 commit comments