Support OAuth 'client_credentials' grant type, improve Mastodon API compat
This commit is contained in:
parent
b0c58bff70
commit
74024e36b6
|
@ -12,26 +12,42 @@ const codeGrantSchema = z.object({
|
||||||
code: z.string(),
|
code: z.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const credentialsGrantSchema = z.object({
|
||||||
|
grant_type: z.literal('client_credentials'),
|
||||||
|
});
|
||||||
|
|
||||||
const createTokenSchema = z.discriminatedUnion('grant_type', [
|
const createTokenSchema = z.discriminatedUnion('grant_type', [
|
||||||
passwordGrantSchema,
|
passwordGrantSchema,
|
||||||
codeGrantSchema,
|
codeGrantSchema,
|
||||||
|
credentialsGrantSchema,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const createTokenController: AppController = async (c) => {
|
const createTokenController: AppController = async (c) => {
|
||||||
const body = await parseBody(c.req.raw);
|
const body = await parseBody(c.req.raw);
|
||||||
const data = createTokenSchema.parse(body);
|
const result = createTokenSchema.safeParse(body);
|
||||||
|
|
||||||
switch (data.grant_type) {
|
if (!result.success) {
|
||||||
|
return c.json({ error: 'Invalid request', issues: result.error.issues }, 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (result.data.grant_type) {
|
||||||
case 'password':
|
case 'password':
|
||||||
return c.json({
|
return c.json({
|
||||||
access_token: data.password,
|
access_token: result.data.password,
|
||||||
token_type: 'Bearer',
|
token_type: 'Bearer',
|
||||||
scope: 'read write follow push',
|
scope: 'read write follow push',
|
||||||
created_at: Math.floor(new Date().getTime() / 1000),
|
created_at: Math.floor(new Date().getTime() / 1000),
|
||||||
});
|
});
|
||||||
case 'authorization_code':
|
case 'authorization_code':
|
||||||
return c.json({
|
return c.json({
|
||||||
access_token: data.code,
|
access_token: result.data.code,
|
||||||
|
token_type: 'Bearer',
|
||||||
|
scope: 'read write follow push',
|
||||||
|
created_at: Math.floor(new Date().getTime() / 1000),
|
||||||
|
});
|
||||||
|
case 'client_credentials':
|
||||||
|
return c.json({
|
||||||
|
access_token: '_',
|
||||||
token_type: 'Bearer',
|
token_type: 'Bearer',
|
||||||
scope: 'read write follow push',
|
scope: 'read write follow push',
|
||||||
created_at: Math.floor(new Date().getTime() / 1000),
|
created_at: Math.floor(new Date().getTime() / 1000),
|
||||||
|
|
|
@ -39,12 +39,20 @@ async function toAccount(event: Event<0>, opts: ToAccountOpts = {}) {
|
||||||
avatar_static: picture || DEFAULT_AVATAR,
|
avatar_static: picture || DEFAULT_AVATAR,
|
||||||
bot: false,
|
bot: false,
|
||||||
created_at: event ? new Date(event.created_at * 1000).toISOString() : new Date().toISOString(),
|
created_at: event ? new Date(event.created_at * 1000).toISOString() : new Date().toISOString(),
|
||||||
|
discoverable: true,
|
||||||
display_name: name,
|
display_name: name,
|
||||||
emojis: toEmojis(event),
|
emojis: toEmojis(event),
|
||||||
fields: [],
|
fields: [],
|
||||||
follow_requests_count: 0,
|
follow_requests_count: 0,
|
||||||
followers_count: 0,
|
followers_count: 0,
|
||||||
following_count: 0,
|
following_count: 0,
|
||||||
|
fqn: parsed05?.handle || npub,
|
||||||
|
header: banner || DEFAULT_BANNER,
|
||||||
|
header_static: banner || DEFAULT_BANNER,
|
||||||
|
last_status_at: null,
|
||||||
|
locked: false,
|
||||||
|
note: lodash.escape(about),
|
||||||
|
roles: [],
|
||||||
source: withSource
|
source: withSource
|
||||||
? {
|
? {
|
||||||
fields: [],
|
fields: [],
|
||||||
|
@ -56,11 +64,6 @@ async function toAccount(event: Event<0>, opts: ToAccountOpts = {}) {
|
||||||
}
|
}
|
||||||
: undefined,
|
: undefined,
|
||||||
statuses_count: 0,
|
statuses_count: 0,
|
||||||
header: banner || DEFAULT_BANNER,
|
|
||||||
header_static: banner || DEFAULT_BANNER,
|
|
||||||
locked: false,
|
|
||||||
note: lodash.escape(about),
|
|
||||||
fqn: parsed05?.handle || npub,
|
|
||||||
url: `${origin}/users/${pubkey}`,
|
url: `${origin}/users/${pubkey}`,
|
||||||
username: parsed05?.nickname || npub.substring(0, 8),
|
username: parsed05?.nickname || npub.substring(0, 8),
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue