2020-06-01 08:36:33 +02:00
|
|
|
import { ContextType } from 'server/types/apiTypes';
|
|
|
|
import { logger } from 'server/helpers/logger';
|
|
|
|
import { requestLimiter } from 'server/helpers/rateLimiter';
|
2020-12-03 23:43:40 +01:00
|
|
|
import { saved } from 'server/helpers/auth';
|
2020-06-01 08:36:33 +02:00
|
|
|
|
|
|
|
export const accountResolvers = {
|
|
|
|
Query: {
|
2020-08-03 16:31:20 +02:00
|
|
|
getAccount: async (_: undefined, __: undefined, context: ContextType) => {
|
|
|
|
const { ip, accounts, id } = context;
|
|
|
|
await requestLimiter(ip, 'getAccount');
|
|
|
|
|
|
|
|
if (!id) {
|
|
|
|
logger.error(`Not authenticated`);
|
|
|
|
throw new Error('NotAuthenticated');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id === 'sso') {
|
|
|
|
return {
|
|
|
|
name: 'SSO Account',
|
|
|
|
id: 'sso',
|
|
|
|
loggedIn: true,
|
|
|
|
type: 'sso',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentAccount = accounts.find(a => a.id === id);
|
|
|
|
|
|
|
|
if (!currentAccount) {
|
|
|
|
logger.error(`No account found for id ${id}`);
|
|
|
|
throw new Error('NoAccountFound');
|
|
|
|
}
|
|
|
|
|
|
|
|
return { ...currentAccount, type: 'server', loggedIn: true };
|
|
|
|
},
|
2020-06-01 08:36:33 +02:00
|
|
|
getServerAccounts: async (
|
|
|
|
_: undefined,
|
2020-06-13 11:46:20 +02:00
|
|
|
__: undefined,
|
2020-06-01 08:36:33 +02:00
|
|
|
context: ContextType
|
|
|
|
) => {
|
2020-08-03 16:31:20 +02:00
|
|
|
const { ip, accounts, id, sso } = context;
|
2020-06-01 08:36:33 +02:00
|
|
|
await requestLimiter(ip, 'getServerAccounts');
|
|
|
|
|
2020-12-03 23:43:40 +01:00
|
|
|
saved.reset();
|
|
|
|
|
2020-06-01 08:36:33 +02:00
|
|
|
let ssoAccount = null;
|
2020-08-03 16:31:20 +02:00
|
|
|
if (id === 'sso' && sso) {
|
|
|
|
const { cert, socket } = sso;
|
2020-06-01 08:36:33 +02:00
|
|
|
logger.debug(
|
|
|
|
`Macaroon${
|
|
|
|
cert ? ', certificate' : ''
|
2020-08-03 16:31:20 +02:00
|
|
|
} and host (${socket}) found for SSO.`
|
2020-06-01 08:36:33 +02:00
|
|
|
);
|
|
|
|
ssoAccount = {
|
|
|
|
name: 'SSO Account',
|
2020-08-03 16:31:20 +02:00
|
|
|
id: 'sso',
|
2020-06-01 08:36:33 +02:00
|
|
|
loggedIn: true,
|
2020-08-03 16:31:20 +02:00
|
|
|
type: 'sso',
|
2020-06-01 08:36:33 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const withStatus =
|
|
|
|
accounts?.map(a => ({
|
|
|
|
...a,
|
2020-08-03 16:31:20 +02:00
|
|
|
loggedIn: a.id === id,
|
|
|
|
type: 'server',
|
2020-06-01 08:36:33 +02:00
|
|
|
})) || [];
|
|
|
|
|
2020-08-03 16:31:20 +02:00
|
|
|
return ssoAccount ? [ssoAccount, ...withStatus] : withStatus;
|
2020-06-01 08:36:33 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|