applicake/doorkeeper — pull request #36 by carvil (2012-01-10 12:04:26+00:00)
Title: Remove repetitions from the Authorised Applications view

Hi guys,

I was having a number of repeated applications being shown on the authorised applications list (/oauth/authorized_applications) because Application.authorized_for doesn't return the unique list of authorisations.

Assuming there's a client application already registered on the provider, here is the flow:
1) User creates account on the provider
2) User tries to login on a client application
3) User is redirected to the provider, where he/she login (if not logged in already, otherwise skips the step)
4) User authorise the application to access the private data
5) User gets redirected to the client app with a valid token.

Now if the user leaves the client application page and later on tries to login again, then steps 2, 3 and 5 happen. If the user now goes to the provider and tries to access /oauth/authorized_applications, there are 2 entries even though they represent the same authorisation...

Here is the problem:

1) Get the resource owner:

ruby-1.9.3-p0 :031 > resource_owner = User.find(14) User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 14]]
=> \#\

2) Fetch the authorisations for the given resource owner:

ruby-1.9.3-p0 :032 > authorized_apps = Application.authorized_for(resource_owner)
Application Load (0.2ms) SELECT "oauth_applications".* FROM "oauth_applications" INNER JOIN "oauth_access_tokens" ON "oauth_access_tokens"."application_id" = "oauth_applications"."id" AND "oauth_access_tokens"."revoked_at" IS NULL INNER JOIN "oauth_applications" "authorized_applications_oauth_applications" ON "authorized_applications_oauth_applications"."id" = "oauth_access_tokens"."application_id" WHERE "oauth_access_tokens"."resource_owner_id" = 14
=> [\#\, \#\]

3) Get unique authorisations

ruby-1.9.3-p0 :033 > authorized_apps.uniq
=> [\#\]

What do you think? Have you encountered this problem?

Thanks,
Carlos