Created
March 22, 2018 16:12
-
-
Save green3g/56819e05969bdf2984c80f0035cc619e to your computer and use it in GitHub Desktop.
A NodeJS feathers api for arcgis tokens
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const axios = require('axios'); | |
| const querystring = require('querystring'); | |
| /* eslint-disable no-unused-vars */ | |
| class Service { | |
| constructor (options) { | |
| this.options = options || {}; | |
| } | |
| get (id, params) { | |
| const arcgis = this.options.arcgis; | |
| // auth data | |
| const data = { | |
| f: 'json', | |
| client: 'referer', | |
| referer: this.options.host, | |
| request: 'getToken', | |
| expiration: arcgis.tokenExpires, // 60 minutes | |
| username: arcgis.username, | |
| password: arcgis.password | |
| }; | |
| // token url | |
| const url = arcgis.url + '/tokens/generateToken'; | |
| // some headers for passing form data | |
| const headers = { | |
| 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', | |
| 'Accept': 'application/json' | |
| }; | |
| return axios.post(url, querystring.stringify(data), { | |
| headers | |
| }).then(result => { | |
| Object.assign(result.data, { | |
| server: arcgis.url + '/rest/services', | |
| ssl: true | |
| }); | |
| return result.data; | |
| }); | |
| } | |
| } | |
| module.exports = function (options) { | |
| return new Service(options); | |
| }; | |
| module.exports.Service = Service; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const {authenticate} = require('@feathersjs/authentication').hooks; | |
| const requireRoles = require('../util/requireRoles'); | |
| module.exports = { | |
| before: { | |
| all: [authenticate('jwt'), requireRoles(['arcgis_user'])], | |
| find: [], | |
| get: [ | |
| ], | |
| create: [], | |
| update: [], | |
| patch: [], | |
| remove: [] | |
| }, | |
| after: { | |
| all: [], | |
| find: [], | |
| get: [], | |
| create: [], | |
| update: [], | |
| patch: [], | |
| remove: [] | |
| }, | |
| error: { | |
| all: [], | |
| find: [], | |
| get: [], | |
| create: [], | |
| update: [], | |
| patch: [], | |
| remove: [] | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Initializes the `arcgis-token` service on path `/api/arcgis-token` | |
| const createService = require('./arcgis-token.class.js'); | |
| const hooks = require('./arcgis-token.hooks'); | |
| module.exports = function (app) { | |
| const paginate = app.get('paginate'); | |
| const arcgis = app.get('arcgis'); | |
| const host = app.get('host'); | |
| const options = { | |
| host, | |
| arcgis, | |
| name: 'arcgis-token', | |
| paginate | |
| }; | |
| // Initialize our service with any options it requires | |
| app.use('/api/arcgis-token', createService(options)); | |
| // Get our initialized service so that we can register hooks and filters | |
| const service = app.service('api/arcgis-token'); | |
| service.hooks(hooks); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment