Skip to content

Commit

Permalink
fix: reduce lookups in whoami call
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Jul 5, 2023
1 parent 567e5a7 commit 3b593b3
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 12 deletions.
1 change: 0 additions & 1 deletion identity/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const (
NoAuthenticatorAssuranceLevel AuthenticatorAssuranceLevel = "aal0"
AuthenticatorAssuranceLevel1 AuthenticatorAssuranceLevel = "aal1"
AuthenticatorAssuranceLevel2 AuthenticatorAssuranceLevel = "aal2"
AuthenticatorAssuranceLevel3 AuthenticatorAssuranceLevel = "aal3"
)

// CredentialsType represents several different credential types, like password credentials, passwordless credentials,
Expand Down
2 changes: 0 additions & 2 deletions identity/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ func TestCredentialsEqual(t *testing.T) {
func TestAALOrder(t *testing.T) {
assert.True(t, NoAuthenticatorAssuranceLevel < AuthenticatorAssuranceLevel1)
assert.True(t, AuthenticatorAssuranceLevel1 < AuthenticatorAssuranceLevel2)
assert.True(t, AuthenticatorAssuranceLevel1 < AuthenticatorAssuranceLevel3)
assert.True(t, AuthenticatorAssuranceLevel2 < AuthenticatorAssuranceLevel3)
}

func TestParseCredentialsType(t *testing.T) {
Expand Down
12 changes: 5 additions & 7 deletions session/manager_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,6 @@ func (s *ManagerHTTP) FetchFromRequest(ctx context.Context, r *http.Request) (_
}

expand := identity.ExpandDefault
if s.r.Config().SessionWhoAmIAAL(r.Context()) == config.HighestAvailableAAL {
// When the session endpoint requires the highest AAL, we fetch all credentials immediately to save a
// query later in "DoesSessionSatisfy". This is a SQL optimization, because the identity manager fetches
// the data in parallel, which is a bit faster than fetching it in sequence.
expand = identity.ExpandEverything
}

se, err := s.r.SessionPersister().GetSessionByToken(ctx, token, ExpandEverything, expand)
if err != nil {
if errors.Is(err, herodot.ErrNotFound) || errors.Is(err, sqlcon.ErrNoRows) {
Expand Down Expand Up @@ -277,6 +270,11 @@ func (s *ManagerHTTP) DoesSessionSatisfy(r *http.Request, sess *Session, request
ctx, span := s.r.Tracer(r.Context()).Tracer().Start(r.Context(), "sessions.ManagerHTTP.DoesSessionSatisfy")
defer otelx.End(span, &err)

// If we already have AAL2 there is no need to check further because it is the highest AAL.
if sess.AuthenticatorAssuranceLevel > identity.AuthenticatorAssuranceLevel1 {
return nil
}

managerOpts := &options{}

for _, o := range opts {
Expand Down
4 changes: 2 additions & 2 deletions session/test/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ func TestPersister(ctx context.Context, conf *config.Config, p interface {
})

t.Run("case=update session", func(t *testing.T) {
expected.AuthenticatorAssuranceLevel = identity.AuthenticatorAssuranceLevel3
expected.AuthenticatorAssuranceLevel = identity.AuthenticatorAssuranceLevel1
require.NoError(t, p.UpsertSession(ctx, &expected))

actual, err := p.GetSessionByToken(ctx, expected.Token, session.ExpandDefault, identity.ExpandDefault)
check(actual, err)
assert.Equal(t, identity.AuthenticatorAssuranceLevel3, actual.AuthenticatorAssuranceLevel)
assert.Equal(t, identity.AuthenticatorAssuranceLevel1, actual.AuthenticatorAssuranceLevel)
})

t.Run("case=remove amr and update", func(t *testing.T) {
Expand Down

0 comments on commit 3b593b3

Please sign in to comment.