Add Twitter proxy verification

This commit is contained in:
Yarmo Mackenbach 2021-03-02 15:10:46 +01:00
parent c9439a40b1
commit cee8fa9172
No known key found for this signature in database
GPG key ID: 37367F4AF4087AD1

View file

@ -16,10 +16,14 @@ limitations under the License.
const bent = require('bent')
const req = bent('GET')
const serviceproviders = require('../serviceproviders')
const utils = require('../utils')
const reURI = /^https:\/\/twitter\.com\/(.*)\/status\/([0-9]*)(?:\?.*)?/
const customRequestHandler = async (spData, opts) => {
const match = spData.proof.uri.match(reURI)
// Attempt direct verification if policy allows it
if (opts.proxyPolicy !== 'always') {
if ('twitterBearerToken' in opts && opts.twitterBearerToken) {
const res = await req(`https://api.twitter.com/1.1/statuses/show.json?id=${match[2]}`, null, {
Accept: 'application/json',
@ -36,6 +40,26 @@ const customRequestHandler = async (spData, opts) => {
}
}
// Attempt proxy verification if policy allows it
if (opts.proxyPolicy !== 'never' && spData.proof.fetch) {
return req(utils.generateProxyURL('twitter', match[2], opts), null, {
Accept: 'application/json',
})
.then(async (res) => {
return await res.json()
})
.then((res) => {
return res.data.text
})
.catch((e) => {
reject(e)
})
}
// No verification
return null
}
const processURI = (uri, opts) => {
if (!opts) {
opts = {}
@ -54,8 +78,8 @@ const processURI = (uri, opts) => {
},
proof: {
uri: uri,
fetch: null,
useProxy: true,
fetch: utils.generateProxyURL('twitter', match[2], opts),
useProxy: false,
format: 'text',
},
claim: {