So I could only find one other PHP based HTTP digest auth example on the internet…and it looked as though it might not even work. I wrote an abstract class as a base that allows you to easily build your own implementation.
You’d use it like so:
class MyAuth extends HTTPDigestAuth { // Implementation of abstract methods } $authenticator = new MyAuth(); $user = $authenticator->authenticate(); if(!$user) { die(); }
The HTTPDigestAuth class looks like:
/* Copyright 2010 Alan Shaw http://freestyle-developments.co.uk Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /** * Object orientated PHP HTTP digest authentication. * * Extend this class and implement abstract functions to create your own * HTTP digest authentication implementation. */ abstract class HTTPDigestAuth { //////////////////////////////////////////////////////////////////////// // @public /** * @return an authenticated user object on success, null otherwise. */ public function authenticate() { if(empty($_SERVER['PHP_AUTH_DIGEST'])) { $this->setHeadersUnauthorized(); $this->getResponseBodyUnauthorized(); return null; } $authClientData = new HTTPDigestAuthClientData($_SERVER['PHP_AUTH_DIGEST']); // Check for stale nonce if($this->isStaleNonce($authClientData->nonce)) { $this->setHeadersUnauthorized(true); $this->getResponseBodyUnauthorized(); return null; } // Check for correct nonce count if($authClientData->nc != $this->getNonceCount($authClientData->nonce) + 1) { $this->setHeadersBadRequest(); $this->getResponseBodyBadRequest('Incorrect nonce count'); return null; } $this->incrementNonceCount($authClientData->nonce); // Check request URI is the same as the auth digest uri if($authClientData->uri != $_SERVER['REQUEST_URI']) { $this->setHeadersBadRequest(); $this->getResponseBodyBadRequest('Digest auth URI != request URI'); return null; } // Check opaque is correct if($authClientData->opaque != $this->getOpaque()) { $this->setHeadersBadRequest(); $this->getResponseBodyBadRequest('Incorrect opaque'); return null; } // Check user exists if(!$this->userExists($authClientData->username)) { $this->setHeadersUnauthorized(); $this->getResponseBodyUnauthorized(); return null; } $ha1 = $this->getHA1ForUser($authClientData->username); // Generate A2 hash if($authClientData->qop == 'auth-int') { $a2 = $_SERVER['REQUEST_METHOD'] . ':' . stripslashes($_SERVER['REQUEST_URI']) . ':' . file_get_contents('php://input'); $ha2 = md5($a2); } else { $a2 = $_SERVER['REQUEST_METHOD'] . ':' . stripslashes($_SERVER['REQUEST_URI']); $ha2 = md5($a2); } // Generate the expected response if($authClientData->qop == 'auth' || $authClientData->qop == 'auth-int') { $expectedResponse = md5($ha1 . ':' . $authClientData->nonce . ':' . $authClientData->nc . ':' . $authClientData->cnonce . ':' . $authClientData->qop . ':' . $ha2); } else { $expectedResponse = md5($expectedResponse = $ha1 . ':' . $authClientData->nonce . ':' . $ha2); } // Check request contained the expected response if($authClientData->response != $expectedResponse) { $this->setHeadersBadRequest(); $this->getResponseBodyBadRequest(); return null; } return $this->getUser($authClientData->username); } //////////////////////////////////////////////////////////////////////// // @private private function setHeadersUnauthorized($stale = false) { header('HTTP/1.1 401 Unauthorized'); $authHeader = 'WWW-Authenticate: Digest realm="' . $this->getAuthRealm() . '",qop="auth-int,auth",algorithm="MD5",nonce="' . $this->createNonce() . '",opaque="' . $this->getOpaque() . '"'; if($stale) { $authHeader .= ',stale=TRUE'; } header($authHeader); } private static function setHeadersBadRequest() { header('HTTP/1.1 400 Bad Request'); } //////////////////////////////////////////////////////////////////////// // @optional protected function getResponseBodyUnauthorized($reason = '') { ?> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Error</title> </head> <body> <h1>401 Unauthorized.</h1> <?php if($reason) { ?> <p><?php echo htmlspecialchars($reason); ?></p> <?php } ?> </body> </HTML> <?php } protected function getResponseBodyBadRequest($reason = '') { ?> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Error</title> </head> <body> <h1>400 Bad Request.</h1> <?php if($reason) { ?> <p><?php echo htmlspecialchars($reason); ?></p> <?php } ?> </body> </HTML> <?php } //////////////////////////////////////////////////////////////////////// // @required /** * Gets the authentication realm for this class * * @return String */ abstract protected function getAuthRealm(); /** * Gets the opaque for this class * * @return String */ abstract protected function getOpaque(); /** * Creates a new nonce to send to the client * * @return String */ abstract protected function createNonce(); /** * Returns whether or not this nonce has expired. Should return true for * non existent nonce. * * @param String $nonce * @return Boolean */ abstract protected function isStaleNonce($nonce); /** * Gets the current request count for a particular nonce * * @param String $nonce The nonce to get the count of * @return uint The current nonce count */ abstract protected function getNonceCount($nonce); /** * Increments the nonce count by 1 * * @param String $nonce The nonce to increment */ abstract protected function incrementNonceCount($nonce); /** * Returns a boolean indicating whether or not a user with the specified * username exists. * * @param String $username * @return Boolean */ abstract protected function userExists($username); /** * Returns the A1 hash for the specified user. * i.e. return md5('username:realm:password') * * @param String $username * @return String */ abstract protected function getHA1ForUser($username); /** * Returns a user instance that belongs to the user with the username * provided. * * @param String $username * @return ??? */ abstract protected function getUser($username); } /** * @private */ class HTTPDigestAuthClientData { public $username; public $nonce; public $nc; public $cnonce; public $qop; public $uri; public $response; public $opaque; public function __construct($header) { preg_match_all('@(username|nonce|uri|nc|cnonce|qop|response|opaque)=[\'"]?([^\'",]+)@', $header, $t); $data = array_combine($t[1], $t[2]); $this->username = $data['username']; $this->nonce = $data['nonce']; $this->nc = $data['nc']; $this->cnonce = $data['cnonce']; $this->qop = $data['qop']; $this->uri = $data['uri']; $this->response = $data['response']; $this->opaque = $data['opaque']; } }
You can download the full source here: github.com/alanshaw/php-http-digest-auth
第3話にて、犬が苦手なためにパフを寮内で飼うことに反対していたが、ゼツボーグから自分を守ろうとしたパフを見て考えを改め、パフを飼うことに賛成する。第3話にて、パフを寮内で飼うことに当初は反対していたが、パフと遊んで交流したことで賛成に転じている。動画を伴わせることが認められ、限られた時間の中で突飛なキーワードを発するなどの、これまでになかった型のCMが次々制作され、流行語の源泉となった(後述)。 ウィリアムズは、谷口藍田に英語や海外事情を教え、藍田からは和漢の学について教えを受けた。
Explore innovative ways to regulate your heart rhythm with how to order super-viagra online . Optimizing your treatment plan? Consider adding prednisone prescription to your regimen Discover affordable choices on the internet for enhancing your health journey. Keeping your digestive system healthy is crucial and https://profitplusfinancial.com/item/prednisone-price/ can assist. Explore options to purchase it virtually for optimal gastrointestinal well-being. Now discover sildalis by browsing our platform. Secure your optimal price immediately.
Yes the Food and Drug Administration has approved isotretinoin for combating serious acne conditions. This signifies a significant advancement in skin care. Zero hassle: acquire your cost of viagra tablets without complication. Maximize your well-being with a holistic approach by opting for https://csicls.org/item/tadalafil-20mg/ a solution thats readily available for boosting male health and vitality.
Zero in on affordability and convenience with URL=https://profitplusfinancial.com/cialis/cialis 10 mg prix en pharmacie belgique/URL your go-to source for acquiring effective ED solutions on the web. Valuable insights reveal that obtaining tadalafil assists in improving heart health showcasing its efficacy. Various individuals opt to buy their health supplies on the internet with https://umichicago.com/drugs/dostinex/ being a popular choice among selections. Shopping for URL=https://altavillaspa.com/generic-for-prednisone/prednisone zu kaufen/URL ? Secure your prescription through our website for trusted malaria prevention.
Nab your essential medication: URL=https://damcf.org/prednisone/buy prednisone without a prescription/URL through our reliable website. This vital remedy is now available effortlessly. Visit canadian pharmacy buy priligy today and locate budget-friendly solutions for handling your pain. X-plore your choices for managing irregular heartbeat with proven solutions. Find out about how https://airjamaicacharter.com/pill/nizagara/ can aid in regulating your heart rate offering tranquility. The availability of URL=https://maker2u.com/item/levitra/levitra cost/URL ensures that individuals can without difficulty obtain crucial medication to control their condition via the internet.