-
-
Notifications
You must be signed in to change notification settings - Fork 963
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refcator: move login hinting to own package
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |