Skip to content

Commit

Permalink
api: Test full oauth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
evanpurkhiser committed Oct 28, 2020
1 parent d884283 commit 656ccd4
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 1,241 deletions.
39 changes: 35 additions & 4 deletions api/twitter-auth.ts
@@ -1,5 +1,11 @@
import {NowRequest, NowResponse} from '@vercel/node';
import {OAuth, oauth1tokenCallback} from 'oauth';
import {OAuth} from 'oauth';
import cookie from 'cookie';

type OauthData = {
token: string;
secret: string;
};

export default async (request: NowRequest, response: NowResponse) => {
var oauth = new OAuth(
Expand All @@ -12,9 +18,34 @@ export default async (request: NowRequest, response: NowResponse) => {
'HMAC-SHA1'
);

const test = await new Promise<Parameters<oauth1tokenCallback>>(resolve =>
oauth.getOAuthRequestToken((...data) => resolve(data))
// Initiate oauth request
if (request.query['oauth_verifier'] === undefined) {
const oauthRequest = await new Promise<OauthData>(resolve =>
oauth.getOAuthRequestToken((_, token, secret) => resolve({token, secret}))
);

response.setHeader('Set-Cookie', [
cookie.serialize('twitter_oauth', JSON.stringify(oauthRequest)),
]);

response.redirect(
`https://api.twitter.com/oauth/authorize?oauth_token=${oauthRequest.token}`
);

return;
}

const verifier = request.query['oauth_verifier'] as string;
const oauthRequest: OauthData = JSON.parse(request.cookies['twitter_oauth']);

const accessToken = await new Promise<OauthData>(resolve =>
oauth.getOAuthAccessToken(
oauthRequest.token,
oauthRequest.secret,
verifier,
(_, token, secret) => resolve({token, secret})
)
);

response.status(200).json(test);
response.status(200).json(accessToken);
};
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -71,6 +71,7 @@
"@sentry/browser": "^5.19.0",
"@sentry/node": "^5.16.1",
"@types/connect": "^3.4.33",
"@types/cookie": "^0.4.0",
"@types/deep-diff": "^1.0.0",
"@types/electron-settings": "^3.1.1",
"@types/http-proxy": "^1.17.4",
Expand Down Expand Up @@ -99,6 +100,7 @@
"@vercel/node": "^1.8.4",
"babel-loader": "^8.0.6",
"connect": "^3.7.0",
"cookie": "^0.4.1",
"cross-env": "^7.0.2",
"date-fns": "^2.14.0",
"deep-diff": "^1.0.2",
Expand All @@ -118,13 +120,11 @@
"mobx-react": "^6.2.2",
"mobx-utils": "^5.5.7",
"module-alias": "^2.2.2",
"next": "^10.0.0",
"node-fetch": "^2.6.1",
"node-gzip": "^1.1.2",
"node-static": "^0.7.11",
"noop2": "^2.0.0",
"oauth": "^0.9.15",
"oauth-electron-twitter": "^1.0.109",
"prolink-connect": "0.2.0-prerelease.12",
"public-ip": "^4.0.1",
"react": "^16.8.6",
Expand Down

1 comment on commit 656ccd4

@vercel
Copy link

@vercel vercel bot commented on 656ccd4 Oct 28, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.