Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

OpenID Implementation #194

Open
wants to merge 19 commits into
from

Conversation

Projects
None yet
2 participants

This branch implements OpenID for 4clojure, affecting issue #33. The
OpenID library used is Stephen Lindsay's clj-openid, which is
available here, but
unfortunately is not yet on clojars.

I tried to keep things as backwards-compatible as possible, so the
code adds a bit of complexity.

There are two ways that OpenIDs can be used: most simply, a user can
log in with an OpenID. In this case, the only thing identifying the
user is the OpenID URL (i.e., there is no email or username).
Alternatively, a user with an existing username can associate an
OpenID with the username. In that case, the user can log in using the
OpenID, but in all other respects should have the same experience as
if he or she had logged in the normal way.

There are a few known issues with this code. First, I have not
thought a lot about the implications of what happens if two users have
access to the same OpenID. This might allow the second user to
"steal" the first user's account and solutions. It seems to me that
this problem is on par with a user giving his or her password away
(i.e., it's more the user's problem than ours). The second issue is
that OpenID has methods for requesting users' information (such as
avatar, email, etc.), but those do not seem to be exposed by
clj-openid, so can not be used by us. One final known issue is that I
got tired of trying to figure out how to use destructuring on the
request parameters in the openid-callback route (at the end of
login.clj) and just got the needed parameters "by hand". This could
be cleaned up by someone who uses destructuring more than I do.

This code has been tested to an extent -- both logging in only with an
OpenID and by associating an OpenID with a normal account -- but
should obviously be tested at a larger scale before being put into
production.

chrismgray added some commits Dec 13, 2011

Minimal OpenID support.
This doesn't actually add the user to the database yet, but it does
validate that the user's OpenID is good.
More refined success and failure states.
Success on receiving an OpenID now mimics the behavior of receiving a
valid username and password.
Allow people with current accounts to associate an OpenID
This is a rather large update that allows users with existing accounts
to associate an OpenID with their current account.  When a user logs
in, the :user returned by session/get-session is now a map, but this
is made backwards-compatible by most of the utility functions
accepting either a string or a map.
Fixed some bugs related to user-attribute confusion
(user-attribute :foo) does not return a map, it just returns the attribute.
URLEncode and URLDecode openids
If the user only has an OpenID, then we need to URLEncode the link to
his or her profile page
Simplify mark-completed
mark-completed is never called with the optional user variable, so I
got rid of it

The optional user was to make it possible to experiment with mark-completed at the repl. We haven't done this for some time now that mark-completed is actually working, but it's still nice to have a more-explicit (or, less "action at a distance") version. Ideally it would always take a user and be side-effect free, but that's a pretty long-term goal.

@amalloy amalloy commented on an outdated diff Dec 14, 2011

