-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
suse dedicated submitUserProfile function for backend
- Loading branch information
Showing
4 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Session } from '@dm3-org/dm3-lib-delivery'; | ||
import { SignedUserProfile, normalizeEnsName } from '@dm3-org/dm3-lib-profile'; | ||
|
||
export async function getUserProfile( | ||
getAccount: (accountAddress: string) => Promise<Session | null>, | ||
ensName: string, | ||
): Promise<SignedUserProfile | undefined> { | ||
const account = normalizeEnsName(ensName); | ||
const session = await getAccount(account); | ||
return session?.signedUserProfile; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { normalizeEnsName, schema } from '@dm3-org/dm3-lib-profile'; | ||
import { validateSchema } from '@dm3-org/dm3-lib-shared'; | ||
import express from 'express'; | ||
import { ethers } from 'ethers'; | ||
import { Server } from 'socket.io'; | ||
import { IDatabase } from '../persistence/getDatabase'; | ||
import { getUserProfile } from './getUserProfile'; | ||
import { submitUserProfile } from './submitUserProfile'; | ||
|
||
export default ( | ||
db: IDatabase, | ||
web3Provider: ethers.providers.JsonRpcProvider, | ||
serverSecret: string, | ||
) => { | ||
const router = express.Router(); | ||
|
||
router.get('/:ensName', async (req: express.Request, res, next) => { | ||
try { | ||
const ensName = normalizeEnsName(req.params.ensName); | ||
|
||
const profile = await getUserProfile(db.getAccount, ensName); | ||
if (profile) { | ||
res.json(profile); | ||
} else { | ||
res.sendStatus(404); | ||
} | ||
} catch (e) { | ||
next(e); | ||
} | ||
}); | ||
|
||
router.post('/:ensName', async (req: express.Request, res, next) => { | ||
try { | ||
const schemaIsValid = validateSchema( | ||
schema.SignedUserProfile, | ||
req.body, | ||
); | ||
|
||
if (!schemaIsValid) { | ||
console.error({ message: 'invalid schema' }); | ||
return res.status(400).send({ error: 'invalid schema' }); | ||
} | ||
const ensName = normalizeEnsName(req.params.ensName); | ||
console.debug({ | ||
method: 'POST', | ||
url: req.url, | ||
ensName, | ||
disableSessionCheck: | ||
process.env.DISABLE_SESSION_CHECK === 'true', | ||
}); | ||
|
||
const data = await submitUserProfile( | ||
web3Provider, | ||
db.getAccount, | ||
db.setAccount, | ||
ensName, | ||
req.body, | ||
serverSecret, | ||
); | ||
console.debug({ | ||
message: 'POST profile', | ||
ensName, | ||
data, | ||
}); | ||
|
||
res.json(data); | ||
} catch (e) { | ||
console.warn({ | ||
message: 'POST profile', | ||
error: JSON.stringify(e), | ||
}); | ||
// eslint-disable-next-line no-console | ||
console.log('POST PROFILE ERROR', e); | ||
res.status(400).send({ | ||
message: `Couldn't store profile`, | ||
error: JSON.stringify(e), | ||
}); | ||
} | ||
}); | ||
|
||
return router; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Session, generateAuthJWT } from '@dm3-org/dm3-lib-delivery'; | ||
import { | ||
SignedUserProfile, | ||
normalizeEnsName, | ||
checkUserProfile, | ||
getDefaultProfileExtension, | ||
} from '@dm3-org/dm3-lib-profile'; | ||
import { logDebug } from '@dm3-org/dm3-lib-shared'; | ||
import { ethers } from 'ethers'; | ||
|
||
export async function submitUserProfile( | ||
provider: ethers.providers.JsonRpcProvider, | ||
getAccount: (accountAddress: string) => Promise<Session | null>, | ||
setAccount: (accountAddress: string, session: Session) => Promise<void>, | ||
ensName: string, | ||
signedUserProfile: SignedUserProfile, | ||
serverSecret: string, | ||
): Promise<string> { | ||
const account = normalizeEnsName(ensName); | ||
|
||
if (!(await checkUserProfile(provider, signedUserProfile, account))) { | ||
logDebug('submitUserProfile - Signature invalid'); | ||
throw Error('Signature invalid.'); | ||
} | ||
if (await getAccount(account)) { | ||
logDebug('submitUserProfile - Profile exists already'); | ||
throw Error('Profile exists already'); | ||
} | ||
const session: Session = { | ||
account, | ||
signedUserProfile, | ||
token: generateAuthJWT(ensName, serverSecret), | ||
createdAt: new Date().getTime(), | ||
profileExtension: getDefaultProfileExtension(), | ||
}; | ||
logDebug({ text: 'submitUserProfile', session }); | ||
await setAccount(account.toLocaleLowerCase(), session); | ||
|
||
return session.token; | ||
} |