1+ // start-gssapi-connection-string
2+ MongoClient mongoClient = MongoClients
3+ .create ("<username>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI" );
4+ // end-gssapi-connection-string
5+
6+ // start-gssapi-mongocredential
7+ MongoCredential credential = MongoCredential .createGSSAPICredential ("<username>" );
8+
9+ MongoClient mongoClient = MongoClients .create (
10+ MongoClientSettings .builder ()
11+ .applyToClusterSettings (builder ->
12+ builder .hosts (Arrays .asList (new ServerAddress ("<hostname>" , <port > ))))
13+ .credential (credential )
14+ .build ());
15+ // end-gssapi-mongocredential
16+
17+ // start-gssapi-connection-string-properties
18+ MongoClient mongoClient = MongoClients
19+ .create ("<username>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME:myService" );
20+ // end-gssapi-connection-string-properties
21+
22+ // start-gssapi-service-key
23+ MongoCredential credential = MongoCredential
24+ .createGSSAPICredential ("<username>" );
25+ credential = credential
26+ .withMechanismProperty (MongoCredential .SERVICE_NAME_KEY , "<myService>" );
27+ // end-gssapi-service-key
28+
29+ // start-gssapi-subject-key
30+ LoginContext loginContext = new LoginContext (<LoginModule implementation from JAAS config >);
31+ loginContext .login ();
32+ Subject subject = loginContext .getSubject ();
33+
34+ MongoCredential credential = MongoCredential
35+ .createGSSAPICredential ("<username>" );
36+ credential = credential
37+ .withMechanismProperty (MongoCredential .JAVA_SUBJECT_KEY , subject );
38+ // end-gssapi-subject-key
39+
40+ // start-gssapi-ticket-cache
41+ /* All MongoClient instances sharing this instance of KerberosSubjectProvider
42+ will share a Kerberos ticket cache */
43+ String myLoginContext = "myContext" ;
44+ MongoCredential credential = MongoCredential
45+ .createGSSAPICredential (<username > );
46+
47+ /* Login context defaults to "com.sun.security.jgss.krb5.initiate"
48+ if unspecified in KerberosSubjectProvider */
49+ credential = credential
50+ .withMechanismProperty (MongoCredential .JAVA_SUBJECT_PROVIDER_KEY ,
51+ new KerberosSubjectProvider (myLoginContext ));
52+ // end-gssapi-ticket-cache
53+
54+ // start-ldap-connection-string
55+ MongoClient mongoClient = MongoClients
56+ .create ("<ldap_username>:<ldap_password>@<hostname>:<port>/?authSource=$external&authMechanism=PLAIN" );
57+ // end-ldap-connection-string
58+
59+ // start-ldap-mongocredential
60+ MongoCredential credential = MongoCredential
61+ .createPlainCredential (<ldap_username >, "$external" , <ldap_password >);
62+
63+ MongoClient mongoClient = MongoClients .create (
64+ MongoClientSettings .builder ()
65+ .applyToClusterSettings (builder ->
66+ builder .hosts (Arrays .asList (new ServerAddress ("<hostname>" , <port > ))))
67+ .credential (credential )
68+ .build ());
69+ // end-ldap-mongocredential
70+
71+ // start-azure-oidc-connection-string
72+ MongoClient mongoClient = MongoClients .create (
73+ "mongodb://<username>@<hostname>:<port>/?" +
74+ "?authMechanism=MONGODB-OIDC" +
75+ "&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<percent-encoded audience>" );
76+ // end-azure-oidc-connection-string
77+
78+ // start-azure-oidc-mongocredential
79+ MongoCredential credential = MongoCredential .createOidcCredential ("<username>" )
80+ .withMechanismProperty ("ENVIRONMENT" , "azure" )
81+ .withMechanismProperty ("TOKEN_RESOURCE" , "<audience>" );
82+
83+ MongoClient mongoClient = MongoClients .create (
84+ MongoClientSettings .builder ()
85+ .applyToClusterSettings (builder ->
86+ builder .hosts (Arrays .asList (new ServerAddress ("<hostname>" , <port > ))))
87+ .credential (credential )
88+ .build ());
89+ // end-azure-oidc-mongocredential
90+
91+ // start-gcp-oidc-connection-string
92+ MongoClient mongoClient = MongoClients .create (
93+ "mongodb://<hostname>:<port>/?" +
94+ "authMechanism=MONGODB-OIDC" +
95+ "&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:<percent-encoded audience>" );
96+ // end-gcp-oidc-connection-string
97+
98+ // start-gcp-oidc-mongocredential
99+ MongoCredential credential = MongoCredential .createOidcCredential ()
100+ .withMechanismProperty ("ENVIRONMENT" , "gcp" )
101+ .withMechanismProperty ("TOKEN_RESOURCE" , "<audience>" );
102+
103+ MongoClient mongoClient = MongoClients .create (
104+ MongoClientSettings .builder ()
105+ .applyToClusterSettings (builder ->
106+ builder .hosts (Arrays .asList (new ServerAddress ("<hostname>" , <port > ))))
107+ .credential (credential )
108+ .build ());
109+ // end-gcp-oidc-mongocredential
110+
111+ // start-oidc-callback-create
112+ MongoCredential credential = MongoCredential .createOidcCredential (null )
113+ .withMechanismProperty ("OIDC_CALLBACK" , (context ) -> {
114+ String accessToken = ...
115+ return new OidcCallbackResult (accessToken );
116+ });
117+ // end-oidc-callback-create
118+
119+ // start-oidc-callback
120+ MongoCredential credential = MongoCredential .createOidcCredential (null )
121+ .withMechanismProperty ("OIDC_CALLBACK" , (context ) -> {
122+ string accessToken = new String (Files .readAllBytes (Paths .get ("access-token.dat" ));
123+ return new OidcCallbackResult (accessToken );
124+ });
125+
126+ MongoClient mongoClient = MongoClients .create (
127+ MongoClientSettings .builder ()
128+ .applyToClusterSettings (builder ->
129+ builder .hosts (Arrays .asList (new ServerAddress ("<hostname>" , <port > ))))
130+ .credential (credential )
131+ .build ());
132+ // end-oidc-callback
0 commit comments