Better permisson Fix #2341 (#4611)

* Better permisson Fix #2341

* add kinds.ts

* test

* fix

* v11

* fix
This commit is contained in:
tamaina
2019-04-15 12:10:40 +09:00
committed by syuilo
parent b357afa30a
commit 94f8a145ec
19 changed files with 183 additions and 49 deletions

View File

@ -14,7 +14,7 @@ export const meta = {
requireCredential: true,
kind: 'favorites-read',
kind: 'read:favorites',
params: {
limit: {

View File

@ -14,7 +14,7 @@ export const meta = {
requireCredential: true,
kind: 'messaging-read',
kind: 'read:messaging',
params: {
limit: {

View File

@ -17,7 +17,7 @@ export const meta = {
requireCredential: true,
kind: 'messaging-read',
kind: 'read:messaging',
params: {
userId: {

View File

@ -20,7 +20,7 @@ export const meta = {
requireCredential: true,
kind: 'messaging-write',
kind: 'write:messaging',
params: {
userId: {

View File

@ -18,7 +18,7 @@ export const meta = {
requireCredential: true,
kind: 'messaging-write',
kind: 'write:messaging',
limit: {
duration: ms('1hour'),

View File

@ -15,7 +15,7 @@ export const meta = {
requireCredential: true,
kind: 'messaging-write',
kind: 'write:messaging',
params: {
messageId: {

View File

@ -18,7 +18,7 @@ export const meta = {
requireCredential: true,
kind: 'favorite-write',
kind: 'write:favorites',
params: {
noteId: {

View File

@ -17,7 +17,7 @@ export const meta = {
requireCredential: true,
kind: 'favorite-write',
kind: 'write:favorites',
params: {
noteId: {

View File

@ -26,7 +26,7 @@ export const meta = {
requireCredential: true,
kind: 'vote-write',
kind: 'write:votes',
params: {
noteId: {

View File

@ -0,0 +1,29 @@
import define from '../define';
import { kindsList } from '../kinds';
export const meta = {
stability: 'stable',
desc: {
'ja-JP': 'パーミッションの一覧を返します。',
'en-US': 'Get the list of permissons.'
},
tags: ['meta'],
requireCredential: false,
params: {
},
res: {
type: 'array',
items: {
type: 'string',
}
},
};
export default define(meta, async () => {
return kindsList;
});

58
src/server/api/kinds.ts Normal file
View File

@ -0,0 +1,58 @@
import endpoints from './endpoints';
import * as locale from '../../../locales/';
import { fromEntries } from '../../prelude/array';
export const kindsList = [
'read:account',
'write:account',
'read:blocks',
'write:blocks',
'read:drive',
'write:drive',
'read:favorites',
'write:favorites',
'read:following',
'write:following',
'read:messaging',
'write:messaging',
'read:mutes',
'write:mutes',
'write:notes',
'read:notifications',
'write:notifications',
'read:reactions',
'write:reactions',
'write:votes'
];
export interface IKindInfo {
endpoints: string[];
descs: { [x: string]: string; };
}
export function kinds() {
const kinds = fromEntries(
kindsList
.map(k => [k, {
endpoints: [],
descs: fromEntries(
Object.keys(locale)
.map(l => [l, locale[l].common.permissions[k] as string] as [string, string])
) as { [x: string]: string; }
}] as [ string, IKindInfo ])
) as { [x: string]: IKindInfo; };
const errors = [] as string[][];
for (const endpoint of endpoints.filter(ep => !ep.meta.secure)) {
if (endpoint.meta.kind) {
const kind = endpoint.meta.kind;
if (kind in kinds) kinds[kind].endpoints.push(endpoint.name);
else errors.push([kind, endpoint.name]);
}
}
if (errors.length > 0) throw Error('\n ' + errors.map((e) => `Unknown kind (permission) "${e[0]}" found at ${e[1]}.`).join('\n '));
return kinds;
}

View File

@ -1,6 +1,14 @@
import config from '../../../config';
import { IKindInfo, kinds } from '../kinds';
export function getDescription(lang = 'ja-JP'): string {
const permissionTable = (Object.entries(kinds()) as [string, IKindInfo][])
.map(e => `|${e[0]}|${e[1].descs[lang]}|${e[1].endpoints.map(f => `[${f}](#operation/${f})`).join(', ')}|`)
.join('\n');
const descriptions = {
'ja-JP': `**Misskey is a decentralized microblogging platform.**
export const description = `
## Usage
**APIはすべてPOSTでリクエスト/レスポンスともにJSON形式です。**
一部のAPIはリクエストに認証情報(APIキー)が必要です。リクエストの際に\`i\`というパラメータでAPIキーを添付してください。
@ -44,4 +52,12 @@ APIキーの生成方法を擬似コードで表すと次のようになりま
\`\`\` js
const i = sha256(userToken + secretKey);
\`\`\`
`;
## Permissions
|Permisson (kind)|Description|Endpoints|
|:--|:--|:--|
${permissionTable}
`
} as { [x: string]: string };
return lang in descriptions ? descriptions[lang] : descriptions['ja-JP'];
}

View File

@ -3,7 +3,7 @@ import { Context } from 'cafy';
import config from '../../../config';
import { errors as basicErrors } from './errors';
import { schemas } from './schemas';
import { description } from './description';
import { getDescription } from './description';
import { convertOpenApiSchema } from '../../../misc/schema';
export function genOpenapiSpec(lang = 'ja-JP') {
@ -13,7 +13,7 @@ export function genOpenapiSpec(lang = 'ja-JP') {
info: {
version: 'v1',
title: 'Misskey API',
description: '**Misskey is a decentralized microblogging platform.**\n\n' + description,
description: getDescription(lang),
'x-logo': { url: '/assets/api-doc.png' }
},
@ -110,7 +110,10 @@ export function genOpenapiSpec(lang = 'ja-JP') {
let desc = (endpoint.meta.desc ? endpoint.meta.desc[lang] : 'No description provided.') + '\n\n';
desc += `**Credential required**: *${endpoint.meta.requireCredential ? 'Yes' : 'No'}*`;
if (endpoint.meta.kind) desc += ` / **Permission**: *${endpoint.meta.kind}*`;
if (endpoint.meta.kind) {
const kind = endpoint.meta.kind;
desc += ` / **Permission**: *${kind}*`;
}
const info = {
operationId: endpoint.name,