1 | _ = require 'lodash'
|
2 | debug = require('debug')('express-meshblu-auth')
|
3 | MeshbluAuthExpress = require './src/meshblu-auth-express'
|
4 |
|
5 | class MeshbluAuth
|
6 | constructor: (options) ->
|
7 | @meshbluAuthExpress = new MeshbluAuthExpress options
|
8 |
|
9 | auth: =>
|
10 | (req, res, next) =>
|
11 | credentials = @meshbluAuthExpress.getFromAnywhere req
|
12 | debug 'uuid', _.get(credentials, 'uuid')
|
13 | return next() unless credentials?
|
14 |
|
15 | {uuid, token} = credentials
|
16 | @meshbluAuthExpress.authDeviceWithMeshblu uuid, token, (error, meshbluAuth) ->
|
17 | return res.status(500).send(error: error.message) if error?
|
18 | req.meshbluAuth = meshbluAuth if meshbluAuth?
|
19 | next()
|
20 |
|
21 | get: =>
|
22 | (req, res, next) =>
|
23 | credentials = @meshbluAuthExpress.getFromAnywhere req
|
24 | debug 'uuid', _.get(credentials, 'uuid')
|
25 | return next() unless credentials?
|
26 |
|
27 | {uuid, token} = credentials
|
28 | @meshbluAuthExpress.getDeviceFromMeshblu uuid, token, (error, response={}) ->
|
29 | return res.status(500).send(error: error.message) if error?
|
30 | {meshbluAuth, meshbluDevice} = response
|
31 | req.meshbluAuth = meshbluAuth if meshbluAuth?
|
32 | req.meshbluDevice = meshbluDevice if meshbluDevice?
|
33 | next()
|
34 |
|
35 | gateway: =>
|
36 | (req, res, next) =>
|
37 | credentials = @meshbluAuthExpress.getFromAnywhere req
|
38 | return res.status(401).send(error: 'Unauthorized') unless credentials?
|
39 | return res.status(403).send(error: 'Forbidden') unless req.meshbluAuth?
|
40 | return next()
|
41 |
|
42 | gatewayDevice: (uuid) =>
|
43 | (req, res, next) =>
|
44 | credentials = @meshbluAuthExpress.getFromAnywhere req
|
45 | return res.status(401).send(error: 'Unauthorized') unless credentials?
|
46 | return res.status(403).send(error: 'Forbidden') unless req.meshbluAuth?
|
47 | return res.status(403).send(error: 'Forbidden') unless req.meshbluAuth.uuid == uuid
|
48 | return next()
|
49 |
|
50 | gatewayRedirect: (location) =>
|
51 | (req, res, next) =>
|
52 | return res.redirect location unless req.meshbluAuth?
|
53 | return next()
|
54 |
|
55 | module.exports = MeshbluAuth
|