Master de II. ULL. 1er cuatrimestre. 2020/2021
Cree y publique un módulo npm que provea un middleware express que provee autenticación para acceder a los ficheros en una determinada ruta.
En npm puede encontrar este ejemplo:
https://github.com/ULL-ESIT-PL-1819/crguezl-authmodule (Repo Privado en GitHub)
Use un fichero JSON para guardar las parejas usuario-clave encriptadas:
Fichero src/server/users.json
1
2
3
4
{
"juana":"$2a$10$BGEs97PfAygEp7CA6dgkvO.wkqtNmBkZhDUHJRKISw90vBL7bIrUS",
"casiano":"$2a$10$C0dosn7LffKfM3WEt9O7X.waDkY0WQFHh7PF76.YkQDOG9aBa3nIC"
}
src/server/server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const express = require('express');
const session = require('express-session');
const auth = require('@ull-esit-pl/auth');
...
const app = express();
...
app.use(session({
secret: 'verySecureSecret',
resave: true,
saveUninitialized: true,
}));
app.use('/', auth({
passwordFile: path.join(__dirname, 'users.json'),
pathToProtect: path.join(__dirname, '../../', 'dist'),
registerView: 'register',
successRegisterView: 'registerSuccess',
errorRegisterView: 'registerError',
loginView: 'login',
successLoginView: 'loginSuccess',
errorLoginView: 'loginError',
logoutView: 'logout',
unauthorizedView: 'unauthorizedView',
}));
...
El ejemplo de uso anterior muestra la interfaz de nuestro módulo. Esta es la cabecera de la función authentication
exportada
por @ull-esit-pl/auth:
auth.js)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function authentication(options) {
const {
passwordFile,
pathToProtect,
registerView,
successRegisterView,
errorRegisterView,
loginView,
successLoginView,
errorLoginView,
logoutView,
unauthorizedView,
} = options;
...
}
The function authentication
returns a router to use as middleware.
It defines the routes
/login
, /register
via GET and POST methods,/logout
via the GET method only. And/content
401
message will be sent with an unauthorized view.passwordFile
: location of the file to store the users credentials.pathToProtect
: the files that will be accessible only when users are logged in.registerView
: view containing the form to register. It will be served at /register
via the HTTP GET method.successRegisterView
: view with the message to render when the user registers successfully.errorRegisterView
: view to render when there is an error in the registration.loginView
: view containing the form to log in. It will be served at /login
via the HTTP GET method.successLoginView
: view with the message to render when the user logs in successfully.errorLoginView
: view to render when there is an error in the login.logoutView
: view to render when the user logs out (visits /logout
).unauthorizedView
: view to render when a user tries to access /content
without being logged inLa siguiente figura muestra la estructura de vistas del ejemplo que estamos usando:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
src/server/views/
├── components
│ ├── errorMessage.ejs
│ ├── foot.ejs
│ ├── form.ejs
│ ├── head.ejs
│ └── successMessage.ejs
├── index.ejs
├── login.ejs
├── loginError.ejs
├── loginSuccess.ejs
├── logout.ejs
├── register.ejs
├── registerError.ejs
├── registerSuccess.ejs
└── unauthorizedView.ejs
El módulo provee rutas y manejadores para el login, el registro y el logout así como para acceder al contenido protegido
auth.js
function authentication(options) {
...
router.use('/content', auth, express.static(pathToProtect));
router.get('/login', (req, res) => {
...
});
router.post('/login', (req, res) => {
...
});
router.get('/register', (req, res) => {
...
});
router.post('/register', (req, res) => {
...
});
// Route to logout
router.get('/logout', (req, res) => {
...
});
return router;
}
module.exports = authentication;
p8-t3-sessions-and-modules-aluXXXX
correspondiente. Proceda de esta forma:
ULL-ESIT-DSI-1819/auth-aluXXXX
con el código del módulo de autorización que publicará en npm en su ámbito @aluXXXX
ULL-ESIT-DSI-1819/server-auth-aluXXXX
que contiene el código del server que usa el módulo y que contiene el tutorialULL-ESIT-DSI-1819/p8-t3-sessions-and-modules-aluXXXX
como submódulos git los dos módulos anteriores. Incluya un README.md
en este repohttps://github.com/ULL-ESIT-DSI-1819/hello-cookies-and-sessions
1
2
[~/campus-virtual/1819/dsi1819/dsi-1819/tema3-web/practicas/p8-t3-sessions-and-modules/cookies(master)]$ pwd
/Users/casiano/campus-virtual/1819/dsi1819/dsi-1819/tema3-web/practicas/p8-t3-sessions-and-modules/cookies
1
var Article = require('../../../models/article');
Those suck for maintenance and they’re ugly.