Explorar o código

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
rcmerci %!s(int64=2) %!d(string=hai) anos
pai
achega
ea71240a4f
Modificáronse 2 ficheiros con 34 adicións e 3 borrados
  1. 4 2
      src/main/frontend/config.cljs
  2. 30 1
      src/main/frontend/handler/user.cljs

+ 4 - 2
src/main/frontend/config.cljs

@@ -31,13 +31,15 @@
       (def LOGIN-URL
         "https://logseq-prod.auth.us-east-1.amazoncognito.com/login?client_id=3c7np6bjtb4r1k1bi9i049ops5&response_type=code&scope=email+openid+phone&redirect_uri=logseq%3A%2F%2Fauth-callback")
       (def API-DOMAIN "api.logseq.com")
-      (def WS-URL "wss://ws.logseq.com/file-sync?graphuuid=%s"))
+      (def WS-URL "wss://ws.logseq.com/file-sync?graphuuid=%s")
+      (def COGNITO-IDP "https://cognito-idp.us-east-1.amazonaws.com/"))
 
   (do (def FILE-SYNC-PROD? false)
       (def LOGIN-URL
         "https://logseq-test2.auth.us-east-2.amazoncognito.com/login?client_id=3ji1a0059hspovjq5fhed3uil8&response_type=code&scope=email+openid+phone&redirect_uri=logseq%3A%2F%2Fauth-callback")
       (def API-DOMAIN "api-dev.logseq.com")
-      (def WS-URL "wss://ws-dev.logseq.com/file-sync?graphuuid=%s")))
+      (def WS-URL "wss://ws-dev.logseq.com/file-sync?graphuuid=%s")
+      (def COGNITO-IDP "https://cognito-idp.us-east-2.amazonaws.com/")))
 
 ;; Feature flags
 ;; =============

+ 30 - 1
src/main/frontend/handler/user.cljs

@@ -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]))