Skip to content

Commit 2c103f3

Browse files
committed
Add password storage Kotlin samples to docs
Issue gh-8172
1 parent 10c66d2 commit 2c103f3

File tree

1 file changed

+112
-8
lines changed

1 file changed

+112
-8
lines changed

docs/manual/src/docs/asciidoc/_includes/about/authentication/password-storage.adoc

+112-8
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,26 @@ You can easily construct an instance of `DelegatingPasswordEncoder` using `Pass
6868

6969
.Create Default DelegatingPasswordEncoder
7070
====
71-
[source,java]
71+
.Java
72+
[source,java,role="primary"]
7273
----
7374
PasswordEncoder passwordEncoder =
7475
PasswordEncoderFactories.createDelegatingPasswordEncoder();
7576
----
77+
78+
.Kotlin
79+
[source,kotlin,role="secondary"]
80+
----
81+
val passwordEncoder: PasswordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder()
82+
----
7683
====
7784

7885
Alternatively, you may create your own custom instance. For example:
7986

8087
.Create Custom DelegatingPasswordEncoder
8188
====
82-
[source,java]
89+
.Java
90+
[source,java,role="primary"]
8391
----
8492
String idForEncode = "bcrypt";
8593
Map encoders = new HashMap<>();
@@ -92,6 +100,20 @@ encoders.put("sha256", new StandardPasswordEncoder());
92100
PasswordEncoder passwordEncoder =
93101
new DelegatingPasswordEncoder(idForEncode, encoders);
94102
----
103+
104+
.Kotlin
105+
[source,kotlin,role="secondary"]
106+
----
107+
val idForEncode = "bcrypt"
108+
val encoders: MutableMap<String, PasswordEncoder> = mutableMapOf()
109+
encoders[idForEncode] = BCryptPasswordEncoder()
110+
encoders["noop"] = NoOpPasswordEncoder.getInstance()
111+
encoders["pbkdf2"] = Pbkdf2PasswordEncoder()
112+
encoders["scrypt"] = SCryptPasswordEncoder()
113+
encoders["sha256"] = StandardPasswordEncoder()
114+
115+
val passwordEncoder: PasswordEncoder = DelegatingPasswordEncoder(idForEncode, encoders)
116+
----
95117
====
96118

97119
[[authentication-password-storage-dpe-format]]
@@ -180,7 +202,8 @@ There are convenience mechanisms to make this easier, but this is still not inte
180202

181203
.withDefaultPasswordEncoder Example
182204
====
183-
[source,java,attrs="-attributes"]
205+
.Java
206+
[source,java,role="primary",attrs="-attributes"]
184207
----
185208
User user = User.withDefaultPasswordEncoder()
186209
.username("user")
@@ -190,13 +213,26 @@ User user = User.withDefaultPasswordEncoder()
190213
System.out.println(user.getPassword());
191214
// {bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG
192215
----
216+
217+
.Kotlin
218+
[source,kotlin,role="secondary",attrs="-attributes"]
219+
----
220+
val user = User.withDefaultPasswordEncoder()
221+
.username("user")
222+
.password("password")
223+
.roles("user")
224+
.build()
225+
println(user.password)
226+
// {bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG
227+
----
193228
====
194229

195230
If you are creating multiple users, you can also reuse the builder.
196231

197232
.withDefaultPasswordEncoder Reusing the Builder
198233
====
199-
[source,java]
234+
.Java
235+
[source,java,role="primary"]
200236
----
201237
UserBuilder users = User.withDefaultPasswordEncoder();
202238
User user = users
@@ -210,6 +246,22 @@ User admin = users
210246
.roles("USER","ADMIN")
211247
.build();
212248
----
249+
250+
.Kotlin
251+
[source,kotlin,role="secondary"]
252+
----
253+
val users = User.withDefaultPasswordEncoder()
254+
val user = users
255+
.username("user")
256+
.password("password")
257+
.roles("USER")
258+
.build()
259+
val admin = users
260+
.username("admin")
261+
.password("password")
262+
.roles("USER", "ADMIN")
263+
.build()
264+
----
213265
====
214266

215267
This does hash the password that is stored, but the passwords are still exposed in memory and in the compiled source code.
@@ -273,14 +325,27 @@ The `BCryptPasswordEncoder` implementation uses the widely supported https://en.
273325
In order to make it more resistent to password cracking, bcrypt is deliberately slow.
274326
Like other adaptive one-way functions, it should be tuned to take about 1 second to verify a password on your system.
275327

276-
[source,java]
328+
.BCryptPasswordEncoder
329+
====
330+
.Java
331+
[source,java,role="primary"]
277332
----
278333
// Create an encoder with strength 16
279334
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(16);
280335
String result = encoder.encode("myPassword");
281336
assertTrue(encoder.matches("myPassword", result));
282337
----
283338
339+
.Kotlin
340+
[source,kotlin,role="secondary"]
341+
----
342+
// Create an encoder with strength 16
343+
val encoder = BCryptPasswordEncoder(16)
344+
val result: String = encoder.encode("myPassword")
345+
assertTrue(encoder.matches("myPassword", result))
346+
----
347+
====
348+
284349
[[authentication-password-storage-argon2]]
285350
== Argon2PasswordEncoder
286351

@@ -290,14 +355,27 @@ In order to defeat password cracking on custom hardware, Argon2 is a deliberatel
290355
Like other adaptive one-way functions, it should be tuned to take about 1 second to verify a password on your system.
291356
The current implementation if the `Argon2PasswordEncoder` requires BouncyCastle.
292357

293-
[source,java]
358+
.Argon2PasswordEncoder
359+
====
360+
.Java
361+
[source,java,role="primary"]
294362
----
295363
// Create an encoder with all the defaults
296364
Argon2PasswordEncoder encoder = new Argon2PasswordEncoder();
297365
String result = encoder.encode("myPassword");
298366
assertTrue(encoder.matches("myPassword", result));
299367
----
300368
369+
.Kotlin
370+
[source,kotlin,role="secondary"]
371+
----
372+
// Create an encoder with all the defaults
373+
val encoder = Argon2PasswordEncoder()
374+
val result: String = encoder.encode("myPassword")
375+
assertTrue(encoder.matches("myPassword", result))
376+
----
377+
====
378+
301379
[[authentication-password-storage-pbkdf2]]
302380
== Pbkdf2PasswordEncoder
303381

@@ -306,29 +384,55 @@ In order to defeat password cracking PBKDF2 is a deliberately slow algorithm.
306384
Like other adaptive one-way functions, it should be tuned to take about 1 second to verify a password on your system.
307385
This algorithm is a good choice when FIPS certification is required.
308386

309-
[source,java]
387+
.Pbkdf2PasswordEncoder
388+
====
389+
.Java
390+
[source,java,role="primary"]
310391
----
311392
// Create an encoder with all the defaults
312393
Pbkdf2PasswordEncoder encoder = new Pbkdf2PasswordEncoder();
313394
String result = encoder.encode("myPassword");
314395
assertTrue(encoder.matches("myPassword", result));
315396
----
316397
398+
.Kotlin
399+
[source,kotlin,role="secondary"]
400+
----
401+
// Create an encoder with all the defaults
402+
val encoder = Pbkdf2PasswordEncoder()
403+
val result: String = encoder.encode("myPassword")
404+
assertTrue(encoder.matches("myPassword", result))
405+
----
406+
====
407+
317408
[[authentication-password-storage-scrypt]]
318409
== SCryptPasswordEncoder
319410

320411
The `SCryptPasswordEncoder` implementation uses https://en.wikipedia.org/wiki/Scrypt[scrypt] algorithm to hash the passwords.
321412
In order to defeat password cracking on custom hardware scrypt is a deliberately slow algorithm that requires large amounts of memory.
322413
Like other adaptive one-way functions, it should be tuned to take about 1 second to verify a password on your system.
323414

324-
[source,java]
415+
.SCryptPasswordEncoder
416+
====
417+
.Java
418+
[source,java,role="primary"]
325419
----
326420
// Create an encoder with all the defaults
327421
SCryptPasswordEncoder encoder = new SCryptPasswordEncoder();
328422
String result = encoder.encode("myPassword");
329423
assertTrue(encoder.matches("myPassword", result));
330424
----
331425
426+
.Kotlin
427+
[source,kotlin,role="secondary"]
428+
----
429+
// Create an encoder with all the defaults
430+
val encoder = SCryptPasswordEncoder()
431+
val result: String = encoder.encode("myPassword")
432+
assertTrue(encoder.matches("myPassword", result))
433+
----
434+
====
435+
332436
[[authentication-password-storage-other]]
333437
== Other PasswordEncoders
334438

0 commit comments

Comments
 (0)