src/foreclojure/problems.clj
@@ -121,9 +121,7 @@
(maybe-update [old-score] dec)))))))))
(defn store-completed-state! [username problem-id code]
- (let [{user-id :_id} (fetch-one :users
- :where {:user username}
- :only [:_id])
+ (let [user-id ((user-attribute :_id) username)
current-time (java.util.Date.)]
@amalloy

amalloy Dec 14, 2011

Owner

The idea with user-attribute is that you'd just (def user-id (user-attribute :_id)) once, and then later you can just call (user-id some-user-name). Doing it in-place all over is weird; it actually shouldn't even have been made public.

@amalloy amalloy commented on an outdated diff Dec 14, 2011

src/foreclojure/problems.clj
@@ -136,8 +134,8 @@
(record-golf-score! user-id problem-id (code-length code))
(save-solution user-id problem-id code)))
-(defn mark-completed [problem code & [user]]
- (let [user (or user (session/session-get :user))
+(defn mark-completed [problem code]
+ (let [user (session/session-get :user)
@amalloy

amalloy Dec 14, 2011

Owner

As I mentioned separately (on this commit, but not the pull request, I guess?), I don't want to remove the optional user argument.

@amalloy amalloy commented on an outdated diff Dec 14, 2011

src/foreclojure/settings.clj
@@ -42,8 +43,14 @@
"Hide my solutions"]
[:br]))
+(defn assoc-openid-box [openid]
+ (list
+ [:p "Associate an OpenID with your account and you can log in with that in the future. Other settings will not change if this field is changed."]
+ (map form-row
+ [[text-field :openid "OpenID" openid]])))
@amalloy

amalloy Dec 14, 2011

Owner

No need to map over a list of a single element. You can just write (list [:p ...] (form-row [text-field ...])).

@amalloy amalloy commented on an outdated diff Dec 14, 2011

src/foreclojure/users.clj
@@ -39,7 +36,7 @@
(first
(reduce (fn [[user-list position rank] new-group]
[(into user-list
- (for [user (sort-by :user new-group)]
+ (for [user (sort-by #(or (:user %) (:openid %)) new-group)]
@amalloy

amalloy Dec 14, 2011

Owner

Looks like #(or (:user %) (:openid %)) needs to be factored out into something like generic-userid.

@amalloy amalloy commented on an outdated diff Dec 14, 2011

src/foreclojure/users.clj
@@ -65,7 +62,7 @@
(true? (:hide-solutions user)))
(defn email-address [username]
- (:email (fetch-one :users :where {:user username})))
+ ((user-attribute :email) username))
@amalloy

amalloy Dec 14, 2011

Owner

Hah, good catch on this one. Belongs in a separate patch, really, and of course shouldn't be using user-attribute inline as I mention above, but definitely the existing implementation is lame.

@amalloy amalloy commented on the diff Dec 14, 2011

src/foreclojure/users.clj
@@ -122,14 +119,15 @@
[:th {:style "width: 200px;"} "Username"]
[:th {:style "width: 180px;"} "Problems Solved"]
[:th "Following"]]]
- (map-indexed (fn [rownum {:keys [_id email position rank user contributor solved]}]
- [:tr (row-class rownum)
- [:td (rank-class position) rank]
- [:td
- (gravatar-img {:email email :class "gravatar"})
- [:a.user-profile-link {:href (str "/user/" user)} user (when contributor [:span.contributor " *"])]]
- [:td.centered (count solved)]
- [:td (following-checkbox user-id following _id user)]])
+ (map-indexed (fn [rownum {:keys [_id email position rank user openid contributor solved]}]
+ (let [user (or user openid)]
@amalloy

amalloy Dec 14, 2011

Owner

This should use the generic-userid (mentioned above) on the thing it's mapping over.

@amalloy amalloy and 1 other commented on an outdated diff Dec 14, 2011

src/foreclojure/users.clj
@@ -191,7 +189,7 @@
(filter ids (get-solved username)))))
(def-page user-profile [username]
- (let [page-title (str "User: " username)
+ (let [page-title (str "User: " (if (string? username) username (:openid username)))
@amalloy

amalloy Dec 14, 2011

Owner

I've seen this pattern a couple times in the patch, and it makes me a little nervous. Shouldn't we know whether we're being given a map or a string?

@chrismgray

chrismgray Dec 14, 2011

Good point, we should just pass a map and use the generic-openid fn you suggested above.

@amalloy amalloy and 1 other commented on an outdated diff Dec 14, 2011

src/foreclojure/users.clj
@@ -291,14 +289,23 @@
:iTotalDisplayRecords (str (count filtered-users))
:aaData page-users}))
+(defn get-user-possible-openid [username]
+ (let [decoded-username (URLDecoder/decode username)]
+ (cond (get-user username) ; registered users don't need decoding
+ username
+ (get-user {:openid decoded-username})
+ {:openid decoded-username}
+ :else
+ nil)))
@amalloy

amalloy Dec 14, 2011

Owner

:else nil isn't really necessary, so I'd remove it, but if you think it somehow improves clarity I am happy to be convinced.

@chrismgray

chrismgray Dec 14, 2011

The only reason I put it in is because we have a (if (nil? (get-user-possible-openid username))) down below.

@amalloy amalloy commented on an outdated diff Dec 14, 2011

src/foreclojure/utils.clj
+ (cond
+ (string? username)
+ (attr (from-mongo
+ (fetch-one :users
+ :where {:user username}
+ :only [attr])))
+ (and (map? username) (:user username))
+ (attr (from-mongo
+ (fetch-one :users
+ :where {:user (:user username)}
+ :only [attr])))
+ (and (map? username) (:openid username))
+ (attr (from-mongo
+ (fetch-one :users
+ :where {:openid (:openid username)}
+ :only [attr]))))))
@amalloy

amalloy Dec 14, 2011

Owner

These last two functions can definitely be cleaned up into a single call to from-mongo with only the :where clause varying according to the cond expression.

chrismgray added some commits Dec 14, 2011

New function user-or-openid.
Factors out #(or (:user %) (:openid %)) pattern.
Refactored get-user and user-attribute
Put the cond inside the where clause of the database fetch.  The one
thing I don't like about this is that if these functions are passed an
invalid object (such as a map without :user or :openid), then we will
have a situation where we do (fetch-one :users :where nil), which will
incorrectly return a result.  Perhaps we should add an :else to these
conds which looks for something we know not to be in the database.

The latest commits to this branch address the concerns that @amalloy had.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment