-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89224f8
commit 7950140
Showing
18 changed files
with
1,143 additions
and
1,050 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
const RE_GT = />/g; | ||
const RE_LT = /</g; | ||
const RE_AMP = /&/g; | ||
const RE_QUOT = /"/g; | ||
|
||
function htmlEncode(html) { | ||
return String(html) | ||
.trim() | ||
.replace(RE_AMP , "&") | ||
.replace(RE_LT , "<") | ||
.replace(RE_GT , ">") | ||
.replace(RE_QUOT, """); | ||
} | ||
|
||
class OperationOutcome | ||
{ | ||
/** | ||
* | ||
* @param {string} message | ||
* @param {"fatal" | "error" | "warning" | "information"|string} [severity] | ||
* @param {string} [issueCode] see http://hl7.org/fhir/valueset-issue-type.html | ||
*/ | ||
constructor(message, issueCode = "processing", severity = "error") | ||
{ | ||
this.message = message | ||
this.issueCode = issueCode | ||
this.severity = severity | ||
} | ||
|
||
toJSON() | ||
{ | ||
return { | ||
"resourceType": "OperationOutcome", | ||
"text": { | ||
"status": "generated", | ||
"div": '<div xmlns="http://www.w3.org/1999/xhtml"><h1>Operation Outcome</h1>' + | ||
'<table border="0"><tr><td style="font-weight:bold;">ERROR</td><td>[]</td>' + | ||
'<td><pre>' + htmlEncode(this.message) + '</pre></td></tr></table></div>' | ||
}, | ||
"issue": [ | ||
{ | ||
"severity" : this.severity, | ||
"code" : this.issueCode, | ||
"diagnostics": this.message | ||
} | ||
] | ||
} | ||
} | ||
} | ||
|
||
module.exports = OperationOutcome |
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 |
---|---|---|
@@ -1,67 +1,40 @@ | ||
const jwt = require("jsonwebtoken"); | ||
const config = require("./config"); | ||
const SMARTHandler = require("./SMARTHandler"); | ||
const Lib = require("./lib"); | ||
|
||
class RegistrationHandler extends SMARTHandler { | ||
|
||
static handleRequest(req, res) { | ||
return new RegistrationHandler(req, res).handle(); | ||
const jwt = require("jsonwebtoken") | ||
const config = require("./config") | ||
const errors = require("./errors") | ||
|
||
/** @type any */ | ||
const assert = require("./lib").assert | ||
|
||
module.exports = function handleRegistration(req, res) { | ||
|
||
// Require "application/x-www-form-urlencoded" POSTs | ||
assert(req.is("application/x-www-form-urlencoded"), errors.form_content_type_required) | ||
|
||
// parse and validate the "iss" parameter | ||
let iss = String(req.body.iss || "").trim() | ||
assert(iss, errors.registration.missing_param, "iss") | ||
|
||
// parse and validate the "pub_key" parameter | ||
let publicKey = String(req.body.pub_key || "").trim() | ||
assert(publicKey, errors.registration.missing_param, "pub_key") | ||
|
||
// parse and validate the "dur" parameter | ||
let dur = parseInt(req.body.dur || "15", 10) | ||
assert(!isNaN(dur) && isFinite(dur) && dur >= 0, errors.registration.invalid_param, "dur") | ||
|
||
// Build the result token | ||
let jwtToken = { pub_key: publicKey, iss } | ||
|
||
// Note that if dur is 0 accessTokensExpireIn will not be included | ||
if (dur) { | ||
jwtToken.accessTokensExpireIn = dur | ||
} | ||
|
||
handle() { | ||
const req = this.request; | ||
const res = this.response; | ||
|
||
// Require "application/x-www-form-urlencoded" POSTs | ||
if (!req.headers["content-type"] || req.headers["content-type"].indexOf("application/x-www-form-urlencoded") !== 0) { | ||
return Lib.replyWithError(res, "form_content_type_required", 401); | ||
} | ||
|
||
this.handleBackendServiceRegistration(); | ||
// Custom errors (if any) | ||
if (req.body.auth_error) { | ||
jwtToken.auth_error = req.body.auth_error | ||
} | ||
|
||
handleBackendServiceRegistration() { | ||
const req = this.request; | ||
const res = this.response; | ||
|
||
// parse and validate the "iss" parameter | ||
let iss = String(req.body.iss || "").trim(); | ||
if (!iss) { | ||
return Lib.replyWithError(res, "missing_parameter", 400, "iss"); | ||
} | ||
|
||
// parse and validate the "pub_key" parameter | ||
let publicKey = String(req.body.pub_key || "").trim(); | ||
if (!publicKey) { | ||
return Lib.replyWithError(res, "missing_parameter", 400, "pub_key"); | ||
} | ||
|
||
// parse and validate the "dur" parameter | ||
let dur = parseInt(req.body.dur || "15", 10); | ||
if (isNaN(dur) || !isFinite(dur) || dur < 0) { | ||
return Lib.replyWithError(res, "invalid_parameter", 400, "dur"); | ||
} | ||
|
||
// Build the result token | ||
let jwtToken = { | ||
pub_key: publicKey, | ||
iss | ||
}; | ||
|
||
// Note that if dur is 0 accessTokensExpireIn will not be included | ||
if (dur) { | ||
jwtToken.accessTokensExpireIn = dur; | ||
} | ||
|
||
// Custom errors (if any) | ||
if (req.body.auth_error) { | ||
jwtToken.auth_error = req.body.auth_error; | ||
} | ||
|
||
// Reply with signed token as text | ||
res.type("text").send(jwt.sign(jwtToken, config.jwtSecret)); | ||
} | ||
// Reply with signed token as text | ||
res.type("text").send(jwt.sign(jwtToken, config.jwtSecret)) | ||
} | ||
|
||
module.exports = RegistrationHandler; |
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
Oops, something went wrong.