2023-06-15 05:32:40 +00:00
|
|
|
//go:build integration
|
|
|
|
|
|
|
|
package user_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/integration"
|
2023-12-05 15:28:17 +00:00
|
|
|
object "github.com/zitadel/zitadel/pkg/grpc/object/v2beta"
|
2023-09-13 12:43:01 +00:00
|
|
|
user "github.com/zitadel/zitadel/pkg/grpc/user/v2beta"
|
2023-06-15 05:32:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServer_RegisterU2F(t *testing.T) {
|
|
|
|
userID := Tester.CreateHumanUser(CTX).GetUserId()
|
|
|
|
|
2023-12-05 15:28:17 +00:00
|
|
|
// We also need a user session
|
|
|
|
Tester.RegisterUserPasskey(CTX, userID)
|
|
|
|
_, sessionToken, _, _ := Tester.CreateVerifiedWebAuthNSession(t, CTX, userID)
|
|
|
|
|
2023-06-15 05:32:40 +00:00
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
req *user.RegisterU2FRequest
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *user.RegisterU2FResponse
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing user id",
|
|
|
|
args: args{
|
|
|
|
ctx: CTX,
|
|
|
|
req: &user.RegisterU2FRequest{},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user mismatch",
|
|
|
|
args: args{
|
|
|
|
ctx: CTX,
|
|
|
|
req: &user.RegisterU2FRequest{
|
|
|
|
UserId: userID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2023-12-05 15:28:17 +00:00
|
|
|
name: "user setting its own passkey",
|
2023-06-15 05:32:40 +00:00
|
|
|
args: args{
|
2023-12-05 15:28:17 +00:00
|
|
|
ctx: Tester.WithAuthorizationToken(CTX, sessionToken),
|
2023-06-15 05:32:40 +00:00
|
|
|
req: &user.RegisterU2FRequest{
|
|
|
|
UserId: userID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &user.RegisterU2FResponse{
|
|
|
|
Details: &object.Details{
|
|
|
|
ResourceOwner: Tester.Organisation.ID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := Client.RegisterU2F(tt.args.ctx, tt.args.req)
|
|
|
|
if tt.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, got)
|
|
|
|
integration.AssertDetails(t, tt.want, got)
|
|
|
|
if tt.want != nil {
|
|
|
|
assert.NotEmpty(t, got.GetU2FId())
|
|
|
|
assert.NotEmpty(t, got.GetPublicKeyCredentialCreationOptions())
|
|
|
|
_, err = Tester.WebAuthN.CreateAttestationResponse(got.GetPublicKeyCredentialCreationOptions())
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_VerifyU2FRegistration(t *testing.T) {
|
|
|
|
userID := Tester.CreateHumanUser(CTX).GetUserId()
|
2023-12-05 15:28:17 +00:00
|
|
|
Tester.RegisterUserPasskey(CTX, userID)
|
|
|
|
_, sessionToken, _, _ := Tester.CreateVerifiedWebAuthNSession(t, CTX, userID)
|
|
|
|
ctx := Tester.WithAuthorizationToken(CTX, sessionToken)
|
|
|
|
|
|
|
|
pkr, err := Client.RegisterU2F(ctx, &user.RegisterU2FRequest{
|
2023-06-15 05:32:40 +00:00
|
|
|
UserId: userID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, pkr.GetPublicKeyCredentialCreationOptions())
|
|
|
|
|
|
|
|
attestationResponse, err := Tester.WebAuthN.CreateAttestationResponse(pkr.GetPublicKeyCredentialCreationOptions())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
req *user.VerifyU2FRegistrationRequest
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *user.VerifyU2FRegistrationResponse
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing user id",
|
|
|
|
args: args{
|
2023-12-05 15:28:17 +00:00
|
|
|
ctx: ctx,
|
2023-06-15 05:32:40 +00:00
|
|
|
req: &user.VerifyU2FRegistrationRequest{
|
|
|
|
U2FId: "123",
|
|
|
|
TokenName: "nice name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
args: args{
|
2023-12-05 15:28:17 +00:00
|
|
|
ctx: ctx,
|
2023-06-15 05:32:40 +00:00
|
|
|
req: &user.VerifyU2FRegistrationRequest{
|
|
|
|
UserId: userID,
|
|
|
|
U2FId: pkr.GetU2FId(),
|
|
|
|
PublicKeyCredential: attestationResponse,
|
|
|
|
TokenName: "nice name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &user.VerifyU2FRegistrationResponse{
|
|
|
|
Details: &object.Details{
|
|
|
|
ResourceOwner: Tester.Organisation.ID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong credential",
|
|
|
|
args: args{
|
2023-12-05 15:28:17 +00:00
|
|
|
ctx: ctx,
|
2023-06-15 05:32:40 +00:00
|
|
|
req: &user.VerifyU2FRegistrationRequest{
|
|
|
|
UserId: userID,
|
|
|
|
U2FId: "123",
|
|
|
|
PublicKeyCredential: &structpb.Struct{
|
|
|
|
Fields: map[string]*structpb.Value{"foo": {Kind: &structpb.Value_StringValue{StringValue: "bar"}}},
|
|
|
|
},
|
|
|
|
TokenName: "nice name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := Client.VerifyU2FRegistration(tt.args.ctx, tt.args.req)
|
|
|
|
if tt.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, got)
|
|
|
|
integration.AssertDetails(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|