feat: reddit auth

This commit is contained in:
cha0s 2020-07-12 21:17:04 -05:00
parent 968f7fc94b
commit 25b679e6a2
5 changed files with 105 additions and 2 deletions

View File

@ -39,6 +39,7 @@
"normalizr": "^3.6.0", "normalizr": "^3.6.0",
"passport": "^0.4.1", "passport": "^0.4.1",
"passport-local": "^1.0.0", "passport-local": "^1.0.0",
"passport-reddit": "0.2.4",
"prop-types": "^15", "prop-types": "^15",
"react": "16.8.6", "react": "16.8.6",
"react-dom": "16.8.6", "react-dom": "16.8.6",

View File

@ -17,6 +17,8 @@ class User extends BaseModel {
defaultValue: false, defaultValue: false,
}, },
hash: Types.STRING, hash: Types.STRING,
redditAccessToken: Types.STRING,
redditUsername: Types.STRING,
}; };
} }

View File

@ -1,7 +1,14 @@
import passport from 'passport'; import passport from 'passport';
import {Strategy as RedditStrategy} from 'passport-reddit';
import {allModels} from './models/registrar'; import {allModels} from './models/registrar';
const {
REDDIT_CALLBACK_URL,
REDDIT_CLIENT_ID,
REDDIT_CLIENT_SECRET,
} = process.env;
passport.serializeUser((user, fn) => fn(null, user.id)); passport.serializeUser((user, fn) => fn(null, user.id));
passport.deserializeUser(async (id, fn) => { passport.deserializeUser(async (id, fn) => {
const {User} = allModels(); const {User} = allModels();
@ -13,4 +20,25 @@ passport.deserializeUser(async (id, fn) => {
} }
}); });
passport.use(new RedditStrategy(
{
clientID: REDDIT_CLIENT_ID,
clientSecret: REDDIT_CLIENT_SECRET,
callbackURL: REDDIT_CALLBACK_URL,
},
async (accessToken, refreshToken, profile, done) => {
const {User} = allModels();
try {
const where = {redditUsername: profile.name};
const user = await User.findOne({where}) || await User.create(where);
user.redditAccessToken = accessToken;
await user.save();
done(undefined, user);
}
catch (error) {
done(error);
}
},
));
export default passport; export default passport;

View File

@ -1,4 +1,25 @@
/* eslint-disable import/no-extraneous-dependencies */
import {randomBytes} from 'crypto';
import passport from 'passport'; import passport from 'passport';
export default function userRoutes(app) { export default function userRoutes(app) {
app.get('/auth/reddit', (req, res, next) => {
req.session.state = randomBytes(32).toString('hex');
passport.authenticate('reddit', {
state: req.session.state,
duration: 'permanent',
})(req, res, next);
});
app.get('/auth/reddit/callback', (req, res, next) => {
if (req.query.state === req.session.state) {
passport.authenticate('reddit', {
successRedirect: '/',
failureRedirect: '/login',
})(req, res, next);
}
else {
res.status(403).end('Forbidden: state mismatch');
}
});
} }

View File

