0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Replaced i18n.t w/ tpl helper in core/server/services/auth (#13458)

refs #13380

The i18n package is deprecated. It is being replaced with the tpl package.

Co-authored-by: Aleksander Chromik <aleksander.chromik@footballco.com>
This commit is contained in:
Aleksander Chromik 2021-10-06 10:42:44 +02:00 committed by GitHub
parent 162502b19e
commit 6ff63dc220
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 19 deletions

View file

@ -3,9 +3,18 @@ const url = require('url');
const models = require('../../../models');
const errors = require('@tryghost/errors');
const limitService = require('../../../services/limits');
const i18n = require('../../../../shared/i18n');
const tpl = require('@tryghost/tpl');
const _ = require('lodash');
const messages = {
incorrectAuthHeaderFormat: 'Authorization header format is "Authorization: Ghost [token]"',
invalidTokenWithMessage: 'Invalid token: {message}',
invalidToken: 'Invalid token',
adminApiKidMissing: 'Admin API kid missing.',
unknownAdminApiKey: 'Unknown Admin API Key',
invalidApiKeyType: 'Invalid API Key type'
};
let JWT_OPTIONS_DEFAULTS = {
algorithms: ['HS256'],
maxAge: '5m'
@ -45,7 +54,7 @@ const authenticate = (req, res, next) => {
if (!token) {
return next(new errors.UnauthorizedError({
message: i18n.t('errors.middleware.auth.incorrectAuthHeaderFormat'),
message: tpl(messages.incorrectAuthHeaderFormat),
code: 'INVALID_AUTH_HEADER'
}));
}
@ -57,7 +66,7 @@ const authenticateWithUrl = (req, res, next) => {
const token = _extractTokenFromUrl(req.originalUrl);
if (!token) {
return next(new errors.UnauthorizedError({
message: i18n.t('errors.middleware.auth.invalidTokenWithMessage', {message: 'No token found in URL'}),
message: tpl(messages.invalidTokenWithMessage, {message: 'No token found in URL'}),
code: 'INVALID_JWT'
}));
}
@ -84,7 +93,7 @@ const authenticateWithToken = async (req, res, next, {token, JWT_OPTIONS}) => {
if (!decoded || !decoded.header) {
return next(new errors.BadRequestError({
message: i18n.t('errors.middleware.auth.invalidToken'),
message: tpl(messages.invalidToken),
code: 'INVALID_JWT'
}));
}
@ -93,7 +102,7 @@ const authenticateWithToken = async (req, res, next, {token, JWT_OPTIONS}) => {
if (!apiKeyId) {
return next(new errors.BadRequestError({
message: i18n.t('errors.middleware.auth.adminApiKidMissing'),
message: tpl(messages.adminApiKidMissing),
code: 'MISSING_ADMIN_API_KID'
}));
}
@ -103,14 +112,14 @@ const authenticateWithToken = async (req, res, next, {token, JWT_OPTIONS}) => {
if (!apiKey) {
return next(new errors.UnauthorizedError({
message: i18n.t('errors.middleware.auth.unknownAdminApiKey'),
message: tpl(messages.unknownAdminApiKey),
code: 'UNKNOWN_ADMIN_API_KEY'
}));
}
if (apiKey.get('type') !== 'admin') {
return next(new errors.UnauthorizedError({
message: i18n.t('errors.middleware.auth.invalidApiKeyType'),
message: tpl(messages.invalidApiKeyType),
code: 'INVALID_API_KEY_TYPE'
}));
}
@ -142,7 +151,7 @@ const authenticateWithToken = async (req, res, next, {token, JWT_OPTIONS}) => {
} catch (err) {
if (err.name === 'TokenExpiredError' || err.name === 'JsonWebTokenError') {
return next(new errors.UnauthorizedError({
message: i18n.t('errors.middleware.auth.invalidTokenWithMessage', {message: err.message}),
message: tpl(messages.invalidTokenWithMessage, {message: err.message}),
code: 'INVALID_JWT',
err
}));

View file

@ -1,7 +1,13 @@
const models = require('../../../models');
const errors = require('@tryghost/errors');
const limitService = require('../../../services/limits');
const i18n = require('../../../../shared/i18n');
const tpl = require('@tryghost/tpl');
const messages = {
invalidRequest: 'Invalid Request',
unknownContentApiKey: 'Unknown Content API Key',
invalidApiKeyType: 'Invalid API Key type'
};
const authenticateContentApiKey = async function authenticateContentApiKey(req, res, next) {
// allow fallthrough to other auth methods or final ensureAuthenticated check
@ -11,7 +17,7 @@ const authenticateContentApiKey = async function authenticateContentApiKey(req,
if (req.query.key.constructor === Array) {
return next(new errors.BadRequestError({
message: i18n.t('errors.middleware.auth.invalidRequest'),
message: tpl(messages.invalidRequest),
code: 'INVALID_REQUEST'
}));
}
@ -23,14 +29,14 @@ const authenticateContentApiKey = async function authenticateContentApiKey(req,
if (!apiKey) {
return next(new errors.UnauthorizedError({
message: i18n.t('errors.middleware.auth.unknownContentApiKey'),
message: tpl(messages.unknownContentApiKey),
code: 'UNKNOWN_CONTENT_API_KEY'
}));
}
if (apiKey.get('type') !== 'content') {
return next(new errors.UnauthorizedError({
message: i18n.t('errors.middleware.auth.invalidApiKeyType'),
message: tpl(messages.invalidApiKeyType),
code: 'INVALID_API_KEY_TYPE'
}));
}

View file

@ -1,11 +1,20 @@
const _ = require('lodash');
const config = require('../../../shared/config');
const errors = require('@tryghost/errors');
const i18n = require('../../../shared/i18n');
const tpl = require('@tryghost/tpl');
const logging = require('@tryghost/logging');
const models = require('../../models');
const mail = require('../mail');
const messages = {
setupAlreadyCompleted: 'Setup has already been completed.',
setupMustBeCompleted: 'Setup must be completed before making this request.',
setupUnableToRun: 'Database missing fixture data. Please reset database and try again.',
sampleBlogDescription: 'Thoughts, stories and ideas.',
yourNewGhostBlog: 'Your New Ghost Site',
unableToSendWelcomeEmail: 'Unable to send welcome email, your site will continue to function.'
};
/**
* Returns setup status
*
@ -29,8 +38,8 @@ function assertSetupCompleted(status) {
return __;
}
const completed = i18n.t('errors.api.authentication.setupAlreadyCompleted');
const notCompleted = i18n.t('errors.api.authentication.setupMustBeCompleted');
const completed = tpl(messages.setupAlreadyCompleted);
const notCompleted = tpl(messages.setupMustBeCompleted);
function throwReason(reason) {
throw new errors.NoPermissionError({message: reason});
@ -51,7 +60,7 @@ async function setupUser(userData) {
if (!owner) {
throw new errors.GhostError({
message: i18n.t('errors.api.authentication.setupUnableToRun')
message: tpl(messages.setupUnableToRun)
});
}
@ -76,7 +85,7 @@ async function doSettings(data, settingsAPI) {
userSettings = [
{key: 'title', value: blogTitle.trim()},
{key: 'description', value: i18n.t('common.api.authentication.sampleBlogDescription')}
{key: 'description', value: tpl(messages.sampleBlogDescription)}
];
await settingsAPI.edit({settings: userSettings}, context);
@ -118,7 +127,7 @@ function sendWelcomeEmail(email, mailAPI) {
.then((content) => {
const message = {
to: email,
subject: i18n.t('common.api.authentication.mail.yourNewGhostBlog'),
subject: tpl(messages.yourNewGhostBlog),
html: content.html,
text: content.text
};
@ -132,7 +141,7 @@ function sendWelcomeEmail(email, mailAPI) {
mailAPI.send(payload, {context: {internal: true}})
.catch((err) => {
err.context = i18n.t('errors.api.authentication.unableToSendWelcomeEmail');
err.context = tpl(messages.unableToSendWelcomeEmail);
logging.error(err);
});
});