🏗️ separate http and internal service

separate between internal and http service due security concern in auth module
This commit is contained in:
2025-08-07 23:07:53 +07:00
parent 0d71710b14
commit ac0b25fb62
9 changed files with 21 additions and 15 deletions

View File

@ -0,0 +1,27 @@
import { AppError } from "../../../../helpers/error/instances/app";
import { githubProvider } from "../../providers/github.provider";
export const githubCallbackService = async (code: string) => {
try {
const github = githubProvider();
const tokens = await github.validateAuthorizationCode(code);
const accessToken = tokens.accessToken();
const userdata = await fetch("https://api.github.com/user", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
const useremail = await fetch("https://api.github.com/user/emails", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
return {
userdata: await userdata.json(),
useremail: await useremail.json(),
};
} catch (error) {
return new AppError(500, "Authentication service error", error);
}
};

View File

@ -0,0 +1,16 @@
import * as arctic from "arctic";
import { githubProvider } from "../../providers/github.provider";
import { AppError } from "../../../../helpers/error/instances/app";
export const githubRequestService = async () => {
try {
const github = githubProvider();
const state = arctic.generateState();
const scopes = ["user:email"];
const url = github.createAuthorizationURL(state, scopes);
return url;
} catch (error) {
throw new AppError(500, "Oops! something happening", error);
}
};

View File

@ -0,0 +1,36 @@
import { AppError } from "../../../../helpers/error/instances/app";
import { googleProvider } from "../../providers/google.provider";
import { redis } from "../../../../utils/databases/redis/connection";
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
export const googleCallbackService = async (query: {
state: string;
code: string;
}) => {
try {
const state = query.state;
const codeVerifier = await redis.get(
`${process.env.APP_NAME}:pkce:${state}`
);
if (!codeVerifier) throw new AppError(408, "Request timeout");
await redis.del(`${process.env.APP_NAME}:pkce:${state}`);
const google = googleProvider();
const tokens = await google.validateAuthorizationCode(
query.code,
codeVerifier
);
const accessToken = tokens.accessToken();
const response = await fetch(
"https://openidconnect.googleapis.com/v1/userinfo",
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
return await response.json();
} catch (error) {
ErrorForwarder(error, 500, "Authentication service error");
}
};

View File

@ -0,0 +1,28 @@
import * as arctic from "arctic";
import { AppError } from "../../../../helpers/error/instances/app";
import { googleProvider } from "../../providers/google.provider";
import { redis } from "../../../../utils/databases/redis/connection";
export const googleRequestService = async () => {
try {
const google = googleProvider();
const state = arctic.generateState();
const codeVerifier = arctic.generateCodeVerifier();
const scopes = ["openid", "profile", "email"];
const url = google.createAuthorizationURL(state, codeVerifier, scopes);
await redis.setex(
`${process.env.APP_NAME}:pkce:${state}`,
300,
codeVerifier
);
return url;
} catch (error) {
throw new AppError(
500,
"Google Auth provider is experiencing issues.",
error
);
}
};