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"
|
2024-02-28 10:21:11 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2023-06-15 05:32:40 +00:00
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/integration"
|
2024-07-26 20:39:55 +00:00
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/object/v2"
|
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/user/v2"
|
2023-06-15 05:32:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServer_RegisterU2F(t *testing.T) {
|
2024-10-10 16:50:53 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
userID := Instance.CreateHumanUser(CTX).GetUserId()
|
|
|
|
otherUser := Instance.CreateHumanUser(CTX).GetUserId()
|
2023-06-15 05:32:40 +00:00
|
|
|
|
2023-12-05 15:28:17 +00:00
|
|
|
// We also need a user session
|
2024-09-06 12:47:57 +00:00
|
|
|
Instance.RegisterUserPasskey(CTX, userID)
|
|
|
|
_, sessionToken, _, _ := Instance.CreateVerifiedWebAuthNSession(t, CTX, userID)
|
|
|
|
Instance.RegisterUserPasskey(CTX, otherUser)
|
|
|
|
_, sessionTokenOtherUser, _, _ := Instance.CreateVerifiedWebAuthNSession(t, CTX, otherUser)
|
2023-12-05 15:28:17 +00:00
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
2024-05-07 05:38:26 +00:00
|
|
|
name: "admin user",
|
2023-06-15 05:32:40 +00:00
|
|
|
args: args{
|
|
|
|
ctx: CTX,
|
|
|
|
req: &user.RegisterU2FRequest{
|
|
|
|
UserId: userID,
|
|
|
|
},
|
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
want: &user.RegisterU2FResponse{
|
|
|
|
Details: &object.Details{
|
|
|
|
ChangeDate: timestamppb.Now(),
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: Instance.DefaultOrg.Id,
|
2024-05-07 05:38:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "other user, no permission",
|
|
|
|
args: args{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: integration.WithAuthorizationToken(CTX, sessionTokenOtherUser),
|
2024-05-07 05:38:26 +00:00
|
|
|
req: &user.RegisterU2FRequest{
|
|
|
|
UserId: userID,
|
|
|
|
},
|
|
|
|
},
|
2023-06-15 05:32:40 +00:00
|
|
|
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{
|
2024-09-06 12:47:57 +00:00
|
|
|
ctx: integration.WithAuthorizationToken(CTX, sessionToken),
|
2023-06-15 05:32:40 +00:00
|
|
|
req: &user.RegisterU2FRequest{
|
|
|
|
UserId: userID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &user.RegisterU2FResponse{
|
|
|
|
Details: &object.Details{
|
2024-02-28 10:21:11 +00:00
|
|
|
ChangeDate: timestamppb.Now(),
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: Instance.DefaultOrg.Id,
|
2023-06-15 05:32:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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())
|
2024-09-06 12:47:57 +00:00
|
|
|
_, err = Instance.WebAuthN.CreateAttestationResponse(got.GetPublicKeyCredentialCreationOptions())
|
2023-06-15 05:32:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_VerifyU2FRegistration(t *testing.T) {
|
2024-10-10 16:50:53 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-07-26 20:39:55 +00:00
|
|
|
ctx, userID, pkr := ctxFromNewUserWithRegisteredU2F(t)
|
2023-06-15 05:32:40 +00:00
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
attestationResponse, err := Instance.WebAuthN.CreateAttestationResponse(pkr.GetPublicKeyCredentialCreationOptions())
|
2023-06-15 05:32:40 +00:00
|
|
|
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{
|
2024-02-28 10:21:11 +00:00
|
|
|
ChangeDate: timestamppb.Now(),
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: Instance.DefaultOrg.Id,
|
2023-06-15 05:32:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-07-26 20:39:55 +00:00
|
|
|
|
|
|
|
func ctxFromNewUserWithRegisteredU2F(t *testing.T) (context.Context, string, *user.RegisterU2FResponse) {
|
2024-09-06 12:47:57 +00:00
|
|
|
userID := Instance.CreateHumanUser(CTX).GetUserId()
|
|
|
|
Instance.RegisterUserPasskey(CTX, userID)
|
|
|
|
_, sessionToken, _, _ := Instance.CreateVerifiedWebAuthNSession(t, CTX, userID)
|
|
|
|
ctx := integration.WithAuthorizationToken(CTX, sessionToken)
|
2024-07-26 20:39:55 +00:00
|
|
|
|
|
|
|
pkr, err := Client.RegisterU2F(ctx, &user.RegisterU2FRequest{
|
|
|
|
UserId: userID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, pkr.GetPublicKeyCredentialCreationOptions())
|
|
|
|
return ctx, userID, pkr
|
|
|
|
}
|
|
|
|
|
|
|
|
func ctxFromNewUserWithVerifiedU2F(t *testing.T) (context.Context, string, string) {
|
|
|
|
ctx, userID, pkr := ctxFromNewUserWithRegisteredU2F(t)
|
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
attestationResponse, err := Instance.WebAuthN.CreateAttestationResponse(pkr.GetPublicKeyCredentialCreationOptions())
|
2024-07-26 20:39:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = Client.VerifyU2FRegistration(ctx, &user.VerifyU2FRegistrationRequest{
|
|
|
|
UserId: userID,
|
|
|
|
U2FId: pkr.GetU2FId(),
|
|
|
|
PublicKeyCredential: attestationResponse,
|
|
|
|
TokenName: "nice name",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
return ctx, userID, pkr.GetU2FId()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_RemoveU2F(t *testing.T) {
|
2024-10-10 16:50:53 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-09-06 12:47:57 +00:00
|
|
|
userIDWithout := Instance.CreateHumanUser(CTX).GetUserId()
|
2024-07-26 20:39:55 +00:00
|
|
|
ctxRegistered, userIDRegistered, pkrRegistered := ctxFromNewUserWithRegisteredU2F(t)
|
|
|
|
_, userIDVerified, u2fVerified := ctxFromNewUserWithVerifiedU2F(t)
|
|
|
|
_, userIDVerifiedPermission, u2fVerifiedPermission := ctxFromNewUserWithVerifiedU2F(t)
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
req *user.RemoveU2FRequest
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *user.RemoveU2FResponse
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing user id",
|
|
|
|
args: args{
|
|
|
|
ctx: ctxRegistered,
|
|
|
|
req: &user.RemoveU2FRequest{
|
|
|
|
U2FId: "123",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing u2f id",
|
|
|
|
args: args{
|
|
|
|
ctx: ctxRegistered,
|
|
|
|
req: &user.RemoveU2FRequest{
|
|
|
|
UserId: "123",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success, registered",
|
|
|
|
args: args{
|
|
|
|
ctx: ctxRegistered,
|
|
|
|
req: &user.RemoveU2FRequest{
|
|
|
|
UserId: userIDRegistered,
|
|
|
|
U2FId: pkrRegistered.GetU2FId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &user.RemoveU2FResponse{
|
|
|
|
Details: &object.Details{
|
|
|
|
ChangeDate: timestamppb.Now(),
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: Instance.DefaultOrg.Id,
|
2024-07-26 20:39:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no u2f, error",
|
|
|
|
args: args{
|
|
|
|
ctx: IamCTX,
|
|
|
|
req: &user.RemoveU2FRequest{
|
|
|
|
UserId: userIDWithout,
|
|
|
|
U2FId: pkrRegistered.GetU2FId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success, IAMOwner permission, verified",
|
|
|
|
args: args{
|
|
|
|
ctx: IamCTX,
|
|
|
|
req: &user.RemoveU2FRequest{
|
|
|
|
UserId: userIDVerified,
|
|
|
|
U2FId: u2fVerified,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &user.RemoveU2FResponse{
|
|
|
|
Details: &object.Details{
|
|
|
|
ChangeDate: timestamppb.Now(),
|
2024-09-06 12:47:57 +00:00
|
|
|
ResourceOwner: Instance.DefaultOrg.Id,
|
2024-07-26 20:39:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "verified, permission error",
|
|
|
|
args: args{
|
|
|
|
ctx: UserCTX,
|
|
|
|
req: &user.RemoveU2FRequest{
|
|
|
|
UserId: userIDVerifiedPermission,
|
|
|
|
U2FId: u2fVerifiedPermission,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := Client.RemoveU2F(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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|