-
-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OIDC: add dynamic client registration util function (#3481)
* rename OidcDiscoveryError to OidcError * oidc client registration functions * test registerOidcClient * tidy test file * reexport OidcDiscoveryError for backwards compatibility
- Loading branch information
Kerry
authored
Jun 21, 2023
1 parent
80fec81
commit df78d7c
Showing
7 changed files
with
251 additions
and
42 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,84 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import fetchMockJest from "fetch-mock-jest"; | ||
|
||
import { OidcError } from "../../../src/oidc/error"; | ||
import { registerOidcClient } from "../../../src/oidc/register"; | ||
|
||
describe("registerOidcClient()", () => { | ||
const issuer = "https://auth.com/"; | ||
const registrationEndpoint = "https://auth.com/register"; | ||
const clientName = "Element"; | ||
const baseUrl = "https://just.testing"; | ||
const dynamicClientId = "xyz789"; | ||
|
||
const delegatedAuthConfig = { | ||
issuer, | ||
registrationEndpoint, | ||
authorizationEndpoint: issuer + "auth", | ||
tokenEndpoint: issuer + "token", | ||
}; | ||
beforeEach(() => { | ||
fetchMockJest.mockClear(); | ||
fetchMockJest.resetBehavior(); | ||
}); | ||
|
||
it("should make correct request to register client", async () => { | ||
fetchMockJest.post(registrationEndpoint, { | ||
status: 200, | ||
body: JSON.stringify({ client_id: dynamicClientId }), | ||
}); | ||
expect(await registerOidcClient(delegatedAuthConfig, clientName, baseUrl)).toEqual(dynamicClientId); | ||
expect(fetchMockJest).toHaveBeenCalledWith(registrationEndpoint, { | ||
headers: { | ||
"Accept": "application/json", | ||
"Content-Type": "application/json", | ||
}, | ||
method: "POST", | ||
body: JSON.stringify({ | ||
client_name: clientName, | ||
client_uri: baseUrl, | ||
response_types: ["code"], | ||
grant_types: ["authorization_code", "refresh_token"], | ||
redirect_uris: [baseUrl], | ||
id_token_signed_response_alg: "RS256", | ||
token_endpoint_auth_method: "none", | ||
application_type: "web", | ||
}), | ||
}); | ||
}); | ||
|
||
it("should throw when registration request fails", async () => { | ||
fetchMockJest.post(registrationEndpoint, { | ||
status: 500, | ||
}); | ||
expect(() => registerOidcClient(delegatedAuthConfig, clientName, baseUrl)).rejects.toThrow( | ||
OidcError.DynamicRegistrationFailed, | ||
); | ||
}); | ||
|
||
it("should throw when registration response is invalid", async () => { | ||
fetchMockJest.post(registrationEndpoint, { | ||
status: 200, | ||
// no clientId in response | ||
body: "{}", | ||
}); | ||
expect(() => registerOidcClient(delegatedAuthConfig, clientName, baseUrl)).rejects.toThrow( | ||
OidcError.DynamicRegistrationInvalid, | ||
); | ||
}); | ||
}); |
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
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,25 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
export enum OidcError { | ||
NotSupported = "OIDC authentication not supported", | ||
Misconfigured = "OIDC is misconfigured", | ||
General = "Something went wrong with OIDC discovery", | ||
OpSupport = "Configured OIDC OP does not support required functions", | ||
DynamicRegistrationNotSupported = "Dynamic registration not supported", | ||
DynamicRegistrationFailed = "Dynamic registration failed", | ||
DynamicRegistrationInvalid = "Dynamic registration invalid response", | ||
} |
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,111 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { IDelegatedAuthConfig } from "../client"; | ||
import { OidcError } from "./error"; | ||
import { Method } from "../http-api"; | ||
import { logger } from "../logger"; | ||
import { ValidatedIssuerConfig } from "./validate"; | ||
|
||
/** | ||
* Client metadata passed to registration endpoint | ||
*/ | ||
export type OidcRegistrationClientMetadata = { | ||
clientName: string; | ||
clientUri: string; | ||
redirectUris: string[]; | ||
}; | ||
|
||
/** | ||
* Make the client registration request | ||
* @param registrationEndpoint - URL as returned from issuer ./well-known/openid-configuration | ||
* @param clientMetadata - registration metadata | ||
* @returns resolves to the registered client id when registration is successful | ||
* @throws when registration request fails, or response is invalid | ||
*/ | ||
const doRegistration = async ( | ||
registrationEndpoint: string, | ||
clientMetadata: OidcRegistrationClientMetadata, | ||
): Promise<string> => { | ||
// https://openid.net/specs/openid-connect-registration-1_0.html | ||
const metadata = { | ||
client_name: clientMetadata.clientName, | ||
client_uri: clientMetadata.clientUri, | ||
response_types: ["code"], | ||
grant_types: ["authorization_code", "refresh_token"], | ||
redirect_uris: clientMetadata.redirectUris, | ||
id_token_signed_response_alg: "RS256", | ||
token_endpoint_auth_method: "none", | ||
application_type: "web", | ||
}; | ||
const headers = { | ||
"Accept": "application/json", | ||
"Content-Type": "application/json", | ||
}; | ||
|
||
try { | ||
const response = await fetch(registrationEndpoint, { | ||
method: Method.Post, | ||
headers, | ||
body: JSON.stringify(metadata), | ||
}); | ||
|
||
if (response.status >= 400) { | ||
throw new Error(OidcError.DynamicRegistrationFailed); | ||
} | ||
|
||
const body = await response.json(); | ||
const clientId = body["client_id"]; | ||
if (!clientId || typeof clientId !== "string") { | ||
throw new Error(OidcError.DynamicRegistrationInvalid); | ||
} | ||
|
||
return clientId; | ||
} catch (error) { | ||
if (Object.values(OidcError).includes((error as Error).message as OidcError)) { | ||
throw error; | ||
} else { | ||
logger.error("Dynamic registration request failed", error); | ||
throw new Error(OidcError.DynamicRegistrationFailed); | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Attempts dynamic registration against the configured registration endpoint | ||
* @param delegatedAuthConfig - Auth config from ValidatedServerConfig | ||
* @param clientName - Client name to register with the OP, eg 'Element' | ||
* @param baseUrl - URL of the home page of the Client, eg 'https://app.element.io/' | ||
* @returns Promise<string> resolved with registered clientId | ||
* @throws when registration is not supported, on failed request or invalid response | ||
*/ | ||
export const registerOidcClient = async ( | ||
delegatedAuthConfig: IDelegatedAuthConfig & ValidatedIssuerConfig, | ||
clientName: string, | ||
baseUrl: string, | ||
): Promise<string> => { | ||
const clientMetadata = { | ||
clientName, | ||
clientUri: baseUrl, | ||
redirectUris: [baseUrl], | ||
}; | ||
if (!delegatedAuthConfig.registrationEndpoint) { | ||
throw new Error(OidcError.DynamicRegistrationNotSupported); | ||
} | ||
const clientId = await doRegistration(delegatedAuthConfig.registrationEndpoint, clientMetadata); | ||
|
||
return clientId; | ||
}; |
Oops, something went wrong.