Skip to content

Commit 263498b

Browse files
committed
wip
1 parent 049c0b5 commit 263498b

File tree

4 files changed

+439
-184
lines changed

4 files changed

+439
-184
lines changed

core/api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
"pino-pretty": "^10.3.1",
183183
"prettier": "^3.2.5",
184184
"protoc-gen-js": "^3.21.2",
185+
"puppeteer": "^22.7.1",
185186
"react": "^18.2.0",
186187
"spectaql": "^2.3.1",
187188
"tiny-secp256k1": "^2.2.3",
Lines changed: 92 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,120 @@
11
import { consentList } from "@/services/hydra"
2-
import axios from "axios"
2+
import { sleep } from "@/utils"
3+
import puppeteer from "puppeteer"
34

45
import { createUserAndWalletFromPhone, getUserIdByPhone, randomPhone } from "test/helpers"
56

67
let userId: UserId
78
const phone = randomPhone()
89
// const phone = "+14152991378" as PhoneNumber
910

10-
const redirectUri = "http://localhost/callback"
11-
const scope = "offline read write"
12-
const grant_types = ["authorization_code", "refresh_token"]
13-
1411
beforeAll(async () => {
1512
await createUserAndWalletFromPhone(phone)
1613
userId = await getUserIdByPhone(phone)
1714
})
1815

19-
async function createOAuthClient() {
20-
const hydraAdminUrl = "http://localhost:4445/admin/clients"
16+
async function performOAuthLogin() {
17+
// create oauth2 client
18+
19+
const browser = await puppeteer.launch()
20+
// const browser = await puppeteer.launch({ headless: true })
21+
const page = await browser.newPage()
22+
23+
// Navigate the page to a URL
24+
await page.goto("http://localhost:3001/api/auth/signin")
25+
26+
await page.waitForSelector(".button")
27+
28+
// Click the button with the text "Sign in with Blink"
29+
await page.click(".button")
30+
31+
console.log("New URL:", page.url())
32+
33+
await page.waitForSelector('[data-testid="sign_in_with_phone_btn"]')
34+
await page.waitForFunction(
35+
"document.querySelector(\"[data-testid='sign_in_with_phone_btn']\").isConnected",
36+
)
37+
await sleep(500)
38+
await page.click('[data-testid="sign_in_with_phone_btn"]')
39+
40+
await page.waitForSelector('[data-testid="phone_number_input"]')
41+
await page.type('[data-testid="phone_number_input"]', phone)
42+
43+
console.log("phone", phone)
44+
45+
await page.click("#accept")
46+
47+
console.log("New URL:", page.url())
48+
49+
await sleep(500)
50+
51+
await page.screenshot({ path: "screenshot1.png" })
52+
53+
page.on("request", (request) => {
54+
console.log(request.url())
55+
})
56+
57+
page.on("response", (response) => {
58+
console.log(response.url())
59+
})
60+
61+
await page.waitForSelector("#code")
62+
await page.type("#code", "000000", { delay: 100 })
63+
64+
await sleep(3000)
65+
await page.screenshot({ path: "screenshot2.png" })
2166

2267
try {
23-
const response = await axios.post(hydraAdminUrl, {
24-
client_name: "integration_test",
25-
grant_types,
26-
response_types: ["code", "id_token"],
27-
redirect_uris: [redirectUri],
28-
scope,
29-
skip_consent: true,
30-
})
31-
32-
const clientId = response.data.client_id
33-
const clientSecret = response.data.client_secret
34-
35-
return { clientId, clientSecret }
36-
} catch (error) {
37-
console.error("Error creating OAuth client:", error.response)
38-
}
39-
}
68+
await page.click('[data-testid="verification_code_submit_btn"]')
69+
} catch {}
4070

