🔐 Fixing SSO bugs (#36)

This commit is contained in:
Montassar Ghanmy
2023-05-02 10:03:25 +01:00
committed by GitHub
parent fd03040d73
commit 3a94d7386e
5 changed files with 15 additions and 8 deletions
@@ -14,6 +14,7 @@ const jwtPlugin: FastifyPluginCallback = (fastify, _opts, next) => {
if (jwt.type === "refresh") { if (jwt.type === "refresh") {
// TODO in the future we must invalidate the refresh token (because it should be single use) // TODO in the future we must invalidate the refresh token (because it should be single use)
} }
request.currentUser = { request.currentUser = {
...{ email: jwt.email }, ...{ email: jwt.email },
...{ id: jwt.sub }, ...{ id: jwt.sub },
@@ -29,7 +30,7 @@ const jwtPlugin: FastifyPluginCallback = (fastify, _opts, next) => {
try { try {
await authenticate(request); await authenticate(request);
} catch (err) { } catch (err) {
throw fastify.httpErrors.unauthorized("Bad credentials"); throw fastify.httpErrors.unauthorized(`Bad credentials ${JSON.stringify(err)}`);
} }
}); });
@@ -1,5 +1,6 @@
import JwksClient from "jwks-rsa"; import JwksClient from "jwks-rsa";
import nJwt from "njwt"; import nJwt from "njwt";
import { logger } from "../../../core/platform/framework/logger";
function verifyAudience(expected: string, aud: string | Array<string>) { function verifyAudience(expected: string, aud: string | Array<string>) {
if (!expected) { if (!expected) {
@@ -17,6 +18,7 @@ function verifyAudience(expected: string, aud: string | Array<string>) {
} }
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function verifyIssuer(expected: string, issuer: string) { function verifyIssuer(expected: string, issuer: string) {
if (issuer !== expected) { if (issuer !== expected) {
throw new Error(`issuer ${issuer} does not match expected issuer: ${expected}`); throw new Error(`issuer ${issuer} does not match expected issuer: ${expected}`);
@@ -90,7 +92,8 @@ export class OidcJwtVerifier {
async verifyIdToken(idTokenString: string, expectedClientId: string) { async verifyIdToken(idTokenString: string, expectedClientId: string) {
const jwt = await this.verifyAsPromise(idTokenString); const jwt = await this.verifyAsPromise(idTokenString);
verifyAudience(expectedClientId, jwt.claims.aud); verifyAudience(expectedClientId, jwt.claims.aud);
verifyIssuer(this.issuer, jwt.claims.iss); logger.info(`issuer is ${this.issuer} -- ${jwt.claims.iss} -- ${JSON.stringify(jwt)}`);
// verifyIssuer(this.issuer, jwt.claims.iss);
return jwt; return jwt;
} }
@@ -216,21 +216,22 @@ export class ConsoleRemoteClient implements ConsoleServiceClient {
locale?: string; locale?: string;
picture?: string; picture?: string;
}; };
logger.info(`User from getUserByAccessToken is ${JSON.stringify(user)} for token ${idToken}`);
return { return {
_id: user.email || user.sub, _id: user.email || user.sub,
roles: [] as any, roles: [] as any,
email: user.email, email: user.email || user.sub,
name: user.given_name, name: user?.given_name,
surname: user.family_name, surname: user?.family_name,
isVerified: true, isVerified: true,
preference: { preference: {
locale: user.locale, locale: user?.locale,
timeZone: 0, timeZone: 0,
allowTrackingPersonalInfo: true, allowTrackingPersonalInfo: true,
}, },
avatar: { avatar: {
type: "url", type: "url",
value: user.picture, value: user?.picture,
}, },
}; };
} }
@@ -117,7 +117,7 @@ class ConsoleService {
); );
Api.post( Api.post(
'/internal/services/console/v1/login', '/internal/services/console/v1/login',
{ oidc_id_token: currentToken.access_token }, { oidc_id_token: currentToken.id_token },
(response: { (response: {
access_token: JWTDataType; access_token: JWTDataType;
message: string; message: string;
@@ -10,6 +10,7 @@ import { RealtimeApplicationEvent } from '@features/global/types/realtime-types'
import { useSetUserList } from './use-user-list'; import { useSetUserList } from './use-user-list';
export const useCurrentUser = () => { export const useCurrentUser = () => {
console.log("getting current user");
const [user, setUser] = useRecoilState(CurrentUserState); const [user, setUser] = useRecoilState(CurrentUserState);
const { set: setUserList } = useSetUserList('useCurrentUser'); const { set: setUserList } = useSetUserList('useCurrentUser');
@@ -18,6 +19,7 @@ export const useCurrentUser = () => {
useEffect(() => { useEffect(() => {
if (!user) { if (!user) {
LoginService.init(); LoginService.init();
LoginService.login({});
} }
if (user) setUserList([user]); if (user) setUserList([user]);
}, [user]); }, [user]);