@ -1840,6 +1840,11 @@ base64id@2.0.0:
resolved "https://npm.i12e.cha0s.io/base64id/-/base64id-2.0.0.tgz#2770ac6bc47d312af97a8bf9a634342e0cd25cb6" resolved "https://npm.i12e.cha0s.io/base64id/-/base64id-2.0.0.tgz#2770ac6bc47d312af97a8bf9a634342e0cd25cb6"
integrity sha512-lGe34o6EHj9y3Kts9R4ZYs/Gr+6N7MCaMlIFA3F1R2O5/m7K06AxfSeO5530PEERE6/WyEg3lsuyw4GHlPZHog== integrity sha512-lGe34o6EHj9y3Kts9R4ZYs/Gr+6N7MCaMlIFA3F1R2O5/m7K06AxfSeO5530PEERE6/WyEg3lsuyw4GHlPZHog==
base64url@3.x.x:
version "3.0.1"
resolved "https://npm.i12e.cha0s.io/base64url/-/base64url-3.0.1.tgz#6399d572e2bc3f90a9a8b22d5dbb0a32d33f788d"
integrity sha512-ir1UPr3dkwexU7FdV8qBBbNDRUhMmIekYMFZfi+C/sLNnRESKPl23nB9b2pltqfOQNnGzsDdId90AEtG5tCx4A==
base@^0.11.1: base@^0.11.1:
version "0.11.2" version "0.11.2"
resolved "https://npm.i12e.cha0s.io/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f" resolved "https://npm.i12e.cha0s.io/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f"
@ -6619,6 +6624,11 @@ oauth-sign@~0.9.0:
resolved "https://npm.i12e.cha0s.io/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455" resolved "https://npm.i12e.cha0s.io/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455"
integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ== integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ==
oauth@0.9.x:
version "0.9.15"
resolved "https://npm.i12e.cha0s.io/oauth/-/oauth-0.9.15.tgz#bd1fefaf686c96b75475aed5196412ff60cfb9c1"
integrity sha1-vR/vr2hslrdUda7VGWQS/2DPucE=
object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1: object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
version "4.1.1" version "4.1.1"
resolved "https://npm.i12e.cha0s.io/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" resolved "https://npm.i12e.cha0s.io/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
@ -7029,6 +7039,42 @@ passport-local@^1.0.0:
dependencies: dependencies:
passport-strategy "1.x.x" passport-strategy "1.x.x"
passport-oauth1@1.x.x:
version "1.1.0"
resolved "https://npm.i12e.cha0s.io/passport-oauth1/-/passport-oauth1-1.1.0.tgz#a7de988a211f9cf4687377130ea74df32730c918"
integrity sha1-p96YiiEfnPRoc3cTDqdN8ycwyRg=
dependencies:
oauth "0.9.x"
passport-strategy "1.x.x"
utils-merge "1.x.x"
passport-oauth2@1.x.x:
version "1.5.0"
resolved "https://npm.i12e.cha0s.io/passport-oauth2/-/passport-oauth2-1.5.0.tgz#64babbb54ac46a4dcab35e7f266ed5294e3c4108"
integrity sha512-kqBt6vR/5VlCK8iCx1/KpY42kQ+NEHZwsSyt4Y6STiNjU+wWICG1i8ucc1FapXDGO15C5O5VZz7+7vRzrDPXXQ==
dependencies:
base64url "3.x.x"
oauth "0.9.x"
passport-strategy "1.x.x"
uid2 "0.0.x"
utils-merge "1.x.x"
passport-oauth@1.x:
version "1.0.0"
resolved "https://npm.i12e.cha0s.io/passport-oauth/-/passport-oauth-1.0.0.tgz#90aff63387540f02089af28cdad39ea7f80d77df"
integrity sha1-kK/2M4dUDwIImvKM2tOep/gNd98=
dependencies:
passport-oauth1 "1.x.x"
passport-oauth2 "1.x.x"
passport-reddit@0.2.4:
version "0.2.4"
resolved "https://npm.i12e.cha0s.io/passport-reddit/-/passport-reddit-0.2.4.tgz#4e5805d919a8f28f80c238f7da2d92a38067acc5"
integrity sha1-TlgF2Rmo8o+Awjj32i2So4BnrMU=
dependencies:
passport-oauth "1.x"
pkginfo "0.3.x"
passport-strategy@1.x.x: passport-strategy@1.x.x:
version "1.0.0" version "1.0.0"
resolved "https://npm.i12e.cha0s.io/passport-strategy/-/passport-strategy-1.0.0.tgz#b5539aa8fc225a3d1ad179476ddf236b440f52e4" resolved "https://npm.i12e.cha0s.io/passport-strategy/-/passport-strategy-1.0.0.tgz#b5539aa8fc225a3d1ad179476ddf236b440f52e4"
@ -7212,6 +7258,11 @@ pkg-up@^2.0.0:
dependencies: dependencies:
find-up "^2.1.0" find-up "^2.1.0"
pkginfo@0.3.x:
version "0.3.1"
resolved "https://npm.i12e.cha0s.io/pkginfo/-/pkginfo-0.3.1.tgz#5b29f6a81f70717142e09e765bbeab97b4f81e21"
integrity sha1-Wyn2qB9wcXFC4J52W76rl7T4HiE=
portfinder@^1.0.26: portfinder@^1.0.26:
version "1.0.26" version "1.0.26"
resolved "https://npm.i12e.cha0s.io/portfinder/-/portfinder-1.0.26.tgz#475658d56ca30bed72ac7f1378ed350bd1b64e70" resolved "https://npm.i12e.cha0s.io/portfinder/-/portfinder-1.0.26.tgz#475658d56ca30bed72ac7f1378ed350bd1b64e70"
@ -9343,7 +9394,7 @@ uid-safe@~2.1.5:
dependencies: dependencies:
random-bytes "~1.0.0" random-bytes "~1.0.0"
uid2@0.0.3: uid2@0.0.3, uid2@0.0.x:
version "0.0.3" version "0.0.3"
resolved "https://npm.i12e.cha0s.io/uid2/-/uid2-0.0.3.tgz#483126e11774df2f71b8b639dcd799c376162b82" resolved "https://npm.i12e.cha0s.io/uid2/-/uid2-0.0.3.tgz#483126e11774df2f71b8b639dcd799c376162b82"
integrity sha1-SDEm4Rd03y9xuLY53NeZw3YWK4I= integrity sha1-SDEm4Rd03y9xuLY53NeZw3YWK4I=
@ -9572,7 +9623,7 @@ utila@^0.4.0, utila@~0.4:
resolved "https://npm.i12e.cha0s.io/utila/-/utila-0.4.0.tgz#8a16a05d445657a3aea5eecc5b12a4fa5379772c" resolved "https://npm.i12e.cha0s.io/utila/-/utila-0.4.0.tgz#8a16a05d445657a3aea5eecc5b12a4fa5379772c"
integrity sha1-ihagXURWV6Oupe7MWxKk+lN5dyw= integrity sha1-ihagXURWV6Oupe7MWxKk+lN5dyw=
utils-merge@1.0.1: utils-merge@1.0.1, utils-merge@1.x.x:
version "1.0.1" version "1.0.1"
resolved "https://npm.i12e.cha0s.io/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" resolved "https://npm.i12e.cha0s.io/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM= integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=