-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add `issuedToken` model To store the token that admin issued. feat: add token issuer web page feat: add token manager web page feat: add token issue api feat: add get tokens api feat: add delete token api feat: add admin login passport Login admin user to use token issuser and manager
- Loading branch information
1 parent
0071d8d
commit f1541ef
Showing
54 changed files
with
31,221 additions
and
1,526 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const mongodb = require('../../../models/mongodb'); | ||
|
||
/** | ||
* | ||
* @param {import('express').Request} req | ||
* @param {import('express').Response} res | ||
*/ | ||
module.exports = async function(req, res) { | ||
try { | ||
await mongodb.issuedToken.deleteOne({ | ||
_id: req.params._id | ||
}); | ||
return res.status(200).send({ | ||
status: true, | ||
message: "Delete success" | ||
}) | ||
} catch(e) { | ||
console.error(e); | ||
return res.status(500).send({ | ||
status: false, | ||
message: e | ||
}); | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const _ = require('lodash'); | ||
const mongodb = require('../../../models/mongodb'); | ||
/** | ||
* | ||
* @param {import('express').Request} req | ||
* @param {import('express').Response} res | ||
*/ | ||
module.exports = async function(req, res) { | ||
if (req.user != process.env.ADMIN_USERNAME) { | ||
return res.status(403).send(); | ||
} | ||
let queryParameter = _.cloneDeep(req.query); | ||
let paginationSkip = queryParameter['_offset'] == undefined ? 0 : queryParameter['_offset']; | ||
let paginationLimit = queryParameter['_count'] == undefined ? 100 : queryParameter['_count']; | ||
_.set(req.query, "_offset", paginationSkip); | ||
_.set(req.query, "_count", paginationLimit); | ||
delete queryParameter['_count']; | ||
delete queryParameter['_offset']; | ||
let docs = await mongodb.issuedToken.find({} , { | ||
accessList: 1, | ||
tokenName: 1, | ||
tokenNote: 1, | ||
_id: 1 | ||
}). | ||
limit(paginationLimit). | ||
skip(paginationSkip). | ||
sort({ | ||
_id: -1 | ||
}). | ||
exec(); | ||
let count = await mongodb.issuedToken.countDocuments({}); | ||
return res.send({ | ||
tokenList : docs, | ||
total: count | ||
}); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
/** | ||
* | ||
* @param {import('express').Request} req | ||
* @param {import('express').Response} res | ||
*/ | ||
module.exports = function (req, res) { | ||
return res.status(200).send(req.user); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
const jwt = require('jsonwebtoken'); | ||
const mongodb = require('../../../models/mongodb'); | ||
|
||
/** | ||
* @param {import('express').Request} req | ||
* @param {import('express').Response} res | ||
*/ | ||
module.exports = async function (req , res) { | ||
try { | ||
if (req.user != process.env.ADMIN_USERNAME) { | ||
return res.status(403).send(); | ||
} | ||
let token = jwt.sign(req.body , "AhKais7aij9tai7i" , {expiresIn: '1y'}); | ||
let tokenObj = new mongodb.issuedToken({ | ||
...req.body , | ||
token: `Bearer ${token}` | ||
}) | ||
await tokenObj.save(); | ||
return res.status(200).send(tokenObj); | ||
} catch(err) { | ||
console.error(err); | ||
return res.status(500).json(err); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const express = require('express'); | ||
const Joi = require('joi'); | ||
const router = express.Router(); | ||
const { user } = require('../apiService'); | ||
const { validateParams } = require('../validator'); | ||
const resourceTypeList = require('../../models/FHIR/resourceType'); | ||
|
||
router.post('/adminLogin', function (req , res , next) { | ||
let passport = require('passport'); | ||
passport.authenticate('admin-login', function (err, user, info) { | ||
if (err) { return next(err); } | ||
if (!user) { | ||
return res.status(401).json(info); | ||
} | ||
req.logIn(user, function (err) { | ||
// Should not cause any errors | ||
if (err) { return next(err); } | ||
return res.json(user); | ||
}); | ||
})(req, res, next); | ||
//next(new Error("missing username or password")); | ||
}); | ||
|
||
router.get('/loginStatus' , user.checkIsLoggedIn, require('./controller/getLoginStatus')); | ||
|
||
router.post('/token/issue', user.checkIsLoggedIn, validateParams({ | ||
accessList: Joi.array().single().items(Joi.object().keys({ | ||
resourceType: Joi.string().valid(...resourceTypeList), | ||
read: Joi.boolean().default(false), | ||
vread: Joi.boolean().default(false), | ||
create: Joi.boolean().default(false), | ||
update: Joi.boolean().default(false), | ||
"search-type": Joi.boolean().default(false), | ||
history: Joi.boolean().default(false), | ||
delete: Joi.boolean().default(false) | ||
}).min(1)).required(), | ||
tokenName: Joi.string().required(), | ||
tokenNote: Joi.string() | ||
}, "body" , { | ||
allowUnknown: false | ||
}) , require('./controller/postTokenIssue')); | ||
|
||
router.get( | ||
'/token', | ||
user.checkIsLoggedIn, | ||
validateParams({ | ||
"_offset": Joi.number().integer(), | ||
"_count": Joi.number().integer() | ||
}, "query" , {allowUnknown: false}), | ||
require('./controller/getIssuedToken')); | ||
|
||
router.delete( | ||
'/token/:_id' , | ||
user.checkIsLoggedIn, | ||
require('./controller/deleteToken')) | ||
|
||
|
||
module.exports = router; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* | ||
* @param {import('mongoose')} mongodb | ||
* @returns | ||
*/ | ||
module.exports = function (mongodb) { | ||
let accessItemSchema = mongodb.Schema({ | ||
resourceType: { | ||
type: String, | ||
default: void 0 | ||
}, | ||
create: { | ||
type: mongodb.SchemaTypes.Boolean, | ||
default: false | ||
} , | ||
delete: { | ||
type: mongodb.SchemaTypes.Boolean, | ||
default: false | ||
}, | ||
read: { | ||
type: mongodb.SchemaTypes.Boolean, | ||
default: false | ||
}, | ||
vread: { | ||
type: mongodb.SchemaTypes.Boolean, | ||
default: false | ||
}, | ||
search: { | ||
type: mongodb.SchemaTypes.Boolean, | ||
default: false | ||
}, | ||
history: { | ||
type: mongodb.SchemaTypes.Boolean, | ||
default: false | ||
} | ||
}, { | ||
_id: false, | ||
id: false, | ||
versionKey: false | ||
}); | ||
|
||
let issuedTokenSchema = mongodb.Schema({ | ||
token: { | ||
type: String, | ||
default: void 0 | ||
}, | ||
tokenName: { | ||
type: String, | ||
default: void 0 | ||
}, | ||
tokenNote: { | ||
type: String, | ||
default: void 0 | ||
}, | ||
accessList : { | ||
type: [accessItemSchema] , | ||
default: void 0 | ||
} | ||
},{ | ||
strict: false, | ||
versionKey : false, | ||
}); | ||
|
||
issuedTokenSchema.index({ | ||
"resourceType" : 1 | ||
}); | ||
|
||
let issuedToken = mongodb.model('issuedToken', issuedTokenSchema, 'issuedToken'); | ||
return issuedToken; | ||
} |
Oops, something went wrong.