Skip to main content
Version: 1.0.1

Initialize Generator

The recovery-code generator is used to generate recovery-codes, which are randomly generated strings in block form.

Create recovery-code generator

You can create an instance of the RecoveryCodeGenerator in the following way:

val recoveryCodeGenerator = RecoveryCodeGenerator()

Spring Boot

Instead of creating a new instance of a generator each time a token is checked, it is also possible to create a bean within Spring. This allows to configure the generator once and this configuration is maintained each time the bean is injected into a component.

@Bean
fun recoveryCodeGenerator(): RecoveryCodeGenerator {
val generator = RecoveryCodeGenerator()
generator.blockLength = 12
generator.numberOfBlocks = 6
return generator
}

This bean can then be injected in the constructor of any class marked with @Component (@Service, ...).

@Component
class CustomComponent(private val recoveryCodeGenerator: RecoveryCodeGenerator) {
//...
}

Customize properties

It is possible to customize the properties of the generator, either by setters or applying them in the constructor.

Code length

The code length specifies how long a generated code will be. If the code length is changed, it is necessary that the user's authenticator app supports this as well.

Could break logic

Most authenticator apps, such as those from Microsoft or Google, use a length of 6 digits. If you change this number, they are no longer usable. However, remember that that they are widely used before you drop support for them.