From 6c88eab2a111ad92c408e3e42b67dcf4e6af3337 Mon Sep 17 00:00:00 2001 From: Raymond Jacobson Date: Mon, 19 Dec 2022 23:05:18 -0800 Subject: [PATCH 1/2] [C-1522] Update instagram routes --- identity-service/src/config.js | 12 ++-- identity-service/src/routes/instagram.js | 84 ++++++------------------ identity-service/src/routes/twitter.js | 3 +- 3 files changed, 29 insertions(+), 70 deletions(-) diff --git a/identity-service/src/config.js b/identity-service/src/config.js index 52e22a8da60..9f281c61097 100644 --- a/identity-service/src/config.js +++ b/identity-service/src/config.js @@ -111,12 +111,6 @@ const config = convict({ env: 'instagramRedirectUrl', default: null }, - instagramProfileUrl: { - doc: 'Instagram profile url', - format: String, - env: 'instagramProfileUrl', - default: 'https://www.instagram.com/%USERNAME%/channel/?__a=1' - }, relayerPrivateKey: { doc: 'L2 Relayer(used to make relay transactions) private key. The source of the funds when funding wallet.', format: String, @@ -713,6 +707,12 @@ const config = convict({ default: '', env: 'aaoAddress' }, + generalAdmissionAddress: { + doc: 'General admission server adress', + format: String, + defaultL: '', + env: 'generalAdmissionAddress' + }, sentryDSN: { doc: 'Sentry DSN key', format: String, diff --git a/identity-service/src/routes/instagram.js b/identity-service/src/routes/instagram.js index 390225ec4d5..73ed4f5ceaa 100644 --- a/identity-service/src/routes/instagram.js +++ b/identity-service/src/routes/instagram.js @@ -16,12 +16,7 @@ const verifiedUserReporter = new VerifiedUserReporter({ source: 'instagram' }) -const getInstagramURL = (username) => { - const instagramProfileUrl = - config.get('instagramProfileUrl') || - 'https://www.instagram.com/%USERNAME%/channel/?__a=1' - return instagramProfileUrl.replace('%USERNAME%', username) -} +const generalAdmissionAddress = config.get('generalAdmissionAddress') /** * This file contains the instagram endpoints for oauth @@ -29,26 +24,11 @@ const getInstagramURL = (username) => { * https://www.instagram.com/developer/authentication/ */ module.exports = function (app) { - app.get( - '/instagram/profile', - handleResponse(async (req, res, next) => { - const username = req.query.username - - if (!username) { - return errorResponseBadRequest('Missing username parameter') - } - try { - const res = await doRequest(getInstagramURL(username)) - const json = JSON.parse(res) - return successResponse(json) - } catch (e) { - return errorResponseServerError( - `Failed to fetch instagram profile for ${username}` - ) - } - }) - ) - + /** + * The first leg of the Instagram Oauth. Given an oauth code, it first + * validates that the user owns the claimed account. Then it sends a + * request to go pull the instagram graph API full user data. + */ app.post( '/instagram', handleResponse(async (req, res, next) => { @@ -96,15 +76,25 @@ module.exports = function (app) { `Another Audius profile has already been authenticated with Instagram user @${igUser.username}!` ) } else { - // Store the access token, user id, and current profile for user in db + // Fetch the instagram full profile + const igProfileReqObj = { + method: 'get', + url: `${generalAdmissionAddress}/social/instagram/${igUser.username}` + } + try { + const instagramProfileRes = await doRequest(reqObj) + const instagramProfile = JSON.parse(instagramProfileRes) + + // Store the access token, user id, and current profile for user in db await models.InstagramUser.upsert({ uuid: igUser.username, - profile: igUser, + profile: instagramProfile, + verified: instagramProfile.is_verified, accessToken }) - return successResponse(igUser) + return successResponse(instagramProfile) } catch (err) { return errorResponseBadRequest(err) } @@ -115,39 +105,6 @@ module.exports = function (app) { }) ) - app.post( - '/instagram/profile', - handleResponse(async (req, res, next) => { - const { profile } = req.body - try { - const checkFields = ['id', 'username'] - const hasMinimumFields = checkFields.every((field) => field in profile) - if (!hasMinimumFields) throw new Error('Invalid profile') - - try { - // Verify the user user id exists in the DB before updating it - const igUser = await models.InstagramUser.findOne({ - where: { - uuid: profile.username - } - }) - if (!igUser) - throw new Error( - `Could not find matching ig user in the db: ${profile.username}` - ) - igUser.profile = profile - await igUser.save() - - return successResponse(profile) - } catch (err) { - return errorResponseBadRequest(err) - } - } catch (err) { - return errorResponseBadRequest(err) - } - }) - ) - /** * After the user finishes onboarding in the client app and has a blockchain userId, we need to associate * the blockchainUserId with the instagram profile @@ -168,7 +125,8 @@ module.exports = function (app) { const isUnassociated = instagramObj && !instagramObj.blockchainUserId const handlesMatch = instagramObj && - instagramObj.profile.username.toLowerCase() === user.handle + instagramObj.profile.username.toLowerCase() === + user.handle.toLowerCase() // only set blockchainUserId if not already set if (isUnassociated && handlesMatch) { instagramObj.blockchainUserId = userId diff --git a/identity-service/src/routes/twitter.js b/identity-service/src/routes/twitter.js index a8020f27458..dfcfb242d1e 100644 --- a/identity-service/src/routes/twitter.js +++ b/identity-service/src/routes/twitter.js @@ -164,7 +164,8 @@ module.exports = function (app) { const isUnassociated = twitterObj && !twitterObj.blockchainUserId const handlesMatch = twitterObj && - twitterObj.twitterProfile.screen_name.toLowerCase() === user.handle + twitterObj.twitterProfile.screen_name.toLowerCase() === + user.handle.toLowerCase() if (isUnassociated && handlesMatch) { twitterObj.blockchainUserId = userId From 68f9f0790924bfc777d3dcab8c4890e5caf7afec Mon Sep 17 00:00:00 2001 From: Raymond Jacobson Date: Mon, 19 Dec 2022 23:13:26 -0800 Subject: [PATCH 2/2] Fix typo --- identity-service/src/config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/identity-service/src/config.js b/identity-service/src/config.js index 9f281c61097..1a579b45bcb 100644 --- a/identity-service/src/config.js +++ b/identity-service/src/config.js @@ -710,7 +710,7 @@ const config = convict({ generalAdmissionAddress: { doc: 'General admission server adress', format: String, - defaultL: '', + default: '', env: 'generalAdmissionAddress' }, sentryDSN: {