1 | MeshbluAuth = require 'express-meshblu-auth'
|
2 | passport = require 'passport'
|
3 |
|
4 | CredentialsDeviceController = require './controllers/credentials-device-controller'
|
5 | FormSchemaController = require './controllers/form-schema-controller'
|
6 | MessagesController = require './controllers/messages-controller'
|
7 | ConfigureController = require './controllers/configure-controller'
|
8 | MessageSchemaController = require './controllers/message-schema-controller'
|
9 | ConfigureSchemaController = require './controllers/configure-schema-controller'
|
10 | OctobluAuthController = require './controllers/octoblu-auth-controller'
|
11 | ResponseSchemaController = require './controllers/response-schema-controller'
|
12 | StaticSchemasController = require './controllers/static-schemas-controller'
|
13 | UserDevicesController = require './controllers/user-devices-controller'
|
14 |
|
15 | class Router
|
16 | constructor: (options) ->
|
17 | {
|
18 | @appOctobluHost
|
19 | @credentialsDeviceService
|
20 | @messagesService
|
21 | @configureService
|
22 | @meshbluConfig
|
23 | @serviceUrl
|
24 | @userDeviceManagerUrl
|
25 | @staticSchemasPath
|
26 | @skipRedirectAfterApiAuth
|
27 | } = options
|
28 |
|
29 | throw new Error 'appOctobluHost is required' unless @appOctobluHost?
|
30 | throw new Error 'credentialsDeviceService is required' unless @credentialsDeviceService?
|
31 | throw new Error 'meshbluConfig is required' unless @meshbluConfig?
|
32 | throw new Error 'messagesService is required' unless @messagesService?
|
33 | throw new Error 'configureService is required' unless @configureService?
|
34 | throw new Error 'serviceUrl is required' unless @serviceUrl?
|
35 | throw new Error 'userDeviceManagerUrl is required' unless @userDeviceManagerUrl?
|
36 |
|
37 | @credentialsDeviceController = new CredentialsDeviceController {@credentialsDeviceService, @appOctobluHost, @serviceUrl, @userDeviceManagerUrl}
|
38 | @formSchemaController = new FormSchemaController {@messagesService, @configureService}
|
39 | @messagesController = new MessagesController {@credentialsDeviceService, @messagesService}
|
40 | @configureController = new ConfigureController {@credentialsDeviceService, @configureService}
|
41 | @messageSchemaController = new MessageSchemaController {@messagesService}
|
42 | @configureSchemaController = new ConfigureSchemaController {@configureService}
|
43 | @octobluAuthController = new OctobluAuthController
|
44 | @responseSchemaController = new ResponseSchemaController {@messagesService}
|
45 | @staticSchemasController = new StaticSchemasController {@staticSchemasPath}
|
46 | @userDevicesController = new UserDevicesController
|
47 |
|
48 | route: (app) =>
|
49 | meshbluAuth = new MeshbluAuth @meshbluConfig
|
50 |
|
51 | app.get '/', (req, res) => res.redirect('/auth/octoblu')
|
52 | app.get '/v1/form-schema', @formSchemaController.list
|
53 | app.get '/v1/message-schema', @messageSchemaController.list
|
54 | app.get '/v1/response-schema', @responseSchemaController.list
|
55 | app.get '/v1/configure-schema', @configureSchemaController.list
|
56 | app.get '/schemas/:name', @staticSchemasController.get
|
57 |
|
58 | app.get '/auth/octoblu', passport.authenticate('octoblu')
|
59 | app.get '/auth/octoblu/callback', passport.authenticate('octoblu', failureRedirect: '/auth/octoblu'), @octobluAuthController.storeAuthAndRedirect
|
60 |
|
61 | app.use meshbluAuth.auth()
|
62 | app.use meshbluAuth.gatewayRedirect('/auth/octoblu')
|
63 |
|
64 | upsert = @credentialsDeviceController.upsertWithRedirect
|
65 | upsert = @credentialsDeviceController.upsertWithoutRedirect if @skipRedirectAfterApiAuth
|
66 |
|
67 | app.get '/auth/api', passport.authenticate('api')
|
68 | app.get '/auth/api/callback', passport.authenticate('api'), upsert
|
69 | app.post '/auth/api/callback', passport.authenticate('api'), upsert
|
70 |
|
71 | app.post '/v1/messages', @messagesController.create
|
72 | app.post '/v1/configure', @configureController.create
|
73 |
|
74 | app.all '/credentials/:credentialsDeviceUuid*', @credentialsDeviceController.getCredentialsDevice
|
75 | app.get '/credentials/:credentialsDeviceUuid', @credentialsDeviceController.get
|
76 | app.get '/credentials/:credentialsDeviceUuid/user-devices', @userDevicesController.list
|
77 | app.post '/credentials/:credentialsDeviceUuid/user-devices', @userDevicesController.create
|
78 | app.delete '/credentials/:credentialsDeviceUuid/user-devices/:userDeviceUuid', @userDevicesController.delete
|
79 |
|
80 |
|
81 | app.use (req, res) => res.redirect '/auth/api'
|
82 |
|
83 | module.exports = Router
|