forked from openstf/stf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add auth middleware in api unit. Now only authorized user can access …
…api unit
- Loading branch information
Showing
5 changed files
with
102 additions
and
0 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,65 @@ | ||
var jwtutil = require('../../../util/jwtutil') | ||
var urlutil = require('../../../util/urlutil') | ||
var logger = require('../../../util/logger') | ||
var dbapi = require('../../../db/api') | ||
|
||
module.exports = function(options) { | ||
return function(req, res, next) { | ||
|
||
var log = logger.createLogger('api:auth') | ||
|
||
if (req.headers.authorization) { | ||
var tokenId = req.headers.authorization.split(" ")[1] | ||
|
||
if (tokenId) { | ||
dbapi.loadAccessToken(tokenId) | ||
.then(function(token) { | ||
var jwt = token.jwt | ||
, data = jwtutil.decode(jwt, options.secret) | ||
|
||
if (data) { | ||
dbapi.loadUser(data.email) | ||
.then(function(user) { | ||
if (user) { | ||
req.user = user | ||
next() | ||
} | ||
}) | ||
} | ||
}) | ||
.catch(function(err) { | ||
log.error('Failed to load token: ', err.stack) | ||
res.json(500, { | ||
success: false, | ||
description: "Bad Access Token" | ||
}) | ||
}) | ||
} else { | ||
log.error("Bad Access Token") | ||
res.json(500, { | ||
success: false, | ||
description: "Bad Access Token Header" | ||
}) | ||
} | ||
} | ||
// TODO: Remove this once frontend become stateless | ||
else if (req.session && req.session.jwt) { | ||
dbapi.loadUser(req.session.jwt.email) | ||
.then(function(user) { | ||
if (user) { | ||
req.user = user | ||
next() | ||
} | ||
else { | ||
// We no longer have the user in the database | ||
res.redirect(options.authUrl) | ||
} | ||
}) | ||
.catch(next) | ||
} | ||
else { | ||
// No session, forward to auth client | ||
res.redirect(options.authUrl) | ||
} | ||
} | ||
} |
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