Skip to content

Commit

Permalink
Fix RPError issuer timeouts with future/auth (#3762)
Browse files Browse the repository at this point in the history
* ♻️ Proxy issuers to avoid calling them when users do not use the adapters

* 🩹 Upload changeset
  • Loading branch information
lostra01 committed May 14, 2024
1 parent 6f8fb48 commit 1706947
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-camels-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"sst": patch
---

Proxy issuer calls to avoid timeouts on unused auth adapters
18 changes: 13 additions & 5 deletions packages/sst/src/node/future/auth/adapter/apple.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import querystring from 'node:querystring';
import { generators, Issuer } from 'openid-client';
import {BaseClient, generators, Issuer} from 'openid-client';

import { useBody, useCookie, useDomainName, usePathParam, useResponse } from '../../../api/index.js';
import { Adapter } from './adapter.js';
Expand All @@ -13,19 +13,27 @@ import { OauthConfig } from './oauth.js';
// userinfo_endpoint are not included in the response.
// await Issuer.discover("https://appleid.apple.com/.well-known/openid-configuration/");

const issuer = await Issuer.discover(
"https://appleid.apple.com/.well-known/openid-configuration"
)
let realIssuer: Issuer<BaseClient>;

const issuer = new Proxy({}, {
get: async function(target, prop: string){
if(!realIssuer){
realIssuer = await Issuer.discover("https://appleid.apple.com/.well-known/openid-configuration");
}
return realIssuer[prop];
}
})

export const AppleAdapter =
/* @__PURE__ */
(config: OauthConfig) => {

return async function () {
const step = usePathParam("step");
const callback = "https://" + useDomainName() + "/callback";
console.log("callback", callback);

const client = new issuer.Client({
const client = new (issuer as Issuer<BaseClient>).Client({
client_id: config.clientID,
client_secret: config.clientSecret,
redirect_uris: [callback],
Expand Down
17 changes: 13 additions & 4 deletions packages/sst/src/node/future/auth/adapter/google.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { Issuer } from "openid-client";
import {BaseClient, Issuer} from "openid-client";
import { OidcAdapter, OidcBasicConfig } from "./oidc.js";
import { OauthAdapter, OauthBasicConfig } from "./oauth.js";

const issuer = await Issuer.discover("https://accounts.google.com");
let realIssuer: Issuer<BaseClient>;

const issuer = new Proxy({}, {
get: async function(target, prop: string){
if(!realIssuer){
realIssuer = await Issuer.discover("https://accounts.google.com");
}
return realIssuer[prop];
}
})

type GooglePrompt = "none" | "consent" | "select_account";
type GoogleAccessType = "offline" | "online";
Expand All @@ -19,7 +28,7 @@ export function GoogleAdapter(config: GoogleConfig) {
/* @__PURE__ */
if (config.mode === "oauth") {
return OauthAdapter({
issuer,
issuer: issuer as Issuer<BaseClient>,
...config,
params: {
...(config.accessType && { access_type: config.accessType }),
Expand All @@ -28,7 +37,7 @@ export function GoogleAdapter(config: GoogleConfig) {
});
}
return OidcAdapter({
issuer,
issuer: issuer as Issuer<BaseClient>,
scope: "openid email profile",
...config,
});
Expand Down
7 changes: 6 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1706947

Please sign in to comment.