49 $mparams = array_merge( [
52 parent::__construct( $mparams );
59 if ( !$this->authCreds ) {
62 if ( $this->authErrorTimestamp !==
null ) {
63 if ( ( time() - $this->authErrorTimestamp ) < 60 ) {
66 $this->authErrorTimestamp =
null;
70 return ( ( time() - $this->authSessionTimestamp ) > $this->params[
'swiftAuthTTL'] );
74 $this->authSessionTimestamp = 0;
75 [ $rcode, $rdesc, $rhdrs, , ] = $req[
'response'];
76 if ( $rcode >= 200 && $rcode <= 299 ) {
78 'auth_token' => $rhdrs[
'x-auth-token'],
79 'storage_url' => $rhdrs[
'x-storage-url']
81 $this->authSessionTimestamp = time();
83 } elseif ( $rcode === 403 ) {
84 $this->authCachedStatus = 401;
85 $this->authCachedReason =
'Authorization Required';
86 $this->authErrorTimestamp = time();
89 $this->authCachedStatus = $rcode;
90 $this->authCachedReason = $rdesc;
91 $this->authErrorTimestamp = time();
100 public function onRequests( array $reqs, Closure $idGeneratorFunc ) {
102 $firstReq = reset( $reqs );
103 if ( $firstReq && count( $reqs ) == 1 && isset( $firstReq[
'isAuth'] ) ) {
109 if ( $needsAuth ===
true ) {
113 $idGeneratorFunc() => [
115 'url' => $this->params[
'swiftAuthUrl'] .
"/v1.0",
117 'x-auth-user' => $this->params[
'swiftUser'],
118 'x-auth-key' => $this->params[
'swiftKey'] ],
123 } elseif ( $needsAuth !==
false ) {
127 foreach ( $reqs as $key => $req ) {
135 $result[$key] = $req;
140 foreach ( $reqs as $key => $req ) {
144 $parts = array_map(
'rawurlencode', explode(
'/', $req[
'url'] ) );
145 $req[
'url'] = $this->authCreds[
'storage_url'] .
'/' . implode(
'/', $parts );
146 $req[
'headers'][
'x-auth-token'] = $this->authCreds[
'auth_token'];
147 $result[$key] = $req;
155 public function onResponses( array $reqs, Closure $idGeneratorFunc ) {
156 $firstReq = reset( $reqs );
157 if ( $firstReq && count( $reqs ) == 1 && isset( $firstReq[
'isAuth'] ) ) {
163 $result = $this->
onRequests( $firstReq[
'chain'], $idGeneratorFunc );
167 foreach ( $firstReq[
'chain'] as $key => $req ) {
175 $result[$key] = $req;
Example virtual rest service for OpenStack Swift.
int null $authCachedStatus
string null $authCachedReason
onResponses(array $reqs, Closure $idGeneratorFunc)
Mangle or replace virtual HTTP(S) requests which have been responded to.
__construct(array $params)
int $authSessionTimestamp
UNIX timestamp.
onRequests(array $reqs, Closure $idGeneratorFunc)
Prepare virtual HTTP(S) requests (for this service) for execution.This method should mangle any of th...
applyAuthResponse(array $req)
int null $authErrorTimestamp
UNIX timestamp.
Virtual HTTP service instance that can be mounted on to a VirtualRESTService.
array $params
Key/value map.