Merge branch 'master' into 1588-frontend-show-link-information

This commit is contained in:
Alexander Friedland 2022-03-21 13:08:48 +01:00 committed by GitHub
commit 8bbe9401b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 66 additions and 32 deletions

View File

@ -40,7 +40,7 @@ EMAIL_SMTP_URL=gmail.com
EMAIL_SMTP_PORT=587 EMAIL_SMTP_PORT=587
EMAIL_LINK_VERIFICATION=http://localhost/checkEmail/{code} EMAIL_LINK_VERIFICATION=http://localhost/checkEmail/{code}
EMAIL_LINK_SETPASSWORD=http://localhost/reset/{code} EMAIL_LINK_SETPASSWORD=http://localhost/reset/{code}
RESEND_TIME=10 EMAIL_CODE_VALID_TIME=10
# Webhook # Webhook
WEBHOOK_ELOPAGE_SECRET=secret WEBHOOK_ELOPAGE_SECRET=secret

View File

@ -8,4 +8,5 @@ export const INALIENABLE_RIGHTS = [
RIGHTS.SEND_RESET_PASSWORD_EMAIL, RIGHTS.SEND_RESET_PASSWORD_EMAIL,
RIGHTS.SET_PASSWORD, RIGHTS.SET_PASSWORD,
RIGHTS.QUERY_TRANSACTION_LINK, RIGHTS.QUERY_TRANSACTION_LINK,
RIGHTS.QUERY_OPT_IN,
] ]

View File

