mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Admin Panel / Settings / Visibility / Optional APIs ticks REST APIs in the shape of Trello's, Jira's, GitHub's and Gitea's, so apps and scripts written for them - Trellinator, Jira and GitHub clients - can use WeKan as their backend, each described on the page with the address a client uses. None has rights of its own. Each is a translator: a request in the other tracker's shape becomes the matching request to WeKan's own REST API, made with the caller's own WeKan token on the loopback address, and the answer becomes the other tracker's JSON. Every WeKan permission therefore applies unchanged - read-only and comment-only roles, assigned-only members, list permissions - and an API that is not ticked answers 404. All of them sit under /api, so WITH_API turns them off with the rest of the REST API. Trello: members, boards, lists, cards, labels, comments and checklists; a deleted card is archived. Jira: projects are boards (the board key), issues are cards (DEV-7), statuses are lists and transitions move cards, with simple JQL. GitHub and Gitea: repositories are boards and issues are cards, open or closed as in Board View / Version Control, with labels, milestones, comments and Link pagination. The new strings are translated in every locale. A runtime test over real HTTP shows a read-only member refused and a Trello-created card readable through Jira and GitHub. Thanks to xet7 ! Fixes #4930.
69 lines
3.2 KiB
JavaScript
69 lines
3.2 KiB
JavaScript
'use strict';
|
|
|
|
// Optional APIs (Admin Panel / Settings / Visibility / Optional APIs): REST
|
|
// APIs in the shape of other trackers', so a client written for Trello, Jira,
|
|
// GitHub or Gitea can use WeKan as its backend. What such a client saves is
|
|
// stored in WeKan's own database, on WeKan boards, under WeKan's permissions:
|
|
// each API sees only the boards its caller may see, and writes only where the
|
|
// caller may write.
|
|
//
|
|
// Every one is OFF until an administrator ticks it, and every one lives under
|
|
// WeKan's own /api, so the WITH_API switch turns them all off with the rest
|
|
// of the REST API. A client is pointed at the base path as its server address.
|
|
//
|
|
// One catalog, read by the Admin Panel and by the routes: an API that is not
|
|
// ticked answers 404, as if it were not there.
|
|
|
|
const OPTIONAL_APIS = [
|
|
{ key: 'trello', name: 'Trello', base: '/api/compat/trello/1', labelKey: 'optional-api-trello', descKey: 'optional-api-trello-desc' },
|
|
{ key: 'jira', name: 'Jira', base: '/api/compat/jira/rest/api/2', labelKey: 'optional-api-jira', descKey: 'optional-api-jira-desc' },
|
|
{ key: 'github', name: 'GitHub', base: '/api/compat/github', labelKey: 'optional-api-github', descKey: 'optional-api-github-desc' },
|
|
{ key: 'gitea', name: 'Gitea', base: '/api/compat/gitea/api/v1', labelKey: 'optional-api-gitea', descKey: 'optional-api-gitea-desc' },
|
|
];
|
|
|
|
const OPTIONAL_API_KEYS = OPTIONAL_APIS.map(api => api.key);
|
|
|
|
/** Whether an optional API is on: only when an administrator ticked it. */
|
|
function isOptionalApiEnabled(setting, key) {
|
|
return !!(setting && setting.optionalApis && setting.optionalApis[key] === true) && OPTIONAL_API_KEYS.includes(key);
|
|
}
|
|
|
|
/** The stored value for the Admin Panel's ticks: known keys only, booleans only. */
|
|
function cleanOptionalApis(input) {
|
|
const out = {};
|
|
for (const key of OPTIONAL_API_KEYS) out[key] = !!(input && input[key] === true);
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* The WeKan token a compatible client sends in its tracker's own way, or ''.
|
|
* Trello ?token=<t> (and ?key=, which is ignored), or Authorization: OAuth oauth_token="<t>"
|
|
* Jira Authorization: Basic base64(<user>:<t>), or Bearer <t>
|
|
* GitHub Authorization: token <t>, or Bearer <t>
|
|
* Gitea Authorization: token <t>, or ?token= / ?access_token=
|
|
* WeKan's own Bearer header is read by the API middleware before this.
|
|
*/
|
|
function compatToken(api, { authorization = '', query = {} } = {}) {
|
|
const auth = String(authorization || '');
|
|
let m = /^(?:token|bearer)\s+(\S+)$/i.exec(auth);
|
|
if (m) return m[1];
|
|
if (api === 'trello') {
|
|
m = /oauth_token="([^"]+)"/i.exec(auth);
|
|
if (m) return m[1];
|
|
return typeof query.token === 'string' ? query.token : '';
|
|
}
|
|
if (api === 'jira') {
|
|
m = /^basic\s+(\S+)$/i.exec(auth);
|
|
if (m) {
|
|
const decoded = Buffer.from(m[1], 'base64').toString('utf8');
|
|
const at = decoded.indexOf(':');
|
|
return at >= 0 ? decoded.slice(at + 1) : '';
|
|
}
|
|
return '';
|
|
}
|
|
if (api === 'gitea') return typeof query.token === 'string' ? query.token : (typeof query.access_token === 'string' ? query.access_token : '');
|
|
return '';
|
|
}
|
|
|
|
module.exports = { OPTIONAL_APIS, OPTIONAL_API_KEYS, isOptionalApiEnabled, cleanOptionalApis, compatToken };
|