2023-06-20 12:36:21 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/pquerna/otp/totp"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
|
|
)
|
|
|
|
|
2023-06-22 12:06:32 +02:00
|
|
|
func TestCommands_AddUserTOTP(t *testing.T) {
|
2023-06-20 12:36:21 +02:00
|
|
|
ctx := authz.NewMockContext("inst1", "org1", "user1")
|
|
|
|
userAgg := &user.NewAggregate("user1", "org1").Aggregate
|
|
|
|
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceowner string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want bool
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wrong user",
|
|
|
|
args: args{
|
|
|
|
userID: "foo",
|
|
|
|
resourceowner: "org1",
|
|
|
|
},
|
2023-08-02 18:57:53 +02:00
|
|
|
wantErr: caos_errs.ThrowPermissionDenied(nil, "AUTH-Bohd2", "Errors.User.UserIDWrong"),
|
2023-06-20 12:36:21 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "create otp error",
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceowner: "org1",
|
|
|
|
},
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
2023-10-19 12:19:10 +02:00
|
|
|
wantErr: caos_errs.ThrowPreconditionFailed(nil, "COMMAND-SqyJz", "Errors.User.NotFound"),
|
2023-06-20 12:36:21 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "push error",
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceowner: "org1",
|
|
|
|
},
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(ctx,
|
|
|
|
userAgg,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewOrgAddedEvent(ctx,
|
|
|
|
userAgg,
|
|
|
|
"org",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewDomainPolicyAddedEvent(ctx,
|
|
|
|
userAgg,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(),
|
2023-10-19 12:19:10 +02:00
|
|
|
expectRandomPushFailed(io.ErrClosedPipe, []eventstore.Command{
|
2023-06-20 12:36:21 +02:00
|
|
|
user.NewHumanOTPAddedEvent(ctx, userAgg, nil),
|
2023-10-19 12:19:10 +02:00
|
|
|
}),
|
2023-06-20 12:36:21 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceowner: "org1",
|
|
|
|
},
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(ctx,
|
|
|
|
userAgg,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewOrgAddedEvent(ctx,
|
|
|
|
userAgg,
|
|
|
|
"org",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewDomainPolicyAddedEvent(ctx,
|
|
|
|
userAgg,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(),
|
2023-10-19 12:19:10 +02:00
|
|
|
expectRandomPush([]eventstore.Command{
|
2023-06-20 12:36:21 +02:00
|
|
|
user.NewHumanOTPAddedEvent(ctx, userAgg, nil),
|
2023-10-19 12:19:10 +02:00
|
|
|
}),
|
2023-06-20 12:36:21 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
multifactors: domain.MultifactorConfigs{
|
|
|
|
OTP: domain.OTPConfig{
|
|
|
|
Issuer: "zitadel.com",
|
|
|
|
CryptoMFA: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-06-22 12:06:32 +02:00
|
|
|
got, err := c.AddUserTOTP(ctx, tt.args.userID, tt.args.resourceowner)
|
2023-06-20 12:36:21 +02:00
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
if tt.want {
|
|
|
|
require.NotNil(t, got)
|
|
|
|
assert.Equal(t, "org1", got.ResourceOwner)
|
|
|
|
assert.NotEmpty(t, got.Secret)
|
|
|
|
assert.NotEmpty(t, got.URI)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 12:06:32 +02:00
|
|
|
func TestCommands_CheckUserTOTP(t *testing.T) {
|
2023-07-10 15:27:00 +02:00
|
|
|
ctx := authz.NewMockContext("", "org1", "user1")
|
2023-06-20 12:36:21 +02:00
|
|
|
|
|
|
|
cryptoAlg := crypto.CreateMockEncryptionAlg(gomock.NewController(t))
|
2023-08-02 18:57:53 +02:00
|
|
|
key, secret, err := domain.NewTOTPKey("example.com", "user1", cryptoAlg)
|
2023-06-20 12:36:21 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
userAgg := &user.NewAggregate("user1", "org1").Aggregate
|
|
|
|
|
|
|
|
code, err := totp.GenerateCode(key.Secret(), time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
code string
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want bool
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wrong user id",
|
|
|
|
args: args{
|
|
|
|
userID: "foo",
|
|
|
|
},
|
2023-08-02 18:57:53 +02:00
|
|
|
wantErr: caos_errs.ThrowPermissionDenied(nil, "AUTH-Bohd2", "Errors.User.UserIDWrong"),
|
2023-06-20 12:36:21 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPAddedEvent(ctx, userAgg, secret),
|
|
|
|
),
|
|
|
|
),
|
2023-10-19 12:19:10 +02:00
|
|
|
expectPush(
|
2023-06-20 12:36:21 +02:00
|
|
|
user.NewHumanOTPVerifiedEvent(ctx, userAgg, ""),
|
2023-10-19 12:19:10 +02:00
|
|
|
),
|
2023-06-20 12:36:21 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
resourceOwner: "org1",
|
|
|
|
code: code,
|
|
|
|
userID: "user1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
multifactors: domain.MultifactorConfigs{
|
|
|
|
OTP: domain.OTPConfig{
|
|
|
|
CryptoMFA: cryptoAlg,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-06-22 12:06:32 +02:00
|
|
|
got, err := c.CheckUserTOTP(ctx, tt.args.userID, tt.args.code, tt.args.resourceOwner)
|
2023-06-20 12:36:21 +02:00
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
if tt.want {
|
|
|
|
require.NotNil(t, got)
|
|
|
|
assert.Equal(t, "org1", got.ResourceOwner)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|