@ -16,6 +16,7 @@ export enum RIGHTS {
CREATE_USER = 'CREATE_USER', CREATE_USER = 'CREATE_USER',
SEND_RESET_PASSWORD_EMAIL = 'SEND_RESET_PASSWORD_EMAIL', SEND_RESET_PASSWORD_EMAIL = 'SEND_RESET_PASSWORD_EMAIL',
SET_PASSWORD = 'SET_PASSWORD', SET_PASSWORD = 'SET_PASSWORD',
QUERY_OPT_IN = 'QUERY_OPT_IN',
UPDATE_USER_INFOS = 'UPDATE_USER_INFOS', UPDATE_USER_INFOS = 'UPDATE_USER_INFOS',
HAS_ELOPAGE = 'HAS_ELOPAGE', HAS_ELOPAGE = 'HAS_ELOPAGE',
CREATE_TRANSACTION_LINK = 'CREATE_TRANSACTION_LINK', CREATE_TRANSACTION_LINK = 'CREATE_TRANSACTION_LINK',

View File

@ -54,8 +54,6 @@ const loginServer = {
LOGIN_SERVER_KEY: process.env.LOGIN_SERVER_KEY || 'a51ef8ac7ef1abf162fb7a65261acd7a', LOGIN_SERVER_KEY: process.env.LOGIN_SERVER_KEY || 'a51ef8ac7ef1abf162fb7a65261acd7a',
} }
// TODO: Hannes if I find you... this looks like blasphemy
const resendTime = parseInt(process.env.RESEND_TIME ? process.env.RESEND_TIME : 'null')
const email = { const email = {
EMAIL: process.env.EMAIL === 'true' || false, EMAIL: process.env.EMAIL === 'true' || false,
EMAIL_USERNAME: process.env.EMAIL_USERNAME || 'gradido_email', EMAIL_USERNAME: process.env.EMAIL_USERNAME || 'gradido_email',
@ -67,7 +65,9 @@ const email = {
process.env.EMAIL_LINK_VERIFICATION || 'http://localhost/checkEmail/{code}', process.env.EMAIL_LINK_VERIFICATION || 'http://localhost/checkEmail/{code}',
EMAIL_LINK_SETPASSWORD: EMAIL_LINK_SETPASSWORD:
process.env.EMAIL_LINK_SETPASSWORD || 'http://localhost/reset-password/{code}', process.env.EMAIL_LINK_SETPASSWORD || 'http://localhost/reset-password/{code}',
RESEND_TIME: isNaN(resendTime) ? 10 : resendTime, EMAIL_CODE_VALID_TIME: process.env.EMAIL_CODE_VALID_TIME
? parseInt(process.env.EMAIL_CODE_VALID_TIME) || 10
: 10,
} }
const webhook = { const webhook = {

View File

@ -156,15 +156,11 @@ const createEmailOptIn = async (
emailOptInTypeId: EMAIL_OPT_IN_REGISTER, emailOptInTypeId: EMAIL_OPT_IN_REGISTER,
}) })
if (emailOptIn) { if (emailOptIn) {
const timeElapsed = Date.now() - new Date(emailOptIn.updatedAt).getTime() if (isOptInCodeValid(emailOptIn)) {
if (timeElapsed <= parseInt(CONFIG.RESEND_TIME.toString()) * 60 * 1000) { throw new Error(`email already sent less than $(CONFIG.EMAIL_CODE_VALID_TIME} minutes ago`)
throw new Error(
'email already sent less than ' + parseInt(CONFIG.RESEND_TIME.toString()) + ' minutes ago',
)
} else {
emailOptIn.updatedAt = new Date()
emailOptIn.resendCount++
} }
emailOptIn.updatedAt = new Date()
emailOptIn.resendCount++
} else { } else {
emailOptIn = new LoginEmailOptIn() emailOptIn = new LoginEmailOptIn()
emailOptIn.verificationCode = random(64) emailOptIn.verificationCode = random(64)
@ -185,17 +181,13 @@ const getOptInCode = async (loginUserId: number): Promise<LoginEmailOptIn> => {
emailOptInTypeId: EMAIL_OPT_IN_RESET_PASSWORD, emailOptInTypeId: EMAIL_OPT_IN_RESET_PASSWORD,
}) })
// Check for 10 minute delay // Check for `CONFIG.EMAIL_CODE_VALID_TIME` minute delay
if (optInCode) { if (optInCode) {
const timeElapsed = Date.now() - new Date(optInCode.updatedAt).getTime() if (isOptInCodeValid(optInCode)) {
if (timeElapsed <= parseInt(CONFIG.RESEND_TIME.toString()) * 60 * 1000) { throw new Error(`email already sent less than $(CONFIG.EMAIL_CODE_VALID_TIME} minutes ago`)
throw new Error(
'email already sent less than ' + parseInt(CONFIG.RESEND_TIME.toString()) + ' minutes ago',
)
} else {
optInCode.updatedAt = new Date()
optInCode.resendCount++
} }
optInCode.updatedAt = new Date()
optInCode.resendCount++
} else { } else {
optInCode = new LoginEmailOptIn() optInCode = new LoginEmailOptIn()
optInCode.verificationCode = random(64) optInCode.verificationCode = random(64)
@ -492,10 +484,9 @@ export class UserResolver {
throw new Error('Could not login with emailVerificationCode') throw new Error('Could not login with emailVerificationCode')
}) })
// Code is only valid for 10minutes // Code is only valid for `CONFIG.EMAIL_CODE_VALID_TIME` minutes
const timeElapsed = Date.now() - new Date(optInCode.updatedAt).getTime() if (!isOptInCodeValid(optInCode)) {
if (timeElapsed > 10 * 60 * 1000) { throw new Error(`email already more than $(CONFIG.EMAIL_CODE_VALID_TIME} minutes ago`)
throw new Error('Code is older than 10 minutes')
} }
// load user // load user
@ -568,6 +559,17 @@ export class UserResolver {
return true return true
} }
@Authorized([RIGHTS.QUERY_OPT_IN])
@Query(() => Boolean)
async queryOptIn(@Arg('optIn') optIn: string): Promise<boolean> {
const optInCode = await LoginEmailOptIn.findOneOrFail({ verificationCode: optIn })
// Code is only valid for `CONFIG.EMAIL_CODE_VALID_TIME` minutes
if (!isOptInCodeValid(optInCode)) {
throw new Error(`email was sent more than $(CONFIG.EMAIL_CODE_VALID_TIME} minutes ago`)
}
return true
}
@Authorized([RIGHTS.UPDATE_USER_INFOS]) @Authorized([RIGHTS.UPDATE_USER_INFOS])
@Mutation(() => Boolean) @Mutation(() => Boolean)
async updateUserInfos( async updateUserInfos(
@ -669,3 +671,7 @@ export class UserResolver {
return hasElopageBuys(userEntity.email) return hasElopageBuys(userEntity.email)
} }
} }
function isOptInCodeValid(optInCode: LoginEmailOptIn) {
const timeElapsed = Date.now() - new Date(optInCode.updatedAt).getTime()
return timeElapsed <= CONFIG.EMAIL_CODE_VALID_TIME * 60 * 1000
}

View File

