2023-05-24 10:22:00 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-22 10:56:43 +00:00
|
|
|
"go.uber.org/mock/gomock"
|
2023-05-24 10:22:00 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
|
|
|
"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"
|
2023-05-24 10:22:00 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/id"
|
|
|
|
id_mock "github.com/zitadel/zitadel/internal/id/mock"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
|
|
webauthn_helper "github.com/zitadel/zitadel/internal/webauthn"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-05-24 10:22:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommands_RegisterUserPasskey(t *testing.T) {
|
|
|
|
ctx := authz.NewMockContextWithPermissions("instance1", "org1", "user1", nil)
|
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(ctx, "example.com")
|
2023-05-24 10:22:00 +00:00
|
|
|
|
|
|
|
webauthnConfig := &webauthn_helper.Config{
|
|
|
|
DisplayName: "test",
|
|
|
|
ExternalSecure: true,
|
|
|
|
}
|
|
|
|
userAgg := &user.NewAggregate("user1", "org1").Aggregate
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
idGenerator id.Generator
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
2023-06-27 12:36:07 +00:00
|
|
|
rpID string
|
2023-05-24 10:22:00 +00:00
|
|
|
authenticator domain.AuthenticatorAttachment
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
2023-06-15 05:32:40 +00:00
|
|
|
want *domain.WebAuthNRegistrationDetails
|
2023-05-24 10:22:00 +00:00
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wrong user",
|
|
|
|
args: args{
|
|
|
|
userID: "foo",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authenticator: domain.AuthenticatorAttachmentCrossPlattform,
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowPermissionDenied(nil, "AUTH-Bohd2", "Errors.User.UserIDWrong"),
|
2023-05-24 10:22:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "get human passwordless error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilterError(io.ErrClosedPipe),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authenticator: domain.AuthenticatorAttachmentCrossPlattform,
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "id generator error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(), // getHumanPasswordlessTokens
|
|
|
|
expectFilter(eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(ctx,
|
|
|
|
userAgg,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
expectFilter(eventFromEventPusher(
|
|
|
|
org.NewOrgAddedEvent(ctx,
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
"org1",
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
expectFilter(eventFromEventPusher(
|
|
|
|
org.NewDomainPolicyAddedEvent(ctx,
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
false, false, false,
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectError(t, io.ErrClosedPipe),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authenticator: domain.AuthenticatorAttachmentCrossPlattform,
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
idGenerator: tt.fields.idGenerator,
|
|
|
|
webauthnConfig: webauthnConfig,
|
|
|
|
}
|
2023-06-27 12:36:07 +00:00
|
|
|
_, err := c.RegisterUserPasskey(ctx, tt.args.userID, tt.args.resourceOwner, tt.args.rpID, tt.args.authenticator)
|
2023-05-24 10:22:00 +00:00
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
// successful case can't be tested due to random challenge.
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_RegisterUserPasskeyWithCode(t *testing.T) {
|
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(context.Background(), "example.com")
|
2023-05-24 10:22:00 +00:00
|
|
|
webauthnConfig := &webauthn_helper.Config{
|
|
|
|
DisplayName: "test",
|
|
|
|
ExternalSecure: true,
|
|
|
|
}
|
|
|
|
alg := crypto.CreateMockEncryptionAlg(gomock.NewController(t))
|
|
|
|
es := eventstoreExpect(t,
|
|
|
|
expectFilter(eventFromEventPusher(testSecretGeneratorAddedEvent(domain.SecretGeneratorTypePasswordlessInitCode))),
|
|
|
|
)
|
2024-04-05 09:35:49 +00:00
|
|
|
code, err := newEncryptedCode(ctx, es.Filter, domain.SecretGeneratorTypePasswordlessInitCode, alg) //nolint:staticcheck
|
2023-05-24 10:22:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
userAgg := &user.NewAggregate("user1", "org1").Aggregate
|
|
|
|
type fields struct {
|
2024-09-11 10:53:55 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
2023-05-24 10:22:00 +00:00
|
|
|
idGenerator id.Generator
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
2023-06-27 12:36:07 +00:00
|
|
|
rpID string
|
2023-05-24 10:22:00 +00:00
|
|
|
authenticator domain.AuthenticatorAttachment
|
|
|
|
codeID string
|
|
|
|
code string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "code verification error",
|
|
|
|
fields: fields{
|
2024-09-11 10:53:55 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-05-24 10:22:00 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPasswordlessInitCodeRequestedEvent(context.Background(),
|
|
|
|
userAgg, "123", code.Crypted, time.Minute, "", false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPasswordlessInitCodeSentEvent(ctx, userAgg, "123"),
|
|
|
|
),
|
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
expectPush(
|
2023-05-24 10:22:00 +00:00
|
|
|
user.NewHumanPasswordlessInitCodeCheckFailedEvent(ctx, userAgg, "123"),
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authenticator: domain.AuthenticatorAttachmentCrossPlattform,
|
|
|
|
codeID: "123",
|
|
|
|
code: "wrong",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(err, "COMMAND-Eeb2a", "Errors.User.Code.Invalid"),
|
2023-05-24 10:22:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code verification ok, get human passwordless error",
|
|
|
|
fields: fields{
|
2024-09-11 10:53:55 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-05-24 10:22:00 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPasswordlessInitCodeRequestedEvent(context.Background(),
|
|
|
|
userAgg, "123", code.Crypted, time.Minute, "", false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPasswordlessInitCodeSentEvent(ctx, userAgg, "123"),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilterError(io.ErrClosedPipe),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authenticator: domain.AuthenticatorAttachmentCrossPlattform,
|
|
|
|
codeID: "123",
|
|
|
|
code: code.Plain,
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
2024-09-11 10:53:55 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
2023-05-24 10:22:00 +00:00
|
|
|
idGenerator: tt.fields.idGenerator,
|
|
|
|
webauthnConfig: webauthnConfig,
|
|
|
|
}
|
2023-06-27 12:36:07 +00:00
|
|
|
_, err := c.RegisterUserPasskeyWithCode(ctx, tt.args.userID, tt.args.resourceOwner, tt.args.authenticator, tt.args.codeID, tt.args.code, tt.args.rpID, alg)
|
2023-05-24 10:22:00 +00:00
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
// successful case can't be tested due to random challenge.
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_verifyUserPasskeyCode(t *testing.T) {
|
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(context.Background(), "example.com")
|
2023-05-24 10:22:00 +00:00
|
|
|
alg := crypto.CreateMockEncryptionAlg(gomock.NewController(t))
|
|
|
|
es := eventstoreExpect(t,
|
|
|
|
expectFilter(eventFromEventPusher(testSecretGeneratorAddedEvent(domain.SecretGeneratorTypePasswordlessInitCode))),
|
|
|
|
)
|
2024-04-05 09:35:49 +00:00
|
|
|
code, err := newEncryptedCode(ctx, es.Filter, domain.SecretGeneratorTypePasswordlessInitCode, alg) //nolint:staticcheck
|
2023-05-24 10:22:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
userAgg := &user.NewAggregate("user1", "org1").Aggregate
|
|
|
|
|
|
|
|
type fields struct {
|
2024-09-11 10:53:55 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
2023-05-24 10:22:00 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
codeID string
|
|
|
|
code string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want *user.HumanPasswordlessInitCodeCheckSucceededEvent
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "filter error",
|
|
|
|
fields: fields{
|
2024-09-11 10:53:55 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-05-24 10:22:00 +00:00
|
|
|
expectFilterError(io.ErrClosedPipe),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
codeID: "123",
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code verification error",
|
|
|
|
fields: fields{
|
2024-09-11 10:53:55 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-05-24 10:22:00 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPasswordlessInitCodeRequestedEvent(context.Background(),
|
|
|
|
userAgg, "123", code.Crypted, time.Minute, "", false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPasswordlessInitCodeSentEvent(ctx, userAgg, "123"),
|
|
|
|
),
|
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
expectPush(
|
2023-05-24 10:22:00 +00:00
|
|
|
user.NewHumanPasswordlessInitCodeCheckFailedEvent(ctx, userAgg, "123"),
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
codeID: "123",
|
|
|
|
code: "wrong",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(err, "COMMAND-Eeb2a", "Errors.User.Code.Invalid"),
|
2023-05-24 10:22:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
fields: fields{
|
2024-09-11 10:53:55 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-05-24 10:22:00 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPasswordlessInitCodeRequestedEvent(context.Background(),
|
|
|
|
userAgg, "123", code.Crypted, time.Minute, "", false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPasswordlessInitCodeSentEvent(ctx, userAgg, "123"),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
codeID: "123",
|
|
|
|
code: code.Plain,
|
|
|
|
},
|
|
|
|
want: user.NewHumanPasswordlessInitCodeCheckSucceededEvent(ctx, userAgg, "123"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
2024-09-11 10:53:55 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
2023-05-24 10:22:00 +00:00
|
|
|
}
|
|
|
|
got, err := c.verifyUserPasskeyCode(ctx, tt.args.userID, tt.args.resourceOwner, tt.args.codeID, tt.args.code, alg)
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
if tt.wantErr == nil {
|
|
|
|
assert.Equal(t, tt.want, got(ctx, userAgg))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_pushUserPasskey(t *testing.T) {
|
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("instance1", "org1", "user1"), "example.com")
|
2023-05-24 10:22:00 +00:00
|
|
|
webauthnConfig := &webauthn_helper.Config{
|
|
|
|
DisplayName: "test",
|
|
|
|
ExternalSecure: true,
|
|
|
|
}
|
|
|
|
userAgg := &user.NewAggregate("user1", "org1").Aggregate
|
|
|
|
|
|
|
|
prep := []expect{
|
|
|
|
expectFilter(), // getHumanPasswordlessTokens
|
|
|
|
expectFilter(eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(ctx,
|
|
|
|
userAgg,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
expectFilter(eventFromEventPusher(
|
|
|
|
org.NewOrgAddedEvent(ctx,
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
"org1",
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
expectFilter(eventFromEventPusher(
|
|
|
|
org.NewDomainPolicyAddedEvent(ctx,
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
false, false, false,
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
expectFilter(eventFromEventPusher(
|
|
|
|
user.NewHumanWebAuthNAddedEvent(eventstore.NewBaseEventForPush(
|
|
|
|
ctx, &org.NewAggregate("org1").Aggregate, user.HumanPasswordlessTokenAddedType,
|
2023-06-27 12:36:07 +00:00
|
|
|
), "111", "challenge", "rpID"),
|
2023-05-24 10:22:00 +00:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
events []eventCallback
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
expectPush func(challenge string) expect
|
|
|
|
args args
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "push error",
|
|
|
|
expectPush: func(challenge string) expect {
|
2023-10-19 10:19:10 +00:00
|
|
|
return expectPushFailed(io.ErrClosedPipe,
|
2023-05-24 10:22:00 +00:00
|
|
|
user.NewHumanPasswordlessAddedEvent(ctx,
|
2023-06-27 12:36:07 +00:00
|
|
|
userAgg, "123", challenge, "rpID",
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
)
|
2023-05-24 10:22:00 +00:00
|
|
|
},
|
|
|
|
args: args{},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
expectPush: func(challenge string) expect {
|
2023-10-19 10:19:10 +00:00
|
|
|
return expectPush(
|
2023-05-24 10:22:00 +00:00
|
|
|
user.NewHumanPasswordlessAddedEvent(ctx,
|
2023-06-27 12:36:07 +00:00
|
|
|
userAgg, "123", challenge, "rpID",
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
)
|
2023-05-24 10:22:00 +00:00
|
|
|
},
|
|
|
|
args: args{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "initcode succeeded event",
|
|
|
|
expectPush: func(challenge string) expect {
|
2023-10-19 10:19:10 +00:00
|
|
|
return expectPush(
|
|
|
|
user.NewHumanPasswordlessAddedEvent(ctx,
|
|
|
|
userAgg, "123", challenge, "rpID",
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordlessInitCodeCheckSucceededEvent(ctx, userAgg, "123"),
|
|
|
|
)
|
2023-05-24 10:22:00 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
events: []eventCallback{func(ctx context.Context, userAgg *eventstore.Aggregate) eventstore.Command {
|
|
|
|
return user.NewHumanPasswordlessInitCodeCheckSucceededEvent(ctx, userAgg, "123")
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: eventstoreExpect(t, prep...),
|
|
|
|
webauthnConfig: webauthnConfig,
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "123"),
|
|
|
|
}
|
2023-06-27 12:36:07 +00:00
|
|
|
wm, userAgg, webAuthN, err := c.createUserPasskey(ctx, "user1", "org1", "rpID", domain.AuthenticatorAttachmentCrossPlattform)
|
2023-05-24 10:22:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
c.eventstore = eventstoreExpect(t, tt.expectPush(webAuthN.Challenge))
|
|
|
|
|
|
|
|
got, err := c.pushUserPasskey(ctx, wm, userAgg, webAuthN, tt.args.events...)
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
if tt.wantErr == nil {
|
|
|
|
assert.NotEmpty(t, got.PublicKeyCredentialCreationOptions)
|
2023-06-15 05:32:40 +00:00
|
|
|
assert.Equal(t, "123", got.ID)
|
2023-05-24 10:22:00 +00:00
|
|
|
assert.Equal(t, "org1", got.ObjectDetails.ResourceOwner)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_AddUserPasskeyCode(t *testing.T) {
|
|
|
|
alg := crypto.CreateMockEncryptionAlg(gomock.NewController(t))
|
|
|
|
userAgg := &user.NewAggregate("user1", "org1").Aggregate
|
|
|
|
type fields struct {
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode encrypedCodeFunc
|
2023-07-10 08:07:10 +00:00
|
|
|
eventstore func(t *testing.T) *eventstore.Eventstore
|
2023-05-24 10:22:00 +00:00
|
|
|
idGenerator id.Generator
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "id generator error",
|
|
|
|
fields: fields{
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: mockEncryptedCode("passkey1", time.Hour),
|
2023-07-10 08:07:10 +00:00
|
|
|
eventstore: expectEventstore(),
|
2023-05-24 10:22:00 +00:00
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectError(t, io.ErrClosedPipe),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
fields: fields{
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: mockEncryptedCode("passkey1", time.Minute),
|
2023-07-10 08:07:10 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-05-24 10:22:00 +00:00
|
|
|
expectFilter(eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
userAgg,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
)),
|
2023-10-19 10:19:10 +00:00
|
|
|
expectPush(
|
|
|
|
user.NewHumanPasswordlessInitCodeRequestedEvent(context.Background(),
|
|
|
|
userAgg,
|
|
|
|
"123", &crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("passkey1"),
|
|
|
|
}, time.Minute, "", false,
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "123"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
2024-04-05 09:35:49 +00:00
|
|
|
newEncryptedCode: tt.fields.newCode,
|
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
idGenerator: tt.fields.idGenerator,
|
2023-05-24 10:22:00 +00:00
|
|
|
}
|
|
|
|
got, err := c.AddUserPasskeyCode(context.Background(), tt.args.userID, tt.args.resourceOwner, alg)
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
2024-08-12 20:32:01 +00:00
|
|
|
assertObjectDetails(t, tt.want, got)
|
2023-05-24 10:22:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_AddUserPasskeyCodeURLTemplate(t *testing.T) {
|
|
|
|
alg := crypto.CreateMockEncryptionAlg(gomock.NewController(t))
|
|
|
|
userAgg := &user.NewAggregate("user1", "org1").Aggregate
|
|
|
|
|
|
|
|
type fields struct {
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode encrypedCodeFunc
|
2023-05-24 10:22:00 +00:00
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
idGenerator id.Generator
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
urlTmpl string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "template error",
|
|
|
|
fields: fields{
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: newEncryptedCode,
|
2023-05-24 10:22:00 +00:00
|
|
|
eventstore: eventstoreExpect(t),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
urlTmpl: "{{",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "DOMAIN-oGh5e", "Errors.User.InvalidURLTemplate"),
|
2023-05-24 10:22:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "id generator error",
|
|
|
|
fields: fields{
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: newEncryptedCode,
|
2023-05-24 10:22:00 +00:00
|
|
|
eventstore: eventstoreExpect(t),
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectError(t, io.ErrClosedPipe),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
urlTmpl: "https://example.com/passkey/register?userID={{.UserID}}&orgID={{.OrgID}}&codeID={{.CodeID}}&code={{.Code}}",
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
fields: fields{
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: mockEncryptedCode("passkey1", time.Minute),
|
2023-05-24 10:22:00 +00:00
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
userAgg,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
)),
|
2023-10-19 10:19:10 +00:00
|
|
|
expectPush(
|
|
|
|
user.NewHumanPasswordlessInitCodeRequestedEvent(context.Background(),
|
|
|
|
userAgg,
|
|
|
|
"123", &crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("passkey1"),
|
|
|
|
},
|
|
|
|
time.Minute,
|
|
|
|
"https://example.com/passkey/register?userID={{.UserID}}&orgID={{.OrgID}}&codeID={{.CodeID}}&code={{.Code}}",
|
|
|
|
false,
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "123"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
urlTmpl: "https://example.com/passkey/register?userID={{.UserID}}&orgID={{.OrgID}}&codeID={{.CodeID}}&code={{.Code}}",
|
|
|
|
},
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
2024-04-05 09:35:49 +00:00
|
|
|
newEncryptedCode: tt.fields.newCode,
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
idGenerator: tt.fields.idGenerator,
|
2023-05-24 10:22:00 +00:00
|
|
|
}
|
|
|
|
got, err := c.AddUserPasskeyCodeURLTemplate(context.Background(), tt.args.userID, tt.args.resourceOwner, alg, tt.args.urlTmpl)
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
2024-08-12 20:32:01 +00:00
|
|
|
assertObjectDetails(t, tt.want, got)
|
2023-05-24 10:22:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_AddUserPasskeyCodeReturn(t *testing.T) {
|
|
|
|
alg := crypto.CreateMockEncryptionAlg(gomock.NewController(t))
|
|
|
|
userAgg := &user.NewAggregate("user1", "org1").Aggregate
|
|
|
|
type fields struct {
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode encrypedCodeFunc
|
2023-05-24 10:22:00 +00:00
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
idGenerator id.Generator
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want *domain.PasskeyCodeDetails
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "id generator error",
|
|
|
|
fields: fields{
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: newEncryptedCode,
|
2023-05-24 10:22:00 +00:00
|
|
|
eventstore: eventstoreExpect(t),
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectError(t, io.ErrClosedPipe),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
fields: fields{
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: mockEncryptedCode("passkey1", time.Minute),
|
2023-05-24 10:22:00 +00:00
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
userAgg,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
)),
|
2023-10-19 10:19:10 +00:00
|
|
|
expectPush(
|
|
|
|
user.NewHumanPasswordlessInitCodeRequestedEvent(context.Background(),
|
|
|
|
userAgg,
|
|
|
|
"123", &crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("passkey1"),
|
|
|
|
}, time.Minute, "", true,
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "123"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
want: &domain.PasskeyCodeDetails{
|
|
|
|
ObjectDetails: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
CodeID: "123",
|
|
|
|
Code: "passkey1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
2024-04-05 09:35:49 +00:00
|
|
|
newEncryptedCode: tt.fields.newCode,
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
idGenerator: tt.fields.idGenerator,
|
2023-05-24 10:22:00 +00:00
|
|
|
}
|
|
|
|
got, err := c.AddUserPasskeyCodeReturn(context.Background(), tt.args.userID, tt.args.resourceOwner, alg)
|
2024-08-12 20:32:01 +00:00
|
|
|
if tt.wantErr != nil {
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(t, tt.want.CodeID, got.CodeID)
|
|
|
|
assert.Equal(t, tt.want.Code, got.Code)
|
|
|
|
assertObjectDetails(t, tt.want.ObjectDetails, got.ObjectDetails)
|
2023-05-24 10:22:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_addUserPasskeyCode(t *testing.T) {
|
|
|
|
alg := crypto.CreateMockEncryptionAlg(gomock.NewController(t))
|
|
|
|
userAgg := &user.NewAggregate("user1", "org1").Aggregate
|
|
|
|
type fields struct {
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode encrypedCodeFunc
|
2023-05-24 10:22:00 +00:00
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
idGenerator id.Generator
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want *domain.PasskeyCodeDetails
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "id generator error",
|
|
|
|
fields: fields{
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: newEncryptedCode,
|
2023-05-24 10:22:00 +00:00
|
|
|
eventstore: eventstoreExpect(t),
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectError(t, io.ErrClosedPipe),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "crypto error",
|
|
|
|
fields: fields{
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: newEncryptedCode,
|
2023-05-24 10:22:00 +00:00
|
|
|
eventstore: eventstoreExpect(t, expectFilterError(io.ErrClosedPipe)),
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "123"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "filter query error",
|
|
|
|
fields: fields{
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: newEncryptedCode,
|
2023-05-24 10:22:00 +00:00
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(eventFromEventPusher(testSecretGeneratorAddedEvent(domain.SecretGeneratorTypePasswordlessInitCode))),
|
|
|
|
expectFilterError(io.ErrClosedPipe),
|
|
|
|
),
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "123"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "push error",
|
|
|
|
fields: fields{
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: mockEncryptedCode("passkey1", time.Minute),
|
2023-05-24 10:22:00 +00:00
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
userAgg,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
)),
|
2023-10-19 10:19:10 +00:00
|
|
|
expectPushFailed(io.ErrClosedPipe,
|
|
|
|
user.NewHumanPasswordlessInitCodeRequestedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"123", &crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("passkey1"),
|
|
|
|
}, time.Minute, "", false,
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "123"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
fields: fields{
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: mockEncryptedCode("passkey1", time.Minute),
|
2023-05-24 10:22:00 +00:00
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
userAgg,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
)),
|
2023-10-19 10:19:10 +00:00
|
|
|
expectPush(
|
|
|
|
user.NewHumanPasswordlessInitCodeRequestedEvent(context.Background(),
|
|
|
|
userAgg,
|
|
|
|
"123", &crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("passkey1"),
|
|
|
|
}, time.Minute, "", false,
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2023-05-24 10:22:00 +00:00
|
|
|
),
|
|
|
|
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "123"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
want: &domain.PasskeyCodeDetails{
|
|
|
|
ObjectDetails: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
CodeID: "123",
|
|
|
|
Code: "passkey1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
2024-04-05 09:35:49 +00:00
|
|
|
newEncryptedCode: tt.fields.newCode,
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
idGenerator: tt.fields.idGenerator,
|
2023-05-24 10:22:00 +00:00
|
|
|
}
|
|
|
|
got, err := c.addUserPasskeyCode(context.Background(), tt.args.userID, tt.args.resourceOwner, alg, "", false)
|
2024-08-12 20:32:01 +00:00
|
|
|
if tt.wantErr != nil {
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(t, tt.want.CodeID, got.CodeID)
|
|
|
|
assert.Equal(t, tt.want.Code, got.Code)
|
|
|
|
assertObjectDetails(t, tt.want.ObjectDetails, got.ObjectDetails)
|
2023-05-24 10:22:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|