2018-12-11 10:57:01 +03:00
|
|
|
const settingsCache = require('../settings/cache');
|
2019-05-08 15:08:25 +03:00
|
|
|
const MembersApi = require('@tryghost/members-api');
|
2020-05-28 21:30:23 +03:00
|
|
|
const logging = require('../../../shared/logging');
|
2018-12-11 10:57:01 +03:00
|
|
|
const mail = require('../mail');
|
2019-09-03 07:39:00 +03:00
|
|
|
const models = require('../../models');
|
2019-10-10 16:28:20 +03:00
|
|
|
const signinEmail = require('./emails/signin');
|
|
|
|
const signupEmail = require('./emails/signup');
|
|
|
|
const subscribeEmail = require('./emails/subscribe');
|
2020-10-29 15:09:50 +03:00
|
|
|
const updateEmail = require('./emails/updateEmail');
|
2020-09-18 19:32:18 +03:00
|
|
|
const SingleUseTokenProvider = require('./SingleUseTokenProvider');
|
2020-10-01 10:01:36 +03:00
|
|
|
const urlUtils = require('../../../shared/url-utils');
|
|
|
|
|
2020-09-30 12:53:35 +03:00
|
|
|
const MAGIC_LINK_TOKEN_VALIDITY = 24 * 60 * 60 * 1000;
|
2018-12-11 10:57:01 +03:00
|
|
|
|
2019-09-03 07:39:00 +03:00
|
|
|
const ghostMailer = new mail.GhostMailer();
|
2018-12-11 10:57:01 +03:00
|
|
|
|
2019-07-18 10:37:11 +03:00
|
|
|
module.exports = createApiInstance;
|
|
|
|
|
2020-05-28 18:55:23 +03:00
|
|
|
function createApiInstance(config) {
|
2019-07-18 10:37:11 +03:00
|
|
|
const membersApiInstance = MembersApi({
|
2019-11-07 13:24:42 +03:00
|
|
|
tokenConfig: config.getTokenConfig(),
|
2019-09-03 07:39:00 +03:00
|
|
|
auth: {
|
2020-06-02 17:20:57 +03:00
|
|
|
getSigninURL: config.getSigninURL.bind(config),
|
2019-11-07 13:24:42 +03:00
|
|
|
allowSelfSignup: config.getAllowSelfSignup(),
|
2020-09-18 19:32:18 +03:00
|
|
|
tokenProvider: new SingleUseTokenProvider(models.SingleUseToken, MAGIC_LINK_TOKEN_VALIDITY)
|
2019-07-18 10:37:11 +03:00
|
|
|
},
|
2019-09-03 07:39:00 +03:00
|
|
|
mail: {
|
|
|
|
transporter: {
|
|
|
|
sendMail(message) {
|
2019-09-16 04:33:38 +03:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2020-04-30 22:26:12 +03:00
|
|
|
logging.warn(message.text);
|
2019-09-16 04:33:38 +03:00
|
|
|
}
|
2019-10-10 16:25:48 +03:00
|
|
|
let msg = Object.assign({
|
2020-08-26 11:00:16 +03:00
|
|
|
from: config.getAuthEmailFromAddress(),
|
2019-10-10 16:25:48 +03:00
|
|
|
subject: 'Signin',
|
|
|
|
forceTextContent: true
|
|
|
|
}, message);
|
2019-10-11 07:21:53 +03:00
|
|
|
|
|
|
|
return ghostMailer.send(msg);
|
2019-09-03 07:39:00 +03:00
|
|
|
}
|
2019-10-01 10:53:23 +03:00
|
|
|
},
|
2019-10-10 16:26:18 +03:00
|
|
|
getSubject(type) {
|
|
|
|
const siteTitle = settingsCache.get('title');
|
2019-10-01 10:53:23 +03:00
|
|
|
switch (type) {
|
|
|
|
case 'subscribe':
|
2019-10-10 16:26:18 +03:00
|
|
|
return `📫 Confirm your subscription to ${siteTitle}`;
|
2019-10-01 10:53:23 +03:00
|
|
|
case 'signup':
|
2019-10-10 16:26:18 +03:00
|
|
|
return `🙌 Complete your sign up to ${siteTitle}!`;
|
2020-06-01 14:21:18 +03:00
|
|
|
case 'updateEmail':
|
|
|
|
return `📫 Confirm your email update for ${siteTitle}!`;
|
2019-10-01 10:53:23 +03:00
|
|
|
case 'signin':
|
|
|
|
default:
|
2019-10-10 16:26:18 +03:00
|
|
|
return `🔑 Secure sign in link for ${siteTitle}`;
|
|
|
|
}
|
|
|
|
},
|
2019-10-10 16:43:30 +03:00
|
|
|
getText(url, type, email) {
|
|
|
|
const siteTitle = settingsCache.get('title');
|
2019-10-01 10:53:23 +03:00
|
|
|
switch (type) {
|
|
|
|
case 'subscribe':
|
2019-10-10 16:43:30 +03:00
|
|
|
return `
|
|
|
|
Hey there,
|
|
|
|
|
|
|
|
You're one tap away from subscribing to ${siteTitle} — please confirm your email address with this link:
|
|
|
|
|
|
|
|
${url}
|
|
|
|
|
2020-09-30 12:53:35 +03:00
|
|
|
For your security, the link will expire in 24 hours time.
|
2019-10-10 16:43:30 +03:00
|
|
|
|
|
|
|
All the best!
|
|
|
|
The team at ${siteTitle}
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
Sent to ${email}
|
|
|
|
If you did not make this request, you can simply delete this message. You will not be subscribed.
|
|
|
|
`;
|
2019-10-01 10:53:23 +03:00
|
|
|
case 'signup':
|
2019-10-10 16:43:30 +03:00
|
|
|
return `
|
|
|
|
Hey there!
|
|
|
|
|
|
|
|
Thanks for signing up for ${siteTitle} — use this link to complete the sign up process and be automatically signed in:
|
|
|
|
|
|
|
|
${url}
|
|
|
|
|
2020-09-30 12:53:35 +03:00
|
|
|
For your security, the link will expire in 24 hours time.
|
2019-10-10 16:43:30 +03:00
|
|
|
|
|
|
|
See you soon!
|
|
|
|
The team at ${siteTitle}
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
Sent to ${email}
|
|
|
|
If you did not make this request, you can simply delete this message. You will not be signed up, and no account will be created for you.
|
|
|
|
`;
|
2020-06-01 14:21:18 +03:00
|
|
|
case 'updateEmail':
|
|
|
|
return `
|
2020-10-29 15:09:50 +03:00
|
|
|
Hey there,
|
2020-06-01 14:21:18 +03:00
|
|
|
|
2020-10-29 15:09:50 +03:00
|
|
|
Please confirm your email address with this link:
|
2020-06-01 14:21:18 +03:00
|
|
|
|
2020-10-29 15:09:50 +03:00
|
|
|
${url}
|
2020-06-01 14:21:18 +03:00
|
|
|
|
2020-10-29 15:09:50 +03:00
|
|
|
For your security, the link will expire in 24 hours time.
|
2020-06-01 14:21:18 +03:00
|
|
|
|
2020-10-29 15:09:50 +03:00
|
|
|
---
|
2020-06-01 14:21:18 +03:00
|
|
|
|
2020-10-29 15:09:50 +03:00
|
|
|
Sent to ${email}
|
|
|
|
If you did not make this request, you can simply delete this message. This email address will not be used.
|
|
|
|
`;
|
2019-10-01 10:53:23 +03:00
|
|
|
case 'signin':
|
|
|
|
default:
|
2019-10-10 16:43:30 +03:00
|
|
|
return `
|
|
|
|
Hey there,
|
|
|
|
|
|
|
|
Welcome back! Use this link to securely sign in to your ${siteTitle} account:
|
|
|
|
|
|
|
|
${url}
|
|
|
|
|
2020-09-30 12:53:35 +03:00
|
|
|
For your security, the link will expire in 24 hours time.
|
2019-10-10 16:43:30 +03:00
|
|
|
|
|
|
|
See you soon!
|
|
|
|
The team at ${siteTitle}
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
Sent to ${email}
|
|
|
|
If you did not make this request, you can safely ignore this email.
|
|
|
|
`;
|
2019-10-01 10:53:23 +03:00
|
|
|
}
|
|
|
|
},
|
2019-10-10 16:28:20 +03:00
|
|
|
getHTML(url, type, email) {
|
|
|
|
const siteTitle = settingsCache.get('title');
|
2020-10-01 10:01:36 +03:00
|
|
|
const siteUrl = urlUtils.urlFor('home', true);
|
|
|
|
const domain = urlUtils.urlFor('home', true).match(new RegExp('^https?://([^/:?#]+)(?:[/:?#]|$)', 'i'));
|
|
|
|
const siteDomain = (domain && domain[1]);
|
2021-03-24 15:55:26 +03:00
|
|
|
const accentColor = settingsCache.get('accent_color');
|
2019-10-01 10:53:23 +03:00
|
|
|
switch (type) {
|
|
|
|
case 'subscribe':
|
2020-10-01 10:01:36 +03:00
|
|
|
return subscribeEmail({url, email, siteTitle, accentColor, siteDomain, siteUrl});
|
2019-10-01 10:53:23 +03:00
|
|
|
case 'signup':
|
2020-10-01 10:01:36 +03:00
|
|
|
return signupEmail({url, email, siteTitle, accentColor, siteDomain, siteUrl});
|
2020-06-01 14:21:18 +03:00
|
|
|
case 'updateEmail':
|
2020-10-29 15:09:50 +03:00
|
|
|
return updateEmail({url, email, siteTitle, accentColor, siteDomain, siteUrl});
|
2019-10-01 10:53:23 +03:00
|
|
|
case 'signin':
|
|
|
|
default:
|
2020-10-01 10:01:36 +03:00
|
|
|
return signinEmail({url, email, siteTitle, accentColor, siteDomain, siteUrl});
|
2019-10-01 10:53:23 +03:00
|
|
|
}
|
2019-09-03 07:39:00 +03:00
|
|
|
}
|
Updated theme layer to use members-ssr (#10676)
* Removed support for cookies in members auth middleware
no-issue
The members middleware will no longer be supporting cookies, the cookie
will be handled by a new middleware specific for serverside rendering,
more informations can be found here:
https://paper.dropbox.com/doc/Members-Auth-II-4WP4vF6coMqDYbSMIajo5
* Removed members auth middleware from site app
no-issue
The site app no longer needs the members auth middleware as it doesn't
support cookies, and will be replaced by ssr specific middleware.
https://paper.dropbox.com/doc/Members-Auth-II-4WP4vF6coMqDYbSMIajo5
* Added comment for session_secret setting
no-issue
We are going to have multiple concepts of sessions, so adding a comment
here to be specific that this is for the Ghost Admin client
* Added theme_session_secret setting dynamic default
no-issue
Sessions for the theme layer will be signed, so we generate a random hex
string to use as a signing key
* Added getPublicConfig method
* Replaced export of httpHandler with POJO apiInstance
no-issue
This is mainly to reduce the public api, so it's easier to document.
* Renamed memberUserObject -> members
no-issue
Simplifies the interface, and is more inline with what we would want to export as an api library.
* Removed use of require options inside members
no-issue
This was too tight of a coupling between Ghost and Members
* Simplified apiInstance definition
no-issue
* Added getMember method to members api
* Added MembersSSR instance to members service
* Wired up routes for members ssr
* Updated members auth middleware to use getPublicConfig
* Removed publicKey static export from members service
* Used real session secret
no-issue
* Added DELETE /members/ssr handler
no-issue
This allows users to log out of the theme layer
* Fixed missing code property
no-issue
Ignition uses the statusCode property to forward status codes to call sites
* Removed superfluous error middleware
no-issue
Before we used generic JWT middleware which would reject, now the
middleware catches it's own error and doesn't error, thus this
middleware is unecessary.
* Removed console.logs
no-issue
* Updated token expirty to hardcoded 20 minutes
no-issue
This returns to our previous state of using short lived tokens, both for
security and simplicity.
* Removed hardcoded default member settings
no-issue
This is no longer needed, as defaults are in default-settings.json
* Removed stripe from default payment processor
no-issue
* Exported `getSiteUrl` method from url utils
no-issue
This keeps inline with newer naming conventions
* Updated how audience access control works
no-issue
Rather than being passed a function, members api now receives an object
which describes which origins have access to which audiences, and how
long those tokens should be allowed to work for. It also allows syntax
for default tokens where audience === origin requesting it. This can be
set to undefined or null to disable this functionality.
{
"http://site.com": {
"http://site.com": {
tokenLength: '5m'
},
"http://othersite.com": {
tokenLength: '1h'
}
},
"*": {
tokenLength: '30m'
}
}
* Updated members service to use access control feature
no-issue
This also cleans up a lot of unecessary variable definitions, and some
other minor cleanups.
* Added status code to auth pages html response
no-issue
This was missing, probably default but better to be explicit
* Updated gateway to have membersApiUrl from config
no-issue
Previously we were parsing the url, this was not very safe as we can
have Ghost hosted on a subdomain, and this would have failed.
* Added issuer to public config for members
no-issue
This can be used to request SSR tokens in the client
* Fixed path for gateway bundle
no-issue
* Updated settings model tests
no-issue
* Revert "Removed stripe from default payment processor"
This reverts commit 1d88d9b6d73a10091070bcc1b7f5779d071c7845.
* Revert "Removed hardcoded default member settings"
This reverts commit 9d899048ba7d4b272b9ac65a95a52af66b30914a.
* Installed @tryghost/members-ssr
* Fixed tests for settings model
2019-04-16 17:50:25 +03:00
|
|
|
},
|
2019-09-06 10:14:21 +03:00
|
|
|
paymentConfig: {
|
2019-11-07 13:24:42 +03:00
|
|
|
stripe: config.getStripePaymentConfig()
|
2019-09-06 10:14:21 +03:00
|
|
|
},
|
2020-07-09 17:17:54 +03:00
|
|
|
models: {
|
|
|
|
/**
|
|
|
|
* Settings do not have their own models, so we wrap the webhook in a "fake" model
|
|
|
|
*/
|
|
|
|
StripeWebhook: {
|
|
|
|
async upsert(data, options) {
|
|
|
|
const settings = [{
|
|
|
|
key: 'members_stripe_webhook_id',
|
|
|
|
value: data.webhook_id
|
|
|
|
}, {
|
|
|
|
key: 'members_stripe_webhook_secret',
|
|
|
|
value: data.secret
|
|
|
|
}];
|
|
|
|
await models.Settings.edit(settings, options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
StripeCustomer: models.MemberStripeCustomer,
|
|
|
|
StripeCustomerSubscription: models.StripeCustomerSubscription,
|
2021-02-04 12:29:04 +03:00
|
|
|
Member: models.Member,
|
2021-02-12 16:13:16 +03:00
|
|
|
MemberSubscribeEvent: models.MemberSubscribeEvent,
|
2021-02-12 16:15:47 +03:00
|
|
|
MemberPaidSubscriptionEvent: models.MemberPaidSubscriptionEvent,
|
2021-02-12 16:14:52 +03:00
|
|
|
MemberLoginEvent: models.MemberLoginEvent,
|
2021-02-12 16:15:27 +03:00
|
|
|
MemberEmailChangeEvent: models.MemberEmailChangeEvent,
|
2021-02-12 16:13:56 +03:00
|
|
|
MemberPaymentEvent: models.MemberPaymentEvent,
|
2021-02-12 16:13:16 +03:00
|
|
|
MemberStatusEvent: models.MemberStatusEvent
|
2020-07-09 17:17:54 +03:00
|
|
|
},
|
2020-04-30 22:26:12 +03:00
|
|
|
logger: logging
|
Updated theme layer to use members-ssr (#10676)
* Removed support for cookies in members auth middleware
no-issue
The members middleware will no longer be supporting cookies, the cookie
will be handled by a new middleware specific for serverside rendering,
more informations can be found here:
https://paper.dropbox.com/doc/Members-Auth-II-4WP4vF6coMqDYbSMIajo5
* Removed members auth middleware from site app
no-issue
The site app no longer needs the members auth middleware as it doesn't
support cookies, and will be replaced by ssr specific middleware.
https://paper.dropbox.com/doc/Members-Auth-II-4WP4vF6coMqDYbSMIajo5
* Added comment for session_secret setting
no-issue
We are going to have multiple concepts of sessions, so adding a comment
here to be specific that this is for the Ghost Admin client
* Added theme_session_secret setting dynamic default
no-issue
Sessions for the theme layer will be signed, so we generate a random hex
string to use as a signing key
* Added getPublicConfig method
* Replaced export of httpHandler with POJO apiInstance
no-issue
This is mainly to reduce the public api, so it's easier to document.
* Renamed memberUserObject -> members
no-issue
Simplifies the interface, and is more inline with what we would want to export as an api library.
* Removed use of require options inside members
no-issue
This was too tight of a coupling between Ghost and Members
* Simplified apiInstance definition
no-issue
* Added getMember method to members api
* Added MembersSSR instance to members service
* Wired up routes for members ssr
* Updated members auth middleware to use getPublicConfig
* Removed publicKey static export from members service
* Used real session secret
no-issue
* Added DELETE /members/ssr handler
no-issue
This allows users to log out of the theme layer
* Fixed missing code property
no-issue
Ignition uses the statusCode property to forward status codes to call sites
* Removed superfluous error middleware
no-issue
Before we used generic JWT middleware which would reject, now the
middleware catches it's own error and doesn't error, thus this
middleware is unecessary.
* Removed console.logs
no-issue
* Updated token expirty to hardcoded 20 minutes
no-issue
This returns to our previous state of using short lived tokens, both for
security and simplicity.
* Removed hardcoded default member settings
no-issue
This is no longer needed, as defaults are in default-settings.json
* Removed stripe from default payment processor
no-issue
* Exported `getSiteUrl` method from url utils
no-issue
This keeps inline with newer naming conventions
* Updated how audience access control works
no-issue
Rather than being passed a function, members api now receives an object
which describes which origins have access to which audiences, and how
long those tokens should be allowed to work for. It also allows syntax
for default tokens where audience === origin requesting it. This can be
set to undefined or null to disable this functionality.
{
"http://site.com": {
"http://site.com": {
tokenLength: '5m'
},
"http://othersite.com": {
tokenLength: '1h'
}
},
"*": {
tokenLength: '30m'
}
}
* Updated members service to use access control feature
no-issue
This also cleans up a lot of unecessary variable definitions, and some
other minor cleanups.
* Added status code to auth pages html response
no-issue
This was missing, probably default but better to be explicit
* Updated gateway to have membersApiUrl from config
no-issue
Previously we were parsing the url, this was not very safe as we can
have Ghost hosted on a subdomain, and this would have failed.
* Added issuer to public config for members
no-issue
This can be used to request SSR tokens in the client
* Fixed path for gateway bundle
no-issue
* Updated settings model tests
no-issue
* Revert "Removed stripe from default payment processor"
This reverts commit 1d88d9b6d73a10091070bcc1b7f5779d071c7845.
* Revert "Removed hardcoded default member settings"
This reverts commit 9d899048ba7d4b272b9ac65a95a52af66b30914a.
* Installed @tryghost/members-ssr
* Fixed tests for settings model
2019-04-16 17:50:25 +03:00
|
|
|
});
|
2019-02-26 06:09:16 +03:00
|
|
|
|
2019-07-18 10:37:11 +03:00
|
|
|
return membersApiInstance;
|
|
|
|
}
|