2023-06-20 10:36:21 +00:00
|
|
|
//go:build integration
|
|
|
|
|
|
|
|
package user_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2023-12-05 15:28:17 +00:00
|
|
|
"time"
|
2023-06-20 10:36:21 +00:00
|
|
|
|
2023-12-05 15:28:17 +00:00
|
|
|
"github.com/pquerna/otp/totp"
|
2023-06-20 10:36:21 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"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-20 10:36:21 +00:00
|
|
|
)
|
|
|
|
|
2023-06-22 10:06:32 +00:00
|
|
|
func TestServer_RegisterTOTP(t *testing.T) {
|
2023-12-05 15:28:17 +00:00
|
|
|
userID := Tester.CreateHumanUser(CTX).GetUserId()
|
|
|
|
Tester.RegisterUserPasskey(CTX, userID)
|
|
|
|
_, sessionToken, _, _ := Tester.CreateVerifiedWebAuthNSession(t, CTX, userID)
|
|
|
|
ctx := Tester.WithAuthorizationToken(CTX, sessionToken)
|
2023-06-20 10:36:21 +00:00
|
|
|
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
2023-06-22 10:06:32 +00:00
|
|
|
req *user.RegisterTOTPRequest
|
2023-06-20 10:36:21 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2023-06-22 10:06:32 +00:00
|
|
|
want *user.RegisterTOTPResponse
|
2023-06-20 10:36:21 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing user id",
|
|
|
|
args: args{
|
2023-12-05 15:28:17 +00:00
|
|
|
ctx: ctx,
|
2023-06-22 10:06:32 +00:00
|
|
|
req: &user.RegisterTOTPRequest{},
|
2023-06-20 10:36:21 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user mismatch",
|
|
|
|
args: args{
|
2023-12-05 15:28:17 +00:00
|
|
|
ctx: ctx,
|
2023-06-22 10:06:32 +00:00
|
|
|
req: &user.RegisterTOTPRequest{
|
2023-06-20 10:36:21 +00:00
|
|
|
UserId: "wrong",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2023-12-05 15:28:17 +00:00
|
|
|
name: "success",
|
2023-06-20 10:36:21 +00:00
|
|
|
args: args{
|
2023-12-05 15:28:17 +00:00
|
|
|
ctx: ctx,
|
2023-06-22 10:06:32 +00:00
|
|
|
req: &user.RegisterTOTPRequest{
|
2023-06-20 10:36:21 +00:00
|
|
|
UserId: userID,
|
|
|
|
},
|
|
|
|
},
|
2023-06-22 10:06:32 +00:00
|
|
|
want: &user.RegisterTOTPResponse{
|
2023-06-20 10:36:21 +00:00
|
|
|
Details: &object.Details{
|
|
|
|
ResourceOwner: Tester.Organisation.ID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-06-22 10:06:32 +00:00
|
|
|
got, err := Client.RegisterTOTP(tt.args.ctx, tt.args.req)
|
2023-06-20 10:36:21 +00:00
|
|
|
if tt.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, got)
|
|
|
|
integration.AssertDetails(t, tt.want, got)
|
|
|
|
assert.NotEmpty(t, got.GetUri())
|
|
|
|
assert.NotEmpty(t, got.GetSecret())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 10:06:32 +00:00
|
|
|
func TestServer_VerifyTOTPRegistration(t *testing.T) {
|
2023-06-20 10:36:21 +00:00
|
|
|
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)
|
2023-06-20 10:36:21 +00:00
|
|
|
|
2023-12-05 15:28:17 +00:00
|
|
|
reg, err := Client.RegisterTOTP(ctx, &user.RegisterTOTPRequest{
|
2023-06-20 10:36:21 +00:00
|
|
|
UserId: userID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
code, err := totp.GenerateCode(reg.Secret, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
2023-06-22 10:06:32 +00:00
|
|
|
req *user.VerifyTOTPRegistrationRequest
|
2023-06-20 10:36:21 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2023-06-22 10:06:32 +00:00
|
|
|
want *user.VerifyTOTPRegistrationResponse
|
2023-06-20 10:36:21 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "user mismatch",
|
|
|
|
args: args{
|
2023-12-05 15:28:17 +00:00
|
|
|
ctx: ctx,
|
2023-06-22 10:06:32 +00:00
|
|
|
req: &user.VerifyTOTPRegistrationRequest{
|
2023-06-20 10:36:21 +00:00
|
|
|
UserId: "wrong",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong code",
|
|
|
|
args: args{
|
2023-12-05 15:28:17 +00:00
|
|
|
ctx: ctx,
|
2023-06-22 10:06:32 +00:00
|
|
|
req: &user.VerifyTOTPRegistrationRequest{
|
2023-06-20 10:36:21 +00:00
|
|
|
UserId: userID,
|
|
|
|
Code: "123",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
args: args{
|
2023-12-05 15:28:17 +00:00
|
|
|
ctx: ctx,
|
2023-06-22 10:06:32 +00:00
|
|
|
req: &user.VerifyTOTPRegistrationRequest{
|
2023-06-20 10:36:21 +00:00
|
|
|
UserId: userID,
|
|
|
|
Code: code,
|
|
|
|
},
|
|
|
|
},
|
2023-06-22 10:06:32 +00:00
|
|
|
want: &user.VerifyTOTPRegistrationResponse{
|
2023-06-20 10:36:21 +00:00
|
|
|
Details: &object.Details{
|
|
|
|
ResourceOwner: Tester.Organisation.ResourceOwner,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-06-22 10:06:32 +00:00
|
|
|
got, err := Client.VerifyTOTPRegistration(tt.args.ctx, tt.args.req)
|
2023-06-20 10:36:21 +00:00
|
|
|
if tt.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, got)
|
|
|
|
integration.AssertDetails(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|