2023-06-07 15:28:42 +00:00
|
|
|
//go:build integration
|
|
|
|
|
|
|
|
package session_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-14 11:16:16 +00:00
|
|
|
"fmt"
|
2023-06-07 15:28:42 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-07-14 11:16:16 +00:00
|
|
|
"github.com/muhlemmer/gu"
|
2023-08-15 09:50:42 +00:00
|
|
|
"github.com/pquerna/otp/totp"
|
2023-06-07 15:28:42 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-05-14 07:20:31 +00:00
|
|
|
"github.com/zitadel/logging"
|
2024-05-30 22:08:48 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
2023-07-14 11:16:16 +00:00
|
|
|
"google.golang.org/grpc/metadata"
|
2024-05-30 22:08:48 +00:00
|
|
|
"google.golang.org/grpc/status"
|
2023-10-12 12:16:59 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-11-06 09:48:28 +00:00
|
|
|
"google.golang.org/protobuf/types/known/durationpb"
|
2024-02-28 10:21:11 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2023-06-21 14:06:18 +00:00
|
|
|
|
2023-06-07 15:28:42 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/integration"
|
2024-05-30 22:08:48 +00:00
|
|
|
mgmt "github.com/zitadel/zitadel/pkg/grpc/management"
|
2024-07-26 20:39:55 +00:00
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/object/v2"
|
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/session/v2"
|
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/user/v2"
|
2023-06-07 15:28:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-02-28 09:30:05 +00:00
|
|
|
CTX context.Context
|
2024-05-30 22:08:48 +00:00
|
|
|
IAMOwnerCTX context.Context
|
2024-02-28 09:30:05 +00:00
|
|
|
Tester *integration.Tester
|
|
|
|
Client session.SessionServiceClient
|
|
|
|
User *user.AddHumanUserResponse
|
|
|
|
DeactivatedUser *user.AddHumanUserResponse
|
|
|
|
LockedUser *user.AddHumanUserResponse
|
2023-06-07 15:28:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
os.Exit(func() int {
|
2023-06-15 05:32:40 +00:00
|
|
|
ctx, errCtx, cancel := integration.Contexts(5 * time.Minute)
|
2023-06-07 15:28:42 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
Tester = integration.NewTester(ctx)
|
|
|
|
defer Tester.Done()
|
|
|
|
Client = Tester.Client.SessionV2
|
|
|
|
|
2023-07-06 06:38:13 +00:00
|
|
|
CTX, _ = Tester.WithAuthorization(ctx, integration.OrgOwner), errCtx
|
2024-05-30 22:08:48 +00:00
|
|
|
IAMOwnerCTX = Tester.WithAuthorization(ctx, integration.IAMOwner)
|
2024-05-14 07:20:31 +00:00
|
|
|
User = createFullUser(CTX)
|
|
|
|
DeactivatedUser = createDeactivatedUser(CTX)
|
|
|
|
LockedUser = createLockedUser(CTX)
|
2023-06-07 15:28:42 +00:00
|
|
|
return m.Run()
|
|
|
|
}())
|
|
|
|
}
|
|
|
|
|
2024-05-14 07:20:31 +00:00
|
|
|
func createFullUser(ctx context.Context) *user.AddHumanUserResponse {
|
|
|
|
userResp := Tester.CreateHumanUser(ctx)
|
|
|
|
Tester.Client.UserV2.VerifyEmail(ctx, &user.VerifyEmailRequest{
|
|
|
|
UserId: userResp.GetUserId(),
|
|
|
|
VerificationCode: userResp.GetEmailCode(),
|
|
|
|
})
|
|
|
|
Tester.Client.UserV2.VerifyPhone(ctx, &user.VerifyPhoneRequest{
|
|
|
|
UserId: userResp.GetUserId(),
|
|
|
|
VerificationCode: userResp.GetPhoneCode(),
|
|
|
|
})
|
|
|
|
Tester.SetUserPassword(ctx, userResp.GetUserId(), integration.UserPassword, false)
|
|
|
|
Tester.RegisterUserPasskey(ctx, userResp.GetUserId())
|
|
|
|
return userResp
|
|
|
|
}
|
|
|
|
|
|
|
|
func createDeactivatedUser(ctx context.Context) *user.AddHumanUserResponse {
|
|
|
|
userResp := Tester.CreateHumanUser(ctx)
|
|
|
|
_, err := Tester.Client.UserV2.DeactivateUser(ctx, &user.DeactivateUserRequest{UserId: userResp.GetUserId()})
|
|
|
|
logging.OnError(err).Fatal("deactivate human user")
|
|
|
|
return userResp
|
|
|
|
}
|
|
|
|
|
|
|
|
func createLockedUser(ctx context.Context) *user.AddHumanUserResponse {
|
|
|
|
userResp := Tester.CreateHumanUser(ctx)
|
|
|
|
_, err := Tester.Client.UserV2.LockUser(ctx, &user.LockUserRequest{UserId: userResp.GetUserId()})
|
|
|
|
logging.OnError(err).Fatal("lock human user")
|
|
|
|
return userResp
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyCurrentSession(t testing.TB, id, token string, sequence uint64, window time.Duration, metadata map[string][]byte, userAgent *session.UserAgent, expirationWindow time.Duration, userID string, factors ...wantFactor) *session.Session {
|
2023-08-15 09:50:42 +00:00
|
|
|
t.Helper()
|
2023-06-07 15:28:42 +00:00
|
|
|
require.NotEmpty(t, id)
|
|
|
|
require.NotEmpty(t, token)
|
|
|
|
|
2023-07-07 08:15:05 +00:00
|
|
|
resp, err := Client.GetSession(CTX, &session.GetSessionRequest{
|
|
|
|
SessionId: id,
|
|
|
|
SessionToken: &token,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
s := resp.GetSession()
|
2023-06-07 15:28:42 +00:00
|
|
|
|
|
|
|
assert.Equal(t, id, s.GetId())
|
|
|
|
assert.WithinRange(t, s.GetCreationDate().AsTime(), time.Now().Add(-window), time.Now().Add(window))
|
|
|
|
assert.WithinRange(t, s.GetChangeDate().AsTime(), time.Now().Add(-window), time.Now().Add(window))
|
|
|
|
assert.Equal(t, sequence, s.GetSequence())
|
|
|
|
assert.Equal(t, metadata, s.GetMetadata())
|
2023-10-12 12:16:59 +00:00
|
|
|
|
|
|
|
if !proto.Equal(userAgent, s.GetUserAgent()) {
|
|
|
|
t.Errorf("user agent =\n%v\nwant\n%v", s.GetUserAgent(), userAgent)
|
|
|
|
}
|
2023-11-06 09:48:28 +00:00
|
|
|
if expirationWindow == 0 {
|
|
|
|
assert.Nil(t, s.GetExpirationDate())
|
|
|
|
} else {
|
|
|
|
assert.WithinRange(t, s.GetExpirationDate().AsTime(), time.Now().Add(-expirationWindow), time.Now().Add(expirationWindow))
|
|
|
|
}
|
2023-10-12 12:16:59 +00:00
|
|
|
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyFactors(t, s.GetFactors(), window, userID, factors)
|
2023-06-07 15:28:42 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
type wantFactor int
|
|
|
|
|
|
|
|
const (
|
|
|
|
wantUserFactor wantFactor = iota
|
|
|
|
wantPasswordFactor
|
2023-08-11 15:36:18 +00:00
|
|
|
wantWebAuthNFactor
|
|
|
|
wantWebAuthNFactorUserVerified
|
2023-08-15 09:50:42 +00:00
|
|
|
wantTOTPFactor
|
2023-06-21 14:06:18 +00:00
|
|
|
wantIntentFactor
|
2023-08-24 09:41:52 +00:00
|
|
|
wantOTPSMSFactor
|
|
|
|
wantOTPEmailFactor
|
2023-06-07 15:28:42 +00:00
|
|
|
)
|
|
|
|
|
2024-05-14 07:20:31 +00:00
|
|
|
func verifyFactors(t testing.TB, factors *session.Factors, window time.Duration, userID string, want []wantFactor) {
|
2023-06-07 15:28:42 +00:00
|
|
|
for _, w := range want {
|
|
|
|
switch w {
|
|
|
|
case wantUserFactor:
|
|
|
|
uf := factors.GetUser()
|
|
|
|
assert.NotNil(t, uf)
|
|
|
|
assert.WithinRange(t, uf.GetVerifiedAt().AsTime(), time.Now().Add(-window), time.Now().Add(window))
|
2024-05-14 07:20:31 +00:00
|
|
|
assert.Equal(t, userID, uf.GetId())
|
2023-06-07 15:28:42 +00:00
|
|
|
case wantPasswordFactor:
|
|
|
|
pf := factors.GetPassword()
|
|
|
|
assert.NotNil(t, pf)
|
|
|
|
assert.WithinRange(t, pf.GetVerifiedAt().AsTime(), time.Now().Add(-window), time.Now().Add(window))
|
2023-08-11 15:36:18 +00:00
|
|
|
case wantWebAuthNFactor:
|
|
|
|
pf := factors.GetWebAuthN()
|
2023-06-07 15:28:42 +00:00
|
|
|
assert.NotNil(t, pf)
|
|
|
|
assert.WithinRange(t, pf.GetVerifiedAt().AsTime(), time.Now().Add(-window), time.Now().Add(window))
|
2023-08-15 09:50:42 +00:00
|
|
|
assert.False(t, pf.GetUserVerified())
|
2023-08-11 15:36:18 +00:00
|
|
|
case wantWebAuthNFactorUserVerified:
|
|
|
|
pf := factors.GetWebAuthN()
|
|
|
|
assert.NotNil(t, pf)
|
|
|
|
assert.WithinRange(t, pf.GetVerifiedAt().AsTime(), time.Now().Add(-window), time.Now().Add(window))
|
2023-08-15 09:50:42 +00:00
|
|
|
assert.True(t, pf.GetUserVerified())
|
|
|
|
case wantTOTPFactor:
|
|
|
|
pf := factors.GetTotp()
|
|
|
|
assert.NotNil(t, pf)
|
|
|
|
assert.WithinRange(t, pf.GetVerifiedAt().AsTime(), time.Now().Add(-window), time.Now().Add(window))
|
2023-06-21 14:06:18 +00:00
|
|
|
case wantIntentFactor:
|
|
|
|
pf := factors.GetIntent()
|
|
|
|
assert.NotNil(t, pf)
|
|
|
|
assert.WithinRange(t, pf.GetVerifiedAt().AsTime(), time.Now().Add(-window), time.Now().Add(window))
|
2023-08-24 09:41:52 +00:00
|
|
|
case wantOTPSMSFactor:
|
|
|
|
pf := factors.GetOtpSms()
|
|
|
|
assert.NotNil(t, pf)
|
|
|
|
assert.WithinRange(t, pf.GetVerifiedAt().AsTime(), time.Now().Add(-window), time.Now().Add(window))
|
|
|
|
case wantOTPEmailFactor:
|
|
|
|
pf := factors.GetOtpEmail()
|
|
|
|
assert.NotNil(t, pf)
|
|
|
|
assert.WithinRange(t, pf.GetVerifiedAt().AsTime(), time.Now().Add(-window), time.Now().Add(window))
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_CreateSession(t *testing.T) {
|
|
|
|
tests := []struct {
|
2023-11-06 09:48:28 +00:00
|
|
|
name string
|
|
|
|
req *session.CreateSessionRequest
|
|
|
|
want *session.CreateSessionResponse
|
|
|
|
wantErr bool
|
|
|
|
wantFactors []wantFactor
|
|
|
|
wantUserAgent *session.UserAgent
|
|
|
|
wantExpirationWindow time.Duration
|
2023-06-07 15:28:42 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty session",
|
|
|
|
req: &session.CreateSessionRequest{
|
|
|
|
Metadata: map[string][]byte{"foo": []byte("bar")},
|
|
|
|
},
|
|
|
|
want: &session.CreateSessionResponse{
|
|
|
|
Details: &object.Details{
|
2024-02-28 10:21:11 +00:00
|
|
|
ChangeDate: timestamppb.Now(),
|
2023-11-16 06:35:50 +00:00
|
|
|
ResourceOwner: Tester.Instance.InstanceID(),
|
2023-06-07 15:28:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-10-12 12:16:59 +00:00
|
|
|
{
|
|
|
|
name: "user agent",
|
|
|
|
req: &session.CreateSessionRequest{
|
|
|
|
Metadata: map[string][]byte{"foo": []byte("bar")},
|
|
|
|
UserAgent: &session.UserAgent{
|
|
|
|
FingerprintId: gu.Ptr("fingerPrintID"),
|
|
|
|
Ip: gu.Ptr("1.2.3.4"),
|
|
|
|
Description: gu.Ptr("Description"),
|
|
|
|
Header: map[string]*session.UserAgent_HeaderValues{
|
|
|
|
"foo": {Values: []string{"foo", "bar"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: &session.CreateSessionResponse{
|
|
|
|
Details: &object.Details{
|
2024-02-28 10:21:11 +00:00
|
|
|
ChangeDate: timestamppb.Now(),
|
2023-11-16 06:35:50 +00:00
|
|
|
ResourceOwner: Tester.Instance.InstanceID(),
|
2023-10-12 12:16:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantUserAgent: &session.UserAgent{
|
|
|
|
FingerprintId: gu.Ptr("fingerPrintID"),
|
|
|
|
Ip: gu.Ptr("1.2.3.4"),
|
|
|
|
Description: gu.Ptr("Description"),
|
|
|
|
Header: map[string]*session.UserAgent_HeaderValues{
|
|
|
|
"foo": {Values: []string{"foo", "bar"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-11-06 09:48:28 +00:00
|
|
|
{
|
|
|
|
name: "negative lifetime",
|
|
|
|
req: &session.CreateSessionRequest{
|
|
|
|
Metadata: map[string][]byte{"foo": []byte("bar")},
|
|
|
|
Lifetime: durationpb.New(-5 * time.Minute),
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "lifetime",
|
|
|
|
req: &session.CreateSessionRequest{
|
|
|
|
Metadata: map[string][]byte{"foo": []byte("bar")},
|
|
|
|
Lifetime: durationpb.New(5 * time.Minute),
|
|
|
|
},
|
|
|
|
want: &session.CreateSessionResponse{
|
|
|
|
Details: &object.Details{
|
2024-02-28 10:21:11 +00:00
|
|
|
ChangeDate: timestamppb.Now(),
|
2023-11-16 06:35:50 +00:00
|
|
|
ResourceOwner: Tester.Instance.InstanceID(),
|
2023-11-06 09:48:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantExpirationWindow: 5 * time.Minute,
|
|
|
|
},
|
2023-06-07 15:28:42 +00:00
|
|
|
{
|
|
|
|
name: "with user",
|
|
|
|
req: &session.CreateSessionRequest{
|
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: User.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Metadata: map[string][]byte{"foo": []byte("bar")},
|
|
|
|
},
|
|
|
|
want: &session.CreateSessionResponse{
|
|
|
|
Details: &object.Details{
|
2024-02-28 10:21:11 +00:00
|
|
|
ChangeDate: timestamppb.Now(),
|
2023-11-16 06:35:50 +00:00
|
|
|
ResourceOwner: Tester.Instance.InstanceID(),
|
2023-06-07 15:28:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantFactors: []wantFactor{wantUserFactor},
|
|
|
|
},
|
2024-02-28 09:30:05 +00:00
|
|
|
{
|
|
|
|
name: "deactivated user",
|
|
|
|
req: &session.CreateSessionRequest{
|
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: DeactivatedUser.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "locked user",
|
|
|
|
req: &session.CreateSessionRequest{
|
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: LockedUser.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-06-07 15:28:42 +00:00
|
|
|
{
|
|
|
|
name: "password without user error",
|
|
|
|
req: &session.CreateSessionRequest{
|
|
|
|
Checks: &session.Checks{
|
|
|
|
Password: &session.CheckPassword{
|
|
|
|
Password: "Difficult",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "passkey without user error",
|
|
|
|
req: &session.CreateSessionRequest{
|
2023-08-11 15:36:18 +00:00
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
WebAuthN: &session.RequestChallenges_WebAuthN{
|
|
|
|
Domain: Tester.Config.ExternalDomain,
|
|
|
|
UserVerificationRequirement: session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_REQUIRED,
|
|
|
|
},
|
2023-06-07 15:28:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-06-27 12:36:07 +00:00
|
|
|
{
|
|
|
|
name: "passkey without domain (not registered) error",
|
|
|
|
req: &session.CreateSessionRequest{
|
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: User.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-11 15:36:18 +00:00
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
WebAuthN: &session.RequestChallenges_WebAuthN{
|
|
|
|
UserVerificationRequirement: session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_REQUIRED,
|
|
|
|
},
|
2023-06-27 12:36:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := Client.CreateSession(CTX, tt.req)
|
|
|
|
if tt.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
integration.AssertDetails(t, tt.want, got)
|
|
|
|
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, got.GetSessionId(), got.GetSessionToken(), got.GetDetails().GetSequence(), time.Minute, tt.req.GetMetadata(), tt.wantUserAgent, tt.wantExpirationWindow, User.GetUserId(), tt.wantFactors...)
|
2023-06-07 15:28:42 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-30 22:08:48 +00:00
|
|
|
func TestServer_CreateSession_lock_user(t *testing.T) {
|
|
|
|
// create a separate org so we don't interfere with any other test
|
|
|
|
org := Tester.CreateOrganization(IAMOwnerCTX,
|
|
|
|
fmt.Sprintf("TestServer_CreateSession_lock_user_%d", time.Now().UnixNano()),
|
|
|
|
fmt.Sprintf("%d@mouse.com", time.Now().UnixNano()),
|
|
|
|
)
|
|
|
|
userID := org.CreatedAdmins[0].GetUserId()
|
|
|
|
Tester.SetUserPassword(IAMOwnerCTX, userID, integration.UserPassword, false)
|
|
|
|
|
|
|
|
// enable password lockout
|
|
|
|
maxAttempts := 2
|
|
|
|
ctxOrg := metadata.AppendToOutgoingContext(IAMOwnerCTX, "x-zitadel-orgid", org.GetOrganizationId())
|
|
|
|
_, err := Tester.Client.Mgmt.AddCustomLockoutPolicy(ctxOrg, &mgmt.AddCustomLockoutPolicyRequest{
|
|
|
|
MaxPasswordAttempts: uint32(maxAttempts),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for i := 0; i <= maxAttempts; i++ {
|
|
|
|
_, err := Client.CreateSession(CTX, &session.CreateSessionRequest{
|
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: userID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Password: &session.CheckPassword{
|
|
|
|
Password: "invalid",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
statusCode := status.Code(err)
|
|
|
|
expectedCode := codes.InvalidArgument
|
|
|
|
// as soon as we hit the limit the user is locked and following request will
|
|
|
|
// already deny any check with a precondition failed since the user is locked
|
|
|
|
if i >= maxAttempts {
|
|
|
|
expectedCode = codes.FailedPrecondition
|
|
|
|
}
|
|
|
|
assert.Equal(t, expectedCode, statusCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
func TestServer_CreateSession_webauthn(t *testing.T) {
|
|
|
|
// create new session with user and request the webauthn challenge
|
2023-06-07 15:28:42 +00:00
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{
|
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: User.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-11 15:36:18 +00:00
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
WebAuthN: &session.RequestChallenges_WebAuthN{
|
|
|
|
Domain: Tester.Config.ExternalDomain,
|
|
|
|
UserVerificationRequirement: session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_REQUIRED,
|
|
|
|
},
|
2023-06-07 15:28:42 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), createResp.GetSessionToken(), createResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId())
|
2023-06-07 15:28:42 +00:00
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
assertionData, err := Tester.WebAuthN.CreateAssertionResponse(createResp.GetChallenges().GetWebAuthN().GetPublicKeyCredentialRequestOptions(), true)
|
2023-06-07 15:28:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
// update the session with webauthn assertion data
|
2023-06-07 15:28:42 +00:00
|
|
|
updateResp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2023-06-07 15:28:42 +00:00
|
|
|
Checks: &session.Checks{
|
2023-08-11 15:36:18 +00:00
|
|
|
WebAuthN: &session.CheckWebAuthN{
|
2023-06-07 15:28:42 +00:00
|
|
|
CredentialAssertionData: assertionData,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), updateResp.GetSessionToken(), updateResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId(), wantUserFactor, wantWebAuthNFactorUserVerified)
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 14:06:18 +00:00
|
|
|
func TestServer_CreateSession_successfulIntent(t *testing.T) {
|
2024-05-07 06:11:20 +00:00
|
|
|
idpID := Tester.AddGenericOAuthProvider(t, CTX)
|
2023-06-21 14:06:18 +00:00
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{
|
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: User.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), createResp.GetSessionToken(), createResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId())
|
2023-06-21 14:06:18 +00:00
|
|
|
|
2024-05-07 06:11:20 +00:00
|
|
|
intentID, token, _, _ := Tester.CreateSuccessfulOAuthIntent(t, CTX, idpID, User.GetUserId(), "id")
|
2023-06-21 14:06:18 +00:00
|
|
|
updateResp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2023-06-21 14:06:18 +00:00
|
|
|
Checks: &session.Checks{
|
2023-08-22 10:05:45 +00:00
|
|
|
IdpIntent: &session.CheckIDPIntent{
|
|
|
|
IdpIntentId: intentID,
|
|
|
|
IdpIntentToken: token,
|
2023-06-21 14:06:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), updateResp.GetSessionToken(), updateResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId(), wantUserFactor, wantIntentFactor)
|
2023-06-21 14:06:18 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 17:21:21 +00:00
|
|
|
func TestServer_CreateSession_successfulIntent_instant(t *testing.T) {
|
2024-05-07 06:11:20 +00:00
|
|
|
idpID := Tester.AddGenericOAuthProvider(t, CTX)
|
2024-04-09 17:21:21 +00:00
|
|
|
|
2024-05-07 06:11:20 +00:00
|
|
|
intentID, token, _, _ := Tester.CreateSuccessfulOAuthIntent(t, CTX, idpID, User.GetUserId(), "id")
|
2024-04-09 17:21:21 +00:00
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{
|
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: User.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
IdpIntent: &session.CheckIDPIntent{
|
|
|
|
IdpIntentId: intentID,
|
|
|
|
IdpIntentToken: token,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), createResp.GetSessionToken(), createResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId(), wantUserFactor, wantIntentFactor)
|
2024-04-09 17:21:21 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 14:06:18 +00:00
|
|
|
func TestServer_CreateSession_successfulIntentUnknownUserID(t *testing.T) {
|
2024-05-07 06:11:20 +00:00
|
|
|
idpID := Tester.AddGenericOAuthProvider(t, CTX)
|
2023-06-21 14:06:18 +00:00
|
|
|
|
2024-05-30 07:06:32 +00:00
|
|
|
// successful intent without known / linked user
|
|
|
|
idpUserID := "id"
|
|
|
|
intentID, token, _, _ := Tester.CreateSuccessfulOAuthIntent(t, CTX, idpID, "", idpUserID)
|
|
|
|
|
|
|
|
// link the user (with info from intent)
|
|
|
|
Tester.CreateUserIDPlink(CTX, User.GetUserId(), idpUserID, idpID, User.GetUserId())
|
|
|
|
|
|
|
|
// session with intent check must now succeed
|
2023-06-21 14:06:18 +00:00
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{
|
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: User.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
2023-08-22 10:05:45 +00:00
|
|
|
IdpIntent: &session.CheckIDPIntent{
|
|
|
|
IdpIntentId: intentID,
|
|
|
|
IdpIntentToken: token,
|
2023-06-21 14:06:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-05-30 07:06:32 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), createResp.GetSessionToken(), createResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId(), wantUserFactor, wantIntentFactor)
|
2023-06-21 14:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_CreateSession_startedIntentFalseToken(t *testing.T) {
|
2024-05-07 06:11:20 +00:00
|
|
|
idpID := Tester.AddGenericOAuthProvider(t, CTX)
|
2023-06-21 14:06:18 +00:00
|
|
|
|
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{
|
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: User.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), createResp.GetSessionToken(), createResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId())
|
2023-06-21 14:06:18 +00:00
|
|
|
|
2024-05-07 06:11:20 +00:00
|
|
|
intentID := Tester.CreateIntent(t, CTX, idpID)
|
2023-06-21 14:06:18 +00:00
|
|
|
_, err = Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2023-06-21 14:06:18 +00:00
|
|
|
Checks: &session.Checks{
|
2023-08-22 10:05:45 +00:00
|
|
|
IdpIntent: &session.CheckIDPIntent{
|
|
|
|
IdpIntentId: intentID,
|
|
|
|
IdpIntentToken: "false",
|
2023-06-21 14:06:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2023-08-15 09:50:42 +00:00
|
|
|
func registerTOTP(ctx context.Context, t *testing.T, userID string) (secret string) {
|
|
|
|
resp, err := Tester.Client.UserV2.RegisterTOTP(ctx, &user.RegisterTOTPRequest{
|
|
|
|
UserId: userID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
secret = resp.GetSecret()
|
|
|
|
code, err := totp.GenerateCode(secret, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = Tester.Client.UserV2.VerifyTOTPRegistration(ctx, &user.VerifyTOTPRegistrationRequest{
|
|
|
|
UserId: userID,
|
|
|
|
Code: code,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
return secret
|
|
|
|
}
|
|
|
|
|
2023-08-24 09:41:52 +00:00
|
|
|
func registerOTPSMS(ctx context.Context, t *testing.T, userID string) {
|
|
|
|
_, err := Tester.Client.UserV2.AddOTPSMS(ctx, &user.AddOTPSMSRequest{
|
|
|
|
UserId: userID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerOTPEmail(ctx context.Context, t *testing.T, userID string) {
|
|
|
|
_, err := Tester.Client.UserV2.AddOTPEmail(ctx, &user.AddOTPEmailRequest{
|
|
|
|
UserId: userID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2024-05-14 07:20:31 +00:00
|
|
|
func TestServer_SetSession_flow_totp(t *testing.T) {
|
|
|
|
userExisting := createFullUser(CTX)
|
|
|
|
|
|
|
|
// create new, empty session
|
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken := createResp.GetSessionToken()
|
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, createResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, "")
|
|
|
|
|
|
|
|
t.Run("check user", func(t *testing.T) {
|
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2024-05-14 07:20:31 +00:00
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: userExisting.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken = resp.GetSessionToken()
|
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, userExisting.GetUserId(), wantUserFactor)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check webauthn, user verified (passkey)", func(t *testing.T) {
|
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2024-05-14 07:20:31 +00:00
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
WebAuthN: &session.RequestChallenges_WebAuthN{
|
|
|
|
Domain: Tester.Config.ExternalDomain,
|
|
|
|
UserVerificationRequirement: session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_REQUIRED,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), resp.GetSessionToken(), resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, userExisting.GetUserId())
|
|
|
|
sessionToken = resp.GetSessionToken()
|
|
|
|
|
|
|
|
assertionData, err := Tester.WebAuthN.CreateAssertionResponse(resp.GetChallenges().GetWebAuthN().GetPublicKeyCredentialRequestOptions(), true)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp, err = Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2024-05-14 07:20:31 +00:00
|
|
|
Checks: &session.Checks{
|
|
|
|
WebAuthN: &session.CheckWebAuthN{
|
|
|
|
CredentialAssertionData: assertionData,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken = resp.GetSessionToken()
|
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, userExisting.GetUserId(), wantUserFactor, wantWebAuthNFactorUserVerified)
|
|
|
|
})
|
|
|
|
|
|
|
|
userAuthCtx := Tester.WithAuthorizationToken(CTX, sessionToken)
|
|
|
|
Tester.RegisterUserU2F(userAuthCtx, userExisting.GetUserId())
|
|
|
|
totpSecret := registerTOTP(userAuthCtx, t, userExisting.GetUserId())
|
|
|
|
registerOTPSMS(userAuthCtx, t, userExisting.GetUserId())
|
|
|
|
registerOTPEmail(userAuthCtx, t, userExisting.GetUserId())
|
|
|
|
|
|
|
|
t.Run("check TOTP", func(t *testing.T) {
|
|
|
|
code, err := totp.GenerateCode(totpSecret, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2024-05-14 07:20:31 +00:00
|
|
|
Checks: &session.Checks{
|
|
|
|
Totp: &session.CheckTOTP{
|
|
|
|
Code: code,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken = resp.GetSessionToken()
|
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, userExisting.GetUserId(), wantUserFactor, wantTOTPFactor)
|
|
|
|
})
|
|
|
|
|
|
|
|
userImport := Tester.CreateHumanUserWithTOTP(CTX, totpSecret)
|
|
|
|
createRespImport, err := Client.CreateSession(CTX, &session.CreateSessionRequest{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionTokenImport := createRespImport.GetSessionToken()
|
|
|
|
verifyCurrentSession(t, createRespImport.GetSessionId(), sessionTokenImport, createRespImport.GetDetails().GetSequence(), time.Minute, nil, nil, 0, "")
|
|
|
|
|
|
|
|
t.Run("check user", func(t *testing.T) {
|
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createRespImport.GetSessionId(),
|
2024-05-14 07:20:31 +00:00
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: userImport.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionTokenImport = resp.GetSessionToken()
|
|
|
|
verifyCurrentSession(t, createRespImport.GetSessionId(), sessionTokenImport, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, userImport.GetUserId(), wantUserFactor)
|
|
|
|
})
|
|
|
|
t.Run("check TOTP", func(t *testing.T) {
|
|
|
|
code, err := totp.GenerateCode(totpSecret, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createRespImport.GetSessionId(),
|
2024-05-14 07:20:31 +00:00
|
|
|
Checks: &session.Checks{
|
|
|
|
Totp: &session.CheckTOTP{
|
|
|
|
Code: code,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionTokenImport = resp.GetSessionToken()
|
|
|
|
verifyCurrentSession(t, createRespImport.GetSessionId(), sessionTokenImport, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, userImport.GetUserId(), wantUserFactor, wantTOTPFactor)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-07 15:28:42 +00:00
|
|
|
func TestServer_SetSession_flow(t *testing.T) {
|
|
|
|
// create new, empty session
|
2023-08-11 15:36:18 +00:00
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{})
|
2023-06-07 15:28:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken := createResp.GetSessionToken()
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, createResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId())
|
2023-06-07 15:28:42 +00:00
|
|
|
|
|
|
|
t.Run("check user", func(t *testing.T) {
|
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2023-06-07 15:28:42 +00:00
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: User.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken = resp.GetSessionToken()
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId(), wantUserFactor)
|
2023-06-07 15:28:42 +00:00
|
|
|
})
|
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
t.Run("check webauthn, user verified (passkey)", func(t *testing.T) {
|
2023-06-07 15:28:42 +00:00
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2023-08-11 15:36:18 +00:00
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
WebAuthN: &session.RequestChallenges_WebAuthN{
|
|
|
|
Domain: Tester.Config.ExternalDomain,
|
|
|
|
UserVerificationRequirement: session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_REQUIRED,
|
|
|
|
},
|
2023-06-07 15:28:42 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), resp.GetSessionToken(), resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId())
|
2023-06-07 15:28:42 +00:00
|
|
|
sessionToken = resp.GetSessionToken()
|
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
assertionData, err := Tester.WebAuthN.CreateAssertionResponse(resp.GetChallenges().GetWebAuthN().GetPublicKeyCredentialRequestOptions(), true)
|
2023-06-07 15:28:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp, err = Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2023-06-07 15:28:42 +00:00
|
|
|
Checks: &session.Checks{
|
2023-08-11 15:36:18 +00:00
|
|
|
WebAuthN: &session.CheckWebAuthN{
|
2023-06-07 15:28:42 +00:00
|
|
|
CredentialAssertionData: assertionData,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-08-11 15:36:18 +00:00
|
|
|
sessionToken = resp.GetSessionToken()
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId(), wantUserFactor, wantWebAuthNFactorUserVerified)
|
2023-08-11 15:36:18 +00:00
|
|
|
})
|
|
|
|
|
2023-08-15 09:50:42 +00:00
|
|
|
userAuthCtx := Tester.WithAuthorizationToken(CTX, sessionToken)
|
|
|
|
Tester.RegisterUserU2F(userAuthCtx, User.GetUserId())
|
|
|
|
totpSecret := registerTOTP(userAuthCtx, t, User.GetUserId())
|
2023-08-24 09:41:52 +00:00
|
|
|
registerOTPSMS(userAuthCtx, t, User.GetUserId())
|
|
|
|
registerOTPEmail(userAuthCtx, t, User.GetUserId())
|
2023-08-15 09:50:42 +00:00
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
t.Run("check webauthn, user not verified (U2F)", func(t *testing.T) {
|
|
|
|
|
|
|
|
for _, userVerificationRequirement := range []session.UserVerificationRequirement{
|
|
|
|
session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_PREFERRED,
|
|
|
|
session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_DISCOURAGED,
|
|
|
|
} {
|
|
|
|
t.Run(userVerificationRequirement.String(), func(t *testing.T) {
|
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2023-08-11 15:36:18 +00:00
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
WebAuthN: &session.RequestChallenges_WebAuthN{
|
|
|
|
Domain: Tester.Config.ExternalDomain,
|
|
|
|
UserVerificationRequirement: userVerificationRequirement,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), resp.GetSessionToken(), resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId())
|
2023-08-11 15:36:18 +00:00
|
|
|
sessionToken = resp.GetSessionToken()
|
|
|
|
|
|
|
|
assertionData, err := Tester.WebAuthN.CreateAssertionResponse(resp.GetChallenges().GetWebAuthN().GetPublicKeyCredentialRequestOptions(), false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp, err = Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2023-08-11 15:36:18 +00:00
|
|
|
Checks: &session.Checks{
|
|
|
|
WebAuthN: &session.CheckWebAuthN{
|
|
|
|
CredentialAssertionData: assertionData,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken = resp.GetSessionToken()
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId(), wantUserFactor, wantWebAuthNFactor)
|
2023-08-11 15:36:18 +00:00
|
|
|
})
|
|
|
|
}
|
2023-06-07 15:28:42 +00:00
|
|
|
})
|
2023-08-15 09:50:42 +00:00
|
|
|
|
|
|
|
t.Run("check TOTP", func(t *testing.T) {
|
|
|
|
code, err := totp.GenerateCode(totpSecret, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2023-08-15 09:50:42 +00:00
|
|
|
Checks: &session.Checks{
|
|
|
|
Totp: &session.CheckTOTP{
|
2023-08-31 07:06:50 +00:00
|
|
|
Code: code,
|
2023-08-15 09:50:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken = resp.GetSessionToken()
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId(), wantUserFactor, wantWebAuthNFactor, wantTOTPFactor)
|
2023-08-15 09:50:42 +00:00
|
|
|
})
|
2023-08-24 09:41:52 +00:00
|
|
|
|
|
|
|
t.Run("check OTP SMS", func(t *testing.T) {
|
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2023-08-24 09:41:52 +00:00
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
OtpSms: &session.RequestChallenges_OTPSMS{ReturnCode: true},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), resp.GetSessionToken(), resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId())
|
2023-08-24 09:41:52 +00:00
|
|
|
sessionToken = resp.GetSessionToken()
|
|
|
|
|
|
|
|
otp := resp.GetChallenges().GetOtpSms()
|
|
|
|
require.NotEmpty(t, otp)
|
|
|
|
|
|
|
|
resp, err = Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2023-08-24 09:41:52 +00:00
|
|
|
Checks: &session.Checks{
|
|
|
|
OtpSms: &session.CheckOTP{
|
2023-08-31 07:06:50 +00:00
|
|
|
Code: otp,
|
2023-08-24 09:41:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken = resp.GetSessionToken()
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId(), wantUserFactor, wantWebAuthNFactor, wantOTPSMSFactor)
|
2023-08-24 09:41:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check OTP Email", func(t *testing.T) {
|
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2023-08-24 09:41:52 +00:00
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
OtpEmail: &session.RequestChallenges_OTPEmail{
|
|
|
|
DeliveryType: &session.RequestChallenges_OTPEmail_ReturnCode_{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), resp.GetSessionToken(), resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId())
|
2023-08-24 09:41:52 +00:00
|
|
|
sessionToken = resp.GetSessionToken()
|
|
|
|
|
|
|
|
otp := resp.GetChallenges().GetOtpEmail()
|
|
|
|
require.NotEmpty(t, otp)
|
|
|
|
|
|
|
|
resp, err = Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
2023-08-24 09:41:52 +00:00
|
|
|
Checks: &session.Checks{
|
|
|
|
OtpEmail: &session.CheckOTP{
|
2023-08-31 07:06:50 +00:00
|
|
|
Code: otp,
|
2023-08-24 09:41:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken = resp.GetSessionToken()
|
2024-05-14 07:20:31 +00:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, User.GetUserId(), wantUserFactor, wantWebAuthNFactor, wantOTPEmailFactor)
|
2023-08-24 09:41:52 +00:00
|
|
|
})
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
2023-07-14 11:16:16 +00:00
|
|
|
|
2023-11-06 09:48:28 +00:00
|
|
|
func TestServer_SetSession_expired(t *testing.T) {
|
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{
|
|
|
|
Lifetime: durationpb.New(20 * time.Second),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// test session token works
|
2024-05-22 05:56:11 +00:00
|
|
|
_, err = Tester.Client.SessionV2.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
Lifetime: durationpb.New(20 * time.Second),
|
2023-11-06 09:48:28 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// ensure session expires and does not work anymore
|
|
|
|
time.Sleep(20 * time.Second)
|
2024-07-26 20:39:55 +00:00
|
|
|
_, err = Client.SetSession(CTX, &session.SetSessionRequest{
|
2024-05-22 05:56:11 +00:00
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
Lifetime: durationpb.New(20 * time.Second),
|
2023-11-06 09:48:28 +00:00
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2023-11-16 06:35:50 +00:00
|
|
|
func TestServer_DeleteSession_token(t *testing.T) {
|
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = Client.DeleteSession(CTX, &session.DeleteSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: gu.Ptr("invalid"),
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
_, err = Client.DeleteSession(CTX, &session.DeleteSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: gu.Ptr(createResp.GetSessionToken()),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_DeleteSession_own_session(t *testing.T) {
|
|
|
|
// create two users for the test and a session each to get tokens for authorization
|
|
|
|
user1 := Tester.CreateHumanUser(CTX)
|
2024-03-28 06:21:21 +00:00
|
|
|
Tester.SetUserPassword(CTX, user1.GetUserId(), integration.UserPassword, false)
|
2023-11-16 06:35:50 +00:00
|
|
|
_, token1, _, _ := Tester.CreatePasswordSession(t, CTX, user1.GetUserId(), integration.UserPassword)
|
|
|
|
|
|
|
|
user2 := Tester.CreateHumanUser(CTX)
|
2024-03-28 06:21:21 +00:00
|
|
|
Tester.SetUserPassword(CTX, user2.GetUserId(), integration.UserPassword, false)
|
2023-11-16 06:35:50 +00:00
|
|
|
_, token2, _, _ := Tester.CreatePasswordSession(t, CTX, user2.GetUserId(), integration.UserPassword)
|
|
|
|
|
|
|
|
// create a new session for the first user
|
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{
|
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: user1.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// delete the new (user1) session must not be possible with user (has no permission)
|
|
|
|
_, err = Client.DeleteSession(Tester.WithAuthorizationToken(context.Background(), token2), &session.DeleteSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
|
2024-02-26 13:11:09 +00:00
|
|
|
// delete the new (user1) session by themselves
|
2023-11-16 06:35:50 +00:00
|
|
|
_, err = Client.DeleteSession(Tester.WithAuthorizationToken(context.Background(), token1), &session.DeleteSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_DeleteSession_with_permission(t *testing.T) {
|
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{
|
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: User.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// delete the new session by ORG_OWNER
|
|
|
|
_, err = Client.DeleteSession(Tester.WithAuthorization(context.Background(), integration.OrgOwner), &session.DeleteSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-07-14 11:16:16 +00:00
|
|
|
func Test_ZITADEL_API_missing_authentication(t *testing.T) {
|
|
|
|
// create new, empty session
|
2023-08-11 15:36:18 +00:00
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{})
|
2023-07-14 11:16:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx := metadata.AppendToOutgoingContext(context.Background(), "Authorization", fmt.Sprintf("Bearer %s", createResp.GetSessionToken()))
|
2024-07-26 20:39:55 +00:00
|
|
|
sessionResp, err := Client.GetSession(ctx, &session.GetSessionRequest{SessionId: createResp.GetSessionId()})
|
2023-07-14 11:16:16 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, sessionResp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ZITADEL_API_success(t *testing.T) {
|
2023-11-06 09:48:28 +00:00
|
|
|
id, token, _, _ := Tester.CreateVerifiedWebAuthNSession(t, CTX, User.GetUserId())
|
2023-07-14 11:16:16 +00:00
|
|
|
|
2023-08-02 16:57:53 +00:00
|
|
|
ctx := Tester.WithAuthorizationToken(context.Background(), token)
|
2024-07-26 20:39:55 +00:00
|
|
|
sessionResp, err := Client.GetSession(ctx, &session.GetSessionRequest{SessionId: id})
|
2023-07-14 11:16:16 +00:00
|
|
|
require.NoError(t, err)
|
2023-08-11 15:36:18 +00:00
|
|
|
|
|
|
|
webAuthN := sessionResp.GetSession().GetFactors().GetWebAuthN()
|
|
|
|
require.NotNil(t, id, webAuthN.GetVerifiedAt().AsTime())
|
|
|
|
require.True(t, webAuthN.GetUserVerified())
|
2023-07-14 11:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ZITADEL_API_session_not_found(t *testing.T) {
|
2023-11-06 09:48:28 +00:00
|
|
|
id, token, _, _ := Tester.CreateVerifiedWebAuthNSession(t, CTX, User.GetUserId())
|
2023-07-14 11:16:16 +00:00
|
|
|
|
|
|
|
// test session token works
|
2023-08-02 16:57:53 +00:00
|
|
|
ctx := Tester.WithAuthorizationToken(context.Background(), token)
|
2024-07-26 20:39:55 +00:00
|
|
|
_, err := Client.GetSession(ctx, &session.GetSessionRequest{SessionId: id})
|
2023-07-14 11:16:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
//terminate the session and test it does not work anymore
|
2024-07-26 20:39:55 +00:00
|
|
|
_, err = Client.DeleteSession(CTX, &session.DeleteSessionRequest{
|
2023-07-14 11:16:16 +00:00
|
|
|
SessionId: id,
|
|
|
|
SessionToken: gu.Ptr(token),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-08-02 16:57:53 +00:00
|
|
|
ctx = Tester.WithAuthorizationToken(context.Background(), token)
|
2024-07-26 20:39:55 +00:00
|
|
|
_, err = Client.GetSession(ctx, &session.GetSessionRequest{SessionId: id})
|
2023-07-14 11:16:16 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
2023-11-06 09:48:28 +00:00
|
|
|
|
|
|
|
func Test_ZITADEL_API_session_expired(t *testing.T) {
|
|
|
|
id, token, _, _ := Tester.CreateVerifiedWebAuthNSessionWithLifetime(t, CTX, User.GetUserId(), 20*time.Second)
|
|
|
|
|
|
|
|
// test session token works
|
|
|
|
ctx := Tester.WithAuthorizationToken(context.Background(), token)
|
2024-07-26 20:39:55 +00:00
|
|
|
_, err := Client.GetSession(ctx, &session.GetSessionRequest{SessionId: id})
|
2023-11-06 09:48:28 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// ensure session expires and does not work anymore
|
|
|
|
time.Sleep(20 * time.Second)
|
2024-07-26 20:39:55 +00:00
|
|
|
sessionResp, err := Client.GetSession(ctx, &session.GetSessionRequest{SessionId: id})
|
2023-11-06 09:48:28 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, sessionResp)
|
|
|
|
}
|