Skip to content

Commit

Permalink
Re-use originFromDomain utility function in Auth0 and Keycloak prov…
Browse files Browse the repository at this point in the history
…iders (#1337)

Co-authored-by: pilcrow <[email protected]>
  • Loading branch information
AmruthPillai and pilcrowonpaper authored Jan 16, 2024
1 parent ee7dd23 commit 58e8b82
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .auri/$8h6jkpso.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
package: "@lucia-auth/oauth"
type: "patch"
---

Update Keycloak provider to accept domain argument with protocol
13 changes: 5 additions & 8 deletions packages/oauth/src/providers/auth0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
validateOAuth2AuthorizationCode
} from "../core/oauth2.js";
import { ProviderUserAuth } from "../core/provider.js";
import { handleRequest, authorizationHeader } from "../utils/request.js";
import {
handleRequest,
authorizationHeader,
originFromDomain
} from "../utils/request.js";

import type { Auth } from "lucia";

Expand Down Expand Up @@ -113,13 +117,6 @@ const getAuth0User = async (appDomain: string, accessToken: string) => {
return auth0User;
};

const originFromDomain = (domain: string): string => {
if (domain.startsWith("https://") || domain.startsWith("http://")) {
return domain;
}
return "https://" + domain;
};

export type Auth0Tokens = {
accessToken: string;
refreshToken: string;
Expand Down
21 changes: 17 additions & 4 deletions packages/oauth/src/providers/keycloak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
} from "../core/oauth2.js";
import { ProviderUserAuth } from "../core/provider.js";
import { decodeIdToken } from "../index.js";
import { handleRequest, authorizationHeader } from "../utils/request.js";
import {
handleRequest,
authorizationHeader,
originFromDomain
} from "../utils/request.js";

import type { Auth } from "lucia";

Expand Down Expand Up @@ -43,7 +47,10 @@ export class KeycloakAuth<
> => {
const scopeConfig = this.config.scope ?? [];
return await createOAuth2AuthorizationUrlWithPKCE(
`https://${this.config.domain}/realms/${this.config.realm}/protocol/openid-connect/auth`,
new URL(
`/realms/${this.config.realm}/protocol/openid-connect/auth`,
originFromDomain(this.config.domain)
),
{
clientId: this.config.clientId,
scope: ["profile", "openid", ...scopeConfig],
Expand Down Expand Up @@ -82,7 +89,10 @@ export class KeycloakAuth<
const rawTokens =
await validateOAuth2AuthorizationCode<AccessTokenResponseBody>(
code,
`https://${this.config.domain}/realms/${this.config.realm}/protocol/openid-connect/token`,
new URL(
`/realms/${this.config.realm}/protocol/openid-connect/token`,
originFromDomain(this.config.domain)
),
{
clientId: this.config.clientId,
redirectUri: this.config.redirectUri,
Expand Down Expand Up @@ -127,7 +137,10 @@ const getKeycloakUser = async (
accessToken: string
): Promise<KeycloakUser> => {
const keycloakUserRequest = new Request(
`https://${domain}/realms/${realm}/protocol/openid-connect/userinfo`,
new URL(
`/realms/${realm}/protocol/openid-connect/userinfo`,
originFromDomain(domain)
),
{
headers: {
Authorization: authorizationHeader("bearer", accessToken)
Expand Down
7 changes: 7 additions & 0 deletions packages/oauth/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,10 @@ export const authorizationHeader = (
}
throw new TypeError("Invalid token type");
};

export const originFromDomain = (domain: string): string => {
if (domain.startsWith("https://") || domain.startsWith("http://")) {
return domain;
}
return "https://" + domain;
};

0 comments on commit 58e8b82

Please sign in to comment.