2023-04-26 05:47:57 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-05-24 18:29:58 +00:00
|
|
|
"time"
|
2023-04-26 05:47:57 +00:00
|
|
|
|
2023-05-24 18:29:58 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/muhlemmer/gu"
|
2023-04-26 05:47:57 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-06-20 12:39:50 +00:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
2023-05-24 18:29:58 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2023-04-26 05:47:57 +00:00
|
|
|
|
2023-05-24 18:29:58 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc"
|
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-04-26 05:47:57 +00:00
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
2023-05-24 18:29:58 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
object_pb "github.com/zitadel/zitadel/pkg/grpc/object/v2alpha"
|
2023-04-26 05:47:57 +00:00
|
|
|
user "github.com/zitadel/zitadel/pkg/grpc/user/v2alpha"
|
|
|
|
)
|
|
|
|
|
2023-06-20 12:39:50 +00:00
|
|
|
var ignoreTypes = []protoreflect.FullName{"google.protobuf.Duration", "google.protobuf.Struct"}
|
|
|
|
|
2023-05-24 18:29:58 +00:00
|
|
|
func Test_intentToIDPInformationPb(t *testing.T) {
|
|
|
|
decryption := func(err error) crypto.EncryptionAlgorithm {
|
|
|
|
mCrypto := crypto.NewMockEncryptionAlgorithm(gomock.NewController(t))
|
|
|
|
mCrypto.EXPECT().Algorithm().Return("enc")
|
|
|
|
mCrypto.EXPECT().DecryptionKeyIDs().Return([]string{"id"})
|
|
|
|
mCrypto.EXPECT().DecryptString(gomock.Any(), gomock.Any()).DoAndReturn(
|
|
|
|
func(code []byte, keyID string) (string, error) {
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(code), nil
|
|
|
|
})
|
|
|
|
return mCrypto
|
|
|
|
}
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
intent *command.IDPIntentWriteModel
|
|
|
|
alg crypto.EncryptionAlgorithm
|
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
resp *user.RetrieveIdentityProviderInformationResponse
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"decryption invalid key id error",
|
|
|
|
args{
|
|
|
|
intent: &command.IDPIntentWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: "intentID",
|
|
|
|
ProcessedSequence: 123,
|
|
|
|
ResourceOwner: "ro",
|
|
|
|
InstanceID: "instanceID",
|
|
|
|
ChangeDate: time.Date(2019, 4, 1, 1, 1, 1, 1, time.Local),
|
|
|
|
},
|
2023-06-20 12:39:50 +00:00
|
|
|
IDPID: "idpID",
|
|
|
|
IDPUser: []byte(`{"userID": "idpUserID", "username": "username"}`),
|
|
|
|
IDPUserID: "idpUserID",
|
|
|
|
IDPUserName: "username",
|
2023-05-24 18:29:58 +00:00
|
|
|
IDPAccessToken: &crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("accessToken"),
|
|
|
|
},
|
|
|
|
IDPIDToken: "idToken",
|
|
|
|
UserID: "userID",
|
|
|
|
State: domain.IDPIntentStateSucceeded,
|
|
|
|
},
|
|
|
|
alg: decryption(caos_errs.ThrowInternal(nil, "id", "invalid key id")),
|
|
|
|
},
|
|
|
|
res{
|
|
|
|
resp: nil,
|
|
|
|
err: caos_errs.ThrowInternal(nil, "id", "invalid key id"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"successful",
|
|
|
|
args{
|
|
|
|
intent: &command.IDPIntentWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: "intentID",
|
|
|
|
ProcessedSequence: 123,
|
|
|
|
ResourceOwner: "ro",
|
|
|
|
InstanceID: "instanceID",
|
|
|
|
ChangeDate: time.Date(2019, 4, 1, 1, 1, 1, 1, time.Local),
|
|
|
|
},
|
2023-06-20 12:39:50 +00:00
|
|
|
IDPID: "idpID",
|
|
|
|
IDPUser: []byte(`{"userID": "idpUserID", "username": "username"}`),
|
|
|
|
IDPUserID: "idpUserID",
|
|
|
|
IDPUserName: "username",
|
2023-05-24 18:29:58 +00:00
|
|
|
IDPAccessToken: &crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("accessToken"),
|
|
|
|
},
|
|
|
|
IDPIDToken: "idToken",
|
|
|
|
UserID: "userID",
|
|
|
|
State: domain.IDPIntentStateSucceeded,
|
|
|
|
},
|
|
|
|
alg: decryption(nil),
|
|
|
|
},
|
|
|
|
res{
|
|
|
|
resp: &user.RetrieveIdentityProviderInformationResponse{
|
|
|
|
Details: &object_pb.Details{
|
|
|
|
Sequence: 123,
|
|
|
|
ChangeDate: timestamppb.New(time.Date(2019, 4, 1, 1, 1, 1, 1, time.Local)),
|
|
|
|
ResourceOwner: "ro",
|
|
|
|
},
|
|
|
|
IdpInformation: &user.IDPInformation{
|
|
|
|
Access: &user.IDPInformation_Oauth{
|
|
|
|
Oauth: &user.IDPOAuthAccessInformation{
|
|
|
|
AccessToken: "accessToken",
|
|
|
|
IdToken: gu.Ptr("idToken"),
|
2023-06-20 12:39:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
IdpId: "idpID",
|
|
|
|
UserId: "idpUserID",
|
|
|
|
UserName: "username",
|
|
|
|
RawInformation: func() *structpb.Struct {
|
|
|
|
s, err := structpb.NewStruct(map[string]interface{}{
|
|
|
|
"userID": "idpUserID",
|
|
|
|
"username": "username",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
return s
|
|
|
|
}(),
|
2023-05-24 18:29:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := intentToIDPInformationPb(tt.args.intent, tt.args.alg)
|
|
|
|
require.ErrorIs(t, err, tt.res.err)
|
2023-06-20 12:39:50 +00:00
|
|
|
grpc.AllFieldsEqual(t, got.ProtoReflect(), tt.res.resp.ProtoReflect(), grpc.CustomMappers)
|
2023-05-24 18:29:58 +00:00
|
|
|
if tt.res.resp != nil {
|
2023-06-20 12:39:50 +00:00
|
|
|
grpc.AllFieldsSet(t, got.ProtoReflect(), ignoreTypes...)
|
2023-05-24 18:29:58 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-06-20 16:23:28 +00:00
|
|
|
|
|
|
|
func Test_authMethodTypesToPb(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
methodTypes []domain.UserAuthMethodType
|
|
|
|
want []user.AuthenticationMethodType
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"empty list",
|
|
|
|
nil,
|
|
|
|
[]user.AuthenticationMethodType{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"list",
|
|
|
|
[]domain.UserAuthMethodType{
|
|
|
|
domain.UserAuthMethodTypePasswordless,
|
|
|
|
},
|
|
|
|
[]user.AuthenticationMethodType{
|
|
|
|
user.AuthenticationMethodType_AUTHENTICATION_METHOD_TYPE_PASSKEY,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, authMethodTypesToPb(tt.methodTypes), "authMethodTypesToPb(%v)", tt.methodTypes)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_authMethodTypeToPb(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
methodType domain.UserAuthMethodType
|
|
|
|
want user.AuthenticationMethodType
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"uspecified",
|
|
|
|
domain.UserAuthMethodTypeUnspecified,
|
|
|
|
user.AuthenticationMethodType_AUTHENTICATION_METHOD_TYPE_UNSPECIFIED,
|
|
|
|
},
|
|
|
|
{
|
2023-08-02 16:57:53 +00:00
|
|
|
"totp",
|
|
|
|
domain.UserAuthMethodTypeTOTP,
|
2023-06-20 16:23:28 +00:00
|
|
|
user.AuthenticationMethodType_AUTHENTICATION_METHOD_TYPE_TOTP,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"u2f",
|
|
|
|
domain.UserAuthMethodTypeU2F,
|
|
|
|
user.AuthenticationMethodType_AUTHENTICATION_METHOD_TYPE_U2F,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"passkey",
|
|
|
|
domain.UserAuthMethodTypePasswordless,
|
|
|
|
user.AuthenticationMethodType_AUTHENTICATION_METHOD_TYPE_PASSKEY,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"password",
|
|
|
|
domain.UserAuthMethodTypePassword,
|
|
|
|
user.AuthenticationMethodType_AUTHENTICATION_METHOD_TYPE_PASSWORD,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"idp",
|
|
|
|
domain.UserAuthMethodTypeIDP,
|
|
|
|
user.AuthenticationMethodType_AUTHENTICATION_METHOD_TYPE_IDP,
|
|
|
|
},
|
2023-08-02 16:57:53 +00:00
|
|
|
{
|
|
|
|
"otp sms",
|
|
|
|
domain.UserAuthMethodTypeOTPSMS,
|
|
|
|
user.AuthenticationMethodType_AUTHENTICATION_METHOD_TYPE_OTP_SMS,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"otp email",
|
|
|
|
domain.UserAuthMethodTypeOTPEmail,
|
|
|
|
user.AuthenticationMethodType_AUTHENTICATION_METHOD_TYPE_OTP_EMAIL,
|
|
|
|
},
|
2023-06-20 16:23:28 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assert.Equalf(t, tt.want, authMethodTypeToPb(tt.methodType), "authMethodTypeToPb(%v)", tt.methodType)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|