Skip to content

Commit

Permalink
consent: Show all granted consent requests (#1206)
Browse files Browse the repository at this point in the history
Instead of just showing consent requests which have remember set to true, show all past consent request.

Closes #1203

Signed-off-by: aeneasr <aeneas@ory.sh>
  • Loading branch information
aeneasr authored Dec 7, 2018
1 parent 14ecdf7 commit f54448c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
2 changes: 1 addition & 1 deletion consent/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (h *Handler) GetConsentSessions(w http.ResponseWriter, r *http.Request, ps
}

limit, offset := pagination.Parse(r, 100, 0, 500)
s, err := h.M.FindPreviouslyGrantedConsentRequestsByUser(r.Context(), user, limit, offset)
s, err := h.M.FindSubjectsGrantedConsentRequests(r.Context(), user, limit, offset)
if errors.Cause(err) == ErrNoPreviousConsentFound {
h.H.Write(w, r, []PreviousConsentSession{})
return
Expand Down
4 changes: 2 additions & 2 deletions consent/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ type Manager interface {
RevokeUserClientConsentSession(ctx context.Context, user, client string) error

VerifyAndInvalidateConsentRequest(ctx context.Context, verifier string) (*HandledConsentRequest, error)
FindPreviouslyGrantedConsentRequests(ctx context.Context, client, user string) ([]HandledConsentRequest, error)
FindPreviouslyGrantedConsentRequestsByUser(ctx context.Context, user string, limit, offset int) ([]HandledConsentRequest, error)
FindGrantedAndRememberedConsentRequests(ctx context.Context, client, user string) ([]HandledConsentRequest, error)
FindSubjectsGrantedConsentRequests(ctx context.Context, user string, limit, offset int) ([]HandledConsentRequest, error)

// Cookie management
GetAuthenticationSession(ctx context.Context, id string) (*AuthenticationSession, error)
Expand Down
50 changes: 35 additions & 15 deletions consent/manager_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,19 +205,43 @@ func (m *MemoryManager) VerifyAndInvalidateConsentRequest(ctx context.Context, v
return nil, errors.WithStack(pkg.ErrNotFound)
}

func (m *MemoryManager) FindPreviouslyGrantedConsentRequests(ctx context.Context, client, subject string) ([]HandledConsentRequest, error) {
func (m *MemoryManager) FindGrantedAndRememberedConsentRequests(ctx context.Context, client, subject string) ([]HandledConsentRequest, error) {
var rs []HandledConsentRequest
filteredByUser, err := m.FindPreviouslyGrantedConsentRequestsByUser(ctx, subject, -1, -1)
if errors.Cause(err) == pkg.ErrNotFound {
return nil, errors.WithStack(ErrNoPreviousConsentFound)
} else if err != nil {
return nil, err
}
for _, c := range m.handledConsentRequests {
cr, err := m.GetConsentRequest(ctx, c.Challenge)
if errors.Cause(err) == pkg.ErrNotFound {
return nil, errors.WithStack(ErrNoPreviousConsentFound)
} else if err != nil {
return nil, err
}

if subject != cr.Subject {
continue
}

for _, c := range filteredByUser {
if client == c.ConsentRequest.Client.GetID() {
rs = append(rs, c)
if client != cr.Client.GetID() {
continue
}

if c.Error != nil {
continue
}

if !c.Remember {
continue
}

if cr.Skip {
continue
}

if c.RememberFor > 0 && c.RequestedAt.Add(time.Duration(c.RememberFor)*time.Second).Before(time.Now().UTC()) {
continue
}

cr.Client.ClientID = cr.Client.GetID()
c.ConsentRequest = cr
rs = append(rs, c)
}

if len(rs) == 0 {
Expand All @@ -227,7 +251,7 @@ func (m *MemoryManager) FindPreviouslyGrantedConsentRequests(ctx context.Context
return rs, nil
}

func (m *MemoryManager) FindPreviouslyGrantedConsentRequestsByUser(ctx context.Context, subject string, limit, offset int) ([]HandledConsentRequest, error) {
func (m *MemoryManager) FindSubjectsGrantedConsentRequests(ctx context.Context, subject string, limit, offset int) ([]HandledConsentRequest, error) {
var rs []HandledConsentRequest
for _, c := range m.handledConsentRequests {
cr, err := m.GetConsentRequest(ctx, c.Challenge)
Expand All @@ -243,10 +267,6 @@ func (m *MemoryManager) FindPreviouslyGrantedConsentRequestsByUser(ctx context.C
continue
}

if !c.Remember {
continue
}

if cr.Skip {
continue
}
Expand Down
6 changes: 3 additions & 3 deletions consent/manager_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func (m *SQLManager) DeleteAuthenticationSession(ctx context.Context, id string)
return nil
}

func (m *SQLManager) FindPreviouslyGrantedConsentRequests(ctx context.Context, client, subject string) ([]HandledConsentRequest, error) {
func (m *SQLManager) FindGrantedAndRememberedConsentRequests(ctx context.Context, client, subject string) ([]HandledConsentRequest, error) {
var a []sqlHandledConsentRequest

if err := m.DB.SelectContext(ctx, &a, m.DB.Rebind(`SELECT h.* FROM
Expand All @@ -424,7 +424,7 @@ LIMIT 1`), subject, client); err != nil {
return m.resolveHandledConsentRequests(ctx, a)
}

func (m *SQLManager) FindPreviouslyGrantedConsentRequestsByUser(ctx context.Context, subject string, limit, offset int) ([]HandledConsentRequest, error) {
func (m *SQLManager) FindSubjectsGrantedConsentRequests(ctx context.Context, subject string, limit, offset int) ([]HandledConsentRequest, error) {
var a []sqlHandledConsentRequest

if err := m.DB.SelectContext(ctx, &a, m.DB.Rebind(`SELECT h.* FROM
Expand All @@ -434,7 +434,7 @@ JOIN
WHERE
r.subject=? AND r.skip=FALSE
AND
(h.error='{}' AND h.remember=TRUE)
(h.error='{}')
ORDER BY h.requested_at DESC
LIMIT ? OFFSET ?
`), subject, limit, offset); err != nil {
Expand Down
9 changes: 7 additions & 2 deletions consent/manager_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func ManagerTests(m Manager, clientManager client.Manager, fositeManager pkg.Fos
{"6", "6", 0},
} {
t.Run("key="+tc.keyC+"-"+tc.keyS, func(t *testing.T) {
rs, err := m.FindPreviouslyGrantedConsentRequests(context.TODO(), "fk-client-"+tc.keyC, "subject"+tc.keyS)
rs, err := m.FindGrantedAndRememberedConsentRequests(context.TODO(), "fk-client-"+tc.keyC, "subject"+tc.keyS)
if tc.expectedLength == 0 {
assert.EqualError(t, err, ErrNoPreviousConsentFound.Error())
} else {
Expand Down Expand Up @@ -470,12 +470,17 @@ func ManagerTests(m Manager, clientManager client.Manager, fositeManager pkg.Fos
},
{
subject: "subjectrv2",
challenges: []string{"challengerv2"},
clients: []string{"fk-client-rv2"},
},
{
subject: "subjectrv3",
challenges: []string{},
clients: []string{},
},
} {
t.Run(fmt.Sprintf("case=%d/subject=%s", i, tc.subject), func(t *testing.T) {
consents, err := m.FindPreviouslyGrantedConsentRequestsByUser(context.TODO(), tc.subject, 100, 0)
consents, err := m.FindSubjectsGrantedConsentRequests(context.TODO(), tc.subject, 100, 0)
assert.Equal(t, len(tc.challenges), len(consents))

if len(tc.challenges) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion consent/strategy_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ func (s *DefaultStrategy) requestConsent(w http.ResponseWriter, r *http.Request,
// return s.forwardConsentRequest(w, r, ar, authenticationSession, nil)
// }

consentSessions, err := s.M.FindPreviouslyGrantedConsentRequests(r.Context(), ar.GetClient().GetID(), authenticationSession.Subject)
consentSessions, err := s.M.FindGrantedAndRememberedConsentRequests(r.Context(), ar.GetClient().GetID(), authenticationSession.Subject)
if errors.Cause(err) == ErrNoPreviousConsentFound {
return s.forwardConsentRequest(w, r, ar, authenticationSession, nil)
} else if err != nil {
Expand Down

0 comments on commit f54448c

Please sign in to comment.