Skip to content

Commit

Permalink
refcator: move login hinting to own package
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Mar 7, 2022
1 parent 8de9d01 commit 1eb2604
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
31 changes: 31 additions & 0 deletions selfservice/flowhelpers/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package flowhelpers

import (
"github.com/ory/kratos/identity"
"github.com/ory/kratos/session"
"net/http"
)

// GuessForcedLoginIdentifier returns the identifier for login flows where the identity needs to refresh the session.
func GuessForcedLoginIdentifier(r *http.Request, d interface {
session.ManagementProvider
identity.PrivilegedPoolProvider
}, f interface {
IsForced() bool
}, ct identity.CredentialsType) (identifier string) {
// This block adds the identifier to the method when the request is forced - as a hint for the user.
if !f.IsForced() {
// do nothing
} else if sess, err := d.SessionManager().FetchFromRequest(r.Context(), r); err != nil {
// do nothing
} else if id, err := d.PrivilegedIdentityPool().GetIdentityConfidential(r.Context(), sess.IdentityID); err != nil {
// do nothing
} else if creds, ok := id.GetCredentials(ct); !ok {
// do nothing
} else if len(creds.Identifiers) == 0 {
// do nothing
} else {
identifier = creds.Identifiers[0]
}
return
}
40 changes: 40 additions & 0 deletions selfservice/flowhelpers/login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package flowhelpers_test

import (
"context"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/internal"
"github.com/ory/kratos/internal/testhelpers"
"github.com/ory/kratos/selfservice/flow/login"
"github.com/ory/kratos/selfservice/flowhelpers"
"github.com/ory/kratos/session"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http/httptest"
"testing"
"time"
)

func TestGuessForcedLoginIdentifier(t *testing.T) {
conf, reg := internal.NewFastRegistryWithMocks(t)
testhelpers.SetDefaultIdentitySchema(conf, "file://./stub/login.schema.json")

i := identity.NewIdentity("")
i.Credentials[identity.CredentialsTypePassword] = identity.Credentials{
Type: identity.CredentialsTypePassword,
Identifiers: []string{"foobar"},
}
require.NoError(t, reg.IdentityManager().Create(context.Background(), i))

sess, err := session.NewActiveSession(i, conf, time.Now(), identity.CredentialsTypePassword)
require.NoError(t, err)
reg.SessionPersister().UpsertSession(context.Background(), sess)

r := httptest.NewRequest("GET", "/login", nil)
r.Header.Set("Authorization", "Bearer "+sess.Token)

var f login.Flow
f.Refresh = true

assert.Equal(t, "foobar", flowhelpers.GuessForcedLoginIdentifier(r, reg, &f, identity.CredentialsTypePassword))
}
11 changes: 11 additions & 0 deletions selfservice/flowhelpers/stub/login.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$id": "https://example.com/person.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Person",
"type": "object",
"properties": {
"traits": {
"type": "object"
}
}
}

0 comments on commit 1eb2604

Please sign in to comment.