2023-04-19 08:46:02 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2024-05-16 05:07:56 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2023-04-19 08:46:02 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-05-16 05:07:56 +00:00
|
|
|
"github.com/muhlemmer/gu"
|
2023-04-19 08:46:02 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-05-16 05:07:56 +00:00
|
|
|
"go.uber.org/mock/gomock"
|
|
|
|
"golang.org/x/text/language"
|
2023-04-19 08:46:02 +00:00
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2024-05-16 05:07:56 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
2023-04-19 08:46:02 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2024-05-16 05:07:56 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/id"
|
|
|
|
"github.com/zitadel/zitadel/internal/id/mock"
|
2023-04-19 08:46:02 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/deviceauth"
|
2024-05-16 05:07:56 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/oidcsession"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-04-19 08:46:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommands_AddDeviceAuth(t *testing.T) {
|
|
|
|
ctx := authz.WithInstanceID(context.Background(), "instance1")
|
|
|
|
pushErr := errors.New("pushErr")
|
|
|
|
now := time.Now()
|
|
|
|
|
2023-12-20 12:21:08 +00:00
|
|
|
unique := deviceauth.NewAddUniqueConstraints("123", "456")
|
2023-04-19 08:46:02 +00:00
|
|
|
require.Len(t, unique, 2)
|
|
|
|
|
|
|
|
type fields struct {
|
2024-05-16 05:07:56 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
2024-05-16 05:07:56 +00:00
|
|
|
ctx context.Context
|
|
|
|
clientID string
|
|
|
|
deviceCode string
|
|
|
|
userCode string
|
|
|
|
expires time.Time
|
|
|
|
scopes []string
|
|
|
|
audience []string
|
|
|
|
needRefreshToken bool
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantDetails *domain.ObjectDetails
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
fields: fields{
|
2024-05-16 05:07:56 +00:00
|
|
|
eventstore: expectEventstore(expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
2023-12-20 12:21:08 +00:00
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
2023-10-19 10:19:10 +00:00
|
|
|
"client_id", "123", "456", now,
|
|
|
|
[]string{"a", "b", "c"},
|
2024-05-16 05:07:56 +00:00
|
|
|
[]string{"projectID", "clientID"}, true,
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2023-04-19 08:46:02 +00:00
|
|
|
)),
|
|
|
|
},
|
|
|
|
args: args{
|
2024-05-16 05:07:56 +00:00
|
|
|
ctx: authz.WithInstanceID(context.Background(), "instance1"),
|
|
|
|
clientID: "client_id",
|
|
|
|
deviceCode: "123",
|
|
|
|
userCode: "456",
|
|
|
|
expires: now,
|
|
|
|
scopes: []string{"a", "b", "c"},
|
|
|
|
audience: []string{"projectID", "clientID"},
|
|
|
|
needRefreshToken: true,
|
2023-04-19 08:46:02 +00:00
|
|
|
},
|
|
|
|
wantDetails: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "instance1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "push error",
|
|
|
|
fields: fields{
|
2024-05-16 05:07:56 +00:00
|
|
|
eventstore: expectEventstore(expectPushFailed(pushErr,
|
2023-10-19 10:19:10 +00:00
|
|
|
deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
2023-12-20 12:21:08 +00:00
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
2023-10-19 10:19:10 +00:00
|
|
|
"client_id", "123", "456", now,
|
|
|
|
[]string{"a", "b", "c"},
|
2024-05-16 05:07:56 +00:00
|
|
|
[]string{"projectID", "clientID"}, false,
|
2023-10-19 10:19:10 +00:00
|
|
|
)),
|
|
|
|
),
|
2023-04-19 08:46:02 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2024-05-16 05:07:56 +00:00
|
|
|
ctx: authz.WithInstanceID(context.Background(), "instance1"),
|
|
|
|
clientID: "client_id",
|
|
|
|
deviceCode: "123",
|
|
|
|
userCode: "456",
|
|
|
|
expires: now,
|
|
|
|
scopes: []string{"a", "b", "c"},
|
|
|
|
audience: []string{"projectID", "clientID"},
|
|
|
|
needRefreshToken: false,
|
2023-04-19 08:46:02 +00:00
|
|
|
},
|
|
|
|
wantErr: pushErr,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
2024-05-16 05:07:56 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
2024-05-16 05:07:56 +00:00
|
|
|
gotDetails, err := c.AddDeviceAuth(tt.args.ctx, tt.args.clientID, tt.args.deviceCode, tt.args.userCode, tt.args.expires, tt.args.scopes, tt.args.audience, tt.args.needRefreshToken)
|
2023-04-19 08:46:02 +00:00
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
2023-10-19 10:19:10 +00:00
|
|
|
assert.Equal(t, tt.wantDetails, gotDetails)
|
2023-04-19 08:46:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_ApproveDeviceAuth(t *testing.T) {
|
|
|
|
ctx := authz.WithInstanceID(context.Background(), "instance1")
|
|
|
|
now := time.Now()
|
|
|
|
pushErr := errors.New("pushErr")
|
|
|
|
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
2024-05-16 05:07:56 +00:00
|
|
|
ctx context.Context
|
|
|
|
id string
|
|
|
|
userID string
|
|
|
|
userOrgID string
|
|
|
|
authMethods []domain.UserAuthMethodType
|
|
|
|
authTime time.Time
|
|
|
|
preferredLanguage *language.Tag
|
|
|
|
userAgent *domain.UserAgent
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantDetails *domain.ObjectDetails
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "not found error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
2023-12-20 12:21:08 +00:00
|
|
|
expectFilter(),
|
2023-04-19 08:46:02 +00:00
|
|
|
),
|
|
|
|
},
|
2023-12-20 12:21:08 +00:00
|
|
|
args: args{
|
2024-05-16 05:07:56 +00:00
|
|
|
ctx, "123", "subj", "orgID",
|
2023-12-20 12:21:08 +00:00
|
|
|
[]domain.UserAuthMethodType{domain.UserAuthMethodTypePassword},
|
2024-05-16 05:07:56 +00:00
|
|
|
time.Unix(123, 456), &language.Afrikaans, &domain.UserAgent{
|
|
|
|
FingerprintID: gu.Ptr("fp1"),
|
|
|
|
IP: net.ParseIP("1.2.3.4"),
|
|
|
|
Description: gu.Ptr("firefox"),
|
|
|
|
Header: http.Header{"foo": []string{"bar"}},
|
|
|
|
},
|
2023-12-20 12:21:08 +00:00
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowNotFound(nil, "COMMAND-Hief9", "Errors.DeviceAuth.NotFound"),
|
2023-04-19 08:46:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "push error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
2023-12-20 12:21:08 +00:00
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
2023-04-19 08:46:02 +00:00
|
|
|
"client_id", "123", "456", now,
|
|
|
|
[]string{"a", "b", "c"},
|
2024-05-16 05:07:56 +00:00
|
|
|
[]string{"projectID", "clientID"}, true,
|
2023-04-19 08:46:02 +00:00
|
|
|
),
|
|
|
|
)),
|
|
|
|
expectPushFailed(pushErr,
|
2023-10-19 10:19:10 +00:00
|
|
|
deviceauth.NewApprovedEvent(
|
2024-05-16 05:07:56 +00:00
|
|
|
ctx, deviceauth.NewAggregate("123", "instance1"), "subj", "orgID",
|
2023-12-20 12:21:08 +00:00
|
|
|
[]domain.UserAuthMethodType{domain.UserAuthMethodTypePassword},
|
2024-05-16 05:07:56 +00:00
|
|
|
time.Unix(123, 456), &language.Afrikaans, &domain.UserAgent{
|
|
|
|
FingerprintID: gu.Ptr("fp1"),
|
|
|
|
IP: net.ParseIP("1.2.3.4"),
|
|
|
|
Description: gu.Ptr("firefox"),
|
|
|
|
Header: http.Header{"foo": []string{"bar"}},
|
|
|
|
},
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2023-04-19 08:46:02 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
2023-12-20 12:21:08 +00:00
|
|
|
args: args{
|
2024-05-16 05:07:56 +00:00
|
|
|
ctx, "123", "subj", "orgID",
|
2023-12-20 12:21:08 +00:00
|
|
|
[]domain.UserAuthMethodType{domain.UserAuthMethodTypePassword},
|
2024-05-16 05:07:56 +00:00
|
|
|
time.Unix(123, 456), &language.Afrikaans, &domain.UserAgent{
|
|
|
|
FingerprintID: gu.Ptr("fp1"),
|
|
|
|
IP: net.ParseIP("1.2.3.4"),
|
|
|
|
Description: gu.Ptr("firefox"),
|
|
|
|
Header: http.Header{"foo": []string{"bar"}},
|
|
|
|
},
|
2023-12-20 12:21:08 +00:00
|
|
|
},
|
2023-04-19 08:46:02 +00:00
|
|
|
wantErr: pushErr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
2023-12-20 12:21:08 +00:00
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
2023-04-19 08:46:02 +00:00
|
|
|
"client_id", "123", "456", now,
|
|
|
|
[]string{"a", "b", "c"},
|
2024-05-16 05:07:56 +00:00
|
|
|
[]string{"projectID", "clientID"}, true,
|
2023-04-19 08:46:02 +00:00
|
|
|
),
|
|
|
|
)),
|
2023-10-19 10:19:10 +00:00
|
|
|
expectPush(
|
|
|
|
deviceauth.NewApprovedEvent(
|
2024-05-16 05:07:56 +00:00
|
|
|
ctx, deviceauth.NewAggregate("123", "instance1"), "subj", "orgID",
|
2023-12-20 12:21:08 +00:00
|
|
|
[]domain.UserAuthMethodType{domain.UserAuthMethodTypePassword},
|
2024-05-16 05:07:56 +00:00
|
|
|
time.Unix(123, 456), &language.Afrikaans, &domain.UserAgent{
|
|
|
|
FingerprintID: gu.Ptr("fp1"),
|
|
|
|
IP: net.ParseIP("1.2.3.4"),
|
|
|
|
Description: gu.Ptr("firefox"),
|
|
|
|
Header: http.Header{"foo": []string{"bar"}},
|
|
|
|
},
|
2023-04-19 08:46:02 +00:00
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2023-04-19 08:46:02 +00:00
|
|
|
),
|
|
|
|
},
|
2023-12-20 12:21:08 +00:00
|
|
|
args: args{
|
2024-05-16 05:07:56 +00:00
|
|
|
ctx, "123", "subj", "orgID",
|
2023-12-20 12:21:08 +00:00
|
|
|
[]domain.UserAuthMethodType{domain.UserAuthMethodTypePassword},
|
2024-05-16 05:07:56 +00:00
|
|
|
time.Unix(123, 456), &language.Afrikaans, &domain.UserAgent{
|
|
|
|
FingerprintID: gu.Ptr("fp1"),
|
|
|
|
IP: net.ParseIP("1.2.3.4"),
|
|
|
|
Description: gu.Ptr("firefox"),
|
|
|
|
Header: http.Header{"foo": []string{"bar"}},
|
|
|
|
},
|
2023-12-20 12:21:08 +00:00
|
|
|
},
|
2023-04-19 08:46:02 +00:00
|
|
|
wantDetails: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "instance1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
}
|
2024-05-16 05:07:56 +00:00
|
|
|
gotDetails, err := c.ApproveDeviceAuth(tt.args.ctx, tt.args.id, tt.args.userID, tt.args.userOrgID, tt.args.authMethods, tt.args.authTime, tt.args.preferredLanguage, tt.args.userAgent)
|
2023-04-19 08:46:02 +00:00
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
assert.Equal(t, gotDetails, tt.wantDetails)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_CancelDeviceAuth(t *testing.T) {
|
|
|
|
ctx := authz.WithInstanceID(context.Background(), "instance1")
|
|
|
|
now := time.Now()
|
|
|
|
pushErr := errors.New("pushErr")
|
|
|
|
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
id string
|
|
|
|
reason domain.DeviceAuthCanceled
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantDetails *domain.ObjectDetails
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "not found error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
2023-12-20 12:21:08 +00:00
|
|
|
expectFilter(),
|
2023-04-19 08:46:02 +00:00
|
|
|
),
|
|
|
|
},
|
2023-12-20 12:21:08 +00:00
|
|
|
args: args{ctx, "123", domain.DeviceAuthCanceledDenied},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowNotFound(nil, "COMMAND-gee5A", "Errors.DeviceAuth.NotFound"),
|
2023-04-19 08:46:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "push error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
2023-12-20 12:21:08 +00:00
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
2023-04-19 08:46:02 +00:00
|
|
|
"client_id", "123", "456", now,
|
|
|
|
[]string{"a", "b", "c"},
|
2024-05-16 05:07:56 +00:00
|
|
|
[]string{"projectID", "clientID"}, true,
|
2023-04-19 08:46:02 +00:00
|
|
|
),
|
|
|
|
)),
|
|
|
|
expectPushFailed(pushErr,
|
2023-10-19 10:19:10 +00:00
|
|
|
deviceauth.NewCanceledEvent(
|
2023-12-20 12:21:08 +00:00
|
|
|
ctx, deviceauth.NewAggregate("123", "instance1"),
|
2023-10-19 10:19:10 +00:00
|
|
|
domain.DeviceAuthCanceledDenied,
|
|
|
|
),
|
2023-04-19 08:46:02 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
2023-12-20 12:21:08 +00:00
|
|
|
args: args{ctx, "123", domain.DeviceAuthCanceledDenied},
|
2023-04-19 08:46:02 +00:00
|
|
|
wantErr: pushErr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success/denied",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
2023-12-20 12:21:08 +00:00
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
2023-04-19 08:46:02 +00:00
|
|
|
"client_id", "123", "456", now,
|
|
|
|
[]string{"a", "b", "c"},
|
2024-05-16 05:07:56 +00:00
|
|
|
[]string{"projectID", "clientID"}, true,
|
2023-04-19 08:46:02 +00:00
|
|
|
),
|
|
|
|
)),
|
2023-10-19 10:19:10 +00:00
|
|
|
expectPush(
|
|
|
|
deviceauth.NewCanceledEvent(
|
2023-12-20 12:21:08 +00:00
|
|
|
ctx, deviceauth.NewAggregate("123", "instance1"),
|
2023-04-19 08:46:02 +00:00
|
|
|
domain.DeviceAuthCanceledDenied,
|
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2023-04-19 08:46:02 +00:00
|
|
|
),
|
|
|
|
},
|
2023-12-20 12:21:08 +00:00
|
|
|
args: args{ctx, "123", domain.DeviceAuthCanceledDenied},
|
2023-04-19 08:46:02 +00:00
|
|
|
wantDetails: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "instance1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success/expired",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
2023-12-20 12:21:08 +00:00
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
2023-04-19 08:46:02 +00:00
|
|
|
"client_id", "123", "456", now,
|
|
|
|
[]string{"a", "b", "c"},
|
2024-05-16 05:07:56 +00:00
|
|
|
[]string{"projectID", "clientID"}, true,
|
2023-04-19 08:46:02 +00:00
|
|
|
),
|
|
|
|
)),
|
2023-10-19 10:19:10 +00:00
|
|
|
expectPush(
|
|
|
|
deviceauth.NewCanceledEvent(
|
2023-12-20 12:21:08 +00:00
|
|
|
ctx, deviceauth.NewAggregate("123", "instance1"),
|
2023-04-19 08:46:02 +00:00
|
|
|
domain.DeviceAuthCanceledExpired,
|
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2023-04-19 08:46:02 +00:00
|
|
|
),
|
|
|
|
},
|
2023-12-20 12:21:08 +00:00
|
|
|
args: args{ctx, "123", domain.DeviceAuthCanceledExpired},
|
2023-04-19 08:46:02 +00:00
|
|
|
wantDetails: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "instance1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
}
|
|
|
|
gotDetails, err := c.CancelDeviceAuth(tt.args.ctx, tt.args.id, tt.args.reason)
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
assert.Equal(t, gotDetails, tt.wantDetails)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-05-16 05:07:56 +00:00
|
|
|
|
|
|
|
func TestCommands_CreateOIDCSessionFromDeviceAuth(t *testing.T) {
|
|
|
|
ctx := authz.WithInstanceID(context.Background(), "instance1")
|
|
|
|
|
|
|
|
type fields struct {
|
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
idGenerator id.Generator
|
|
|
|
defaultAccessTokenLifetime time.Duration
|
|
|
|
defaultRefreshTokenLifetime time.Duration
|
|
|
|
defaultRefreshTokenIdleLifetime time.Duration
|
|
|
|
keyAlgorithm crypto.EncryptionAlgorithm
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
deviceCode string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want *OIDCSession
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "device auth filter error",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilterError(io.ErrClosedPipe),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx,
|
|
|
|
"device1",
|
|
|
|
},
|
|
|
|
wantErr: io.ErrClosedPipe,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not yet approved",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
"clientID", "123", "456", time.Now().Add(time.Minute),
|
|
|
|
[]string{"openid", "offline_access"},
|
|
|
|
[]string{"audience"}, false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx,
|
|
|
|
"123",
|
|
|
|
},
|
|
|
|
wantErr: DeviceAuthStateError(domain.DeviceAuthStateInitiated),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not found",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx,
|
|
|
|
"123",
|
|
|
|
},
|
|
|
|
wantErr: zerrors.ThrowNotFound(nil, "COMMAND-ua1Vo", "Errors.DeviceAuth.NotFound"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "expired",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
"clientID", "123", "456", time.Now().Add(-time.Minute),
|
|
|
|
[]string{"openid", "offline_access"},
|
|
|
|
[]string{"audience"}, false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPushSlow(time.Second, deviceauth.NewCanceledEvent(ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
domain.DeviceAuthCanceledExpired,
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx,
|
|
|
|
"123",
|
|
|
|
},
|
|
|
|
wantErr: DeviceAuthStateError(domain.DeviceAuthStateExpired),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "already expired",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
"clientID", "123", "456", time.Now().Add(-time.Minute),
|
|
|
|
[]string{"openid", "offline_access"},
|
|
|
|
[]string{"audience"}, false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewCanceledEvent(ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
domain.DeviceAuthCanceledExpired,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx,
|
|
|
|
"123",
|
|
|
|
},
|
|
|
|
wantErr: DeviceAuthStateError(domain.DeviceAuthStateExpired),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "denied",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
"clientID", "123", "456", time.Now().Add(-time.Minute),
|
|
|
|
[]string{"openid", "offline_access"},
|
|
|
|
[]string{"audience"}, false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewCanceledEvent(ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
domain.DeviceAuthCanceledDenied,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx,
|
|
|
|
"123",
|
|
|
|
},
|
|
|
|
wantErr: DeviceAuthStateError(domain.DeviceAuthStateDenied),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "already done",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
"clientID", "123", "456", time.Now().Add(-time.Minute),
|
|
|
|
[]string{"openid", "offline_access"},
|
|
|
|
[]string{"audience"}, false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewCanceledEvent(ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
domain.DeviceAuthCanceledDenied,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewDoneEvent(ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx,
|
|
|
|
"123",
|
|
|
|
},
|
|
|
|
wantErr: DeviceAuthStateError(domain.DeviceAuthStateDone),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "approved, success",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
"clientID", "123", "456", time.Now().Add(-time.Minute),
|
|
|
|
[]string{"openid", "offline_access"},
|
|
|
|
[]string{"audience"}, false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewApprovedEvent(ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
"userID", "org1",
|
|
|
|
[]domain.UserAuthMethodType{domain.UserAuthMethodTypePassword},
|
|
|
|
testNow, &language.Afrikaans, &domain.UserAgent{
|
|
|
|
FingerprintID: gu.Ptr("fp1"),
|
|
|
|
IP: net.ParseIP("1.2.3.4"),
|
|
|
|
Description: gu.Ptr("firefox"),
|
|
|
|
Header: http.Header{"foo": []string{"bar"}},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(), // token lifetime
|
|
|
|
expectPush(
|
|
|
|
oidcsession.NewAddedEvent(context.Background(), &oidcsession.NewAggregate("V2_oidcSessionID", "org1").Aggregate,
|
|
|
|
"userID", "org1", "", "clientID", []string{"audience"}, []string{"openid", "offline_access"},
|
|
|
|
[]domain.UserAuthMethodType{domain.UserAuthMethodTypePassword}, testNow, "", &language.Afrikaans, &domain.UserAgent{
|
|
|
|
FingerprintID: gu.Ptr("fp1"),
|
|
|
|
IP: net.ParseIP("1.2.3.4"),
|
|
|
|
Description: gu.Ptr("firefox"),
|
|
|
|
Header: http.Header{"foo": []string{"bar"}},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
oidcsession.NewAccessTokenAddedEvent(context.Background(),
|
|
|
|
&oidcsession.NewAggregate("V2_oidcSessionID", "org1").Aggregate,
|
|
|
|
"at_accessTokenID", []string{"openid", "offline_access"}, time.Hour, domain.TokenReasonAuthRequest, nil,
|
|
|
|
),
|
|
|
|
user.NewUserTokenV2AddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate, "at_accessTokenID"),
|
|
|
|
deviceauth.NewDoneEvent(ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
idGenerator: mock.NewIDGeneratorExpectIDs(t, "oidcSessionID", "accessTokenID"),
|
|
|
|
defaultAccessTokenLifetime: time.Hour,
|
|
|
|
defaultRefreshTokenLifetime: 7 * 24 * time.Hour,
|
|
|
|
defaultRefreshTokenIdleLifetime: 24 * time.Hour,
|
|
|
|
keyAlgorithm: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx,
|
|
|
|
"123",
|
|
|
|
},
|
|
|
|
want: &OIDCSession{
|
|
|
|
TokenID: "V2_oidcSessionID-at_accessTokenID",
|
|
|
|
ClientID: "clientID",
|
|
|
|
UserID: "userID",
|
|
|
|
Audience: []string{"audience"},
|
|
|
|
Expiration: time.Time{}.Add(time.Hour),
|
|
|
|
Scope: []string{"openid", "offline_access"},
|
|
|
|
AuthMethods: []domain.UserAuthMethodType{domain.UserAuthMethodTypePassword},
|
|
|
|
AuthTime: testNow,
|
|
|
|
PreferredLanguage: &language.Afrikaans,
|
|
|
|
UserAgent: &domain.UserAgent{
|
|
|
|
FingerprintID: gu.Ptr("fp1"),
|
|
|
|
IP: net.ParseIP("1.2.3.4"),
|
|
|
|
Description: gu.Ptr("firefox"),
|
|
|
|
Header: http.Header{"foo": []string{"bar"}},
|
|
|
|
},
|
|
|
|
Reason: domain.TokenReasonAuthRequest,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "approved, with refresh token",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
"clientID", "123", "456", time.Now().Add(-time.Minute),
|
|
|
|
[]string{"openid", "offline_access"},
|
|
|
|
[]string{"audience"}, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithInstanceID(
|
|
|
|
"instance1",
|
|
|
|
deviceauth.NewApprovedEvent(ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
"userID", "org1",
|
|
|
|
[]domain.UserAuthMethodType{domain.UserAuthMethodTypePassword},
|
|
|
|
testNow, &language.Afrikaans, &domain.UserAgent{
|
|
|
|
FingerprintID: gu.Ptr("fp1"),
|
|
|
|
IP: net.ParseIP("1.2.3.4"),
|
|
|
|
Description: gu.Ptr("firefox"),
|
|
|
|
Header: http.Header{"foo": []string{"bar"}},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(), // token lifetime
|
|
|
|
expectPush(
|
|
|
|
oidcsession.NewAddedEvent(context.Background(), &oidcsession.NewAggregate("V2_oidcSessionID", "org1").Aggregate,
|
|
|
|
"userID", "org1", "", "clientID", []string{"audience"}, []string{"openid", "offline_access"},
|
|
|
|
[]domain.UserAuthMethodType{domain.UserAuthMethodTypePassword}, testNow, "", &language.Afrikaans, &domain.UserAgent{
|
|
|
|
FingerprintID: gu.Ptr("fp1"),
|
|
|
|
IP: net.ParseIP("1.2.3.4"),
|
|
|
|
Description: gu.Ptr("firefox"),
|
|
|
|
Header: http.Header{"foo": []string{"bar"}},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
oidcsession.NewAccessTokenAddedEvent(context.Background(),
|
|
|
|
&oidcsession.NewAggregate("V2_oidcSessionID", "org1").Aggregate,
|
|
|
|
"at_accessTokenID", []string{"openid", "offline_access"}, time.Hour, domain.TokenReasonAuthRequest, nil,
|
|
|
|
),
|
|
|
|
user.NewUserTokenV2AddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate, "at_accessTokenID"),
|
|
|
|
oidcsession.NewRefreshTokenAddedEvent(context.Background(), &oidcsession.NewAggregate("V2_oidcSessionID", "org1").Aggregate,
|
|
|
|
"rt_refreshTokenID", 7*24*time.Hour, 24*time.Hour,
|
|
|
|
),
|
|
|
|
deviceauth.NewDoneEvent(ctx,
|
|
|
|
deviceauth.NewAggregate("123", "instance1"),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
idGenerator: mock.NewIDGeneratorExpectIDs(t, "oidcSessionID", "accessTokenID", "refreshTokenID"),
|
|
|
|
defaultAccessTokenLifetime: time.Hour,
|
|
|
|
defaultRefreshTokenLifetime: 7 * 24 * time.Hour,
|
|
|
|
defaultRefreshTokenIdleLifetime: 24 * time.Hour,
|
|
|
|
keyAlgorithm: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx,
|
|
|
|
"123",
|
|
|
|
},
|
|
|
|
want: &OIDCSession{
|
|
|
|
TokenID: "V2_oidcSessionID-at_accessTokenID",
|
|
|
|
ClientID: "clientID",
|
|
|
|
UserID: "userID",
|
|
|
|
Audience: []string{"audience"},
|
|
|
|
Expiration: time.Time{}.Add(time.Hour),
|
|
|
|
Scope: []string{"openid", "offline_access"},
|
|
|
|
AuthMethods: []domain.UserAuthMethodType{domain.UserAuthMethodTypePassword},
|
|
|
|
AuthTime: testNow,
|
|
|
|
PreferredLanguage: &language.Afrikaans,
|
|
|
|
UserAgent: &domain.UserAgent{
|
|
|
|
FingerprintID: gu.Ptr("fp1"),
|
|
|
|
IP: net.ParseIP("1.2.3.4"),
|
|
|
|
Description: gu.Ptr("firefox"),
|
|
|
|
Header: http.Header{"foo": []string{"bar"}},
|
|
|
|
},
|
|
|
|
Reason: domain.TokenReasonAuthRequest,
|
|
|
|
RefreshToken: "VjJfb2lkY1Nlc3Npb25JRC1ydF9yZWZyZXNoVG9rZW5JRDp1c2VySUQ", //V2_oidcSessionID-rt_refreshTokenID:userID
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
idGenerator: tt.fields.idGenerator,
|
|
|
|
defaultAccessTokenLifetime: tt.fields.defaultAccessTokenLifetime,
|
|
|
|
defaultRefreshTokenLifetime: tt.fields.defaultRefreshTokenLifetime,
|
|
|
|
defaultRefreshTokenIdleLifetime: tt.fields.defaultRefreshTokenIdleLifetime,
|
|
|
|
keyAlgorithm: tt.fields.keyAlgorithm,
|
|
|
|
}
|
|
|
|
got, err := c.CreateOIDCSessionFromDeviceAuth(tt.args.ctx, tt.args.deviceCode)
|
|
|
|
c.jobs.Wait()
|
|
|
|
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
|
|
|
|
if got != nil {
|
|
|
|
assert.WithinRange(t, got.AuthTime, tt.want.AuthTime.Add(-time.Second), tt.want.AuthTime.Add(time.Second))
|
|
|
|
got.AuthTime = time.Time{}
|
|
|
|
tt.want.AuthTime = time.Time{}
|
|
|
|
}
|
|
|
|
assert.Equal(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|