2021-03-19 10:12:56 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-20 10:36:21 +00:00
|
|
|
"io"
|
2023-08-15 12:47:05 +00:00
|
|
|
"net"
|
2021-03-19 10:12:56 +00:00
|
|
|
"testing"
|
2023-06-20 10:36:21 +00:00
|
|
|
"time"
|
2021-03-19 10:12:56 +00:00
|
|
|
|
2023-06-20 10:36:21 +00:00
|
|
|
"github.com/pquerna/otp/totp"
|
2021-03-19 10:12:56 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-06-20 10:36:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-22 10:56:43 +00:00
|
|
|
"go.uber.org/mock/gomock"
|
2021-03-19 10:12:56 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2023-06-20 10:36:21 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
feat: trusted (instance) domains (#8369)
# Which Problems Are Solved
ZITADEL currently selects the instance context based on a HTTP header
(see https://github.com/zitadel/zitadel/issues/8279#issue-2399959845 and
checks it against the list of instance domains. Let's call it instance
or API domain.
For any context based URL (e.g. OAuth, OIDC, SAML endpoints, links in
emails, ...) the requested domain (instance domain) will be used. Let's
call it the public domain.
In cases of proxied setups, all exposed domains (public domains) require
the domain to be managed as instance domain.
This can either be done using the "ExternalDomain" in the runtime config
or via system API, which requires a validation through CustomerPortal on
zitadel.cloud.
# How the Problems Are Solved
- Two new headers / header list are added:
- `InstanceHostHeaders`: an ordered list (first sent wins), which will
be used to match the instance.
(For backward compatibility: the `HTTP1HostHeader`, `HTTP2HostHeader`
and `forwarded`, `x-forwarded-for`, `x-forwarded-host` are checked
afterwards as well)
- `PublicHostHeaders`: an ordered list (first sent wins), which will be
used as public host / domain. This will be checked against a list of
trusted domains on the instance.
- The middleware intercepts all requests to the API and passes a
`DomainCtx` object with the hosts and protocol into the context
(previously only a computed `origin` was passed)
- HTTP / GRPC server do not longer try to match the headers to instances
themself, but use the passed `http.DomainContext` in their interceptors.
- The `RequestedHost` and `RequestedDomain` from authz.Instance are
removed in favor of the `http.DomainContext`
- When authenticating to or signing out from Console UI, the current
`http.DomainContext(ctx).Origin` (already checked by instance
interceptor for validity) is used to compute and dynamically add a
`redirect_uri` and `post_logout_redirect_uri`.
- Gateway passes all configured host headers (previously only did
`x-zitadel-*`)
- Admin API allows to manage trusted domain
# Additional Changes
None
# Additional Context
- part of #8279
- open topics:
- "single-instance" mode
- Console UI
2024-07-31 15:00:38 +00:00
|
|
|
http_util "github.com/zitadel/zitadel/internal/api/http"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-08-15 12:47:05 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-03-19 10:12:56 +00:00
|
|
|
)
|
|
|
|
|
2023-08-02 16:57:53 +00:00
|
|
|
func TestCommandSide_AddHumanTOTP(t *testing.T) {
|
2021-03-19 10:12:56 +00:00
|
|
|
type fields struct {
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore func(t *testing.T) *eventstore.Eventstore
|
|
|
|
permissionCheck domain.PermissionCheck
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
|
|
|
ctx context.Context
|
|
|
|
orgID string
|
|
|
|
userID string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2024-05-07 05:38:26 +00:00
|
|
|
ctx: authz.NewMockContext("instanceID", "org1", "user1"),
|
2021-03-19 10:12:56 +00:00
|
|
|
orgID: "org1",
|
|
|
|
userID: "",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not existing, not found error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2024-05-07 05:38:26 +00:00
|
|
|
ctx: authz.NewMockContext("instanceID", "org1", "user1"),
|
2021-03-19 10:12:56 +00:00
|
|
|
orgID: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
{
|
|
|
|
name: "other user, no permission error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user2", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
permissionCheck: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: authz.NewMockContext("instanceID", "org1", "user1"),
|
|
|
|
orgID: "org1",
|
|
|
|
userID: "user2",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: zerrors.IsPermissionDenied,
|
|
|
|
},
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
{
|
|
|
|
name: "org not existing, not found error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2024-05-07 05:38:26 +00:00
|
|
|
ctx: authz.NewMockContext("instanceID", "org1", "user1"),
|
2021-03-19 10:12:56 +00:00
|
|
|
orgID: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "org iam policy not existing, not found error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
2023-10-19 10:19:10 +00:00
|
|
|
org.NewOrgAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("org1", "org1").Aggregate,
|
|
|
|
"org",
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(),
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2024-05-07 05:38:26 +00:00
|
|
|
ctx: authz.NewMockContext("instanceID", "org1", "user1"),
|
2021-03-19 10:12:56 +00:00
|
|
|
orgID: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "otp already exists, already exists error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
2023-10-19 10:19:10 +00:00
|
|
|
org.NewOrgAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("org1", "org1").Aggregate,
|
|
|
|
"org",
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
2023-10-19 10:19:10 +00:00
|
|
|
org.NewDomainPolicyAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
user.NewHumanOTPVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"agent1",
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2024-05-07 05:38:26 +00:00
|
|
|
ctx: authz.NewMockContext("instanceID", "org1", "user1"),
|
2021-03-19 10:12:56 +00:00
|
|
|
orgID: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorAlreadyExists,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
checkPermission: tt.fields.permissionCheck,
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
2023-08-02 16:57:53 +00:00
|
|
|
got, err := r.AddHumanTOTP(tt.args.ctx, tt.args.userID, tt.args.orgID)
|
2021-03-19 10:12:56 +00:00
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.Equal(t, tt.res.want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:57:53 +00:00
|
|
|
func TestCommands_createHumanTOTP(t *testing.T) {
|
2023-06-20 10:36:21 +00:00
|
|
|
type fields struct {
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore func(t *testing.T) *eventstore.Eventstore
|
|
|
|
checkPermission domain.PermissionCheck
|
2023-06-20 10:36:21 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want bool
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "user not existing, not found error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-06-20 10:36:21 +00:00
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2024-05-07 05:38:26 +00:00
|
|
|
ctx: authz.NewMockContext("instanceID", "org1", "user1"),
|
2023-06-20 10:36:21 +00:00
|
|
|
resourceOwner: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowPreconditionFailed(nil, "COMMAND-SqyJz", "Errors.User.NotFound"),
|
2023-06-20 10:36:21 +00:00
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
{
|
|
|
|
name: "other user, no permission error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user2", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: authz.NewMockContext("instanceID", "org1", "user1"),
|
|
|
|
resourceOwner: "org1",
|
|
|
|
userID: "user2",
|
|
|
|
},
|
|
|
|
wantErr: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
|
|
|
},
|
2023-06-20 10:36:21 +00:00
|
|
|
{
|
|
|
|
name: "org not existing, not found error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-06-20 10:36:21 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2024-05-07 05:38:26 +00:00
|
|
|
ctx: authz.NewMockContext("instanceID", "org1", "user1"),
|
2023-06-20 10:36:21 +00:00
|
|
|
resourceOwner: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowPreconditionFailed(nil, "COMMAND-55M9f", "Errors.Org.NotFound"),
|
2023-06-20 10:36:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "org iam policy not existing, not found error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-06-20 10:36:21 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewOrgAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("org1", "org1").Aggregate,
|
|
|
|
"org",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(),
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2024-05-07 05:38:26 +00:00
|
|
|
ctx: authz.NewMockContext("instanceID", "org1", "user1"),
|
2023-06-20 10:36:21 +00:00
|
|
|
resourceOwner: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowPreconditionFailed(nil, "COMMAND-8ugTs", "Errors.Org.DomainPolicy.NotFound"),
|
2023-06-20 10:36:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "otp already exists, already exists error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-06-20 10:36:21 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewOrgAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("org1", "org1").Aggregate,
|
|
|
|
"org",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewDomainPolicyAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"agent1")),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2024-05-07 05:38:26 +00:00
|
|
|
ctx: authz.NewMockContext("instanceID", "org1", "user1"),
|
2023-06-20 10:36:21 +00:00
|
|
|
resourceOwner: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowAlreadyExists(nil, "COMMAND-do9se", "Errors.User.MFA.OTP.AlreadyReady"),
|
2023-06-20 10:36:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "issuer not in context",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-06-20 10:36:21 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewOrgAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("org1", "org1").Aggregate,
|
|
|
|
"org",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewDomainPolicyAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2024-05-07 05:38:26 +00:00
|
|
|
ctx: authz.NewMockContext("instanceID", "org1", "user1"),
|
2023-06-20 10:36:21 +00:00
|
|
|
resourceOwner: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowInternal(nil, "TOTP-ieY3o", "Errors.Internal"),
|
2023-06-20 10:36:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-06-20 10:36:21 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewOrgAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("org1", "org1").Aggregate,
|
|
|
|
"org",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewDomainPolicyAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
feat: trusted (instance) domains (#8369)
# Which Problems Are Solved
ZITADEL currently selects the instance context based on a HTTP header
(see https://github.com/zitadel/zitadel/issues/8279#issue-2399959845 and
checks it against the list of instance domains. Let's call it instance
or API domain.
For any context based URL (e.g. OAuth, OIDC, SAML endpoints, links in
emails, ...) the requested domain (instance domain) will be used. Let's
call it the public domain.
In cases of proxied setups, all exposed domains (public domains) require
the domain to be managed as instance domain.
This can either be done using the "ExternalDomain" in the runtime config
or via system API, which requires a validation through CustomerPortal on
zitadel.cloud.
# How the Problems Are Solved
- Two new headers / header list are added:
- `InstanceHostHeaders`: an ordered list (first sent wins), which will
be used to match the instance.
(For backward compatibility: the `HTTP1HostHeader`, `HTTP2HostHeader`
and `forwarded`, `x-forwarded-for`, `x-forwarded-host` are checked
afterwards as well)
- `PublicHostHeaders`: an ordered list (first sent wins), which will be
used as public host / domain. This will be checked against a list of
trusted domains on the instance.
- The middleware intercepts all requests to the API and passes a
`DomainCtx` object with the hosts and protocol into the context
(previously only a computed `origin` was passed)
- HTTP / GRPC server do not longer try to match the headers to instances
themself, but use the passed `http.DomainContext` in their interceptors.
- The `RequestedHost` and `RequestedDomain` from authz.Instance are
removed in favor of the `http.DomainContext`
- When authenticating to or signing out from Console UI, the current
`http.DomainContext(ctx).Origin` (already checked by instance
interceptor for validity) is used to compute and dynamically add a
`redirect_uri` and `post_logout_redirect_uri`.
- Gateway passes all configured host headers (previously only did
`x-zitadel-*`)
- Admin API allows to manage trusted domain
# Additional Changes
None
# Additional Context
- part of #8279
- open topics:
- "single-instance" mode
- Console UI
2024-07-31 15:00:38 +00:00
|
|
|
ctx: http_util.WithRequestedHost(authz.NewMockContext("instanceID", "org1", "user1"), "zitadel.com"),
|
2023-06-20 10:36:21 +00:00
|
|
|
resourceOwner: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
{
|
|
|
|
name: "success, other user",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user2", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewOrgAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("org1", "org1").Aggregate,
|
|
|
|
"org",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewDomainPolicyAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
feat: trusted (instance) domains (#8369)
# Which Problems Are Solved
ZITADEL currently selects the instance context based on a HTTP header
(see https://github.com/zitadel/zitadel/issues/8279#issue-2399959845 and
checks it against the list of instance domains. Let's call it instance
or API domain.
For any context based URL (e.g. OAuth, OIDC, SAML endpoints, links in
emails, ...) the requested domain (instance domain) will be used. Let's
call it the public domain.
In cases of proxied setups, all exposed domains (public domains) require
the domain to be managed as instance domain.
This can either be done using the "ExternalDomain" in the runtime config
or via system API, which requires a validation through CustomerPortal on
zitadel.cloud.
# How the Problems Are Solved
- Two new headers / header list are added:
- `InstanceHostHeaders`: an ordered list (first sent wins), which will
be used to match the instance.
(For backward compatibility: the `HTTP1HostHeader`, `HTTP2HostHeader`
and `forwarded`, `x-forwarded-for`, `x-forwarded-host` are checked
afterwards as well)
- `PublicHostHeaders`: an ordered list (first sent wins), which will be
used as public host / domain. This will be checked against a list of
trusted domains on the instance.
- The middleware intercepts all requests to the API and passes a
`DomainCtx` object with the hosts and protocol into the context
(previously only a computed `origin` was passed)
- HTTP / GRPC server do not longer try to match the headers to instances
themself, but use the passed `http.DomainContext` in their interceptors.
- The `RequestedHost` and `RequestedDomain` from authz.Instance are
removed in favor of the `http.DomainContext`
- When authenticating to or signing out from Console UI, the current
`http.DomainContext(ctx).Origin` (already checked by instance
interceptor for validity) is used to compute and dynamically add a
`redirect_uri` and `post_logout_redirect_uri`.
- Gateway passes all configured host headers (previously only did
`x-zitadel-*`)
- Admin API allows to manage trusted domain
# Additional Changes
None
# Additional Context
- part of #8279
- open topics:
- "single-instance" mode
- Console UI
2024-07-31 15:00:38 +00:00
|
|
|
ctx: http_util.WithRequestedHost(authz.NewMockContext("instanceID", "org1", "user1"), "zitadel.com"),
|
2024-05-07 05:38:26 +00:00
|
|
|
resourceOwner: "org1",
|
|
|
|
userID: "user2",
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
},
|
2023-06-20 10:36:21 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
2023-06-20 10:36:21 +00:00
|
|
|
multifactors: domain.MultifactorConfigs{
|
|
|
|
OTP: domain.OTPConfig{
|
|
|
|
CryptoMFA: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-06-22 10:06:32 +00:00
|
|
|
got, err := c.createHumanTOTP(tt.args.ctx, tt.args.userID, tt.args.resourceOwner)
|
2023-06-20 10:36:21 +00:00
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
if tt.want {
|
|
|
|
require.NotNil(t, got)
|
|
|
|
assert.NotNil(t, got.wm)
|
|
|
|
assert.NotNil(t, got.userAgg)
|
|
|
|
require.NotNil(t, got.key)
|
|
|
|
assert.NotEmpty(t, got.key.URL())
|
|
|
|
assert.NotEmpty(t, got.key.Secret())
|
|
|
|
assert.Len(t, got.cmds, 1)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:57:53 +00:00
|
|
|
func TestCommands_HumanCheckMFATOTPSetup(t *testing.T) {
|
2023-07-10 13:27:00 +00:00
|
|
|
ctx := authz.NewMockContext("", "org1", "user1")
|
2023-06-20 10:36:21 +00:00
|
|
|
|
|
|
|
cryptoAlg := crypto.CreateMockEncryptionAlg(gomock.NewController(t))
|
2024-05-14 07:20:31 +00:00
|
|
|
key, err := domain.NewTOTPKey("example.com", "user1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
secret, err := crypto.Encrypt([]byte(key.Secret()), cryptoAlg)
|
2023-06-20 10:36:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
userAgg := &user.NewAggregate("user1", "org1").Aggregate
|
2024-05-07 05:38:26 +00:00
|
|
|
userAgg2 := &user.NewAggregate("user2", "org1").Aggregate
|
2023-06-20 10:36:21 +00:00
|
|
|
|
|
|
|
code, err := totp.GenerateCode(key.Secret(), time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
type fields struct {
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore func(t *testing.T) *eventstore.Eventstore
|
|
|
|
checkPermission domain.PermissionCheck
|
2023-06-20 10:36:21 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
code string
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want bool
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
2024-05-07 05:38:26 +00:00
|
|
|
name: "missing user id",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
2023-06-20 10:36:21 +00:00
|
|
|
args: args{},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowPreconditionFailed(nil, "COMMAND-8N9ds", "Errors.User.UserIDMissing"),
|
2023-06-20 10:36:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "filter error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-06-20 10:36:21 +00:00
|
|
|
expectFilterError(io.ErrClosedPipe),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
{
|
|
|
|
name: "other user, no permission error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPAddedEvent(ctx, userAgg2, secret),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
resourceOwner: "org1",
|
|
|
|
userID: "user2",
|
|
|
|
},
|
|
|
|
wantErr: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
|
|
|
},
|
2023-06-20 10:36:21 +00:00
|
|
|
{
|
|
|
|
name: "otp not existing error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-06-20 10:36:21 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPAddedEvent(ctx, userAgg, secret),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPRemovedEvent(ctx, userAgg),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
resourceOwner: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowNotFound(nil, "COMMAND-3Mif9s", "Errors.User.MFA.OTP.NotExisting"),
|
2023-06-20 10:36:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "otp already ready error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-06-20 10:36:21 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPAddedEvent(ctx, userAgg, secret),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPVerifiedEvent(context.Background(),
|
|
|
|
userAgg,
|
|
|
|
"agent1",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
resourceOwner: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowPreconditionFailed(nil, "COMMAND-qx4ls", "Errors.Users.MFA.OTP.AlreadyReady"),
|
2023-06-20 10:36:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong code",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-06-20 10:36:21 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPAddedEvent(ctx, userAgg, secret),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
resourceOwner: "org1",
|
|
|
|
code: "wrong",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "EVENT-8isk2", "Errors.User.MFA.OTP.InvalidCode"),
|
2023-06-20 10:36:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "push error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-06-20 10:36:21 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPAddedEvent(ctx, userAgg, secret),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPushFailed(io.ErrClosedPipe,
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPVerifiedEvent(ctx,
|
|
|
|
userAgg,
|
|
|
|
"agent1",
|
|
|
|
),
|
2023-06-20 10:36:21 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
resourceOwner: "org1",
|
|
|
|
code: code,
|
|
|
|
userID: "user1",
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-06-20 10:36:21 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPAddedEvent(ctx, userAgg, secret),
|
|
|
|
),
|
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
expectPush(
|
2023-06-20 10:36:21 +00:00
|
|
|
user.NewHumanOTPVerifiedEvent(ctx,
|
|
|
|
userAgg,
|
|
|
|
"agent1",
|
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2023-06-20 10:36:21 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
resourceOwner: "org1",
|
|
|
|
code: code,
|
|
|
|
userID: "user1",
|
|
|
|
},
|
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
{
|
|
|
|
name: "success, other user",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPAddedEvent(ctx, userAgg2, secret),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanOTPVerifiedEvent(ctx,
|
|
|
|
userAgg2,
|
|
|
|
"agent1",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
resourceOwner: "org1",
|
|
|
|
code: code,
|
|
|
|
userID: "user2",
|
|
|
|
},
|
|
|
|
},
|
2023-06-20 10:36:21 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
2023-06-20 10:36:21 +00:00
|
|
|
multifactors: domain.MultifactorConfigs{
|
|
|
|
OTP: domain.OTPConfig{
|
|
|
|
CryptoMFA: cryptoAlg,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-08-02 16:57:53 +00:00
|
|
|
got, err := c.HumanCheckMFATOTPSetup(ctx, tt.args.userID, tt.args.code, "agent1", tt.args.resourceOwner)
|
2023-06-20 10:36:21 +00:00
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
if tt.want {
|
|
|
|
require.NotNil(t, got)
|
|
|
|
assert.Equal(t, "org1", got.ResourceOwner)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:57:53 +00:00
|
|
|
func TestCommandSide_RemoveHumanTOTP(t *testing.T) {
|
2021-03-19 10:12:56 +00:00
|
|
|
type fields struct {
|
2024-07-10 12:31:28 +00:00
|
|
|
eventstore func(t *testing.T) *eventstore.Eventstore
|
|
|
|
checkPermission domain.PermissionCheck
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
|
|
|
ctx context.Context
|
|
|
|
orgID string
|
|
|
|
userID string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
orgID: "org1",
|
|
|
|
userID: "",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "otp not existing, not found error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
orgID: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsNotFound,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-07-10 12:31:28 +00:00
|
|
|
name: "otp, no permission error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
orgID: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: zerrors.IsPermissionDenied,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "otp remove, ok",
|
2021-03-19 10:12:56 +00:00
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPRemovedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
2024-07-10 12:31:28 +00:00
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
orgID: "org1",
|
|
|
|
userID: "user1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2024-07-10 12:31:28 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
2023-08-02 16:57:53 +00:00
|
|
|
got, err := r.HumanRemoveTOTP(tt.args.ctx, tt.args.userID, tt.args.orgID)
|
2021-03-19 10:12:56 +00:00
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
if tt.res.err == nil {
|
2024-08-12 20:32:01 +00:00
|
|
|
assertObjectDetails(t, tt.res.want, got)
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 16:57:53 +00:00
|
|
|
|
|
|
|
func TestCommandSide_AddHumanOTPSMS(t *testing.T) {
|
|
|
|
ctx := authz.NewMockContext("inst1", "org1", "user1")
|
|
|
|
type fields struct {
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
checkPermission domain.PermissionCheck
|
2023-08-02 16:57:53 +00:00
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "COMMAND-QSF2s", "Errors.User.UserIDMissing"),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong user, permission denied error",
|
|
|
|
fields: fields{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user2", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
2024-05-07 05:38:26 +00:00
|
|
|
userID: "user2",
|
2023-08-02 16:57:53 +00:00
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2024-05-07 05:38:26 +00:00
|
|
|
err: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "otp sms already exists, already exists error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowAlreadyExists(nil, "COMMAND-Ad3g2", "Errors.User.MFA.OTP.AlreadyReady"),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "phone not verified, precondition failed error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPreconditionFailed(nil, "COMMAND-Q54j2", "Errors.User.MFA.OTP.NotReady"),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "phone removed, precondition failed error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneChangedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"+4179654321",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneVerifiedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneRemovedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPreconditionFailed(nil, "COMMAND-Q54j2", "Errors.User.MFA.OTP.NotReady"),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful add",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneChangedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"+4179654321",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneVerifiedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-08-02 16:57:53 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
{
|
|
|
|
name: "successful add, other user",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneChangedEvent(ctx,
|
|
|
|
&user.NewAggregate("user2", "org1").Aggregate,
|
|
|
|
"+4179654321",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneVerifiedEvent(ctx,
|
|
|
|
&user.NewAggregate("user2", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user2", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user2",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-02 16:57:53 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
2023-08-02 16:57:53 +00:00
|
|
|
}
|
|
|
|
got, err := r.AddHumanOTPSMS(tt.args.ctx, tt.args.userID, tt.args.resourceOwner)
|
|
|
|
assert.ErrorIs(t, err, tt.res.err)
|
2024-08-12 20:32:01 +00:00
|
|
|
assertObjectDetails(t, tt.res.want, got)
|
2023-08-02 16:57:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 12:47:05 +00:00
|
|
|
func TestCommandSide_AddHumanOTPSMSWithCheckSucceeded(t *testing.T) {
|
|
|
|
ctx := authz.NewMockContext("inst1", "org1", "user1")
|
|
|
|
type fields struct {
|
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
authRequest *domain.AuthRequest
|
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "successful add",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneChangedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"+4179654321",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneVerifiedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful add with auth request",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneChangedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"+4179654321",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneVerifiedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
user.NewHumanOTPSMSCheckSucceededEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authRequest: &domain.AuthRequest{
|
|
|
|
ID: "authRequestID",
|
|
|
|
AgentID: "userAgentID",
|
|
|
|
BrowserInfo: &domain.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
}
|
|
|
|
got, err := r.AddHumanOTPSMSWithCheckSucceeded(tt.args.ctx, tt.args.userID, tt.args.resourceOwner, tt.args.authRequest)
|
|
|
|
assert.ErrorIs(t, err, tt.res.err)
|
2024-08-12 20:32:01 +00:00
|
|
|
assertObjectDetails(t, tt.res.want, got)
|
2023-08-15 12:47:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:57:53 +00:00
|
|
|
func TestCommandSide_RemoveHumanOTPSMS(t *testing.T) {
|
|
|
|
ctx := authz.NewMockContext("inst1", "org1", "user1")
|
|
|
|
type fields struct {
|
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
checkPermission domain.PermissionCheck
|
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "COMMAND-S3br2", "Errors.User.UserIDMissing"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "other user not permission, permission denied error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "other",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "otp sms not added, not found error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowNotFound(nil, "COMMAND-Sr3h3", "Errors.User.MFA.OTP.NotExisting"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful remove",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPSMSRemovedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
|
|
|
}
|
|
|
|
got, err := r.RemoveHumanOTPSMS(tt.args.ctx, tt.args.userID, tt.args.resourceOwner)
|
|
|
|
assert.ErrorIs(t, err, tt.res.err)
|
2024-08-12 20:32:01 +00:00
|
|
|
assertObjectDetails(t, tt.res.want, got)
|
2023-08-15 12:47:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_HumanSendOTPSMS(t *testing.T) {
|
|
|
|
ctx := authz.NewMockContext("inst1", "org1", "user1")
|
2023-08-23 08:04:29 +00:00
|
|
|
defaultGenerators := &SecretGenerators{
|
|
|
|
OTPSMS: &crypto.GeneratorConfig{
|
|
|
|
Length: 8,
|
|
|
|
Expiry: time.Hour,
|
|
|
|
IncludeLowerLetters: true,
|
|
|
|
IncludeUpperLetters: true,
|
|
|
|
IncludeDigits: true,
|
|
|
|
IncludeSymbols: true,
|
|
|
|
},
|
|
|
|
}
|
2023-08-15 12:47:05 +00:00
|
|
|
type fields struct {
|
2023-08-23 08:04:29 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
userEncryption crypto.EncryptionAlgorithm
|
|
|
|
defaultSecretGenerators *SecretGenerators
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
authRequest *domain.AuthRequest
|
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
2023-08-23 08:04:29 +00:00
|
|
|
eventstore: expectEventstore(),
|
|
|
|
defaultSecretGenerators: defaultGenerators,
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "COMMAND-S3SF1", "Errors.User.UserIDMissing"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "otp sms not added, not found error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
2023-08-23 08:04:29 +00:00
|
|
|
defaultSecretGenerators: defaultGenerators,
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPreconditionFailed(nil, "COMMAND-SFD52", "Errors.User.MFA.OTP.NotReady"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful add",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSecretGeneratorAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
domain.SecretGeneratorTypeOTPSMS,
|
|
|
|
8,
|
|
|
|
time.Hour,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPSMSCodeAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("12345678"),
|
|
|
|
},
|
|
|
|
time.Hour,
|
|
|
|
nil,
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
|
|
|
),
|
2023-08-23 08:04:29 +00:00
|
|
|
userEncryption: crypto.CreateMockEncryptionAlgWithCode(gomock.NewController(t), "12345678"),
|
|
|
|
defaultSecretGenerators: defaultGenerators,
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful add (without secret config)",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPSMSCodeAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("12345678"),
|
|
|
|
},
|
|
|
|
time.Hour,
|
|
|
|
nil,
|
|
|
|
),
|
2023-08-23 08:04:29 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
userEncryption: crypto.CreateMockEncryptionAlgWithCode(gomock.NewController(t), "12345678"),
|
|
|
|
defaultSecretGenerators: defaultGenerators,
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful add with auth request",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSecretGeneratorAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
domain.SecretGeneratorTypeOTPSMS,
|
|
|
|
8,
|
|
|
|
time.Hour,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPSMSCodeAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("12345678"),
|
|
|
|
},
|
|
|
|
time.Hour,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
|
|
|
),
|
2023-08-23 08:04:29 +00:00
|
|
|
userEncryption: crypto.CreateMockEncryptionAlgWithCode(gomock.NewController(t), "12345678"),
|
|
|
|
defaultSecretGenerators: defaultGenerators,
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authRequest: &domain.AuthRequest{
|
|
|
|
ID: "authRequestID",
|
|
|
|
AgentID: "userAgentID",
|
|
|
|
BrowserInfo: &domain.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2023-08-23 08:04:29 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
userEncryption: tt.fields.userEncryption,
|
|
|
|
defaultSecretGenerators: tt.fields.defaultSecretGenerators,
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
|
|
|
err := r.HumanSendOTPSMS(tt.args.ctx, tt.args.userID, tt.args.resourceOwner, tt.args.authRequest)
|
|
|
|
assert.ErrorIs(t, err, tt.res.err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_HumanOTPSMSCodeSent(t *testing.T) {
|
|
|
|
ctx := authz.NewMockContext("inst1", "org1", "user1")
|
|
|
|
type fields struct {
|
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "COMMAND-AE2h2", "Errors.User.UserIDMissing"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "otp sms not added, not found error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPreconditionFailed(nil, "COMMAND-SD3gh", "Errors.User.MFA.OTP.NotReady"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful add",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPSMSCodeSentEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
}
|
|
|
|
err := r.HumanOTPSMSCodeSent(tt.args.ctx, tt.args.userID, tt.args.resourceOwner)
|
|
|
|
assert.ErrorIs(t, err, tt.res.err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_HumanCheckOTPSMS(t *testing.T) {
|
|
|
|
ctx := authz.NewMockContext("inst1", "org1", "user1")
|
|
|
|
type fields struct {
|
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
userEncryption crypto.EncryptionAlgorithm
|
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
code string
|
|
|
|
resourceOwner string
|
|
|
|
authRequest *domain.AuthRequest
|
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "",
|
|
|
|
code: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "COMMAND-S453v", "Errors.User.UserIDMissing"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code missing, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
code: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "COMMAND-SJl2g", "Errors.User.Code.Empty"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "otp sms not added, precondition failed error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
code: "code",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPreconditionFailed(nil, "COMMAND-d2r52", "Errors.User.MFA.OTP.NotReady"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "otp sms code not added, precondition failed error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
code: "code",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPreconditionFailed(nil, "COMMAND-S34gh", "Errors.User.Code.NotFound"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid code, error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanOTPSMSCodeAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("other-code"),
|
|
|
|
},
|
|
|
|
time.Hour,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-04-10 09:14:55 +00:00
|
|
|
expectFilter(), // recheck
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLockoutPolicyAddedEvent(ctx,
|
|
|
|
&org.NewAggregate("orgID").Aggregate,
|
|
|
|
3, 3, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanOTPSMSCheckFailedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
userEncryption: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
code: "code",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authRequest: &domain.AuthRequest{
|
|
|
|
ID: "authRequestID",
|
|
|
|
AgentID: "userAgentID",
|
|
|
|
BrowserInfo: &domain.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "CODE-woT0xc", "Errors.User.Code.Invalid"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid code, max attempts reached, error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanOTPSMSCodeAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("other-code"),
|
|
|
|
},
|
|
|
|
time.Hour,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(), // recheck
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLockoutPolicyAddedEvent(ctx,
|
|
|
|
&org.NewAggregate("orgID").Aggregate,
|
|
|
|
1, 1, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPSMSCheckFailedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
2024-04-10 09:14:55 +00:00
|
|
|
user.NewUserLockedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
userEncryption: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
code: "code",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authRequest: &domain.AuthRequest{
|
|
|
|
ID: "authRequestID",
|
|
|
|
AgentID: "userAgentID",
|
|
|
|
BrowserInfo: &domain.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "CODE-woT0xc", "Errors.User.Code.Invalid"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanOTPSMSCodeAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("code"),
|
|
|
|
},
|
|
|
|
time.Hour,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-04-10 09:14:55 +00:00
|
|
|
expectFilter(), // recheck
|
2023-08-15 12:47:05 +00:00
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPSMSCheckSucceededEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
userEncryption: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
code: "code",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authRequest: &domain.AuthRequest{
|
|
|
|
ID: "authRequestID",
|
|
|
|
AgentID: "userAgentID",
|
|
|
|
BrowserInfo: &domain.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-04-10 09:14:55 +00:00
|
|
|
{
|
|
|
|
name: "code ok, locked in the meantime",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPSMSAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanOTPSMSCodeAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("code"),
|
|
|
|
},
|
|
|
|
time.Hour,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter( // recheck
|
|
|
|
user.NewUserLockedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
userEncryption: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
code: "code",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authRequest: &domain.AuthRequest{
|
|
|
|
ID: "authRequestID",
|
|
|
|
AgentID: "userAgentID",
|
|
|
|
BrowserInfo: &domain.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: zerrors.ThrowPreconditionFailed(nil, "COMMAND-S6h4R", "Errors.User.Locked"),
|
|
|
|
},
|
|
|
|
},
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
userEncryption: tt.fields.userEncryption,
|
|
|
|
}
|
|
|
|
err := r.HumanCheckOTPSMS(tt.args.ctx, tt.args.userID, tt.args.code, tt.args.resourceOwner, tt.args.authRequest)
|
|
|
|
assert.ErrorIs(t, err, tt.res.err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_AddHumanOTPEmail(t *testing.T) {
|
|
|
|
ctx := authz.NewMockContext("inst1", "org1", "user1")
|
|
|
|
type fields struct {
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
checkPermission domain.PermissionCheck
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "COMMAND-Sg1hz", "Errors.User.UserIDMissing"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
{
|
|
|
|
name: "wrong user, permission denied error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user2", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user2",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
|
|
|
},
|
|
|
|
},
|
2023-08-15 12:47:05 +00:00
|
|
|
{
|
|
|
|
name: "otp email already exists, already exists error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowAlreadyExists(nil, "COMMAND-MKL2s", "Errors.User.MFA.OTP.AlreadyReady"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "email not verified, precondition failed error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPreconditionFailed(nil, "COMMAND-KLJ2d", "Errors.User.MFA.OTP.NotReady"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful add",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailChangedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"email@test.ch",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-07 05:38:26 +00:00
|
|
|
{
|
|
|
|
name: "successful add, other user",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailChangedEvent(ctx,
|
|
|
|
&user.NewAggregate("user2", "org1").Aggregate,
|
|
|
|
"email@test.ch",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(ctx,
|
|
|
|
&user.NewAggregate("user2", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user2", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user2",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2024-05-07 05:38:26 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
|
|
|
got, err := r.AddHumanOTPEmail(tt.args.ctx, tt.args.userID, tt.args.resourceOwner)
|
|
|
|
assert.ErrorIs(t, err, tt.res.err)
|
2024-08-12 20:32:01 +00:00
|
|
|
assertObjectDetails(t, tt.res.want, got)
|
2023-08-15 12:47:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_AddHumanOTPEmailWithCheckSucceeded(t *testing.T) {
|
|
|
|
ctx := authz.NewMockContext("inst1", "org1", "user1")
|
|
|
|
type fields struct {
|
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
authRequest *domain.AuthRequest
|
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "successful add",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailChangedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"email@test.ch",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful add with auth request",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailChangedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"email@test.ch",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
user.NewHumanOTPEmailCheckSucceededEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authRequest: &domain.AuthRequest{
|
|
|
|
ID: "authRequestID",
|
|
|
|
AgentID: "userAgentID",
|
|
|
|
BrowserInfo: &domain.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
}
|
|
|
|
got, err := r.AddHumanOTPEmailWithCheckSucceeded(tt.args.ctx, tt.args.userID, tt.args.resourceOwner, tt.args.authRequest)
|
|
|
|
assert.ErrorIs(t, err, tt.res.err)
|
2024-08-12 20:32:01 +00:00
|
|
|
assertObjectDetails(t, tt.res.want, got)
|
2023-08-15 12:47:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_RemoveHumanOTPEmail(t *testing.T) {
|
|
|
|
ctx := authz.NewMockContext("inst1", "org1", "user1")
|
|
|
|
type fields struct {
|
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
checkPermission domain.PermissionCheck
|
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "COMMAND-S2h11", "Errors.User.UserIDMissing"),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "other user not permission, permission denied error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "other",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-08-15 12:47:05 +00:00
|
|
|
name: "otp email not added, not found error",
|
2023-08-02 16:57:53 +00:00
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowNotFound(nil, "COMMAND-b312D", "Errors.User.MFA.OTP.NotExisting"),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful remove",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2023-08-15 12:47:05 +00:00
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
2023-08-02 16:57:53 +00:00
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPEmailRemovedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-08-02 16:57:53 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
|
|
|
}
|
2023-08-15 12:47:05 +00:00
|
|
|
got, err := r.RemoveHumanOTPEmail(tt.args.ctx, tt.args.userID, tt.args.resourceOwner)
|
2023-08-02 16:57:53 +00:00
|
|
|
assert.ErrorIs(t, err, tt.res.err)
|
2024-08-12 20:32:01 +00:00
|
|
|
assertObjectDetails(t, tt.res.want, got)
|
2023-08-02 16:57:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 12:47:05 +00:00
|
|
|
func TestCommandSide_HumanSendOTPEmail(t *testing.T) {
|
2023-08-02 16:57:53 +00:00
|
|
|
ctx := authz.NewMockContext("inst1", "org1", "user1")
|
2023-08-23 08:04:29 +00:00
|
|
|
defaultGenerators := &SecretGenerators{
|
|
|
|
OTPEmail: &crypto.GeneratorConfig{
|
|
|
|
Length: 8,
|
|
|
|
Expiry: time.Hour,
|
|
|
|
IncludeLowerLetters: true,
|
|
|
|
IncludeUpperLetters: true,
|
|
|
|
IncludeDigits: true,
|
|
|
|
IncludeSymbols: true,
|
|
|
|
},
|
|
|
|
}
|
2023-08-02 16:57:53 +00:00
|
|
|
type fields struct {
|
2023-08-23 08:04:29 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
userEncryption crypto.EncryptionAlgorithm
|
|
|
|
defaultSecretGenerators *SecretGenerators
|
2023-08-02 16:57:53 +00:00
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
2023-08-15 12:47:05 +00:00
|
|
|
authRequest *domain.AuthRequest
|
2023-08-02 16:57:53 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
2023-08-23 08:04:29 +00:00
|
|
|
eventstore: expectEventstore(),
|
|
|
|
defaultSecretGenerators: defaultGenerators,
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "COMMAND-S3SF1", "Errors.User.UserIDMissing"),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-08-15 12:47:05 +00:00
|
|
|
name: "otp email not added, not found error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
2023-08-23 08:04:29 +00:00
|
|
|
defaultSecretGenerators: defaultGenerators,
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPreconditionFailed(nil, "COMMAND-SFD52", "Errors.User.MFA.OTP.NotReady"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful add",
|
2023-08-02 16:57:53 +00:00
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSecretGeneratorAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
domain.SecretGeneratorTypeOTPEmail,
|
|
|
|
8,
|
|
|
|
time.Hour,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPEmailCodeAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("12345678"),
|
|
|
|
},
|
|
|
|
time.Hour,
|
|
|
|
nil,
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
2023-08-02 16:57:53 +00:00
|
|
|
),
|
2023-08-23 08:04:29 +00:00
|
|
|
userEncryption: crypto.CreateMockEncryptionAlgWithCode(gomock.NewController(t), "12345678"),
|
|
|
|
defaultSecretGenerators: defaultGenerators,
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful add (without secret config)",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPEmailCodeAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("12345678"),
|
|
|
|
},
|
|
|
|
time.Hour,
|
|
|
|
nil,
|
|
|
|
),
|
2023-08-23 08:04:29 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
userEncryption: crypto.CreateMockEncryptionAlgWithCode(gomock.NewController(t), "12345678"),
|
|
|
|
defaultSecretGenerators: defaultGenerators,
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-08-15 12:47:05 +00:00
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-08-15 12:47:05 +00:00
|
|
|
name: "successful add with auth request",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSecretGeneratorAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
domain.SecretGeneratorTypeOTPEmail,
|
|
|
|
8,
|
|
|
|
time.Hour,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPEmailCodeAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("12345678"),
|
|
|
|
},
|
|
|
|
time.Hour,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
|
|
|
),
|
2023-08-23 08:04:29 +00:00
|
|
|
userEncryption: crypto.CreateMockEncryptionAlgWithCode(gomock.NewController(t), "12345678"),
|
|
|
|
defaultSecretGenerators: defaultGenerators,
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authRequest: &domain.AuthRequest{
|
|
|
|
ID: "authRequestID",
|
|
|
|
AgentID: "userAgentID",
|
|
|
|
BrowserInfo: &domain.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2023-08-23 08:04:29 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
userEncryption: tt.fields.userEncryption,
|
|
|
|
defaultSecretGenerators: tt.fields.defaultSecretGenerators,
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
|
|
|
err := r.HumanSendOTPEmail(tt.args.ctx, tt.args.userID, tt.args.resourceOwner, tt.args.authRequest)
|
|
|
|
assert.ErrorIs(t, err, tt.res.err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_HumanOTPEmailCodeSent(t *testing.T) {
|
|
|
|
ctx := authz.NewMockContext("inst1", "org1", "user1")
|
|
|
|
type fields struct {
|
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "COMMAND-AE2h2", "Errors.User.UserIDMissing"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "otp email not added, not found error",
|
2023-08-02 16:57:53 +00:00
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPreconditionFailed(nil, "COMMAND-SD3gh", "Errors.User.MFA.OTP.NotReady"),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful add",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2023-08-15 12:47:05 +00:00
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
2023-08-02 16:57:53 +00:00
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPEmailCodeSentEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-08-02 16:57:53 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
}
|
2023-08-15 12:47:05 +00:00
|
|
|
err := r.HumanOTPEmailCodeSent(tt.args.ctx, tt.args.userID, tt.args.resourceOwner)
|
2023-08-02 16:57:53 +00:00
|
|
|
assert.ErrorIs(t, err, tt.res.err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 12:47:05 +00:00
|
|
|
func TestCommandSide_HumanCheckOTPEmail(t *testing.T) {
|
2023-08-02 16:57:53 +00:00
|
|
|
ctx := authz.NewMockContext("inst1", "org1", "user1")
|
|
|
|
type fields struct {
|
2023-08-15 12:47:05 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
userEncryption crypto.EncryptionAlgorithm
|
2023-08-02 16:57:53 +00:00
|
|
|
}
|
|
|
|
type (
|
|
|
|
args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
2023-08-15 12:47:05 +00:00
|
|
|
code string
|
2023-08-02 16:57:53 +00:00
|
|
|
resourceOwner string
|
2023-08-15 12:47:05 +00:00
|
|
|
authRequest *domain.AuthRequest
|
2023-08-02 16:57:53 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "",
|
2023-08-15 12:47:05 +00:00
|
|
|
code: "",
|
2023-08-02 16:57:53 +00:00
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "COMMAND-S453v", "Errors.User.UserIDMissing"),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-08-15 12:47:05 +00:00
|
|
|
name: "code missing, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
code: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "COMMAND-SJl2g", "Errors.User.Code.Empty"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "otp email not added, precondition failed error",
|
2023-08-02 16:57:53 +00:00
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
2023-08-15 12:47:05 +00:00
|
|
|
userID: "user1",
|
|
|
|
code: "code",
|
2023-08-02 16:57:53 +00:00
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPreconditionFailed(nil, "COMMAND-d2r52", "Errors.User.MFA.OTP.NotReady"),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-08-15 12:47:05 +00:00
|
|
|
name: "otp email code not added, precondition failed error",
|
2023-08-02 16:57:53 +00:00
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
2023-08-15 12:47:05 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-08-02 16:57:53 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
2023-08-15 12:47:05 +00:00
|
|
|
code: "code",
|
2023-08-02 16:57:53 +00:00
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPreconditionFailed(nil, "COMMAND-S34gh", "Errors.User.Code.NotFound"),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-08-15 12:47:05 +00:00
|
|
|
name: "invalid code, error",
|
2023-08-02 16:57:53 +00:00
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanOTPEmailCodeAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("other-code"),
|
|
|
|
},
|
|
|
|
time.Hour,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
2023-08-02 16:57:53 +00:00
|
|
|
),
|
2024-04-10 09:14:55 +00:00
|
|
|
expectFilter(), // recheck
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLockoutPolicyAddedEvent(ctx,
|
|
|
|
&org.NewAggregate("orgID").Aggregate,
|
|
|
|
3, 3, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanOTPEmailCheckFailedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
userEncryption: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
code: "code",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authRequest: &domain.AuthRequest{
|
|
|
|
ID: "authRequestID",
|
|
|
|
AgentID: "userAgentID",
|
|
|
|
BrowserInfo: &domain.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "CODE-woT0xc", "Errors.User.Code.Invalid"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid code, max attempts reached, error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanOTPEmailCodeAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("other-code"),
|
|
|
|
},
|
|
|
|
time.Hour,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(), // recheck
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLockoutPolicyAddedEvent(ctx,
|
|
|
|
&org.NewAggregate("orgID").Aggregate,
|
|
|
|
1, 1, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-08-02 16:57:53 +00:00
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPEmailCheckFailedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
2024-04-10 09:14:55 +00:00
|
|
|
user.NewUserLockedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-08-02 16:57:53 +00:00
|
|
|
),
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
userEncryption: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
code: "code",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authRequest: &domain.AuthRequest{
|
|
|
|
ID: "authRequestID",
|
|
|
|
AgentID: "userAgentID",
|
|
|
|
BrowserInfo: &domain.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "CODE-woT0xc", "Errors.User.Code.Invalid"),
|
2023-08-15 12:47:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanOTPEmailCodeAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("code"),
|
|
|
|
},
|
|
|
|
time.Hour,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-04-10 09:14:55 +00:00
|
|
|
expectFilter(), // recheck
|
2023-08-15 12:47:05 +00:00
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanOTPEmailCheckSucceededEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
userEncryption: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
2023-08-15 12:47:05 +00:00
|
|
|
code: "code",
|
2023-08-02 16:57:53 +00:00
|
|
|
resourceOwner: "org1",
|
2023-08-15 12:47:05 +00:00
|
|
|
authRequest: &domain.AuthRequest{
|
|
|
|
ID: "authRequestID",
|
|
|
|
AgentID: "userAgentID",
|
|
|
|
BrowserInfo: &domain.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-04-10 09:14:55 +00:00
|
|
|
{
|
|
|
|
name: "code ok, locked in the meantime",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanOTPEmailAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanOTPEmailCodeAddedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("code"),
|
|
|
|
},
|
|
|
|
time.Hour,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "authRequestID",
|
|
|
|
UserAgentID: "userAgentID",
|
|
|
|
BrowserInfo: &user.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter( // recheck
|
|
|
|
user.NewUserLockedEvent(ctx,
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
userEncryption: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: ctx,
|
|
|
|
userID: "user1",
|
|
|
|
code: "code",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authRequest: &domain.AuthRequest{
|
|
|
|
ID: "authRequestID",
|
|
|
|
AgentID: "userAgentID",
|
|
|
|
BrowserInfo: &domain.BrowserInfo{
|
|
|
|
UserAgent: "user-agent",
|
|
|
|
AcceptLanguage: "en",
|
|
|
|
RemoteIP: net.IP{192, 0, 2, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: zerrors.ThrowPreconditionFailed(nil, "COMMAND-S6h4R", "Errors.User.Locked"),
|
|
|
|
},
|
|
|
|
},
|
2023-08-02 16:57:53 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2023-08-15 12:47:05 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
userEncryption: tt.fields.userEncryption,
|
2023-08-02 16:57:53 +00:00
|
|
|
}
|
2023-08-15 12:47:05 +00:00
|
|
|
err := r.HumanCheckOTPEmail(tt.args.ctx, tt.args.userID, tt.args.code, tt.args.resourceOwner, tt.args.authRequest)
|
2023-08-02 16:57:53 +00:00
|
|
|
assert.ErrorIs(t, err, tt.res.err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|