2023-06-07 17:28:42 +02:00
|
|
|
//go:build integration
|
|
|
|
|
|
|
|
package session_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-14 13:16:16 +02:00
|
|
|
"fmt"
|
2023-06-07 17:28:42 +02:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-07-14 13:16:16 +02:00
|
|
|
"github.com/muhlemmer/gu"
|
2023-08-15 12:50:42 +03:00
|
|
|
"github.com/pquerna/otp/totp"
|
2023-06-07 17:28:42 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-07-14 13:16:16 +02:00
|
|
|
"google.golang.org/grpc/metadata"
|
2023-10-12 15:16:59 +03:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-11-06 11:48:28 +02:00
|
|
|
"google.golang.org/protobuf/types/known/durationpb"
|
2023-06-21 16:06:18 +02:00
|
|
|
|
2023-06-07 17:28:42 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/integration"
|
2023-09-13 14:43:01 +02:00
|
|
|
object "github.com/zitadel/zitadel/pkg/grpc/object/v2beta"
|
|
|
|
session "github.com/zitadel/zitadel/pkg/grpc/session/v2beta"
|
|
|
|
user "github.com/zitadel/zitadel/pkg/grpc/user/v2beta"
|
2023-06-07 17:28:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-07-10 15:27:00 +02:00
|
|
|
CTX context.Context
|
|
|
|
Tester *integration.Tester
|
|
|
|
Client session.SessionServiceClient
|
|
|
|
User *user.AddHumanUserResponse
|
2023-06-07 17:28:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
os.Exit(func() int {
|
2023-06-15 07:32:40 +02:00
|
|
|
ctx, errCtx, cancel := integration.Contexts(5 * time.Minute)
|
2023-06-07 17:28:42 +02:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
Tester = integration.NewTester(ctx)
|
|
|
|
defer Tester.Done()
|
|
|
|
Client = Tester.Client.SessionV2
|
|
|
|
|
2023-07-06 08:38:13 +02:00
|
|
|
CTX, _ = Tester.WithAuthorization(ctx, integration.OrgOwner), errCtx
|
2023-06-07 17:28:42 +02:00
|
|
|
User = Tester.CreateHumanUser(CTX)
|
2023-08-24 11:41:52 +02:00
|
|
|
Tester.Client.UserV2.VerifyEmail(CTX, &user.VerifyEmailRequest{
|
|
|
|
UserId: User.GetUserId(),
|
|
|
|
VerificationCode: User.GetEmailCode(),
|
|
|
|
})
|
|
|
|
Tester.Client.UserV2.VerifyPhone(CTX, &user.VerifyPhoneRequest{
|
|
|
|
UserId: User.GetUserId(),
|
|
|
|
VerificationCode: User.GetPhoneCode(),
|
|
|
|
})
|
2023-07-14 13:16:16 +02:00
|
|
|
Tester.SetUserPassword(CTX, User.GetUserId(), integration.UserPassword)
|
2023-06-07 17:28:42 +02:00
|
|
|
Tester.RegisterUserPasskey(CTX, User.GetUserId())
|
|
|
|
return m.Run()
|
|
|
|
}())
|
|
|
|
}
|
|
|
|
|
2023-11-06 11:48:28 +02:00
|
|
|
func verifyCurrentSession(t testing.TB, id, token string, sequence uint64, window time.Duration, metadata map[string][]byte, userAgent *session.UserAgent, expirationWindow time.Duration, factors ...wantFactor) *session.Session {
|
2023-08-15 12:50:42 +03:00
|
|
|
t.Helper()
|
2023-06-07 17:28:42 +02:00
|
|
|
require.NotEmpty(t, id)
|
|
|
|
require.NotEmpty(t, token)
|
|
|
|
|
2023-07-07 11:15:05 +03:00
|
|
|
resp, err := Client.GetSession(CTX, &session.GetSessionRequest{
|
|
|
|
SessionId: id,
|
|
|
|
SessionToken: &token,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
s := resp.GetSession()
|
2023-06-07 17:28:42 +02: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 15:16:59 +03:00
|
|
|
|
|
|
|
if !proto.Equal(userAgent, s.GetUserAgent()) {
|
|
|
|
t.Errorf("user agent =\n%v\nwant\n%v", s.GetUserAgent(), userAgent)
|
|
|
|
}
|
2023-11-06 11:48:28 +02: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 15:16:59 +03:00
|
|
|
|
2023-06-07 17:28:42 +02:00
|
|
|
verifyFactors(t, s.GetFactors(), window, factors)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
type wantFactor int
|
|
|
|
|
|
|
|
const (
|
|
|
|
wantUserFactor wantFactor = iota
|
|
|
|
wantPasswordFactor
|
2023-08-11 18:36:18 +03:00
|
|
|
wantWebAuthNFactor
|
|
|
|
wantWebAuthNFactorUserVerified
|
2023-08-15 12:50:42 +03:00
|
|
|
wantTOTPFactor
|
2023-06-21 16:06:18 +02:00
|
|
|
wantIntentFactor
|
2023-08-24 11:41:52 +02:00
|
|
|
wantOTPSMSFactor
|
|
|
|
wantOTPEmailFactor
|
2023-06-07 17:28:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func verifyFactors(t testing.TB, factors *session.Factors, window time.Duration, want []wantFactor) {
|
|
|
|
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))
|
|
|
|
assert.Equal(t, User.GetUserId(), uf.GetId())
|
|
|
|
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 18:36:18 +03:00
|
|
|
case wantWebAuthNFactor:
|
|
|
|
pf := factors.GetWebAuthN()
|
2023-06-07 17:28:42 +02:00
|
|
|
assert.NotNil(t, pf)
|
|
|
|
assert.WithinRange(t, pf.GetVerifiedAt().AsTime(), time.Now().Add(-window), time.Now().Add(window))
|
2023-08-15 12:50:42 +03:00
|
|
|
assert.False(t, pf.GetUserVerified())
|
2023-08-11 18:36:18 +03: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 12:50:42 +03: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 16:06:18 +02: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 11:41:52 +02: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 17:28:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_CreateSession(t *testing.T) {
|
|
|
|
tests := []struct {
|
2023-11-06 11:48:28 +02:00
|
|
|
name string
|
|
|
|
req *session.CreateSessionRequest
|
|
|
|
want *session.CreateSessionResponse
|
|
|
|
wantErr bool
|
|
|
|
wantFactors []wantFactor
|
|
|
|
wantUserAgent *session.UserAgent
|
|
|
|
wantExpirationWindow time.Duration
|
2023-06-07 17:28:42 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty session",
|
|
|
|
req: &session.CreateSessionRequest{
|
|
|
|
Metadata: map[string][]byte{"foo": []byte("bar")},
|
|
|
|
},
|
|
|
|
want: &session.CreateSessionResponse{
|
|
|
|
Details: &object.Details{
|
|
|
|
ResourceOwner: Tester.Organisation.ID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-10-12 15:16:59 +03: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{
|
|
|
|
ResourceOwner: Tester.Organisation.ID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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 11:48:28 +02: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{
|
|
|
|
ResourceOwner: Tester.Organisation.ID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantExpirationWindow: 5 * time.Minute,
|
|
|
|
},
|
2023-06-07 17:28:42 +02: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{
|
|
|
|
ResourceOwner: Tester.Organisation.ID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantFactors: []wantFactor{wantUserFactor},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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 18:36:18 +03:00
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
WebAuthN: &session.RequestChallenges_WebAuthN{
|
|
|
|
Domain: Tester.Config.ExternalDomain,
|
|
|
|
UserVerificationRequirement: session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_REQUIRED,
|
|
|
|
},
|
2023-06-07 17:28:42 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-06-27 14:36:07 +02: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 18:36:18 +03:00
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
WebAuthN: &session.RequestChallenges_WebAuthN{
|
|
|
|
UserVerificationRequirement: session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_REQUIRED,
|
|
|
|
},
|
2023-06-27 14:36:07 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-06-07 17:28:42 +02: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)
|
|
|
|
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, got.GetSessionId(), got.GetSessionToken(), got.GetDetails().GetSequence(), time.Minute, tt.req.GetMetadata(), tt.wantUserAgent, tt.wantExpirationWindow, tt.wantFactors...)
|
2023-06-07 17:28:42 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-11 18:36:18 +03:00
|
|
|
func TestServer_CreateSession_webauthn(t *testing.T) {
|
|
|
|
// create new session with user and request the webauthn challenge
|
2023-06-07 17:28:42 +02:00
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{
|
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: User.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-11 18:36:18 +03:00
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
WebAuthN: &session.RequestChallenges_WebAuthN{
|
|
|
|
Domain: Tester.Config.ExternalDomain,
|
|
|
|
UserVerificationRequirement: session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_REQUIRED,
|
|
|
|
},
|
2023-06-07 17:28:42 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), createResp.GetSessionToken(), createResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0)
|
2023-06-07 17:28:42 +02:00
|
|
|
|
2023-08-11 18:36:18 +03:00
|
|
|
assertionData, err := Tester.WebAuthN.CreateAssertionResponse(createResp.GetChallenges().GetWebAuthN().GetPublicKeyCredentialRequestOptions(), true)
|
2023-06-07 17:28:42 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-11 18:36:18 +03:00
|
|
|
// update the session with webauthn assertion data
|
2023-06-07 17:28:42 +02:00
|
|
|
updateResp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: createResp.GetSessionToken(),
|
|
|
|
Checks: &session.Checks{
|
2023-08-11 18:36:18 +03:00
|
|
|
WebAuthN: &session.CheckWebAuthN{
|
2023-06-07 17:28:42 +02:00
|
|
|
CredentialAssertionData: assertionData,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), updateResp.GetSessionToken(), updateResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, wantUserFactor, wantWebAuthNFactorUserVerified)
|
2023-06-07 17:28:42 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 16:06:18 +02:00
|
|
|
func TestServer_CreateSession_successfulIntent(t *testing.T) {
|
|
|
|
idpID := Tester.AddGenericOAuthProvider(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)
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), createResp.GetSessionToken(), createResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0)
|
2023-06-21 16:06:18 +02:00
|
|
|
|
2023-08-16 13:29:57 +02:00
|
|
|
intentID, token, _, _ := Tester.CreateSuccessfulOAuthIntent(t, idpID, User.GetUserId(), "id")
|
2023-06-21 16:06:18 +02:00
|
|
|
updateResp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: createResp.GetSessionToken(),
|
|
|
|
Checks: &session.Checks{
|
2023-08-22 12:05:45 +02:00
|
|
|
IdpIntent: &session.CheckIDPIntent{
|
|
|
|
IdpIntentId: intentID,
|
|
|
|
IdpIntentToken: token,
|
2023-06-21 16:06:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), updateResp.GetSessionToken(), updateResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, wantUserFactor, wantIntentFactor)
|
2023-06-21 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_CreateSession_successfulIntentUnknownUserID(t *testing.T) {
|
|
|
|
idpID := Tester.AddGenericOAuthProvider(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)
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), createResp.GetSessionToken(), createResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0)
|
2023-06-21 16:06:18 +02:00
|
|
|
|
|
|
|
idpUserID := "id"
|
2023-08-16 13:29:57 +02:00
|
|
|
intentID, token, _, _ := Tester.CreateSuccessfulOAuthIntent(t, idpID, "", idpUserID)
|
2023-06-21 16:06:18 +02:00
|
|
|
updateResp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: createResp.GetSessionToken(),
|
|
|
|
Checks: &session.Checks{
|
2023-08-22 12:05:45 +02:00
|
|
|
IdpIntent: &session.CheckIDPIntent{
|
|
|
|
IdpIntentId: intentID,
|
|
|
|
IdpIntentToken: token,
|
2023-06-21 16:06:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
Tester.CreateUserIDPlink(CTX, User.GetUserId(), idpUserID, idpID, User.GetUserId())
|
|
|
|
updateResp, err = Client.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: createResp.GetSessionToken(),
|
|
|
|
Checks: &session.Checks{
|
2023-08-22 12:05:45 +02:00
|
|
|
IdpIntent: &session.CheckIDPIntent{
|
|
|
|
IdpIntentId: intentID,
|
|
|
|
IdpIntentToken: token,
|
2023-06-21 16:06:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), updateResp.GetSessionToken(), updateResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, wantUserFactor, wantIntentFactor)
|
2023-06-21 16:06:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_CreateSession_startedIntentFalseToken(t *testing.T) {
|
|
|
|
idpID := Tester.AddGenericOAuthProvider(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)
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), createResp.GetSessionToken(), createResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0)
|
2023-06-21 16:06:18 +02:00
|
|
|
|
|
|
|
intentID := Tester.CreateIntent(t, idpID)
|
|
|
|
_, err = Client.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: createResp.GetSessionToken(),
|
|
|
|
Checks: &session.Checks{
|
2023-08-22 12:05:45 +02:00
|
|
|
IdpIntent: &session.CheckIDPIntent{
|
|
|
|
IdpIntentId: intentID,
|
|
|
|
IdpIntentToken: "false",
|
2023-06-21 16:06:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2023-08-15 12:50:42 +03: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 11:41:52 +02: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)
|
|
|
|
}
|
|
|
|
|
2023-06-07 17:28:42 +02:00
|
|
|
func TestServer_SetSession_flow(t *testing.T) {
|
|
|
|
// create new, empty session
|
2023-08-11 18:36:18 +03:00
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{})
|
2023-06-07 17:28:42 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken := createResp.GetSessionToken()
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, createResp.GetDetails().GetSequence(), time.Minute, nil, nil, 0)
|
2023-06-07 17:28:42 +02:00
|
|
|
|
|
|
|
t.Run("check user", func(t *testing.T) {
|
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: sessionToken,
|
|
|
|
Checks: &session.Checks{
|
|
|
|
User: &session.CheckUser{
|
|
|
|
Search: &session.CheckUser_UserId{
|
|
|
|
UserId: User.GetUserId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken = resp.GetSessionToken()
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, wantUserFactor)
|
2023-06-07 17:28:42 +02:00
|
|
|
})
|
|
|
|
|
2023-08-11 18:36:18 +03:00
|
|
|
t.Run("check webauthn, user verified (passkey)", func(t *testing.T) {
|
2023-06-07 17:28:42 +02:00
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: sessionToken,
|
2023-08-11 18:36:18 +03:00
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
WebAuthN: &session.RequestChallenges_WebAuthN{
|
|
|
|
Domain: Tester.Config.ExternalDomain,
|
|
|
|
UserVerificationRequirement: session.UserVerificationRequirement_USER_VERIFICATION_REQUIREMENT_REQUIRED,
|
|
|
|
},
|
2023-06-07 17:28:42 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), resp.GetSessionToken(), resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0)
|
2023-06-07 17:28:42 +02:00
|
|
|
sessionToken = resp.GetSessionToken()
|
|
|
|
|
2023-08-11 18:36:18 +03:00
|
|
|
assertionData, err := Tester.WebAuthN.CreateAssertionResponse(resp.GetChallenges().GetWebAuthN().GetPublicKeyCredentialRequestOptions(), true)
|
2023-06-07 17:28:42 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp, err = Client.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: sessionToken,
|
|
|
|
Checks: &session.Checks{
|
2023-08-11 18:36:18 +03:00
|
|
|
WebAuthN: &session.CheckWebAuthN{
|
2023-06-07 17:28:42 +02:00
|
|
|
CredentialAssertionData: assertionData,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-08-11 18:36:18 +03:00
|
|
|
sessionToken = resp.GetSessionToken()
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, wantUserFactor, wantWebAuthNFactorUserVerified)
|
2023-08-11 18:36:18 +03:00
|
|
|
})
|
|
|
|
|
2023-08-15 12:50:42 +03:00
|
|
|
userAuthCtx := Tester.WithAuthorizationToken(CTX, sessionToken)
|
|
|
|
Tester.RegisterUserU2F(userAuthCtx, User.GetUserId())
|
|
|
|
totpSecret := registerTOTP(userAuthCtx, t, User.GetUserId())
|
2023-08-24 11:41:52 +02:00
|
|
|
registerOTPSMS(userAuthCtx, t, User.GetUserId())
|
|
|
|
registerOTPEmail(userAuthCtx, t, User.GetUserId())
|
2023-08-15 12:50:42 +03:00
|
|
|
|
2023-08-11 18:36:18 +03: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{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: sessionToken,
|
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
WebAuthN: &session.RequestChallenges_WebAuthN{
|
|
|
|
Domain: Tester.Config.ExternalDomain,
|
|
|
|
UserVerificationRequirement: userVerificationRequirement,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), resp.GetSessionToken(), resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0)
|
2023-08-11 18:36:18 +03: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{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: sessionToken,
|
|
|
|
Checks: &session.Checks{
|
|
|
|
WebAuthN: &session.CheckWebAuthN{
|
|
|
|
CredentialAssertionData: assertionData,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken = resp.GetSessionToken()
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, wantUserFactor, wantWebAuthNFactor)
|
2023-08-11 18:36:18 +03:00
|
|
|
})
|
|
|
|
}
|
2023-06-07 17:28:42 +02:00
|
|
|
})
|
2023-08-15 12:50:42 +03: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{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: sessionToken,
|
|
|
|
Checks: &session.Checks{
|
|
|
|
Totp: &session.CheckTOTP{
|
2023-08-31 09:06:50 +02:00
|
|
|
Code: code,
|
2023-08-15 12:50:42 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken = resp.GetSessionToken()
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, wantUserFactor, wantWebAuthNFactor, wantTOTPFactor)
|
2023-08-15 12:50:42 +03:00
|
|
|
})
|
2023-08-24 11:41:52 +02:00
|
|
|
|
|
|
|
t.Run("check OTP SMS", func(t *testing.T) {
|
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: sessionToken,
|
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
OtpSms: &session.RequestChallenges_OTPSMS{ReturnCode: true},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), resp.GetSessionToken(), resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0)
|
2023-08-24 11:41:52 +02:00
|
|
|
sessionToken = resp.GetSessionToken()
|
|
|
|
|
|
|
|
otp := resp.GetChallenges().GetOtpSms()
|
|
|
|
require.NotEmpty(t, otp)
|
|
|
|
|
|
|
|
resp, err = Client.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: sessionToken,
|
|
|
|
Checks: &session.Checks{
|
|
|
|
OtpSms: &session.CheckOTP{
|
2023-08-31 09:06:50 +02:00
|
|
|
Code: otp,
|
2023-08-24 11:41:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken = resp.GetSessionToken()
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, wantUserFactor, wantWebAuthNFactor, wantOTPSMSFactor)
|
2023-08-24 11:41:52 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check OTP Email", func(t *testing.T) {
|
|
|
|
resp, err := Client.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: sessionToken,
|
|
|
|
Challenges: &session.RequestChallenges{
|
|
|
|
OtpEmail: &session.RequestChallenges_OTPEmail{
|
|
|
|
DeliveryType: &session.RequestChallenges_OTPEmail_ReturnCode_{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), resp.GetSessionToken(), resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0)
|
2023-08-24 11:41:52 +02:00
|
|
|
sessionToken = resp.GetSessionToken()
|
|
|
|
|
|
|
|
otp := resp.GetChallenges().GetOtpEmail()
|
|
|
|
require.NotEmpty(t, otp)
|
|
|
|
|
|
|
|
resp, err = Client.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: sessionToken,
|
|
|
|
Checks: &session.Checks{
|
|
|
|
OtpEmail: &session.CheckOTP{
|
2023-08-31 09:06:50 +02:00
|
|
|
Code: otp,
|
2023-08-24 11:41:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
sessionToken = resp.GetSessionToken()
|
2023-11-06 11:48:28 +02:00
|
|
|
verifyCurrentSession(t, createResp.GetSessionId(), sessionToken, resp.GetDetails().GetSequence(), time.Minute, nil, nil, 0, wantUserFactor, wantWebAuthNFactor, wantOTPEmailFactor)
|
2023-08-24 11:41:52 +02:00
|
|
|
})
|
2023-06-07 17:28:42 +02:00
|
|
|
}
|
2023-07-14 13:16:16 +02:00
|
|
|
|
2023-11-06 11:48:28 +02: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
|
|
|
|
sessionResp, err := Tester.Client.SessionV2.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: createResp.GetSessionToken(),
|
|
|
|
Lifetime: durationpb.New(20 * time.Second),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// ensure session expires and does not work anymore
|
|
|
|
time.Sleep(20 * time.Second)
|
|
|
|
_, err = Tester.Client.SessionV2.SetSession(CTX, &session.SetSessionRequest{
|
|
|
|
SessionId: createResp.GetSessionId(),
|
|
|
|
SessionToken: sessionResp.GetSessionToken(),
|
|
|
|
Lifetime: durationpb.New(20 * time.Second),
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2023-07-14 13:16:16 +02:00
|
|
|
func Test_ZITADEL_API_missing_authentication(t *testing.T) {
|
|
|
|
// create new, empty session
|
2023-08-11 18:36:18 +03:00
|
|
|
createResp, err := Client.CreateSession(CTX, &session.CreateSessionRequest{})
|
2023-07-14 13:16:16 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx := metadata.AppendToOutgoingContext(context.Background(), "Authorization", fmt.Sprintf("Bearer %s", createResp.GetSessionToken()))
|
|
|
|
sessionResp, err := Tester.Client.SessionV2.GetSession(ctx, &session.GetSessionRequest{SessionId: createResp.GetSessionId()})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, sessionResp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ZITADEL_API_missing_mfa(t *testing.T) {
|
|
|
|
id, token, _, _ := Tester.CreatePasswordSession(t, CTX, User.GetUserId(), integration.UserPassword)
|
|
|
|
|
2023-08-02 18:57:53 +02:00
|
|
|
ctx := Tester.WithAuthorizationToken(context.Background(), token)
|
2023-07-14 13:16:16 +02:00
|
|
|
sessionResp, err := Tester.Client.SessionV2.GetSession(ctx, &session.GetSessionRequest{SessionId: id})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, sessionResp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ZITADEL_API_success(t *testing.T) {
|
2023-11-06 11:48:28 +02:00
|
|
|
id, token, _, _ := Tester.CreateVerifiedWebAuthNSession(t, CTX, User.GetUserId())
|
2023-07-14 13:16:16 +02:00
|
|
|
|
2023-08-02 18:57:53 +02:00
|
|
|
ctx := Tester.WithAuthorizationToken(context.Background(), token)
|
2023-07-14 13:16:16 +02:00
|
|
|
sessionResp, err := Tester.Client.SessionV2.GetSession(ctx, &session.GetSessionRequest{SessionId: id})
|
|
|
|
require.NoError(t, err)
|
2023-08-11 18:36:18 +03:00
|
|
|
|
|
|
|
webAuthN := sessionResp.GetSession().GetFactors().GetWebAuthN()
|
|
|
|
require.NotNil(t, id, webAuthN.GetVerifiedAt().AsTime())
|
|
|
|
require.True(t, webAuthN.GetUserVerified())
|
2023-07-14 13:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ZITADEL_API_session_not_found(t *testing.T) {
|
2023-11-06 11:48:28 +02:00
|
|
|
id, token, _, _ := Tester.CreateVerifiedWebAuthNSession(t, CTX, User.GetUserId())
|
2023-07-14 13:16:16 +02:00
|
|
|
|
|
|
|
// test session token works
|
2023-08-02 18:57:53 +02:00
|
|
|
ctx := Tester.WithAuthorizationToken(context.Background(), token)
|
2023-07-14 13:16:16 +02:00
|
|
|
_, err := Tester.Client.SessionV2.GetSession(ctx, &session.GetSessionRequest{SessionId: id})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
//terminate the session and test it does not work anymore
|
|
|
|
_, err = Tester.Client.SessionV2.DeleteSession(CTX, &session.DeleteSessionRequest{
|
|
|
|
SessionId: id,
|
|
|
|
SessionToken: gu.Ptr(token),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-08-02 18:57:53 +02:00
|
|
|
ctx = Tester.WithAuthorizationToken(context.Background(), token)
|
2023-07-14 13:16:16 +02:00
|
|
|
_, err = Tester.Client.SessionV2.GetSession(ctx, &session.GetSessionRequest{SessionId: id})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
2023-11-06 11:48:28 +02: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)
|
|
|
|
_, err := Tester.Client.SessionV2.GetSession(ctx, &session.GetSessionRequest{SessionId: id})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// ensure session expires and does not work anymore
|
|
|
|
time.Sleep(20 * time.Second)
|
|
|
|
sessionResp, err := Tester.Client.SessionV2.GetSession(ctx, &session.GetSessionRequest{SessionId: id})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, sessionResp)
|
|
|
|
}
|