-
Notifications
You must be signed in to change notification settings - Fork 158
/
cookies.js
113 lines (102 loc) · 3.06 KB
/
cookies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import axios from 'axios'
import cookieParser from 'set-cookie-parser'
import {
getHeaders,
getLoginHeaders,
cookiesToString
} from './lib/http_util.js'
import { encrypt } from './lib/encrypt.js'
async function getAttachCookies(url, cookies = []) {
const headers = getHeaders()
headers.authority = 'passport.shang-ma.com'
headers.referer = 'https://passport.shang-ma.com'
let config = {
method: 'get',
maxBodyLength: Infinity,
validateStatus: status => {
return status >= 200 && status < 400
},
maxRedirects: 0, // 设置为 0 禁止重定向
url: url,
headers: headers
}
let verify
try {
const response = await axios.request(config)
verify = response.data.verify
const setCookieHeader = response.headers['set-cookie']
if (setCookieHeader) {
const parsedCookies = cookieParser.parse(setCookieHeader)
const cookieDictList = parsedCookies.reduce((acc, cookie) => {
const { name, value, maxAge } = cookie
if (maxAge === undefined || maxAge > 0) {
acc.push({ [name]: value })
}
return acc
}, [])
cookies.push(...cookieDictList)
}
if (
response.status >= 300 &&
response.status < 400 &&
response.headers.location
) {
return getAttachCookies(response.headers.location, cookies)
} else {
if (verify) {
cookies.push({ sso_verify_passport: verify })
}
}
} catch (error) {
console.error('请求出错:', error)
}
return cookies
}
async function getInitCookies() {
const url = 'https://passport.shang-ma.com/api/sso/client/attach'
return getAttachCookies(url)
}
async function getLoginCookies(initCookie) {
const loginCookies = []
loginCookies.push(...initCookie)
const headers = getLoginHeaders(true)
headers.cookie = cookiesToString(initCookie)
headers.authority = 'passport.shang-ma.com'
headers.referer = 'https://passport.shang-ma.com'
headers.authorization =
'JwtApp eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.MmwTuGPzzXKkDFQOX9Gn-Av9YO9J_4Q7LXeXr23js20'
const username = process.env.SM_USERNAME
const password = process.env.SM_PASSWORD
const encryptBody = encrypt(
JSON.stringify({ username: username, password: password })
)
let data = JSON.stringify({
encryptBody: encryptBody
})
let config = {
method: 'post',
maxBodyLength: Infinity,
url: 'https://passport.shang-ma.com/api/login/index',
headers: headers,
data: data
}
const response = await axios.request(config)
if (response.headers['set-cookie']) {
const parsedCookies = cookieParser.parse(response.headers['set-cookie'])
const targetCookie = parsedCookies.find(
cookie => cookie.name === 'SHM_JWT_TOKEN'
)
if (targetCookie) {
loginCookies.push({ SHM_JWT_TOKEN: targetCookie.value })
} else {
console.error('get SHM_JWT_TOKEN failed ')
}
}
return loginCookies
}
export default async function getCookie() {
const cookies = await getInitCookies()
const loginCookies = await getLoginCookies(cookies)
return loginCookies
}
// getCookie()