@ -119,6 +119,12 @@ export const communities = gql`
} }
` `
export const queryOptIn = gql`
query($optIn: String!) {
queryOptIn(optIn: $optIn)
}
`
export const queryTransactionLink = gql` export const queryTransactionLink = gql`
query($code: String!) { query($code: String!) {
queryTransactionLink(code: $code) { queryTransactionLink(code: $code) {

View File

@ -220,13 +220,14 @@
"uppercase": "Großbuchstabe erforderlich." "uppercase": "Großbuchstabe erforderlich."
}, },
"thx": { "thx": {
"activateEmail": "Dein Konto wurde noch nicht aktiviert. Bitte überprüfe deine E-Mail und klicke den Aktivierungslink!", "activateEmail": "Dein Konto wurde noch nicht aktiviert. Bitte überprüfe deine E-Mail und klicke den Aktivierungslink oder fordere einen neuen Aktivierungslink über die Password Reset Seite.",
"checkEmail": "Deine E-Mail wurde erfolgreich verifiziert. Du kannst dich jetzt anmelden.", "checkEmail": "Deine E-Mail wurde erfolgreich verifiziert. Du kannst dich jetzt anmelden.",
"email": "Wir haben dir eine E-Mail gesendet.", "email": "Wir haben dir eine E-Mail gesendet.",
"emailActivated": "Danke dass Du deine E-Mail bestätigt hast.", "emailActivated": "Danke dass Du deine E-Mail bestätigt hast.",
"errorTitle": "Achtung!", "errorTitle": "Achtung!",
"register": "Du bist jetzt registriert, bitte überprüfe deine Emails und klicke auf den Aktivierungslink.", "register": "Du bist jetzt registriert, bitte überprüfe deine Emails und klicke auf den Aktivierungslink.",
"reset": "Dein Passwort wurde geändert.", "reset": "Dein Passwort wurde geändert.",
"resetPassword": "Den Code den Du genutzt hast ist zu alt bitte fordere ein neuen über die Passwort Reset Seite an.",
"title": "Danke!" "title": "Danke!"
} }
}, },

View File

@ -220,13 +220,14 @@
"uppercase": "One uppercase letter required." "uppercase": "One uppercase letter required."
}, },
"thx": { "thx": {
"activateEmail": "Your account has not been activated yet, please check your emails and click the activation link!", "activateEmail": "Your account has not been activated yet, please check your emails and click the activation link or order a new activation link over the password reset page.",
"checkEmail": "Your email has been successfully verified. You can sign in now.", "checkEmail": "Your email has been successfully verified. You can sign in now.",
"email": "We have sent you an email.", "email": "We have sent you an email.",
"emailActivated": "Thank you your email has been activated.", "emailActivated": "Thank you your email has been activated.",
"errorTitle": "Attention!", "errorTitle": "Attention!",
"register": "You are registered now, please check your emails and click the activation link.", "register": "You are registered now, please check your emails and click the activation link.",
"reset": "Your password has been changed.", "reset": "Your password has been changed.",
"resetPassword": "The code you used was to old please order a new on over the password reset page.",
"title": "Thank you!" "title": "Thank you!"
} }
}, },

View File

@ -9,6 +9,7 @@ import { toastErrorSpy } from '@test/testSetup'
const localVue = global.localVue const localVue = global.localVue
const apolloMutationMock = jest.fn() const apolloMutationMock = jest.fn()
const apolloQueryMock = jest.fn().mockResolvedValue()
const routerPushMock = jest.fn() const routerPushMock = jest.fn()
@ -40,6 +41,7 @@ const mocks = {
}, },
$apollo: { $apollo: {
mutate: apolloMutationMock, mutate: apolloMutationMock,
query: apolloQueryMock,
}, },
} }

View File

@ -49,6 +49,7 @@
<script> <script>
import InputPasswordConfirmation from '@/components/Inputs/InputPasswordConfirmation' import InputPasswordConfirmation from '@/components/Inputs/InputPasswordConfirmation'
import { setPassword } from '@/graphql/mutations' import { setPassword } from '@/graphql/mutations'
import { queryOptIn } from '@/graphql/queries'
const textFields = { const textFields = {
reset: { reset: {
@ -107,7 +108,22 @@ export default {
this.$router.push('/forgot-password/resetPassword') this.$router.push('/forgot-password/resetPassword')
}) })
}, },
checkOptInCode() {
this.$apollo
.query({
query: queryOptIn,
variables: {
optIn: this.$route.params.optin,
},
})
.then()
.catch((error) => {
this.toastError(error.message)
this.$router.push('/forgot-password/resetPassword')
})
},
setDisplaySetup() { setDisplaySetup() {
this.checkOptInCode()
if (this.$route.path.includes('checkEmail')) { if (this.$route.path.includes('checkEmail')) {
this.displaySetup = textFields.checkEmail this.displaySetup = textFields.checkEmail
} }

View File

@ -99,11 +99,11 @@ describe('Thx', () => {
}) })
it('renders the thanks redirect button', () => { it('renders the thanks redirect button', () => {
expect(wrapper.find('a.btn').text()).toBe('login') expect(wrapper.find('a.btn').text()).toBe('settings.password.reset')
}) })
it('links the redirect button to /login', () => { it('links the redirect button to /forgot-password', () => {
expect(wrapper.find('a.btn').attributes('href')).toBe('/login') expect(wrapper.find('a.btn').attributes('href')).toBe('/forgot-password')
}) })
}) })
}) })

View File

@ -48,8 +48,8 @@ const textFields = {
login: { login: {
headline: 'site.thx.errorTitle', headline: 'site.thx.errorTitle',
subtitle: 'site.thx.activateEmail', subtitle: 'site.thx.activateEmail',
button: 'login', button: 'settings.password.reset',
linkTo: '/login', linkTo: '/forgot-password',
}, },
} }