Skip to content

Commit

Permalink
suse dedicated submitUserProfile function for backend
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNi245 committed Jul 29, 2024
1 parent fbbad39 commit 4c68234
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
Auth,
Profile,
errorHandler,
getCachedWebProvider,
getServerSecret,
Expand All @@ -16,6 +15,7 @@ import http from 'http';
import path from 'path';
import { getDatabase } from './persistence/getDatabase';
import Storage from './storage';
import Profile from './profile/profile';

const app = express();
app.use(express.json({ limit: '50mb' }));
Expand Down
11 changes: 11 additions & 0 deletions packages/backend/src/profile/getUserProfile.ts
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;
}
82 changes: 82 additions & 0 deletions packages/backend/src/profile/profile.ts
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;
};
40 changes: 40 additions & 0 deletions packages/backend/src/profile/submitUserProfile.ts
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;
}

0 comments on commit 4c68234

Please sign in to comment.