Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add loginMattermost private mutation #10943

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/server/graphql/private/mutations/loginMattermost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import AuthToken from '../../../database/types/AuthToken'
import getKysely from '../../../postgres/getKysely'
import encodeAuthToken from '../../../utils/encodeAuthToken'
import {MutationResolvers} from '../resolverTypes'

const loginMattermost: MutationResolvers['loginMattermost'] = async (
_source,
{email}
) => {
const pg = getKysely()
const user = await pg
.selectFrom('User')
.selectAll()
.where('email', '=', email)
.executeTakeFirst()
if (!user) {
return {error: {message: 'Unknown user'}}
}
const {id: userId, tms} = user
const authToken = new AuthToken({sub: userId, tms})

return {
userId,
authToken: encodeAuthToken(authToken),
isNewUser: false
}
}

export default loginMattermost
25 changes: 25 additions & 0 deletions packages/server/graphql/private/typeDefs/Mutation.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,21 @@ type Mutation {
isDangerous: Boolean!
): String

"""
add/remove a flag on an org asking them to pay
"""
flagConversionModal(
"""
true to turn the modal on, false to turn it off
"""
active: Boolean!

"""
the orgId to toggle the flag for
"""
orgId: ID!
): FlagConversionModalPayload

"""
hard deletes a user and all its associated objects
"""
Expand Down Expand Up @@ -436,6 +451,16 @@ type Mutation {
samlName: ID!
): UserLogInPayload!

"""
Log in with email from a trusted Mattermost
"""
loginMattermost(
"""
The email of the user
"""
email: Email!
): UserLogInPayload!

"""
Processes recurrence for meetings that should start and end.
"""
Expand Down
56 changes: 44 additions & 12 deletions packages/server/integrations/mattermost/mattermostWebhookHandler.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
import {createVerifier, httpbis} from 'http-message-signatures'
import {HttpRequest, HttpResponse} from 'uWebSockets.js'
import appOrigin from '../../appOrigin'
import AuthToken from '../../database/types/AuthToken'
import uWSAsyncHandler from '../../graphql/uWSAsyncHandler'
import parseBody from '../../parseBody'
import getKysely from '../../postgres/getKysely'
import publishWebhookGQL from '../../utils/publishWebhookGQL'
import ConnectionContext from '../../socketHelpers/ConnectionContext'
import uwsGetIP from '../../utils/uwsGetIP'
import checkBlacklistJWT from '../../utils/checkBlacklistJWT'
import activeClients from '../../activeClients'
import handleConnect from '../../socketHandlers/handleConnect'
import getVerifiedAuthToken from '../../utils/getVerifiedAuthToken'
import encodeAuthToken from '../../utils/encodeAuthToken'
import {MMSocket} from '../../socketHelpers/transports/MMSocket'

const MATTERMOST_SECRET = process.env.MATTERMOST_SECRET


const login = async (email: string) => {
const pg = getKysely()
const user = await pg
.selectFrom('User')
.selectAll()
.where('email', '=', email)
.executeTakeFirstOrThrow()
const authToken = new AuthToken({sub: user.id, tms: user.tms})
return encodeAuthToken(authToken)
const query = `
mutation LoginMattermost($email: String!) {
loginMattermost(email: $email) {
error {
message
}
authToken
}
}
`

const loginResult = await publishWebhookGQL<any>(query, {email})
const {authToken} = loginResult?.data?.loginMattermost ?? {}
return authToken as string
}

const mattermostWebhookHandler = uWSAsyncHandler(async (res: HttpResponse, req: HttpRequest) => {
if (!MATTERMOST_SECRET) {
res.writeStatus('404').end()
return
}

const ip = uwsGetIP(res, req)
const connectionId = req.getHeader('x-correlation-id')

const headers = {
'content-type': req.getHeader('content-type'),
'content-digest': req.getHeader('content-digest'),
Expand Down Expand Up @@ -66,11 +83,26 @@ const mattermostWebhookHandler = uWSAsyncHandler(async (res: HttpResponse, req:
return
}

const authToken = await login(email)
const authToken = getVerifiedAuthToken(await login(email))

const connectionContext = new ConnectionContext(new MMSocket(connectionId), authToken, ip)
// TODO: Find a nicer solution to this. This is the easiest to multiplex the webhook in the MM-Plugin.
// If we don't use the client provided connectionId, the plugin would need to keep state to correlate userIDs with connectionIds.
// This might be superseeded once we establish the Parabol userId <-> Mattermost userId mapping on Parabol side.
connectionContext.id = connectionId
const {sub: userId, iat} = authToken
const isBlacklistedJWT = await checkBlacklistJWT(userId, iat)
if (isBlacklistedJWT) {
return
}

activeClients.set(connectionContext)
const nextAuthToken = await handleConnect(connectionContext)

res
.writeStatus('200')
.writeHeader('Content-Type', 'application/json')
.end(JSON.stringify({authToken}))
.end(JSON.stringify({authToken: nextAuthToken ?? encodeAuthToken(authToken)}))
})

export default mattermostWebhookHandler
Loading