Guest User

Untitled

a guest
Jul 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. "dependencies": {
  2. "babel-preset-es2015": "^6.24.1",
  3. "body-parser": "^1.16.0",
  4. "connect-timeout": "^1.8.0",
  5. "core-js": "^2.4.1",
  6. "cors": "^2.8.1",
  7. "express": "^4.14.1",
  8. "joi": "^10.2.2",
  9. "jsonapi-serializer": "^3.5.3",
  10. "mongoose": "^4.10.4",
  11. "mongoose-rename-id": "^1.0.2",
  12. "nedb": "^1.8.0",
  13. "path": "^0.12.7",
  14. "randomstring": "^1.1.5",
  15. "request": "^2.79.0",
  16. "request-promise": "^4.1.1",
  17. "swagger-express-mw": "^0.1.0",
  18. "swagger-ui": "^2.2.10",
  19. "yamljs": "^0.2.8"
  20.  
  21. "devDependencies": {
  22. "babel-cli": "^6.22.2",
  23. "babel-core": "^6.22.1",
  24. "babel-preset-es2015": "^6.24.1",
  25. "babel-preset-es2015-node": "^6.1.1",
  26. "babel-preset-es2016-node4": "^6.0.1",
  27. "babel-preset-es2017": "^6.22.0",
  28. "chai": "^3.5.0",
  29. "chai-as-promised": "^6.0.0",
  30. "husky": "^0.13.1",
  31. "mocha": "^3.2.0",
  32. "mocha-duplicate-reporter": "^0.2.1",
  33. "mocha-multi": "^0.9.0",
  34. "mocha-sonar-generic-test-coverage": "0.0.1",
  35. "nock": "^9.0.2",
  36. "node-mocks-http": "^1.6.1",
  37. "nodemon": "^1.11.0",
  38. "nyc": "^10.1.2",
  39. "pact": "^1.0.0",
  40. "rimraf": "^2.5.4",
  41. "sinon": "^1.17.7",
  42. "sinon-chai": "^2.8.0",
  43. "snazzy": "^6.0.0",
  44. "standard": "^8.6.0",
  45. "unit.js": "^2.0.0"
  46.  
  47. .babelrc-
  48. {
  49. "presets": [
  50. "es2016-node4",
  51. "es2017",
  52. "es2015"
  53. ]
  54. }
  55.  
  56. 'use strict'
  57. import * as path from 'path'
  58. import {PORT} from 'config'
  59.  
  60. // config module loads the configuration from the folder the process is run
  61. process.env[ 'NODE_CONFIG_DIR' ] = path.resolve(__dirname, '/config')
  62.  
  63. import server from './server'
  64.  
  65. // Start the server
  66. server(PORT)
  67.  
  68. import cors from 'cors'
  69. import SwaggerExpress from 'swagger-express-mw'
  70. import bodyParser from 'body-parser'
  71. import timeout from 'connect-timeout'
  72. import * as YAML from 'yamljs'
  73. import * as path from 'path'
  74. import account from '../src/api/controllers/accounts.js'
  75.  
  76. const version = {
  77. file: YAML.load(path.join(__dirname, 'version.yml'))
  78. }
  79.  
  80. var config = {
  81. appRoot: __dirname,
  82. swaggerFile: path.resolve(__dirname, 'api', 'swagger', 'swagger.json'),
  83. basePath: '/api/v1',
  84. swaggerSecurityHandlers:{
  85. basicAuth :async function(req, authOrSecDef, scopesOrApiKey, callback){
  86. try{
  87. //let data = true
  88. console.log(req.headers.authorization)
  89.  
  90. let data = await account.authenticate(req.headers.authorization)
  91. if(data.statusCode==200){
  92. console.log(data)
  93. callback(null,true);
  94. }
  95. else
  96. callback(new Error("Access denied"))
  97. }catch(err){
  98. callback(new Error("Access denied"))
  99. }
  100. console.log("response ...."+data)
  101. }
  102. }
  103.  
  104. }
  105.  
  106. export default function start (serverPort) {
  107. SwaggerExpress.create(config, function (err, swaggerExpress) {
  108. if (err) {
  109. throw err
  110. }
  111.  
  112. var app = require('express')()
  113.  
  114. // Hack to override the host and port
  115. app.get(path.resolve(config.basePath, '/api-docs'), function (req, res) {
  116. swaggerExpress.runner.swagger.host = req.get('host')
  117. // Set correct version for the API
  118. swaggerExpress.runner.swagger.info.version = version.file.build.name
  119. res.json(swaggerExpress.runner.swagger)
  120. })
  121.  
  122. // Customize SwaggerUI
  123. var swaggerUIParams = {
  124. swaggerUi: config.basePath + '/docs',
  125. apiDocs: config.basePath + '/api-docs'
  126. }
  127.  
  128. // Add for version
  129. app.get('/version', function (req, res) {
  130. // Load yaml file using YAML.load
  131.  
  132. res.json(version.file.build)
  133. })
  134.  
  135. // serves the Swagger documents and Swagger UI
  136. app.use(swaggerExpress.runner.swaggerTools.swaggerUi(swaggerUIParams))
  137.  
  138. app.use(cors())
  139.  
  140. app.use(bodyParser.json())
  141.  
  142. app.use(timeout('6s'))
  143.  
  144. // install middleware
  145. swaggerExpress.register(app)
  146.  
  147. app.listen(serverPort)
  148. })
  149. }
  150.  
  151. import request from 'request-promise'
  152.  
  153. export default account()
  154.  
  155. function account() {
  156.  
  157. return {
  158.  
  159. authenticate: async function authenticate (authheader) {
  160. console.log("Sending call for Account")
  161. let temp = (authheader).split(" ")
  162. console.log(temp[1])
  163. let buf = new Buffer(temp[1], 'base64'); // create a buffer and tell it the
  164. data coming in is base64
  165. let plain_auth = buf.toString(); // read it back out as a string
  166. console.log("Decoded Authorization ", plain_auth);
  167. const cred = plain_auth.split(':')
  168.  
  169.  
  170. const options={
  171.  
  172. uri: `http://localhost:3000/api/account/${cred[0]}`,
  173. json: true,
  174. resolveWithFullResponse: true,
  175. headers: {
  176. 'Content-Type': 'application/json; charset=utf-8',
  177. 'Accept': 'application/json; charset=utf-8'
  178. // 'Authorization':authheader
  179. },
  180. method: 'GET'
  181.  
  182. }
  183.  
  184. return request(options).then((response) => {
  185.  
  186. console.log(response.statusCode)
  187. return {
  188. "statusCode":response.statusCode,
  189. "body":response.body
  190. }
  191.  
  192. }).catch((err) => {
  193. console.log(err);
  194. console.log('errorstatuscode:' + err.statusCode)
  195. });
  196.  
  197.  
  198. }
  199.  
  200.  
  201.  
  202. }
  203.  
  204. }
Add Comment
Please, Sign In to add comment