2022-03-03 11:20:43 +00:00
package validator
import (
"testing"
"time"
2022-03-03 23:46:38 +00:00
"github.com/go-webauthn/webauthn/protocol"
2022-03-03 11:20:43 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/authelia/authelia/v4/internal/configuration/schema"
)
2023-04-14 16:04:42 +00:00
func TestWebAuthnShouldSetDefaultValues ( t * testing . T ) {
2022-03-03 11:20:43 +00:00
validator := schema . NewStructValidator ( )
config := & schema . Configuration {
2023-04-14 16:04:42 +00:00
WebAuthn : schema . WebAuthnConfiguration { } ,
2022-03-03 11:20:43 +00:00
}
2023-04-14 16:04:42 +00:00
ValidateWebAuthn ( config , validator )
2022-03-03 11:20:43 +00:00
require . Len ( t , validator . Errors ( ) , 0 )
2023-04-14 16:04:42 +00:00
assert . Equal ( t , schema . DefaultWebAuthnConfiguration . DisplayName , config . WebAuthn . DisplayName )
assert . Equal ( t , schema . DefaultWebAuthnConfiguration . Timeout , config . WebAuthn . Timeout )
assert . Equal ( t , schema . DefaultWebAuthnConfiguration . ConveyancePreference , config . WebAuthn . ConveyancePreference )
assert . Equal ( t , schema . DefaultWebAuthnConfiguration . UserVerification , config . WebAuthn . UserVerification )
2022-03-03 11:20:43 +00:00
}
2023-04-14 16:04:42 +00:00
func TestWebAuthnShouldSetDefaultTimeoutWhenNegative ( t * testing . T ) {
2022-03-03 11:20:43 +00:00
validator := schema . NewStructValidator ( )
config := & schema . Configuration {
2023-04-14 16:04:42 +00:00
WebAuthn : schema . WebAuthnConfiguration {
2022-03-03 11:20:43 +00:00
Timeout : - 1 ,
} ,
}
2023-04-14 16:04:42 +00:00
ValidateWebAuthn ( config , validator )
2022-03-03 11:20:43 +00:00
require . Len ( t , validator . Errors ( ) , 0 )
2023-04-14 16:04:42 +00:00
assert . Equal ( t , schema . DefaultWebAuthnConfiguration . Timeout , config . WebAuthn . Timeout )
2022-03-03 11:20:43 +00:00
}
2023-04-14 16:04:42 +00:00
func TestWebAuthnShouldNotSetDefaultValuesWhenConfigured ( t * testing . T ) {
2022-03-03 11:20:43 +00:00
validator := schema . NewStructValidator ( )
config := & schema . Configuration {
2023-04-14 16:04:42 +00:00
WebAuthn : schema . WebAuthnConfiguration {
2022-03-03 11:20:43 +00:00
DisplayName : "Test" ,
Timeout : time . Second * 50 ,
ConveyancePreference : protocol . PreferNoAttestation ,
UserVerification : protocol . VerificationDiscouraged ,
} ,
}
2023-04-14 16:04:42 +00:00
ValidateWebAuthn ( config , validator )
2022-03-03 11:20:43 +00:00
require . Len ( t , validator . Errors ( ) , 0 )
2023-04-14 16:04:42 +00:00
assert . Equal ( t , "Test" , config . WebAuthn . DisplayName )
assert . Equal ( t , time . Second * 50 , config . WebAuthn . Timeout )
assert . Equal ( t , protocol . PreferNoAttestation , config . WebAuthn . ConveyancePreference )
assert . Equal ( t , protocol . VerificationDiscouraged , config . WebAuthn . UserVerification )
2022-03-03 11:20:43 +00:00
2023-04-14 16:04:42 +00:00
config . WebAuthn . ConveyancePreference = protocol . PreferIndirectAttestation
config . WebAuthn . UserVerification = protocol . VerificationPreferred
2022-03-03 11:20:43 +00:00
2023-04-14 16:04:42 +00:00
ValidateWebAuthn ( config , validator )
2022-03-03 11:20:43 +00:00
require . Len ( t , validator . Errors ( ) , 0 )
2023-04-14 16:04:42 +00:00
assert . Equal ( t , protocol . PreferIndirectAttestation , config . WebAuthn . ConveyancePreference )
assert . Equal ( t , protocol . VerificationPreferred , config . WebAuthn . UserVerification )
2022-03-03 11:20:43 +00:00
2023-04-14 16:04:42 +00:00
config . WebAuthn . ConveyancePreference = protocol . PreferDirectAttestation
config . WebAuthn . UserVerification = protocol . VerificationRequired
2022-03-03 11:20:43 +00:00
2023-04-14 16:04:42 +00:00
ValidateWebAuthn ( config , validator )
2022-03-03 11:20:43 +00:00
require . Len ( t , validator . Errors ( ) , 0 )
2023-04-14 16:04:42 +00:00
assert . Equal ( t , protocol . PreferDirectAttestation , config . WebAuthn . ConveyancePreference )
assert . Equal ( t , protocol . VerificationRequired , config . WebAuthn . UserVerification )
2022-03-03 11:20:43 +00:00
}
2023-04-14 16:04:42 +00:00
func TestWebAuthnShouldRaiseErrorsOnInvalidOptions ( t * testing . T ) {
2022-03-03 11:20:43 +00:00
validator := schema . NewStructValidator ( )
config := & schema . Configuration {
2023-04-14 16:04:42 +00:00
WebAuthn : schema . WebAuthnConfiguration {
2022-03-03 11:20:43 +00:00
DisplayName : "Test" ,
Timeout : time . Second * 50 ,
ConveyancePreference : "no" ,
UserVerification : "yes" ,
} ,
}
2023-04-14 16:04:42 +00:00
ValidateWebAuthn ( config , validator )
2022-03-03 11:20:43 +00:00
require . Len ( t , validator . Errors ( ) , 2 )
2023-04-13 10:58:18 +00:00
assert . EqualError ( t , validator . Errors ( ) [ 0 ] , "webauthn: option 'attestation_conveyance_preference' must be one of 'none', 'indirect', or 'direct' but it's configured as 'no'" )
assert . EqualError ( t , validator . Errors ( ) [ 1 ] , "webauthn: option 'user_verification' must be one of 'none', 'indirect', or 'direct' but it's configured as 'yes'" )
2022-03-03 11:20:43 +00:00
}