我想让 OpenID 连接在我的小 luminus 项目中工作。我对 luminus/ring/compojure 中的工作流程有点陌生(主要来自 django、flask 和 servlet)。我已经成功重定向到谷歌,所以我从谷歌得到了“代码”,但是我需要在登录用户之前再向谷歌发出一个请求,这个调用需要另一个用户没有参与的回调,所以我需要像承诺一样搁置用户的请求,但我不确定这部分在 compojure 中是如何工作的。
; this is my code that redirects them to Google, where they accept
(defn login [params]
(let [google-oauth2-client-id (System/getenv "GOOGLE_OAUTH2_CLIENT_ID")
base-url "https://accounts.google.com/o/oauth2/auth"
args {"client_id" google-oauth2-client-id
"response_type" "code"
"scope" "openid email"
"redirect_uri" "http://localhost:3000/oauth2Callback"
"state" "anti-forgery here"}]
(assert google-oauth2-client-id "can't find GOOGLE_OAUTH2_CLIENT_ID in environment")
(redirect (str base-url "?" (make-query-string args)))
)
)
; this is my code handling Google's first response
(defn oauth2-callback [params]
; params has the code to send to Google
; here I should send another request to google that comes back to another callback like oauth2-token-callback that processes the request to the user in the current context
(redirect "/youreloggedin")
)
在此方法结束时,我应该向用户发送一条消息,说明他们已登录,但我需要等到请求返回。这个工作流程在 luminus 中是如何处理的?
解决了。我没有意识到我可以忽略回调参数。
(client/post "https://www.googleapis.com/oauth2/v3/token"
{:headers {"X-Api-Version" "2"}
:content-type :application/x-www-form-urlencoded
:form-params {:code (params :code)
:client_id (System/getenv "GOOGLE_OAUTH2_CLIENT_ID")
:client_secret (System/getenv "GOOGLE_OAUTH2_CLIENT_SECRET")
:redirect_uri "http://localhost:3000/oauth2Callback" ; ignored
:grant_type "authorization_code"
}
:as :auto ; decode the body straight to hash (if possible)
})