|
| 1 | +/* gcc example.c -o example $(pkg-config --cflags --libs libmongoc-1.0) */ |
| 2 | + |
| 3 | +/* ./example-scram */ |
| 4 | + |
| 5 | +#include <mongoc.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | + |
| 9 | +int |
| 10 | +main (int argc, |
| 11 | + char *argv[]) |
| 12 | +{ |
| 13 | + mongoc_client_t *client = NULL; |
| 14 | + mongoc_database_t *database = NULL; |
| 15 | + mongoc_collection_t *collection = NULL; |
| 16 | + mongoc_cursor_t *cursor = NULL; |
| 17 | + bson_error_t error; |
| 18 | + const char *uristr = "mongodb://127.0.0.1/"; |
| 19 | + const char *authuristr; |
| 20 | + bson_t roles; |
| 21 | + bson_t query; |
| 22 | + const bson_t *doc; |
| 23 | + |
| 24 | + if (argc != 2) { |
| 25 | + printf("%s - [implicit|scram|cr]\n", argv[0]); |
| 26 | + return 1; |
| 27 | + } |
| 28 | + |
| 29 | + if (strcmp(argv[1], "implicit") == 0) { |
| 30 | + authuristr = "mongodb://user,=:[email protected]/test"; |
| 31 | + } else if (strcmp(argv[1], "scram") == 0) { |
| 32 | + authuristr = "mongodb://user,=:[email protected]/test?authMechanism=SCRAM-SHA-1"; |
| 33 | + } else if (strcmp(argv[1], "cr") == 0) { |
| 34 | + authuristr = "mongodb://user,=:[email protected]/test?authMechanism=MONGODB-CR"; |
| 35 | + } else { |
| 36 | + printf("%s - [implicit|scram|cr]\n", argv[0]); |
| 37 | + } |
| 38 | + |
| 39 | + mongoc_init (); |
| 40 | + |
| 41 | + bson_init (&roles); |
| 42 | + bson_init (&query); |
| 43 | + |
| 44 | + client = mongoc_client_new (uristr); |
| 45 | + |
| 46 | + if (!client) { |
| 47 | + fprintf (stderr, "Failed to parse URI.\n"); |
| 48 | + goto CLEANUP; |
| 49 | + } |
| 50 | + |
| 51 | + database = mongoc_client_get_database (client, "test"); |
| 52 | + |
| 53 | + BCON_APPEND (&roles, |
| 54 | + "0", "{", "role", "root", "db", "admin", "}"); |
| 55 | + |
| 56 | + mongoc_database_add_user (database, "user,=", "pass", &roles, NULL, &error); |
| 57 | + |
| 58 | + mongoc_database_destroy (database); |
| 59 | + database = NULL; |
| 60 | + |
| 61 | + mongoc_client_destroy (client); |
| 62 | + client = NULL; |
| 63 | + |
| 64 | + client = mongoc_client_new (authuristr); |
| 65 | + |
| 66 | + if (!client) { |
| 67 | + fprintf (stderr, "failed to parse SCRAM uri\n"); |
| 68 | + goto CLEANUP; |
| 69 | + } |
| 70 | + |
| 71 | + collection = mongoc_client_get_collection (client, "test", "test"); |
| 72 | + |
| 73 | + cursor = mongoc_collection_find (collection, 0, 0, 0, 0, &query, NULL, NULL); |
| 74 | + |
| 75 | + mongoc_cursor_next (cursor, &doc); |
| 76 | + |
| 77 | + if (mongoc_cursor_error (cursor, &error)) { |
| 78 | + fprintf (stderr, "Auth error: %s\n", error.message); |
| 79 | + goto CLEANUP; |
| 80 | + } |
| 81 | + |
| 82 | +CLEANUP: |
| 83 | + |
| 84 | + bson_destroy (&roles); |
| 85 | + bson_destroy (&query); |
| 86 | + |
| 87 | + if (collection) { |
| 88 | + mongoc_collection_destroy (collection); |
| 89 | + } |
| 90 | + |
| 91 | + if (database) { |
| 92 | + mongoc_database_destroy (database); |
| 93 | + } |
| 94 | + |
| 95 | + if (client) { |
| 96 | + mongoc_client_destroy (client); |
| 97 | + } |
| 98 | + |
| 99 | + if (cursor) { |
| 100 | + mongoc_cursor_destroy (cursor); |
| 101 | + } |
| 102 | + |
| 103 | + mongoc_cleanup (); |
| 104 | + |
| 105 | + return EXIT_SUCCESS; |
| 106 | +} |
0 commit comments