I, moron
[m6w6/ext-http] / http_auth_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include "php.h"
23 #include "ext/standard/base64.h"
24
25 #include "SAPI.h"
26
27 #include "php_http.h"
28 #include "php_http_api.h"
29 #include "php_http_std_defs.h"
30 #include "php_http_send_api.h"
31
32 /*
33 * TODO:
34 * - Digest Auth
35 */
36
37 /* {{{ STATUS http_auth_header(char *, char*) */
38 PHP_HTTP_API STATUS _http_auth_header(const char *type, const char *realm TSRMLS_DC)
39 {
40 char realm_header[1024] = {0};
41 snprintf(realm_header, 1023, "WWW-Authenticate: %s realm=\"%s\"", type, realm);
42 return http_send_status_header(401, realm_header);
43 }
44 /* }}} */
45
46 /* {{{ STATUS http_auth_credentials(char **, char **) */
47 PHP_HTTP_API STATUS _http_auth_credentials(char **user, char **pass TSRMLS_DC)
48 {
49 if (strncmp(sapi_module.name, "isapi", 5)) {
50 zval *zuser, *zpass;
51
52 HTTP_GSC(zuser, "PHP_AUTH_USER", FAILURE);
53 HTTP_GSC(zpass, "PHP_AUTH_PW", FAILURE);
54
55 *user = estrndup(Z_STRVAL_P(zuser), Z_STRLEN_P(zuser));
56 *pass = estrndup(Z_STRVAL_P(zpass), Z_STRLEN_P(zpass));
57
58 return SUCCESS;
59 } else {
60 zval *zauth = NULL;
61 HTTP_GSC(zauth, "HTTP_AUTHORIZATION", FAILURE);
62 {
63 int decoded_len;
64 char *colon, *decoded = php_base64_decode(Z_STRVAL_P(zauth), Z_STRLEN_P(zauth), &decoded_len);
65
66 if (colon = strchr(decoded + 6, ':')) {
67 *user = estrndup(decoded + 6, colon - decoded - 6);
68 *pass = estrndup(colon + 1, decoded + decoded_len - colon - 6 - 1);
69
70 return SUCCESS;
71 } else {
72 return FAILURE;
73 }
74 }
75 }
76 }
77 /* }}} */
78
79
80 /*
81 * Local variables:
82 * tab-width: 4
83 * c-basic-offset: 4
84 * End:
85 * vim600: sw=4 ts=4 fdm=marker
86 * vim<600: sw=4 ts=4
87 */
88