Skip to content
Merged
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
12 changes: 6 additions & 6 deletions identity-service/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -713,6 +707,12 @@ const config = convict({
default: '',
env: 'aaoAddress'
},
generalAdmissionAddress: {
doc: 'General admission server adress',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

address*

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you 😮‍💨

format: String,
default: '',
env: 'generalAdmissionAddress'
},
sentryDSN: {
doc: 'Sentry DSN key',
format: String,
Expand Down
84 changes: 21 additions & 63 deletions identity-service/src/routes/instagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,19 @@ 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
* For official documentation on the instagram oauth flow check out their site
* 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) => {
Expand Down Expand Up @@ -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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

accessToken
})

return successResponse(igUser)
return successResponse(instagramProfile)
} catch (err) {
return errorResponseBadRequest(err)
}
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion identity-service/src/routes/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down