Skip to content

Commit 08337bd

Browse files
committed
chore: add authorizationParams tests
1 parent 3377f1a commit 08337bd

File tree

1 file changed

+161
-8
lines changed

1 file changed

+161
-8
lines changed

src/server/auth-client.test.ts

Lines changed: 161 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
6666
nonce,
6767
keyPair = DEFAULT.keyPair,
6868
onParRequest,
69-
backchannelAuth
69+
onBackchannelAuthRequest
7070
}: {
7171
tokenEndpointResponse?: oauth.TokenEndpointResponse | oauth.OAuth2Error;
7272
tokenEndpointErrorResponse?: oauth.OAuth2Error;
@@ -76,10 +76,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
7676
nonce?: string;
7777
keyPair?: jose.GenerateKeyPairResult;
7878
onParRequest?: (request: Request) => Promise<void>;
79-
backchannelAuth?: {
80-
interval: number;
81-
expiresIn: number;
82-
};
79+
onBackchannelAuthRequest?: (request: Request) => Promise<void>;
8380
} = {}) {
8481
// this function acts as a mock authorization server
8582
return vi.fn(
@@ -137,7 +134,6 @@ ca/T0LLtgmbMmxSv/MmzIg==
137134
// PAR endpoint
138135
if (url.pathname === "/oauth/par") {
139136
if (onParRequest) {
140-
// TODO: for some reason the input here is a URL and not a request
141137
await onParRequest(new Request(input, init));
142138
}
143139

@@ -150,11 +146,15 @@ ca/T0LLtgmbMmxSv/MmzIg==
150146
}
151147
// Backchannel Authorize endpoint
152148
if (url.pathname === "/bc-authorize") {
149+
if (onBackchannelAuthRequest) {
150+
await onBackchannelAuthRequest(new Request(input, init));
151+
}
152+
153153
return Response.json(
154154
{
155155
auth_req_id: "auth-req-id",
156-
expires_in: backchannelAuth?.expiresIn ?? 30,
157-
interval: backchannelAuth?.interval ?? 0.01
156+
expires_in: 30,
157+
interval: 0.01
158158
},
159159
{
160160
status: 200
@@ -5744,6 +5744,159 @@ ca/T0LLtgmbMmxSv/MmzIg==
57445744
);
57455745
expect(res).toBeNull();
57465746
});
5747+
5748+
it("should forward any statically configured authorization parameters", async () => {
5749+
const customScope = "openid profile email offline_access custom_scope";
5750+
const customAudience = "urn:mystore:api";
5751+
const customParamValue = "custom_value";
5752+
5753+
const secret = await generateSecret(32);
5754+
const transactionStore = new TransactionStore({
5755+
secret
5756+
});
5757+
const sessionStore = new StatelessSessionStore({
5758+
secret
5759+
});
5760+
const authClient = new AuthClient({
5761+
transactionStore,
5762+
sessionStore,
5763+
5764+
domain: DEFAULT.domain,
5765+
clientId: DEFAULT.clientId,
5766+
clientSecret: DEFAULT.clientSecret,
5767+
5768+
secret,
5769+
appBaseUrl: DEFAULT.appBaseUrl,
5770+
5771+
routes: getDefaultRoutes(),
5772+
authorizationParameters: {
5773+
scope: customScope,
5774+
audience: customAudience,
5775+
custom_param: customParamValue
5776+
},
5777+
fetch: getMockAuthorizationServer({
5778+
onBackchannelAuthRequest: async (req) => {
5779+
const formBody = await req.formData();
5780+
expect(formBody.get("scope")).toEqual(customScope);
5781+
expect(formBody.get("audience")).toEqual(customAudience);
5782+
expect(formBody.get("custom_param")).toEqual(customParamValue);
5783+
}
5784+
})
5785+
});
5786+
5787+
const [error, _] = await authClient.backchannelAuthentication({
5788+
bindingMessage: "test-message",
5789+
loginHint: {
5790+
sub: DEFAULT.sub
5791+
}
5792+
});
5793+
5794+
expect(error).toBeNull();
5795+
});
5796+
5797+
it("should forward any dynamically specified authorization parameters", async () => {
5798+
const customScope = "openid profile email offline_access custom_scope";
5799+
const customAudience = "urn:mystore:api";
5800+
const customParamValue = "custom_value";
5801+
5802+
const secret = await generateSecret(32);
5803+
const transactionStore = new TransactionStore({
5804+
secret
5805+
});
5806+
const sessionStore = new StatelessSessionStore({
5807+
secret
5808+
});
5809+
const authClient = new AuthClient({
5810+
transactionStore,
5811+
sessionStore,
5812+
5813+
domain: DEFAULT.domain,
5814+
clientId: DEFAULT.clientId,
5815+
clientSecret: DEFAULT.clientSecret,
5816+
5817+
secret,
5818+
appBaseUrl: DEFAULT.appBaseUrl,
5819+
5820+
routes: getDefaultRoutes(),
5821+
fetch: getMockAuthorizationServer({
5822+
onBackchannelAuthRequest: async (req) => {
5823+
const formBody = await req.formData();
5824+
expect(formBody.get("scope")).toEqual(customScope);
5825+
expect(formBody.get("audience")).toEqual(customAudience);
5826+
expect(formBody.get("custom_param")).toEqual(customParamValue);
5827+
}
5828+
})
5829+
});
5830+
5831+
const [error, _] = await authClient.backchannelAuthentication({
5832+
bindingMessage: "test-message",
5833+
loginHint: {
5834+
sub: DEFAULT.sub
5835+
},
5836+
authorizationParams: {
5837+
scope: customScope,
5838+
audience: customAudience,
5839+
custom_param: customParamValue
5840+
}
5841+
});
5842+
5843+
expect(error).toBeNull();
5844+
});
5845+
5846+
it("should give precedence to dynamically provided authorization parameters over statically configured ones", async () => {
5847+
const customScope = "openid profile email offline_access custom_scope";
5848+
const customParamValue = "custom_value";
5849+
5850+
const secret = await generateSecret(32);
5851+
const transactionStore = new TransactionStore({
5852+
secret
5853+
});
5854+
const sessionStore = new StatelessSessionStore({
5855+
secret
5856+
});
5857+
const authClient = new AuthClient({
5858+
transactionStore,
5859+
sessionStore,
5860+
5861+
domain: DEFAULT.domain,
5862+
clientId: DEFAULT.clientId,
5863+
clientSecret: DEFAULT.clientSecret,
5864+
5865+
secret,
5866+
appBaseUrl: DEFAULT.appBaseUrl,
5867+
5868+
routes: getDefaultRoutes(),
5869+
authorizationParameters: {
5870+
scope: customScope,
5871+
audience: "static-config-aud",
5872+
custom_param: customParamValue
5873+
},
5874+
fetch: getMockAuthorizationServer({
5875+
onBackchannelAuthRequest: async (req) => {
5876+
const formBody = await req.formData();
5877+
expect(formBody.get("scope")).toEqual(customScope);
5878+
expect(formBody.get("audience")).toEqual(
5879+
"dynamically-specific-aud"
5880+
);
5881+
expect(formBody.get("custom_param")).toEqual(customParamValue);
5882+
}
5883+
})
5884+
});
5885+
5886+
const [error, _] = await authClient.backchannelAuthentication({
5887+
bindingMessage: "test-message",
5888+
loginHint: {
5889+
sub: DEFAULT.sub
5890+
},
5891+
authorizationParams: {
5892+
scope: customScope,
5893+
audience: "dynamically-specific-aud",
5894+
custom_param: customParamValue
5895+
}
5896+
});
5897+
5898+
expect(error).toBeNull();
5899+
});
57475900
});
57485901
});
57495902

0 commit comments

Comments
 (0)