Skip to content

Commit 7caf490

Browse files
committed
Switch to pure HTML authorize loop
1 parent 0f5f949 commit 7caf490

File tree

5 files changed

+53
-26
lines changed

5 files changed

+53
-26
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,18 @@ token they want to use. For example, given the following `FAUXAUTH_CONFIG`:
128128
something like the following form will be rendered:
129129

130130
```html
131-
<form action="" id="root-form">
131+
<form action="/authorize" method="post" id="root-form">
132132
<label for="role-select">
133133
Select token
134-
<select id="role-select">
134+
<select id="role-select" name="code">
135135
<option value="288e5e60aa9220000000">Headteacher</option>
136136
<option value="c4f9e4bfffa600000000">Teacher</option>
137137
<option value="76555f344527c0000000">Student</option>
138138
</select>
139139
</label>
140+
141+
<!-- hidden inputs -->
142+
140143
<button id="submit-button" type="submit">Authenticate</button>
141144
</form>
142145
```

e2e/tests/fauxauth.e2e.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,16 @@ describe("fauxauth", () => {
102102
});
103103
expect(res.statusCode).toBe(200);
104104

105-
await browser.url("/authorize?client_id=1ae9b0ca17e754106b51");
105+
await browser.url("/authorize?client_id=1ae9b0ca17e754106b51&state=bananas&redirect_uri=http%3A%2F%2Fexample.org%2Ftest");
106106
const select = await browser.$("#role-select");
107107
await select.selectByVisibleText("User");
108108
const button = await browser.$("#submit-button");
109109
await button.click();
110110

111-
const url = await browser.getUrl();
112111
const codePattern = /code=([a-z0-9]{20})/i;
112+
const url = await browser.getUrl();
113+
expect(url).toMatch(/^http:\/\/example.org\/test/);
114+
expect(url).toMatch(/state=bananas/);
113115
expect(url).toMatch(codePattern);
114116
const [, code] = codePattern.exec(url);
115117

src/routes/authorize.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ export default (configuration: Configuration) => {
5959
configuration.codes[code] = configuration.tokenMap![role];
6060
});
6161

62-
res.render("index", { redirectUrl, roles });
62+
res.render("index", { query: { ...query, redirect_uri: redirectUrl }, roles });
63+
});
64+
65+
router.post("/", (req, res) => {
66+
const { redirect_uri: pathname, ...query } = req.body;
67+
res.redirect(format({ pathname, query }));
6368
});
6469

6570
return router;

tests/integration/authorize.integration.spec.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,36 @@ describe("authorize endpoint", () => {
103103
.expect(404);
104104
});
105105

106-
it("uses a token map if provided", () => {
106+
describe("with token map", () => {
107107
const tokenMap = { role: "tokenforthatrole" };
108-
return request(appFactory({ ...defaultConfiguration, tokenMap }))
109-
.get(endpoint)
110-
.query({ client_id: defaultConfiguration.clientId })
111-
.expect(200)
112-
.expect("Content-Type", /html/);
108+
109+
beforeEach(() => {
110+
app = appFactory({ ...defaultConfiguration, tokenMap });
111+
});
112+
113+
it("uses a token map if provided", () => {
114+
return request(app)
115+
.get(endpoint)
116+
.query({ client_id: defaultConfiguration.clientId })
117+
.expect(200)
118+
.expect("Content-Type", /html/);
119+
});
120+
121+
it("handles the post from the form", () => {
122+
const code = "mycode";
123+
const redirectUri = "/path/to";
124+
const state = "state";
125+
126+
return request(app)
127+
.post(endpoint)
128+
.type("form")
129+
.send({ redirect_uri: redirectUri, state, code })
130+
.expect(302)
131+
.then((res) => {
132+
const { query, pathname } = parse(res.get("Location"), true);
133+
expect(pathname).toBe(redirectUri);
134+
expect(query).toEqual({ code, state });
135+
});
136+
});
113137
});
114138
});

views/index.ejs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
<form action="" id="root-form">
1+
<form action="/authorize" method="post" id="root-form">
22
<label for="role-select">
33
Select token
4-
<select id="role-select">
4+
<select id="role-select" name="code">
55
<% Object.keys(roles).forEach(function (role) { %>
66
<%- `<option value="${roles[role]}">${role}</option>` %>
77
<% }); %>
88
</select>
99
</label>
10+
11+
<% Object.keys(query).forEach(function (param) { %>
12+
<%- `<input hidden name="${param}" type="text" value="${query[param]}" />` %>
13+
<% }); %>
14+
15+
1016
<button id="submit-button" type="submit">Authenticate</button>
1117
</form>
12-
13-
<script>
14-
var redirectUrl = "<%= redirectUrl %>";
15-
window.addEventListener("load", function () {
16-
document.getElementById("root-form").onsubmit = function () {
17-
window.location = redirectUrl.replace(
18-
"code=placeholder",
19-
`code=${document.getElementById("role-select").value}`
20-
);
21-
return false;
22-
};
23-
});
24-
</script>

0 commit comments

Comments
 (0)