Skip to content

Commit

Permalink
feat(simple-oauth): add support to explicitly allow authentication fo…
Browse files Browse the repository at this point in the history
…r token requests
  • Loading branch information
chfoidl committed Jun 18, 2024
1 parent 95ae86d commit f3deed9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/rotten-yaks-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@drupal-kit/simple-oauth": patch
---

Add support to explicitly allow authenticated token requests on per request basis
2 changes: 1 addition & 1 deletion packages/simple-oauth/src/DrupalkitSimpleOauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const DrupalkitSimpleOauth = (
{
method: "POST",
body,
unauthenticated: true,
unauthenticated: requestOptions?.unauthenticated !== false,
headers: {
"content-type": "application/x-www-form-urlencoded",
},
Expand Down
37 changes: 37 additions & 0 deletions packages/simple-oauth/tests/DrupalkitSimpleOauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,43 @@ test.serial("Request token with custom request options", async (t) => {
);
});

test.serial("Request token authenticated", async (t) => {
t.plan(3);

const authinfo = "Bearer abc123";

const drupalkit = createDrupalkit();
drupalkit.setAuth(authinfo);

drupalkit.hook.before("request", (options) => {
t.is(options.cache, "no-cache");
});

server.use(
http.post("*/oauth/token", async ({ request }) => {
t.is(request.headers.get("X-Custom"), "1");
t.is(request.headers.get("Authorization"), authinfo);

return HttpResponse.json(TokenResponse);
}),
);

await drupalkit.simpleOauth.requestToken(
"client_credentials",
{
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
},
{
cache: "no-cache",
headers: {
"X-Custom": "1",
},
unauthenticated: false,
},
);
});

test.serial("Request token with explicit endpoint", async (t) => {
const drupalkit = createDrupalkit({
baseUrl: BASE_URL,
Expand Down

0 comments on commit f3deed9

Please sign in to comment.