-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
133 lines (107 loc) · 3.59 KB
/
index.ts
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* eslint-disable @typescript-eslint/camelcase */
/* eslint-disable no-console */
import { FieldResolver } from 'nexus'
/**
* Авторизация
*/
export const signin: FieldResolver<'Mutation', 'signin'> = async (
_,
args,
ctx
) => {
console.log('ctx.config', ctx.config)
const { login_context, password, username } = args
const formBody = new URLSearchParams()
formBody.append('login_context', login_context || 'mgr')
formBody.append('username', username)
formBody.append('password', password)
formBody.append('login', '1')
formBody.append('returnUrl', '/manager/')
const result = await fetch(ctx.config.managerUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
/**
* Запрещаем редирект, так как иначе MODX в ответе присылает редирект
* и выполняется за раз сразу два запроса.
* При этом в последнем запросе прилетает кукис без авторизации. Нужен именно первый.
*/
redirect: 'manual', // manual, *follow, error
body: formBody,
})
.then(async (r) => {
const setCookie = await r.headers.get('set-cookie')
console.log('signin setCookie', setCookie)
if (setCookie) {
const PHPSESSID = setCookie
.split(';')
.find((n) => n.startsWith('PHPSESSID'))
/**
* Запрашиваем код админки, чтобы получить токен
*/
return await fetch(ctx.config.managerUrl, {
headers: {
cookie: PHPSESSID || '',
},
})
.then(async (r) => {
const response2 = await r.text()
/**
* ищем в ответе токен
*/
const match = response2.match(/"auth":"(.+?)"/)
const token = match && match[1]
if (token) {
/**
* Устанавливаем куки авторизованного пользователя
*/
ctx.res?.cookie(setCookie, undefined)
}
return token
})
.catch(console.error)
}
})
.catch(console.error)
if (!result) {
throw new Error('Ошибка авторизации')
}
return result
}
/**
* Список пользователей
*/
export const users: FieldResolver<'Query', 'users'> = async (_, _args, ctx) => {
const formBody = new URLSearchParams()
formBody.append('action', 'Security/User/GetList')
formBody.append('start', '0')
formBody.append('limit', '20')
console.log('ctx.req?.headers.cookie', ctx.req?.headers.cookie)
console.log('ctx.req?.signedCookies', ctx.req?.signedCookies)
console.log('ctx.req?.headers.authorization', ctx.req?.headers.authorization)
const result = fetch(ctx.config.connectorsrUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
cookie: ctx.req?.headers.cookie || '',
modAuth: ctx.req?.headers.authorization || '',
},
body: formBody,
}).then(async (r) => {
const response = await r.text()
console.log('users response', response)
console.log('users response headers', r.headers)
try {
const data = JSON.parse(response)
console.log('data', data)
return data.results || []
} catch (error) {
throw new Error(error)
}
// return r;
})
// .catch(console.error)
console.log('users result', result)
return result
}