mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-27 06:13:09 +02:00
upd: change parser and fix fetchAccessToken
This commit is contained in:
parent
30cc0a9b18
commit
37d7a4b604
2 changed files with 41 additions and 18 deletions
|
@ -1,5 +1,6 @@
|
||||||
import { Inject, Injectable } from '@nestjs/common';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
import megalodon, { Entity, MegalodonInterface } from 'megalodon';
|
import megalodon, { Entity, MegalodonInterface } from 'megalodon';
|
||||||
|
import querystring from 'querystring';
|
||||||
import { IsNull } from 'typeorm';
|
import { IsNull } from 'typeorm';
|
||||||
import multer from 'fastify-multer';
|
import multer from 'fastify-multer';
|
||||||
import type { UsersRepository } from '@/models/_.js';
|
import type { UsersRepository } from '@/models/_.js';
|
||||||
|
@ -45,13 +46,20 @@ export class MastodonApiServerService {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
fastify.addContentTypeParser(['application/x-www-form-urlencoded'], { parseAs: 'string' }, (req, body, done) => {
|
fastify.addContentTypeParser('application/x-www-form-urlencoded', function (request, payload, done) {
|
||||||
const dataObj: any = {};
|
let body = '';
|
||||||
const parsedData = new URLSearchParams(body as string);
|
payload.on('data', function (data) {
|
||||||
for (const pair of parsedData.entries()) {
|
body += data;
|
||||||
dataObj[pair[0]] = pair[1];
|
});
|
||||||
}
|
payload.on('end', function () {
|
||||||
done(null, dataObj);
|
try {
|
||||||
|
const parsed = querystring.parse(body);
|
||||||
|
done(null, parsed);
|
||||||
|
} catch (e: any) {
|
||||||
|
done(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
payload.on('error', done);
|
||||||
});
|
});
|
||||||
|
|
||||||
fastify.register(multer.contentParser);
|
fastify.register(multer.contentParser);
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
import { Inject, Injectable } from '@nestjs/common';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
import megalodon, { MegalodonInterface } from 'megalodon';
|
import megalodon, { MegalodonInterface } from 'megalodon';
|
||||||
|
import querystring from 'querystring';
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
/* import { kinds } from '@/misc/api-permissions.js';
|
/* import { kinds } from '@/misc/api-permissions.js';
|
||||||
import type { Config } from '@/config.js';
|
import type { Config } from '@/config.js';
|
||||||
|
@ -12,6 +13,14 @@ import { DI } from '@/di-symbols.js'; */
|
||||||
import { bindThis } from '@/decorators.js';
|
import { bindThis } from '@/decorators.js';
|
||||||
import type { FastifyInstance } from 'fastify';
|
import type { FastifyInstance } from 'fastify';
|
||||||
|
|
||||||
|
function getClient(BASE_URL: string, authorization: string | undefined): MegalodonInterface {
|
||||||
|
const accessTokenArr = authorization?.split(' ') ?? [null];
|
||||||
|
const accessToken = accessTokenArr[accessTokenArr.length - 1];
|
||||||
|
const generator = (megalodon as any).default;
|
||||||
|
const client = generator('misskey', BASE_URL, accessToken) as MegalodonInterface;
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class OAuth2ProviderService {
|
export class OAuth2ProviderService {
|
||||||
constructor(
|
constructor(
|
||||||
|
@ -42,13 +51,20 @@ export class OAuth2ProviderService {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
fastify.addContentTypeParser(['application/x-www-form-urlencoded'], { parseAs: 'string' }, (req, body, done) => {
|
fastify.addContentTypeParser('application/x-www-form-urlencoded', function (request, payload, done) {
|
||||||
const dataObj: any = {};
|
let body = '';
|
||||||
const parsedData = new URLSearchParams(body as string);
|
payload.on('data', function (data) {
|
||||||
for (const pair of parsedData.entries()) {
|
body += data;
|
||||||
dataObj[pair[0]] = pair[1];
|
});
|
||||||
}
|
payload.on('end', function () {
|
||||||
done(null, dataObj);
|
try {
|
||||||
|
const parsed = querystring.parse(body);
|
||||||
|
done(null, parsed);
|
||||||
|
} catch (e: any) {
|
||||||
|
done(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
payload.on('error', done);
|
||||||
});
|
});
|
||||||
|
|
||||||
fastify.get('/oauth/authorize', async (request, reply) => {
|
fastify.get('/oauth/authorize', async (request, reply) => {
|
||||||
|
@ -75,8 +91,7 @@ export class OAuth2ProviderService {
|
||||||
}
|
}
|
||||||
let client_id: any = body.client_id;
|
let client_id: any = body.client_id;
|
||||||
const BASE_URL = `${request.protocol}://${request.hostname}`;
|
const BASE_URL = `${request.protocol}://${request.hostname}`;
|
||||||
const generator = (megalodon as any).default;
|
const client = getClient(BASE_URL, '');
|
||||||
const client = generator(BASE_URL, null) as MegalodonInterface;
|
|
||||||
let token = null;
|
let token = null;
|
||||||
if (body.code) {
|
if (body.code) {
|
||||||
//m = body.code.match(/^([a-zA-Z0-9]{8})([a-zA-Z0-9]{4})([a-zA-Z0-9]{4})([a-zA-Z0-9]{4})([a-zA-Z0-9]{12})/);
|
//m = body.code.match(/^([a-zA-Z0-9]{8})([a-zA-Z0-9]{4})([a-zA-Z0-9]{4})([a-zA-Z0-9]{4})([a-zA-Z0-9]{12})/);
|
||||||
|
@ -107,8 +122,8 @@ export class OAuth2ProviderService {
|
||||||
};
|
};
|
||||||
reply.send(ret);
|
reply.send(ret);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
/* console.error(err); */
|
console.error(err);
|
||||||
reply.code(401).send(err.response);
|
reply.code(401).send(err.response.data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue