|
| 1 | +/** |
| 2 | + * Module dependencies. |
| 3 | + */ |
| 4 | +import * as passport from "passport"; |
| 5 | +import * as LocalStrategy from "passport-local"; |
| 6 | +import * as ClientPasswordStrategy from "passport-oauth2-client-password"; |
| 7 | +import * as BearerStrategy from "passport-http-bearer"; |
| 8 | +import * as bcrypt from "bcrypt"; |
| 9 | +import {BasicStrategy} from "passport-http"; |
| 10 | +import {User} from "../models/entities/User"; |
| 11 | +import {AccessToken} from "../models/entities/AccessToken"; |
| 12 | +import {Client} from "../models/entities/Client"; |
| 13 | +import {sign, verify} from 'jsonwebtoken'; |
| 14 | +import {AuthError} from "../errors/AuthError"; |
| 15 | + |
| 16 | +const FacebookTokenStrategy = require('passport-facebook-token'); |
| 17 | + |
| 18 | +export class Auth { |
| 19 | + |
| 20 | + static serializeUser() { |
| 21 | + passport.serializeUser(function (user: any, done) { |
| 22 | + done(null, user.id); |
| 23 | + }); |
| 24 | + |
| 25 | + passport.deserializeUser(function (id: number, done) { |
| 26 | + User.find<User>({where: {id}}).then(function (user: User) { |
| 27 | + done(null, user); |
| 28 | + }); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * LocalStrategy |
| 34 | + * |
| 35 | + * This strategy is used to authenticate users based on a username and password. |
| 36 | + * Anytime a request is made to authorize an application, we must ensure that |
| 37 | + * a user is logged in before asking them to approve the request. |
| 38 | + */ |
| 39 | + static useLocalStrategy() { |
| 40 | + passport.use(new LocalStrategy( async(userName, password, done) => { |
| 41 | + let user = await User.findOne<User>({where: {email: userName}}); |
| 42 | + if(user) { |
| 43 | + const authorized = await this.comparePasswords(password, user.password); |
| 44 | + if(authorized) { |
| 45 | + return done(null, user); |
| 46 | + } else { |
| 47 | + return done(null, false) |
| 48 | + } |
| 49 | + } else { |
| 50 | + return done("No user found", false); |
| 51 | + } |
| 52 | + })); |
| 53 | + } |
| 54 | + |
| 55 | + static async comparePasswords(pass1: string | undefined, pass2: string | undefined): Promise<boolean> { |
| 56 | + if(pass1 && pass2) { |
| 57 | + return bcrypt.compare(pass1, pass2); |
| 58 | + } else { |
| 59 | + return false; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * BearerStrategy |
| 65 | + * |
| 66 | + * This strategy is used to authenticate users based on an access token (aka a |
| 67 | + * bearer token). The user must have previously authorized a client |
| 68 | + * application, which is issued an access token to make requests on behalf of |
| 69 | + * the authorizing user. |
| 70 | + */ |
| 71 | + static useBearerStrategy() { |
| 72 | + passport.use(new BearerStrategy((token, done) => { |
| 73 | + AccessToken.findOne<AccessToken>({where: {token: token}}).then(accessToken => { |
| 74 | + if (accessToken) { |
| 75 | + const jwtSecret: string | undefined = process.env.JWT_SECRET; |
| 76 | + if(jwtSecret) { |
| 77 | + verify(accessToken.token, jwtSecret, (err, decodedToken: any) => { |
| 78 | + if (decodedToken && accessToken.userId === decodedToken.id) { |
| 79 | + User.find({where: {id: accessToken.userId}}).then(user => { |
| 80 | + return done(null, user); |
| 81 | + }).catch(error => { |
| 82 | + return done(new AuthError(error.message), false); |
| 83 | + }); |
| 84 | + } else { |
| 85 | + done(new AuthError(err.message), false); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + } else { |
| 90 | + return done(new AuthError("Unauthorized"), false); |
| 91 | + } |
| 92 | + }).catch(function (error) { |
| 93 | + return done(new AuthError(error.message), false); |
| 94 | + }); |
| 95 | + })); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + /** |
| 100 | + * BasicStrategy & ClientPasswordStrategy |
| 101 | + * |
| 102 | + * These strategies are used to authenticate registered OAuth clients. They are |
| 103 | + * employed to protect the `token` endpoint, which consumers use to obtain |
| 104 | + * access tokens. The OAuth 2.0 specification suggests that clients use the |
| 105 | + * HTTP Basic scheme to authenticate. Use of the client password strategy |
| 106 | + * allows clients to send the same credentials in the request body (as opposed |
| 107 | + * to the `Authorization` header). While this approach is not recommended by |
| 108 | + * the specification, in practice it is quite common. |
| 109 | + */ |
| 110 | + static useBasicStrategy() { |
| 111 | + passport.use(new BasicStrategy( |
| 112 | + function (clientId, clientSecret, done) { |
| 113 | + Client.findOne({ |
| 114 | + where: {clientId: clientId} |
| 115 | + }).then(function (client: any) { |
| 116 | + if (!client) return done(null, false); |
| 117 | + if (!bcrypt.compareSync(clientSecret, client.clientSecret)) return done(null, false); |
| 118 | + return done(null, client); |
| 119 | + }).catch(function (error) { |
| 120 | + return done(error); |
| 121 | + }); |
| 122 | + } |
| 123 | + )); |
| 124 | + |
| 125 | + passport.use(new ClientPasswordStrategy( |
| 126 | + function (clientId, clientSecret, done) { |
| 127 | + Client.findOne({ |
| 128 | + where: {clientId: clientId} |
| 129 | + }).then(function (client: any) { |
| 130 | + if (!client) return done(null, false); |
| 131 | + if (!bcrypt.compareSync(clientSecret, client.clientSecret)) return done(null, false); |
| 132 | + return done(null, client); |
| 133 | + }).catch(function (error) { |
| 134 | + return done(error); |
| 135 | + }); |
| 136 | + } |
| 137 | + )); |
| 138 | + } |
| 139 | + |
| 140 | + static useFacebookTokenStrategy() { |
| 141 | + passport.use(new FacebookTokenStrategy({ |
| 142 | + clientID: process.env.FACEBOOK_CLIENT_ID, |
| 143 | + clientSecret: process.env.FACEBOOK_CLIENT_SECRET |
| 144 | + }, (accessToken, refreshToken, profile, done) => { |
| 145 | + const parsedProfile = profile._json; |
| 146 | + User.findOne<User>({where: {email: parsedProfile.email}}).then(user => { |
| 147 | + if (user) { |
| 148 | + AccessToken.findOne<AccessToken>({where: {userId: user.id}}).then(accessToken => { |
| 149 | + if (accessToken) { |
| 150 | + const jwtSecret: string | undefined = process.env.JWT_SECRET; |
| 151 | + if(jwtSecret) { |
| 152 | + verify(accessToken.token, jwtSecret, (err, decodedToken: any) => { |
| 153 | + if (decodedToken && accessToken.userId === decodedToken.id) { |
| 154 | + return done(null, accessToken); |
| 155 | + } else { |
| 156 | + return done(new AuthError(err.message), false); |
| 157 | + } |
| 158 | + }); |
| 159 | + } else { |
| 160 | + return done(new AuthError("JWT Secret undefined"), false); |
| 161 | + } |
| 162 | + } else { |
| 163 | + const jwtSecret: string | undefined = process.env.JWT_SECRET; |
| 164 | + if(jwtSecret) { |
| 165 | + sign(user, jwtSecret, {expiresIn: "10h"}, (error, token) => { |
| 166 | + AccessToken.create({ |
| 167 | + token: token, |
| 168 | + userId: user.id |
| 169 | + }).then((accessToken: AccessToken) => { |
| 170 | + return done(null, accessToken); |
| 171 | + }).catch(error => { |
| 172 | + return done(error, false); |
| 173 | + }); |
| 174 | + }); |
| 175 | + } else { |
| 176 | + return done(new AuthError("JWT Secret undefined"), false); |
| 177 | + } |
| 178 | + |
| 179 | + } |
| 180 | + }); |
| 181 | + } else { |
| 182 | + return done("No account found with email: " + parsedProfile.email); |
| 183 | + } |
| 184 | + }); |
| 185 | + } |
| 186 | + )); |
| 187 | + } |
| 188 | + |
| 189 | + public static getBearerMiddleware() { |
| 190 | + return passport.authenticate('bearer', {session: false, failWithError: true}); |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
0 commit comments