feat: use identifier field to token exchange and call teams verifier in session controller

feat-hitphone-integration
Henrriky 2024-05-08 16:12:20 -03:00
parent 23a2e91740
commit 4935bf5a80
3 changed files with 11 additions and 9 deletions

View File

@ -7,7 +7,7 @@ import { RefreshTokenService } from "../services/AuthServices/RefreshTokenServic
import createOrUpdateOnlineUserService from "../services/UserServices/CreateOrUpdateOnlineUserService"; import createOrUpdateOnlineUserService from "../services/UserServices/CreateOrUpdateOnlineUserService";
import { removeUserFromOlineList } from "../helpers/removeUserFromOnlineList"; import { removeUserFromOlineList } from "../helpers/removeUserFromOnlineList";
import { TokenExchangeService, verifyTokenFromWebService } from "../services/AuthServices/TokenExchangeService"; import { TokenExchangeService, verifyTokenFromTeamsService, verifyTokenFromWebService } from "../services/AuthServices/TokenExchangeService";
// const usersSocket = require("./../libs/socket"); // const usersSocket = require("./../libs/socket");
const usersSocket = require("../libs/socket"); const usersSocket = require("../libs/socket");
@ -91,7 +91,7 @@ export const tokenExchange = async (
const platformIsFromWeb = platform === "web"; const platformIsFromWeb = platform === "web";
let tokenExchanged = await TokenExchangeService({ let tokenExchanged = await TokenExchangeService({
token: token, token: token,
tokenVerifier: platformIsFromWeb ? verifyTokenFromWebService : verifyTokenFromWebService tokenVerifier: platformIsFromWeb ? verifyTokenFromWebService : verifyTokenFromTeamsService
}) })
SendRefreshToken(res, tokenExchanged.refreshToken); SendRefreshToken(res, tokenExchanged.refreshToken);

View File

@ -41,10 +41,6 @@ export const verifyTokenFromWebService = async (token: string): Promise<TokenVer
export const verifyTokenFromTeamsService = async (token: string): Promise<TokenVerifierResponse> => { export const verifyTokenFromTeamsService = async (token: string): Promise<TokenVerifierResponse> => {
const { payload: userPayload } = await verifyTeamsToken(token) const { payload: userPayload } = await verifyTeamsToken(token)
if (userPayload.aud !== authConfig.hitphone.teams.CLIENT_ID) {
throw new AppError("ERR_TOKEN_AUD_INVALID", 401);
}
const requiredFields = ["aud", "tid", "oid", "preferred_username"]; const requiredFields = ["aud", "tid", "oid", "preferred_username"];
for (const key of requiredFields) { for (const key of requiredFields) {
if (!userPayload[key]) { if (!userPayload[key]) {
@ -52,6 +48,10 @@ export const verifyTokenFromTeamsService = async (token: string): Promise<TokenV
} }
} }
if (userPayload.aud !== authConfig.hitphone.teams.CLIENT_ID) {
throw new AppError("ERR_TOKEN_AUD_INVALID", 401);
}
const exists = await clientExists(userPayload.tid); const exists = await clientExists(userPayload.tid);
if (!exists) { if (!exists) {
@ -61,9 +61,11 @@ export const verifyTokenFromTeamsService = async (token: string): Promise<TokenV
return { name: userPayload.preferred_username, email: userPayload.preferred_username, sub: userPayload.sub } return { name: userPayload.preferred_username, email: userPayload.preferred_username, sub: userPayload.sub }
} }
type TokenVerifier = (token: string) => Promise<TokenVerifierResponse>
type TokenExchangeServiceRequest = { type TokenExchangeServiceRequest = {
token: string; token: string;
tokenVerifier: (token: string) => Promise<TokenVerifierResponse> tokenVerifier: TokenVerifier
} }
export type TokenExchangeServiceResponse = { export type TokenExchangeServiceResponse = {
@ -82,7 +84,7 @@ export const TokenExchangeService = async (
const { email } = await tokenVerifier(token); const { email } = await tokenVerifier(token);
const user = await User.findOne({ const user = await User.findOne({
where: { email }, where: { identifier: email },
include: ["queues"] include: ["queues"]
}); });

View File

@ -3,7 +3,7 @@ import authConfig from "../../../config/auth";
import { responseOk } from "./utils/fetch"; import { responseOk } from "./utils/fetch";
export const fetchWithKey: typeof fetch = async (endpoint, options) => { export const fetchWithKey: typeof fetch = async (endpoint, options) => {
const response = await fetch(authConfig.hitphone.CLIENT_SERVICE_URL + endpoint, { const response = await fetch(authConfig.hitphone.CLIENT_SERVICE_URL + '/api/' + endpoint, {
...options, ...options,
headers: { headers: {
...options?.headers, ...options?.headers,