enhance: add login fn for e2e test (#8214)

* enhance: add login fn for e2e test
* expose login-with-username-password-e2e fn to js/window
* export login-with-username-password-e2e
* require sha256&hmac
* update login-with-username-password-e2e
This commit is contained in:
rcmerci
2023-01-04 23:12:11 +08:00
committed by GitHub
parent 84889f59e4
commit ea71240a4f
2 changed files with 34 additions and 3 deletions

View File

@@ -9,7 +9,10 @@
[cljs-time.core :as t]
[cljs-time.coerce :as tc]
[cljs-http.client :as http]
[cljs.core.async :as async :refer [go <!]]))
[cljs.core.async :as async :refer [go <!]]
[goog.crypt.Sha256]
[goog.crypt.Hmac]
[goog.crypt :as crypt]))
(defn set-preferred-format!
[format]
@@ -155,6 +158,32 @@
(#(state/pub-event! [:user/fetch-info-and-graphs])))
(debug/pprint "login-callback" resp)))))
(defn ^:export login-with-username-password-e2e
[username password client-id client-secret]
(let [text-encoder (new js/TextEncoder)
key (.encode text-encoder client-secret)
hasher (new crypt/Sha256)
hmacer (new crypt/Hmac hasher key)
secret-hash (.encodeByteArray ^js crypt/base64 (.getHmac hmacer (str username client-id)))
payload {"AuthParameters" {"USERNAME" username,
"PASSWORD" password,
"SECRET_HASH" secret-hash}
"AuthFlow" "USER_PASSWORD_AUTH",
"ClientId" client-id}
headers {"X-Amz-Target" "AWSCognitoIdentityProviderService.InitiateAuth",
"Content-Type" "application/x-amz-json-1.1"}]
(go
(let [resp (<! (http/post config/COGNITO-IDP {:headers headers
:body (js/JSON.stringify (clj->js payload))}))]
(assert (= 200 (:status resp)))
(let [body (js->clj (js/JSON.parse (:body resp)))
access-token (get-in body ["AuthenticationResult" "AccessToken"])
id-token (get-in body ["AuthenticationResult" "IdToken"])
refresh-token (get-in body ["AuthenticationResult" "RefreshToken"])]
(set-tokens! id-token access-token refresh-token)
(state/pub-event! [:user/fetch-info-and-graphs])
{:id-token id-token :access-token access-token :refresh-token refresh-token})))))
(defn logout []
(clear-tokens)
(state/pub-event! [:user/logout]))