mirror of
https://github.com/openstf/stf
synced 2025-10-04 18:29:17 +02:00
Rename "roles" to "units". Put units in their own folders.
This commit is contained in:
parent
7d9d64ddcb
commit
3a9b193f68
63 changed files with 105 additions and 105 deletions
124
lib/units/auth/mock.js
Normal file
124
lib/units/auth/mock.js
Normal file
|
@ -0,0 +1,124 @@
|
|||
var http = require('http')
|
||||
|
||||
var express = require('express')
|
||||
var validator = require('express-validator')
|
||||
var cookieSession = require('cookie-session')
|
||||
var bodyParser = require('body-parser')
|
||||
var serveStatic = require('serve-static')
|
||||
var csrf = require('csurf')
|
||||
var Promise = require('bluebird')
|
||||
|
||||
var logger = require('../../util/logger')
|
||||
var requtil = require('../../util/requtil')
|
||||
var jwtutil = require('../../util/jwtutil')
|
||||
var pathutil = require('../../util/pathutil')
|
||||
var urlutil = require('../../util/urlutil')
|
||||
var lifecycle = require('../../util/lifecycle')
|
||||
|
||||
module.exports = function(options) {
|
||||
var log = logger.createLogger('auth-mock')
|
||||
, app = express()
|
||||
, server = Promise.promisifyAll(http.createServer(app))
|
||||
|
||||
lifecycle.observe(function() {
|
||||
log.info('Waiting for client connections to end')
|
||||
return server.closeAsync()
|
||||
.catch(function() {
|
||||
// Okay
|
||||
})
|
||||
})
|
||||
|
||||
app.set('view engine', 'jade')
|
||||
app.set('views', pathutil.resource('auth/mock/views'))
|
||||
app.set('strict routing', true)
|
||||
app.set('case sensitive routing', true)
|
||||
|
||||
app.use(cookieSession({
|
||||
name: options.ssid
|
||||
, keys: [options.secret]
|
||||
}))
|
||||
app.use(bodyParser.json())
|
||||
app.use(csrf())
|
||||
app.use(validator())
|
||||
app.use('/static/bower_components',
|
||||
serveStatic(pathutil.resource('bower_components')))
|
||||
app.use('/static/auth/mock', serveStatic(pathutil.resource('auth/mock')))
|
||||
|
||||
app.use(function(req, res, next) {
|
||||
res.cookie('XSRF-TOKEN', req.csrfToken());
|
||||
next()
|
||||
})
|
||||
|
||||
app.get('/static/auth/mock/views/partials/:name.html', function(req, res) {
|
||||
var whitelist = {
|
||||
'signin': true
|
||||
}
|
||||
|
||||
if (whitelist[req.params.name]) {
|
||||
res.render('partials/' + req.params.name)
|
||||
}
|
||||
else {
|
||||
res.send(404)
|
||||
}
|
||||
})
|
||||
|
||||
app.get('/', function(req, res) {
|
||||
res.redirect('/auth/mock/')
|
||||
})
|
||||
|
||||
app.get('/auth/mock/', function(req, res) {
|
||||
res.render('index')
|
||||
})
|
||||
|
||||
app.post('/api/v1/auth/mock', function(req, res) {
|
||||
var log = logger.createLogger('auth-mock')
|
||||
log.setLocalIdentifier(req.ip)
|
||||
switch (req.accepts(['json'])) {
|
||||
case 'json':
|
||||
requtil.validate(req, function() {
|
||||
req.checkBody('name').notEmpty()
|
||||
req.checkBody('email').isEmail()
|
||||
})
|
||||
.then(function() {
|
||||
log.info('Authenticated "%s"', req.body.email)
|
||||
var token = jwtutil.encode({
|
||||
payload: {
|
||||
email: req.body.email
|
||||
, name: req.body.name
|
||||
}
|
||||
, secret: options.secret
|
||||
})
|
||||
res.status(200)
|
||||
.json({
|
||||
success: true
|
||||
, redirect: urlutil.addParams(options.appUrl, {
|
||||
jwt: token
|
||||
})
|
||||
})
|
||||
})
|
||||
.catch(requtil.ValidationError, function(err) {
|
||||
res.status(400)
|
||||
.json({
|
||||
success: false
|
||||
, error: 'ValidationError'
|
||||
, validationErrors: err.errors
|
||||
})
|
||||
})
|
||||
.catch(function(err) {
|
||||
log.error('Unexpected error', err.stack)
|
||||
res.status(500)
|
||||
.json({
|
||||
success: false
|
||||
, error: 'ServerError'
|
||||
})
|
||||
})
|
||||
break
|
||||
default:
|
||||
res.send(406)
|
||||
break
|
||||
}
|
||||
})
|
||||
|
||||
server.listen(options.port)
|
||||
log.info('Listening on port %d', options.port)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue