Skip to content

Instantly share code, notes, and snippets.

@weavejester
Created June 10, 2009 19:56
Show Gist options
  • Save weavejester/127459 to your computer and use it in GitHub Desktop.
Save weavejester/127459 to your computer and use it in GitHub Desktop.
(ns compojure.openid
"Compojure OpenID wrapper around jopenid library."
(:use compojure.control)
(:use compojure.encodings)
(:use compojure.http)
(:use compojure.http.session)
(:import com.javaeedev.openid.OpenIdManager)
(:import javax.servlet.http.HttpServletRequest))
(defn- make-manager
[options]
(doto (OpenIdManager.)
(.setReturnTo (options :return-to))
(.setRealm (options :realm))
(.setTimeOut (options :timeout 10000))))
(defn- openid-lookup
[manager openid-url]
(let [endpoint (.lookupEndpoint manager openid-url)
association (.lookupAssociation manager endpoint)
auth-url (.getAuthenticationUrl manager endpoint association)]
[(session-assoc :openid-mac (.getMacKey association))
(redirect-to auth-url)]))
(defn- request-proxy
[params]
(proxy [HttpServletRequest] []
(getParameter [key]
(params (keyword key)))))
(defn- auth->map
[auth]
{:identity (.getIdentity auth)
:email (.getEmail auth)})
(defn- set-session-auth
[manager session params]
(let [mac-key (base64-decode-bytes (session :openid-mac))
request (request-proxy params)
auth (.getAuthentication manager request mac-key)]
(session-assoc :openid (auth->map auth))))
(defn openid-auth
[path options]
(let [manager (make-manager options)]
(routes
(POST path
(openid-lookup manager (params :openid)))
(GET (options :return-to)
[(set-session-auth manager session params)
(redirect-to (options :success-uri))]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment