({
context,
cookie,
logger,
sessionToken,
}: {
context: ImpureGraphContext;
cookie?: string;
logger: Logger;
sessionToken?: string;
})
| 128 | }; |
| 129 | |
| 130 | export const getUserAndSession = async ({ |
| 131 | context, |
| 132 | cookie, |
| 133 | logger, |
| 134 | sessionToken, |
| 135 | }: { |
| 136 | context: ImpureGraphContext; |
| 137 | cookie?: string; |
| 138 | logger: Logger; |
| 139 | sessionToken?: string; |
| 140 | }): Promise<{ |
| 141 | session?: Session; |
| 142 | user?: User; |
| 143 | }> => { |
| 144 | const authentication = { actorId: systemAccountId }; |
| 145 | |
| 146 | const kratosSession = await kratosFrontendApi |
| 147 | .toSession({ |
| 148 | cookie, |
| 149 | xSessionToken: sessionToken, |
| 150 | }) |
| 151 | .then(({ data }) => data) |
| 152 | .catch((err: AxiosError) => { |
| 153 | if (err.response && err.response.status === 403) { |
| 154 | logger.debug( |
| 155 | "Session requires AAL2 but only has AAL1. Treating as unauthenticated.", |
| 156 | ); |
| 157 | return undefined; |
| 158 | } |
| 159 | logger.debug( |
| 160 | `Kratos response error: Could not fetch session, got: [${ |
| 161 | err.response?.status |
| 162 | }] ${JSON.stringify(err.response?.data)}`, |
| 163 | ); |
| 164 | return undefined; |
| 165 | }); |
| 166 | |
| 167 | if (kratosSession) { |
| 168 | const { identity } = kratosSession; |
| 169 | |
| 170 | if (!identity) { |
| 171 | throw new Error("Could not find kratos identity for session"); |
| 172 | } |
| 173 | |
| 174 | const { id: kratosIdentityId, traits } = identity as KratosUserIdentity; |
| 175 | |
| 176 | let user = await getUser(context, authentication, { |
| 177 | kratosIdentityId, |
| 178 | emails: traits.emails, |
| 179 | }); |
| 180 | |
| 181 | if (!user) { |
| 182 | const hashInstance = await getHashInstance(context, authentication); |
| 183 | |
| 184 | if (!hashInstance.userSelfRegistrationIsEnabled) { |
| 185 | throw new Error("User registration is disabled."); |
| 186 | } |
| 187 |
no test coverage detected