41-
async function performOAuthLogin({
42-
clientId,
43-
clientSecret,
44-
}: {
45-
clientId: string
46-
clientSecret: string
47-
}) {
48-
// create oauth2 client
71+
await page.screenshot({ path: "screenshot3.png" })
72+
await sleep(3000)
4973

50-
const responseType = "code"
51-
const randomState = "MKfNw-q60talMJ4GU_h1kHFvcPtnQkZI0XLpTkHvJL4"
74+
await page.screenshot({ path: "screenshot4.png" })
75+
await sleep(3000)
76+
await page.screenshot({ path: "screenshot5.png" })
5277

53-
const authUrl = `http://localhost:4444/oauth2/auth?response_type=${responseType}&client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${encodeURIComponent(scope)}&state=${randomState}`
78+
// const authUrl = `http://localhost:4444/oauth2/auth?response_type=${responseType}&client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${encodeURIComponent(scope)}&state=${randomState}`
5479

5580
// https://oauth.blink.sv/oauth2/auth?client_id=73ae7c3e-e526-412a-856c-25d1ae0cbc55&scope=read%20write&response_type=code&redirect_uri=https%3A%2F%2Fdashboard.blink.sv%2Fapi%2Fauth%2Fcallback%2Fblink&state=MKfNw-q60talMJ4GU_h1kHFvcPtnQkZI0XLpTkHvJL4
5681

5782
// Simulate user going to the authorization URL and logging in
5883
// This part would require a real user interaction or a browser automation tool like puppeteer
5984

60-
let data
61-
try {
62-
const res = await axios.get(authUrl)
63-
data = res.data
64-
} catch (error) {
65-
console.error("Error getting auth URL:", error)
66-
return
67-
}
85+
// let data
86+
// try {
87+
// const res = await axios.get(authUrl)
88+
// data = res.data
89+
// } catch (error) {
90+
// console.error("Error getting auth URL:", error)
91+
// return
92+
// }
6893

6994
// You need to extract the code from the callback response
70-
const code = data.code // Simplified: Actual extraction depends on your OAuth provider
71-
72-
console.log("data", data)
73-
console.log("code", code)
74-
75-
try {
76-
// Exchange the code for a token
77-
const tokenResponse = await axios.post("http://localhost:4444/oauth2/token", {
78-
code,
79-
redirect_uri: redirectUri,
80-
client_id: clientId,
81-
client_secret: clientSecret,
82-
grant_type: "authorization_code",
83-
})
84-
85-
const accessToken = tokenResponse.data.access_token
86-
87-
// Use the access token to get user info or other secured resources
88-
// Update the consent list as needed
89-
return accessToken // This might be used for further secured requests
90-
} catch (error) {
91-
console.error("Error exchanging code for token:", error)
92-
}
95+
// const code = data.code // Simplified: Actual extraction depends on your OAuth provider
96+
97+
// console.log("data", data)
98+
// console.log("code", code)
99+
100+
// try {
101+
// // Exchange the code for a token
102+
// const tokenResponse = await axios.post("http://localhost:4444/oauth2/token", {
103+
// code,
104+
// redirect_uri: redirectUri,
105+
// client_id: clientId,
106+
// client_secret: clientSecret,
107+
// grant_type: "authorization_code",
108+
// })
109+
110+
// const accessToken = tokenResponse.data.access_token
111+
112+
// // Use the access token to get user info or other secured resources
113+
// // Update the consent list as needed
114+
// return accessToken // This might be used for further secured requests
115+
// } catch (error) {
116+
// console.error("Error exchanging code for token:", error)
117+
// }
93118
}
94119

95120
describe("Hydra", () => {
@@ -99,12 +124,7 @@ describe("Hydra", () => {
99124
})
100125

101126
it("get consent list when the user had perform oauth2 login", async () => {
102-
const res = await createOAuthClient()
103-
if (!res) return
104-
const { clientId, clientSecret } = res
105-
console.log("clientId", clientId, "clientSecret", clientSecret)
106-
107-
const accessToken = await performOAuthLogin({ clientId, clientSecret })
127+
const accessToken = await performOAuthLogin()
108128
console.log("accessToken", accessToken)
109129
})
110130
})

dev/bin/setup-hydra-client.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ hydra_cli create client \
2121
--format json \
2222
--scope read --scope write \
2323
--redirect-uri "$redirect_uri" > "${HYDRA_CLIENT_JSON}" \
24-
--name "${hydra_client_name}"
24+
--name "${hydra_client_name}" \
25+
--skip-consent
2526

2627
CLIENT_ID=$(jq -r '.client_id' < "${HYDRA_CLIENT_JSON}")
2728
CLIENT_SECRET=$(jq -r '.client_secret' < "${HYDRA_CLIENT_JSON}")

0 commit comments

Comments
 (0)