MCPcopy Create free account
hub / github.com/bewcloud/bewcloud / validateAndCreateSession

Method validateAndCreateSession

lib/models/oidc.ts:108–209  ·  view source on GitHub ↗
(request: Request)

Source from the content-addressed store, hash-verified

106 }
107
108 static async validateAndCreateSession(request: Request) {
109 const urlSearchParams = new URL(request.url).searchParams;
110 const state = urlSearchParams.get('state');
111
112 if (!state) {
113 throw new Error('Missing OIDC "state" parameter');
114 }
115
116 const oidcStateCache = new SimpleCache(`oidc:state:${state}`);
117
118 let expectedState: string;
119 let expectedCodeVerifier: string;
120
121 try {
122 const cacheValue = await oidcStateCache.get();
123
124 const { state, codeVerifier } = JSON.parse(cacheValue) as {
125 state: OidcExtraState;
126 codeVerifier: string;
127 };
128
129 expectedState = btoa(JSON.stringify(state));
130 expectedCodeVerifier = codeVerifier;
131 } catch (error) {
132 console.log(`Failed to verify/parse OIDC code: ${error}`);
133 console.error(error);
134
135 throw new Error('Invalid OIDC code');
136 }
137
138 const config = await AppConfig.getConfig();
139
140 const baseUrl = config.auth.baseUrl;
141 const oidcBaseUrl = config.auth.singleSignOnUrl;
142 const emailAttribute = config.auth.singleSignOnEmailAttribute;
143 const oidcOptions = oidcBaseUrl.startsWith('http://')
144 ? { execute: [openIdClient.allowInsecureRequests] }
145 : undefined;
146
147 const oidcConfig = await openIdClient.discovery(
148 new URL(oidcBaseUrl),
149 OIDC_CLIENT_ID,
150 OIDC_CLIENT_SECRET,
151 undefined,
152 oidcOptions,
153 );
154
155 const tokens = await openIdClient.authorizationCodeGrant(
156 oidcConfig,
157 new URL(`${baseUrl}${redirectUrlPath}?${urlSearchParams.toString()}`),
158 {
159 pkceCodeVerifier: expectedCodeVerifier,
160 expectedState,
161 },
162 );
163
164 const oidcParams = this.decodeJwt(tokens.id_token!);
165

Callers 1

getFunction · 0.80

Calls 12

getMethod · 0.95
decodeJwtMethod · 0.95
parseStateMethod · 0.95
createSessionResponseFunction · 0.90
getConfigMethod · 0.80
isSignupAllowedMethod · 0.80
isThereAnAdminMethod · 0.80
getByEmailMethod · 0.80
hasMethod · 0.80
getMethod · 0.45
parseMethod · 0.45
createMethod · 0.45

Tested by

no test coverage detected