Support OAuth 'client_credentials' grant type, improve Mastodon API compat

This commit is contained in:
Alex Gleason 2023-05-24 11:19:02 -05:00
parent b0c58bff70
commit 74024e36b6
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 28 additions and 9 deletions

View File

@ -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),

View File

@ -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),
}; };