-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Basic smtp setup * 🚧 WIP * 🔐 complete authentication flow with passport Co-authored-by: Omar Ajoue <krynble@gmail.com>
- Loading branch information
1 parent
34cba4b
commit faae122
Showing
9 changed files
with
107 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// @ts-nocheck | ||
|
||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import * as jwt from 'jsonwebtoken'; | ||
|
||
import { Db } from '../..'; | ||
import config = require('../../../config'); | ||
|
||
const options = { | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
secretOrKey: 'abc', //config.get('jwt_key'), | ||
}; | ||
|
||
export function useJwt(passport) { | ||
// The JWT payload is passed into the verify callback | ||
passport.use( | ||
new Strategy(options, async function (jwt_payload, done) { | ||
// We will assign the `sub` property on the JWT to the database ID of user | ||
const user = await Db.collections.User.findOne({ | ||
id: jwt_payload.id, | ||
email: jwt_payload.email, | ||
}); | ||
if (!user) { | ||
return done(null, false, { message: 'User not found' }); | ||
} | ||
return done(null, user); | ||
}), | ||
); | ||
} | ||
|
||
export function issueJWT(user) { | ||
const { id, email } = user; | ||
const expiresIn = 14 * 86400000; // 14 days | ||
|
||
const payload = { | ||
id, | ||
email, | ||
}; | ||
|
||
const signedToken = jwt.sign(payload, options.secretOrKey, { | ||
expiresIn: expiresIn / 1000 /* in seconds */, | ||
}); | ||
|
||
return { | ||
token: 'Bearer ' + signedToken, | ||
expiresIn, | ||
validTill: Date.now() + expiresIn, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// @ts-nocheck | ||
|
||
import * as express from 'express'; | ||
import * as passport from 'passport'; | ||
import { Db, ResponseHelper } from '../..'; | ||
import { User } from '../databases/entities/User'; | ||
import { issueJWT, useJwt } from './jwt'; | ||
|
||
useJwt(passport); | ||
|
||
export function authenticationRoutes(): void { | ||
// ---------------------------------------- | ||
// authentication middleware | ||
// ---------------------------------------- | ||
this.app.use(passport.initialize()); | ||
this.app.use((req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
// just temp for development | ||
if (req.url.includes('login')) { | ||
return next(); | ||
} | ||
// get access to this from Server.ts | ||
// if (authIgnoreRegex.exec(req.url)) { | ||
// return next(); | ||
// } | ||
return passport.authenticate('jwt', { session: false })(req, res, next); | ||
}); | ||
|
||
// ---------------------------------------- | ||
// login a user | ||
// ---------------------------------------- | ||
|
||
this.app.get( | ||
`/${this.restEndpoint}/login`, | ||
ResponseHelper.send(async (req: express.Request, res: express.Response) => { | ||
const user = await Db.collections.User!.findOne({ firstName: 'Ben' }); | ||
|
||
return issueJWT(user); | ||
}), | ||
); | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/cli/src/UserManagement/email/templates/instanceSetup.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<h1>Hi there!</h1> | ||
<p>Welcome to n8n, {{firstName}} {{lastName}}</p> | ||
<p>Your instance is set up!</p> | ||
<p>Use your email to login: {{email}} and the chosen password.</p> | ||
<p>Have fun automating!</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters