MCPcopy
hub / github.com/msgbyte/tianji / getAuthSession

Function getAuthSession

src/server/model/auth.ts:145–197  ·  view source on GitHub ↗
(
  req: IncomingMessage,
  secure = false
)

Source from the content-addressed store, hash-verified

143 * Pure request of auth session
144 */
145export async function getAuthSession(
146 req: IncomingMessage,
147 secure = false
148): Promise<Session | null> {
149 const protocol = secure ? 'https:' : 'http:';
150 const url = createActionURL(
151 'session',
152 protocol,
153 // @ts-expect-error
154 new Headers(req.headers),
155 process.env,
156 authConfig.basePath
157 );
158
159 if (!req.headers.cookie) {
160 logger.warn('No cookie in request, can not get auth session:', {
161 protocol,
162 headers: req.headers,
163 });
164 }
165
166 const response = await Auth(
167 new Request(url, { headers: { cookie: req.headers.cookie ?? '' } }),
168 authConfig
169 );
170
171 const { status = 200 } = response;
172
173 // Read text first to avoid "Body has already been read" error
174 const raw = await response.text();
175
176 let data;
177 try {
178 data = JSON.parse(raw);
179 } catch (error) {
180 logger.error('Failed to parse auth session response:', error);
181 return null;
182 }
183
184 if (!data || !Object.keys(data).length) {
185 logger.error('Can not get info, auth session raw:', {
186 raw,
187 protocol,
188 headers: req.headers,
189 });
190 return null;
191 }
192
193 if (status === 200) {
194 return data;
195 }
196 throw new Error(data.message);
197}
198
199function toAdapterUser(
200 user: Pick<

Callers 1

initSocketioFunction · 0.85

Calls 4

warnMethod · 0.80
parseMethod · 0.80
errorMethod · 0.80
textMethod · 0.65

Tested by

no test coverage detected