Skip to content

Commit

Permalink
fix(parse): fix to parsing pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Apr 27, 2023
1 parent 8046b0d commit c8ee900
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
4 changes: 2 additions & 2 deletions _abnf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const challenge = sequence(
suffix("+", SP),
either(
namedCapture("token68", token68),
namedCapture("authParam", /.+/),
namedCapture("authParam", /.*/),
),
),
);
Expand Down Expand Up @@ -61,7 +61,7 @@ const authParam = sequence(
);

if (import.meta.main) {
console.log("challenge:", challenge);
console.log("challenge:", optimize(challenge).toRegExp());
console.log("element: ", element);
console.log("authParam: ", optimize(authParam).toRegExp());
console.log("token: ", optimize(token).toRegExp());
Expand Down
7 changes: 6 additions & 1 deletion auth_param.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@
"must_fail": true
},
{
"name": "invalid quoted-pair",
"name": "invalid quoted-pair char",
"header": "a=\"\u0000\"",
"must_fail": true
},
{
"name": "invalid quoted-pair syntax",
"header": "a=\"a\\\"",
"must_fail": true
}
]
17 changes: 17 additions & 0 deletions authorization.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
[
{
"name": "no params",
"header": "A",
"expected": {
"authScheme": "A",
"params": null
}
},
{
"name": "empty params",
"header": "B ",
"expected": {
"authScheme": "B",
"params": {}
},
"normalize": "B"
},
{
"name": "basic",
"ref": "https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization#basic_authentication",
Expand Down
4 changes: 3 additions & 1 deletion parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { head, isString, toLowerCase } from "./deps.ts";
import { Msg } from "./constants.ts";
import type { Authorization, AuthParams } from "./types.ts";

/** Generate from _abnf.ts. */
const reAuthorization =
/^(?<authScheme>[!#$%&'*+.^_`|~\dA-Za-z-]+)(?: +(?:(?<token68>(?:[A-Za-z]|\d|[-._~+/])+=*)|(?<authParam>.+)))?$/;
/^(?<authScheme>[\w!#$%&'*+.^`|~-]+)(?: +(?:(?<token68>(?:[A-Za-z]|\d|[+./_~-])+=*)|(?<authParam>.*)))?$/;

/** Parse string into {@link Authorization}.
*
Expand Down Expand Up @@ -57,6 +58,7 @@ type ParsedGroups = {
readonly authParam: string | undefined;
};

/** Generate from _abnf.ts. */
const reAuthParam =
/^(?<key>[\w!#$%&'*+.^`|~-]+)[\t ]*=[\t ]*(?:(?<token>[\w!#$%&'*+.^`|~-]+)|(?<quotedString>"(?:\t| |!|[ \x23-\x5B\x5D-\x7E]|[\x80-\xFF]|\\(?:\t| |[\x21-\x7E]|[\x80-\xFF]))*"))$/;

Expand Down
4 changes: 3 additions & 1 deletion stringify_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ describe("stringifyAuthorization", () => {
authorization.forEach((suite) => {
it(suite.name, () => {
if (!suite.must_fail) {
const input = suite.normalize ?? suite.header;

assertEquals(
stringifyAuthorization(suite.expected as Authorization),
suite.header,
input,
);
}
});
Expand Down

0 comments on commit c8ee900

Please sign in to comment.