Skip to content

Commit 6abfa11

Browse files
committed
PHPC-104: Throw MongoDB\AuthenticationException on auth failure
1 parent b0c41db commit 6abfa11

File tree

6 files changed

+177
-1
lines changed

6 files changed

+177
-1
lines changed

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ if test "$PHONGO" != "no"; then
173173
src/MongoDB/Exception.c \
174174
src/MongoDB/RuntimeException.c \
175175
src/MongoDB/ConnectionException.c \
176+
src/MongoDB/AuthenticationException.c \
176177
src/MongoDB/SSLConnectionException.c \
177178
src/MongoDB/DuplicateKeyException.c \
178179
src/MongoDB/WriteException.c \

php_phongo.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ zend_class_entry* phongo_exception_from_mongoc_domain(uint32_t /* mongoc_error_d
9292
switch(code) {
9393
case 11000: /* Duplicate key */
9494
return php_phongo_duplicatekeyexception_ce;
95+
case MONGOC_ERROR_CLIENT_AUTHENTICATE:
96+
return php_phongo_authenticationexception_ce;
97+
9598
case MONGOC_ERROR_STREAM_INVALID_TYPE:
9699
case MONGOC_ERROR_STREAM_INVALID_STATE:
97100
case MONGOC_ERROR_STREAM_NAME_RESOLUTION:
@@ -103,7 +106,6 @@ zend_class_entry* phongo_exception_from_mongoc_domain(uint32_t /* mongoc_error_d
103106
case MONGOC_ERROR_CLIENT_TOO_BIG:
104107
case MONGOC_ERROR_CLIENT_TOO_SMALL:
105108
case MONGOC_ERROR_CLIENT_GETNONCE:
106-
case MONGOC_ERROR_CLIENT_AUTHENTICATE:
107109
case MONGOC_ERROR_CLIENT_NO_ACCEPTABLE_PEER:
108110
case MONGOC_ERROR_CLIENT_IN_EXHAUST:
109111
case MONGOC_ERROR_PROTOCOL_INVALID_REPLY:
@@ -1438,6 +1440,7 @@ PHP_MINIT_FUNCTION(phongo)
14381440
PHP_MINIT(Exception)(INIT_FUNC_ARGS_PASSTHRU);
14391441
PHP_MINIT(RuntimeException)(INIT_FUNC_ARGS_PASSTHRU);
14401442
PHP_MINIT(ConnectionException)(INIT_FUNC_ARGS_PASSTHRU);
1443+
PHP_MINIT(AuthenticationException)(INIT_FUNC_ARGS_PASSTHRU);
14411444
PHP_MINIT(SSLConnectionException)(INIT_FUNC_ARGS_PASSTHRU);
14421445
PHP_MINIT(WriteException)(INIT_FUNC_ARGS_PASSTHRU);
14431446
PHP_MINIT(DuplicateKeyException)(INIT_FUNC_ARGS_PASSTHRU);

php_phongo_classes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ extern PHONGO_API zend_class_entry *php_phongo_writeresult_ce;
191191
extern PHONGO_API zend_class_entry *php_phongo_exception_ce;
192192
extern PHONGO_API zend_class_entry *php_phongo_runtimeexception_ce;
193193
extern PHONGO_API zend_class_entry *php_phongo_connectionexception_ce;
194+
extern PHONGO_API zend_class_entry *php_phongo_authenticationexception_ce;
194195
extern PHONGO_API zend_class_entry *php_phongo_sslconnectionexception_ce;
195196
extern PHONGO_API zend_class_entry *php_phongo_duplicatekeyexception_ce;
196197
extern PHONGO_API zend_class_entry *php_phongo_writeexception_ce;
@@ -227,6 +228,7 @@ PHP_MINIT_FUNCTION(WriteResult);
227228
PHP_MINIT_FUNCTION(Exception);
228229
PHP_MINIT_FUNCTION(RuntimeException);
229230
PHP_MINIT_FUNCTION(ConnectionException);
231+
PHP_MINIT_FUNCTION(AuthenticationException);
230232
PHP_MINIT_FUNCTION(SSLConnectionException);
231233
PHP_MINIT_FUNCTION(DuplicateKeyException);
232234
PHP_MINIT_FUNCTION(WriteException);

src/MongoDB/AuthenticationException.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
+---------------------------------------------------------------------------+
3+
| PHP Driver for MongoDB |
4+
+---------------------------------------------------------------------------+
5+
| Copyright 2013-2014 MongoDB, Inc. |
6+
| |
7+
| Licensed under the Apache License, Version 2.0 (the "License"); |
8+
| you may not use this file except in compliance with the License. |
9+
| You may obtain a copy of the License at |
10+
| |
11+
| http://www.apache.org/licenses/LICENSE-2.0 |
12+
| |
13+
| Unless required by applicable law or agreed to in writing, software |
14+
| distributed under the License is distributed on an "AS IS" BASIS, |
15+
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16+
| See the License for the specific language governing permissions and |
17+
| limitations under the License. |
18+
+---------------------------------------------------------------------------+
19+
| Copyright (c) 2014, MongoDB, Inc. |
20+
+---------------------------------------------------------------------------+
21+
*/
22+
23+
#ifdef HAVE_CONFIG_H
24+
# include "config.h"
25+
#endif
26+
27+
/* External libs */
28+
#include <bson.h>
29+
#include <mongoc.h>
30+
31+
/* PHP Core stuff */
32+
#include <php.h>
33+
#include <php_ini.h>
34+
#include <ext/standard/info.h>
35+
#include <Zend/zend_interfaces.h>
36+
#include <ext/spl/spl_iterators.h>
37+
/* Our Compatability header */
38+
#include "php_compat_53.h"
39+
40+
/* Our stuffz */
41+
#include "php_phongo.h"
42+
#include "php_bson.h"
43+
#include <ext/spl/spl_exceptions.h>
44+
45+
46+
PHONGO_API zend_class_entry *php_phongo_authenticationexception_ce;
47+
48+
/* {{{ MongoDB\AuthenticationException */
49+
50+
static zend_function_entry php_phongo_authenticationexception_me[] = {
51+
PHP_FE_END
52+
};
53+
54+
/* }}} */
55+
56+
57+
/* {{{ PHP_MINIT_FUNCTION */
58+
PHP_MINIT_FUNCTION(AuthenticationException)
59+
{
60+
(void)type;
61+
(void)module_number;
62+
zend_class_entry ce;
63+
64+
INIT_NS_CLASS_ENTRY(ce, "MongoDB", "AuthenticationException", php_phongo_authenticationexception_me);
65+
php_phongo_authenticationexception_ce = zend_register_internal_class_ex(&ce, php_phongo_connectionexception_ce, NULL TSRMLS_CC);
66+
67+
return SUCCESS;
68+
}
69+
/* }}} */
70+
71+
72+
73+
/*
74+
* Local variables:
75+
* tab-width: 4
76+
* c-basic-offset: 4
77+
* End:
78+
* vim600: noet sw=4 ts=4 fdm=marker
79+
* vim<600: noet sw=4 ts=4
80+
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Connect to MongoDB with using default auth mechanism #002
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
$username = "root";
10+
$password = "tooring";
11+
$database = "admin";
12+
13+
$parsed = parse_url(MONGODB_STANDALONE_AUTH_URI);
14+
$dsn = sprintf("mongodb://%s:%s@%s:%d/%s", $username, $password, $parsed["host"], $parsed["port"], $database);
15+
$manager = new MongoDB\Manager($dsn);
16+
17+
$batch = new MongoDB\WriteBatch;
18+
19+
$batch->insert(array("my" => "value"));
20+
throws(function() use($manager, $batch) {
21+
$retval = $manager->executeWriteBatch(NS, $batch);
22+
}, "MongoDB\AuthenticationException");
23+
24+
?>
25+
===DONE===
26+
<?php exit(0); ?>
27+
--EXPECT--
28+
OK: Got MongoDB\AuthenticationException
29+
===DONE===
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
--TEST--
2+
Connect to MongoDB with using PLAIN auth mechanism #002
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
$username = "root";
10+
$password = "toor";
11+
$database = "admin";
12+
13+
$parsed = parse_url(MONGODB_STANDALONE_PLAIN_URI);
14+
$dsn = sprintf("mongodb://%s:%s@%s:%d/%s", $username, $password, $parsed["host"], $parsed["port"], $database);
15+
$adminmanager = new MongoDB\Manager($dsn);
16+
17+
$cmd = array(
18+
"createUser" => "bugs",
19+
"roles" => [["role" => "readWrite", "db" => DATABASE_NAME]],
20+
);
21+
$command = new MongoDB\Command($cmd);
22+
try {
23+
$result = $adminmanager->executeCommand('$external', $command);
24+
echo "User Created\n";
25+
} catch(Exception $e) {
26+
echo $e->getMessage(), "\n";
27+
}
28+
29+
30+
31+
$username = "bugs";
32+
$password = "wrong-password";
33+
$database = '$external';
34+
35+
$dsn = sprintf("mongodb://%s:%s@%s:%d/%s?authMechanism=PLAIN", $username, $password, $parsed["host"], $parsed["port"], $database);
36+
$manager = new MongoDB\Manager($dsn);
37+
38+
$batch = new MongoDB\WriteBatch();
39+
$batch->insert(array("very" => "important"));
40+
throws(function() use($manager, $batch) {
41+
$manager->executeWriteBatch(NS, $batch);
42+
}, "MongoDB\AuthenticationException");
43+
44+
$cmd = array(
45+
"dropUser" => "bugs",
46+
);
47+
$command = new MongoDB\Command($cmd);
48+
try {
49+
$result = $adminmanager->executeCommand('$external', $command);
50+
echo "User deleted\n";
51+
} catch(Exception $e) {
52+
echo $e->getMessage(), "\n";
53+
}
54+
?>
55+
===DONE===
56+
<?php exit(0); ?>
57+
--EXPECT--
58+
User Created
59+
OK: Got MongoDB\AuthenticationException
60+
User deleted
61+
===DONE===

0 commit comments

Comments
 (0)