@ -16,7 +16,11 @@
package org.thingsboard.server.controller ;
import io.swagger.v3.oas.annotations.Parameter ;
import io.swagger.v3.oas.annotations.media.ArraySchema ;
import io.swagger.v3.oas.annotations.media.Content ;
import io.swagger.v3.oas.annotations.media.Schema ;
import io.swagger.v3.oas.annotations.responses.ApiResponse ;
import io.swagger.v3.oas.annotations.responses.ApiResponses ;
import jakarta.validation.Valid ;
import lombok.Data ;
import lombok.RequiredArgsConstructor ;
@ -66,6 +70,11 @@ public class TwoFactorAuthConfigController extends BaseController {
" \"SMS\": {\n \"providerType\": \"SMS\",\n \"useByDefault\": false,\n \"phoneNumber\": \"+380501253652\"\n }\n" +
" }\n}\n```" +
ControllerConstants . AVAILABLE_FOR_ANY_AUTHORIZED_USER )
@ApiResponses ( value = {
@ApiResponse ( responseCode = "200" , description = "OK" ,
content = @Content ( mediaType = "application/json" ,
schema = @Schema ( implementation = AccountTwoFaSettings . class ) ) )
} )
@GetMapping ( "/account/settings" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')" )
public AccountTwoFaSettings getAccountTwoFaSettings ( ) throws ThingsboardException {
@ -98,6 +107,11 @@ public class TwoFactorAuthConfigController extends BaseController {
"}\n```" + NEW_LINE +
"Will throw an error (Bad Request) if the provider is not configured for usage. " +
ControllerConstants . AVAILABLE_FOR_ANY_AUTHORIZED_USER )
@ApiResponses ( value = {
@ApiResponse ( responseCode = "200" , description = "OK" ,
content = @Content ( mediaType = "application/json" ,
schema = @Schema ( implementation = TwoFaAccountConfig . class ) ) )
} )
@PostMapping ( "/account/config/generate" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')" )
public TwoFaAccountConfig generateTwoFaAccountConfig ( @Parameter ( description = "2FA provider type to generate new account config for" , schema = @Schema ( defaultValue = "TOTP" , requiredMode = Schema . RequiredMode . REQUIRED ) )
@ -128,7 +142,10 @@ public class TwoFactorAuthConfigController extends BaseController {
ControllerConstants . AVAILABLE_FOR_ANY_AUTHORIZED_USER )
@PostMapping ( "/account/config/submit" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')" )
public void submitTwoFaAccountConfig ( @Valid @RequestBody TwoFaAccountConfig accountConfig ) throws Exception {
public void submitTwoFaAccountConfig ( @io.swagger.v3.oas.annotations.parameters.RequestBody ( description = "2FA account config to submit for verification" ,
required = true , content = @Content ( mediaType = "application/json" ,
schema = @Schema ( implementation = TwoFaAccountConfig . class ) ) )
@Valid @RequestBody TwoFaAccountConfig accountConfig ) throws Exception {
SecurityUser user = getCurrentUser ( ) ;
twoFactorAuthService . prepareVerificationCode ( user , accountConfig , false ) ;
}
@ -138,9 +155,17 @@ public class TwoFactorAuthConfigController extends BaseController {
"Returns whole account's 2FA settings object.\n" +
"Will throw an error (Bad Request) if the provider is not configured for usage. " +
ControllerConstants . AVAILABLE_FOR_ANY_AUTHORIZED_USER )
@ApiResponses ( value = {
@ApiResponse ( responseCode = "200" , description = "OK" ,
content = @Content ( mediaType = "application/json" ,
schema = @Schema ( implementation = AccountTwoFaSettings . class ) ) )
} )
@PostMapping ( "/account/config" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')" )
public AccountTwoFaSettings verifyAndSaveTwoFaAccountConfig ( @Valid @RequestBody TwoFaAccountConfig accountConfig ,
public AccountTwoFaSettings verifyAndSaveTwoFaAccountConfig ( @io.swagger.v3.oas.annotations.parameters.RequestBody ( description = "2FA account config to submit for verification" ,
required = true , content = @Content ( mediaType = "application/json" ,
schema = @Schema ( implementation = TwoFaAccountConfig . class ) ) )
@Valid @RequestBody TwoFaAccountConfig accountConfig ,
@RequestParam ( required = false ) String verificationCode ) throws Exception {
SecurityUser user = getCurrentUser ( ) ;
if ( twoFaConfigManager . getTwoFaAccountConfig ( user . getTenantId ( ) , user . getId ( ) , accountConfig . getProviderType ( ) ) . isPresent ( ) ) {
@ -166,9 +191,17 @@ public class TwoFactorAuthConfigController extends BaseController {
"```\n{\n \"useByDefault\": true\n}\n```\n" +
"Returns whole account's 2FA settings object.\n" +
ControllerConstants . AVAILABLE_FOR_ANY_AUTHORIZED_USER )
@ApiResponses ( value = {
@ApiResponse ( responseCode = "200" , description = "OK" ,
content = @Content ( mediaType = "application/json" ,
schema = @Schema ( implementation = AccountTwoFaSettings . class ) ) )
} )
@PutMapping ( "/account/config" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')" )
public AccountTwoFaSettings updateTwoFaAccountConfig ( @RequestParam TwoFaProviderType providerType ,
@io.swagger.v3.oas.annotations.parameters.RequestBody ( description = "2FA account config update request" ,
required = true , content = @Content ( mediaType = "application/json" ,
schema = @Schema ( implementation = TwoFaAccountConfigUpdateRequest . class ) ) )
@RequestBody TwoFaAccountConfigUpdateRequest updateRequest ) throws ThingsboardException {
SecurityUser user = getCurrentUser ( ) ;
@ -182,6 +215,11 @@ public class TwoFactorAuthConfigController extends BaseController {
"Delete 2FA config for a given 2FA provider type. \n" +
"Returns whole account's 2FA settings object.\n" +
ControllerConstants . AVAILABLE_FOR_ANY_AUTHORIZED_USER )
@ApiResponses ( value = {
@ApiResponse ( responseCode = "200" , description = "OK" ,
content = @Content ( mediaType = "application/json" ,
schema = @Schema ( implementation = AccountTwoFaSettings . class ) ) )
} )
@DeleteMapping ( "/account/config" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')" )
public AccountTwoFaSettings deleteTwoFaAccountConfig ( @RequestParam TwoFaProviderType providerType ) throws ThingsboardException {
@ -196,6 +234,11 @@ public class TwoFactorAuthConfigController extends BaseController {
"```\n[\n \"TOTP\",\n \"EMAIL\",\n \"SMS\"\n]\n```" +
ControllerConstants . AVAILABLE_FOR_ANY_AUTHORIZED_USER
)
@ApiResponses ( value = {
@ApiResponse ( responseCode = "200" , description = "OK" ,
content = @Content ( mediaType = "application/json" ,
array = @ArraySchema ( schema = @Schema ( implementation = TwoFaProviderType . class ) ) ) )
} )
@GetMapping ( "/providers" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')" )
public List < TwoFaProviderType > getAvailableTwoFaProviders ( ) throws ThingsboardException {
@ -210,6 +253,11 @@ public class TwoFactorAuthConfigController extends BaseController {
notes = "Get platform settings for 2FA. The settings are described for savePlatformTwoFaSettings API method. " +
"If 2FA is not configured, then an empty response will be returned." +
ControllerConstants . SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH )
@ApiResponses ( value = {
@ApiResponse ( responseCode = "200" , description = "OK" ,
content = @Content ( mediaType = "application/json" ,
schema = @Schema ( implementation = PlatformTwoFaSettings . class ) ) )
} )
@GetMapping ( "/settings" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN')" )
public PlatformTwoFaSettings getPlatformTwoFaSettings ( ) throws ThingsboardException {
@ -257,9 +305,16 @@ public class TwoFactorAuthConfigController extends BaseController {
" \"totalAllowedTimeForVerification\": 600\n" +
"}\n```" +
ControllerConstants . SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH )
@ApiResponses ( value = {
@ApiResponse ( responseCode = "200" , description = "OK" ,
content = @Content ( mediaType = "application/json" ,
schema = @Schema ( implementation = PlatformTwoFaSettings . class ) ) )
} )
@PostMapping ( "/settings" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN')" )
public PlatformTwoFaSettings savePlatformTwoFaSettings ( @Parameter ( description = "Settings value" , required = true )
public PlatformTwoFaSettings savePlatformTwoFaSettings ( @io.swagger.v3.oas.annotations.parameters.RequestBody ( description = "Platform 2FA settings" ,
required = true , content = @Content ( mediaType = "application/json" ,
schema = @Schema ( implementation = PlatformTwoFaSettings . class ) ) )
@RequestBody PlatformTwoFaSettings twoFaSettings ) throws ThingsboardException {
return twoFaConfigManager . savePlatformTwoFaSettings ( getTenantId ( ) , twoFaSettings ) ;
}