diff --git a/.env.exemple b/.env.exemple new file mode 100644 index 0000000000000000000000000000000000000000..41cb9efb6286629090a4284d526ebf0009c7d9fd --- /dev/null +++ b/.env.exemple @@ -0,0 +1,3 @@ +AUTH_SECRET_KEY= +AUTH_ID= +SESSION_SECRET= \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 047ed8ddcba16d4919393021a249c86222f25499..96c946c8e1e692868ec6a7bf6a5d91b96624a0ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -220,6 +220,69 @@ "minimist": "^1.2.0" } }, + "@hapi/address": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/@hapi/address/-/address-2.1.4.tgz", + "integrity": "sha512-QD1PhQk+s31P1ixsX0H0Suoupp3VMXzIVMSwobR3F3MSUO2YCV0B7xqLcUw/Bh8yuvd3LhpyqLQWTNcRmp6IdQ==" + }, + "@hapi/boom": { + "version": "7.4.11", + "resolved": "https://registry.npmjs.org/@hapi/boom/-/boom-7.4.11.tgz", + "integrity": "sha512-VSU/Cnj1DXouukYxxkes4nNJonCnlogHvIff1v1RVoN4xzkKhMXX+GRmb3NyH1iar10I9WFPDv2JPwfH3GaV0A==", + "requires": { + "@hapi/hoek": "8.x.x" + } + }, + "@hapi/bourne": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@hapi/bourne/-/bourne-1.3.2.tgz", + "integrity": "sha512-1dVNHT76Uu5N3eJNTYcvxee+jzX4Z9lfciqRRHCU27ihbUcYi+iSc2iml5Ke1LXe1SyJCLA0+14Jh4tXJgOppA==" + }, + "@hapi/formula": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@hapi/formula/-/formula-1.2.0.tgz", + "integrity": "sha512-UFbtbGPjstz0eWHb+ga/GM3Z9EzqKXFWIbSOFURU0A/Gku0Bky4bCk9/h//K2Xr3IrCfjFNhMm4jyZ5dbCewGA==" + }, + "@hapi/hoek": { + "version": "8.5.1", + "resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-8.5.1.tgz", + "integrity": "sha512-yN7kbciD87WzLGc5539Tn0sApjyiGHAJgKvG9W8C7O+6c7qmoQMfVs0W4bX17eqz6C78QJqqFrtgdK5EWf6Qow==" + }, + "@hapi/joi": { + "version": "16.1.8", + "resolved": "https://registry.npmjs.org/@hapi/joi/-/joi-16.1.8.tgz", + "integrity": "sha512-wAsVvTPe+FwSrsAurNt5vkg3zo+TblvC5Bb1zMVK6SJzZqw9UrJnexxR+76cpePmtUZKHAPxcQ2Bf7oVHyahhg==", + "requires": { + "@hapi/address": "^2.1.2", + "@hapi/formula": "^1.2.0", + "@hapi/hoek": "^8.2.4", + "@hapi/pinpoint": "^1.0.2", + "@hapi/topo": "^3.1.3" + } + }, + "@hapi/pinpoint": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@hapi/pinpoint/-/pinpoint-1.0.2.tgz", + "integrity": "sha512-dtXC/WkZBfC5vxscazuiJ6iq4j9oNx1SHknmIr8hofarpKUZKmlUVYVIhNVzIEgK5Wrc4GMHL5lZtt1uS2flmQ==" + }, + "@hapi/topo": { + "version": "3.1.6", + "resolved": "https://registry.npmjs.org/@hapi/topo/-/topo-3.1.6.tgz", + "integrity": "sha512-tAag0jEcjwH+P2quUfipd7liWCNX2F8NvYjQp2wtInsZxnMlypdw0FtAOLxtvvkO+GSRRbmNi8m/5y42PQJYCQ==", + "requires": { + "@hapi/hoek": "^8.3.0" + } + }, + "@hapi/wreck": { + "version": "15.1.0", + "resolved": "https://registry.npmjs.org/@hapi/wreck/-/wreck-15.1.0.tgz", + "integrity": "sha512-tQczYRTTeYBmvhsek/D49En/5khcShaBEmzrAaDjMrFXKJRuF8xA8+tlq1ETLBFwGd6Do6g2OC74rt11kzawzg==", + "requires": { + "@hapi/boom": "7.x.x", + "@hapi/bourne": "1.x.x", + "@hapi/hoek": "8.x.x" + } + }, "@istanbuljs/load-nyc-config": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.0.0.tgz", @@ -765,7 +828,6 @@ "version": "1.19.0", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.0.tgz", "integrity": "sha512-W98JrE0j2K78swW4ukqMleo8R7h/pFETjM2DQ90MF6XK2i4LO4W3gQ71Lt4w3bfm2EvVSyWHplECvB5sK22yFQ==", - "dev": true, "requires": { "@types/connect": "*", "@types/node": "*" @@ -790,7 +852,6 @@ "version": "3.4.33", "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.33.tgz", "integrity": "sha512-2+FrkXY4zllzTNfJth7jOqEHC+enpLeGslEhpnTAkg21GkRrWV4SsAtqchtT4YS9/nODBU2/ZfsBY2X4J/dX7A==", - "dev": true, "requires": { "@types/node": "*" } @@ -799,7 +860,6 @@ "version": "4.17.2", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.2.tgz", "integrity": "sha512-5mHFNyavtLoJmnusB8OKJ5bshSzw+qkMIBAobLrIM48HJvunFva9mOa6aBwh64lBFyNwBbs0xiEFuj4eU/NjCA==", - "dev": true, "requires": { "@types/body-parser": "*", "@types/express-serve-static-core": "*", @@ -810,12 +870,20 @@ "version": "4.17.2", "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.2.tgz", "integrity": "sha512-El9yMpctM6tORDAiBwZVLMcxoTMcqqRO9dVyYcn7ycLWbvR8klrDn8CAOwRfZujZtWD7yS/mshTdz43jMOejbg==", - "dev": true, "requires": { "@types/node": "*", "@types/range-parser": "*" } }, + "@types/express-session": { + "version": "1.15.16", + "resolved": "https://registry.npmjs.org/@types/express-session/-/express-session-1.15.16.tgz", + "integrity": "sha512-vWQpNt9t/zc4bTX+Ow5powZb9n3NwOM0SYsAJ7PYj5vliB6FA40ye5sW5fZTw8+ekbzJf/sgvtQocf7IryJBJw==", + "requires": { + "@types/express": "*", + "@types/node": "*" + } + }, "@types/istanbul-lib-coverage": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.1.tgz", @@ -854,8 +922,7 @@ "@types/mime": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-2.0.1.tgz", - "integrity": "sha512-FwI9gX75FgVBJ7ywgnq/P7tw+/o1GUbtP0KzbtusLigAOgIgNISRK0ZPl4qertvXSIE8YbsVJueQ90cDt9YYyw==", - "dev": true + "integrity": "sha512-FwI9gX75FgVBJ7ywgnq/P7tw+/o1GUbtP0KzbtusLigAOgIgNISRK0ZPl4qertvXSIE8YbsVJueQ90cDt9YYyw==" }, "@types/mongodb": { "version": "3.3.16", @@ -880,25 +947,27 @@ "@types/node": { "version": "13.7.1", "resolved": "https://registry.npmjs.org/@types/node/-/node-13.7.1.tgz", - "integrity": "sha512-Zq8gcQGmn4txQEJeiXo/KiLpon8TzAl0kmKH4zdWctPj05nWwp1ClMdAVEloqrQKfaC48PNLdgN/aVaLqUrluA==", - "dev": true + "integrity": "sha512-Zq8gcQGmn4txQEJeiXo/KiLpon8TzAl0kmKH4zdWctPj05nWwp1ClMdAVEloqrQKfaC48PNLdgN/aVaLqUrluA==" }, "@types/range-parser": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz", - "integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==", - "dev": true + "integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==" }, "@types/serve-static": { "version": "1.13.3", "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.3.tgz", "integrity": "sha512-oprSwp094zOglVrXdlo/4bAHtKTAxX6VT8FOZlBKrmyLbNvE1zxZyJ6yikMVtHIvwP45+ZQGJn+FdXGKTozq0g==", - "dev": true, "requires": { "@types/express-serve-static-core": "*", "@types/mime": "*" } }, + "@types/simple-oauth2": { + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/@types/simple-oauth2/-/simple-oauth2-2.5.2.tgz", + "integrity": "sha512-ueSZbyJ8/69T2ADkcjOFu9VndpM+5lHP0ppMsr31Mi6ufY51rnmg3EdaMpjNFSqDpfZAGrihutEJFZrV70/ABA==" + }, "@types/stack-utils": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-1.0.1.tgz", @@ -1127,6 +1196,14 @@ "integrity": "sha512-wMHVg2EOHaMRxbzgFJ9gtjOOCrI80OHLG14rxi28XwOW8ux6IiEbRCGGGqCtdAIg4FQCbW20k9RsT4y3gJlFug==", "dev": true }, + "axios": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", + "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", + "requires": { + "follow-redirects": "1.5.10" + } + }, "babel-jest": { "version": "25.1.0", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-25.1.0.tgz", @@ -1799,6 +1876,11 @@ "whatwg-url": "^7.0.0" } }, + "date-fns": { + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.9.0.tgz", + "integrity": "sha512-khbFLu/MlzLjEzy9Gh8oY1hNt/Dvxw3J6Rbc28cVoYWQaC1S3YI4xwkF9ZWcjDLscbZlY9hISMr66RFzZagLsA==" + }, "debug": { "version": "3.2.6", "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz", @@ -1939,6 +2021,11 @@ "is-obj": "^1.0.0" } }, + "dotenv": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", + "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==" + }, "duplexer3": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", @@ -2232,6 +2319,41 @@ } } }, + "express-session": { + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.0.tgz", + "integrity": "sha512-t4oX2z7uoSqATbMfsxWMbNjAL0T5zpvcJCk3Z9wnPPN7ibddhnmDZXHfEcoBMG2ojKXZoCyPMc5FbtK+G7SoDg==", + "requires": { + "cookie": "0.4.0", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "~2.0.0", + "on-headers": "~1.0.2", + "parseurl": "~1.3.3", + "safe-buffer": "5.2.0", + "uid-safe": "~2.1.5" + }, + "dependencies": { + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + } + }, + "depd": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", + "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==" + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + } + } + }, "extend": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", @@ -2405,6 +2527,29 @@ "path-exists": "^4.0.0" } }, + "follow-redirects": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", + "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", + "requires": { + "debug": "=3.1.0" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + } + } + }, "for-in": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", @@ -5053,6 +5198,11 @@ "ee-first": "1.1.1" } }, + "on-headers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz", + "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==" + }, "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -5353,6 +5503,11 @@ "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==" }, + "random-bytes": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz", + "integrity": "sha1-T2ih3Arli9P7lYSMMDJNt11kNgs=" + }, "range-parser": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", @@ -5984,6 +6139,28 @@ "integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=", "dev": true }, + "simple-oauth2": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/simple-oauth2/-/simple-oauth2-3.3.0.tgz", + "integrity": "sha512-mBWkLHH7XY6WSy271j6CeEHuGN61K/TeUawXpX0K9tsLnlrqt9bpXYR/tYMI6+o6QWqSdvVaItGlZKOfMVFOGA==", + "requires": { + "@hapi/hoek": "^8.5.0", + "@hapi/joi": "^16.1.8", + "@hapi/wreck": "^15.1.0", + "date-fns": "^2.9.0", + "debug": "^4.1.1" + }, + "dependencies": { + "debug": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", + "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "requires": { + "ms": "^2.1.1" + } + } + } + }, "sisteransi": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.4.tgz", @@ -6634,6 +6811,14 @@ "integrity": "sha512-/P5lkRXkWHNAbcJIiHPfRoKqyd7bsyCma1hZNUGfn20qm64T6ZBlrzprymeu918H+mB/0rIg2gGK/BXkhhYgBw==", "dev": true }, + "uid-safe": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/uid-safe/-/uid-safe-2.1.5.tgz", + "integrity": "sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==", + "requires": { + "random-bytes": "~1.0.0" + } + }, "undefsafe": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.3.tgz", diff --git a/package.json b/package.json index 81dafb9a203592c2b9e02f5686a463d0e6fccf2e..f9e130bf4211ae883752c2536716c6b6b0f1cdb6 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,14 @@ "typescript": "^3.7.5" }, "dependencies": { + "@types/express-session": "^1.15.16", + "@types/simple-oauth2": "^2.5.2", + "axios": "^0.19.2", "body-parser": "^1.19.0", + "dotenv": "^8.2.0", "express": "^4.17.1", - "mongoose": "^5.9.1" + "express-session": "^1.17.0", + "mongoose": "^5.9.1", + "simple-oauth2": "^3.3.0" } } diff --git a/src/index.ts b/src/index.ts index 6bba568456d48acf42d833e1879b23c8388c6e30..e880caa51515ba7eca2a69fe1f83c570176280a9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,29 @@ +import "./utils/env" import express, { Request, Response, NextFunction, Application } from "express"; import bodyParser from "body-parser"; +import expressSession from "express-session"; +import authRoute from './routes/auth'; const app: Application = express(); app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded +app.use(expressSession({ + secret: process.env.SESSION_SECRET || "alma", + resave: false, + saveUninitialized: true, + cookie: { secure: false } +})); -app.get("/", (req: Request, res: Response) => res.send("Hel World!")); +app.get("/", (req: Request, res: Response) => { + let message = "World!"; + if(req.session!.user) + message = req.session!.user!.sn || "World!"; + res.send( "Hello " + message) +}); + +// Register routes +authRoute(app); app.use((err: any, req: Request, res: Response, next: NextFunction) => { res.status(500).send("Houston, we have a problem!"); @@ -15,4 +32,4 @@ app.use((err: any, req: Request, res: Response, next: NextFunction) => { console.error(err.stack); }); -app.listen(3000, () => console.log(`Example app listening on port 8000!`)); +app.listen(8000, () => console.log(`Example app listening on port 8000!`)); diff --git a/src/middlewares/auth/authenticated.ts b/src/middlewares/auth/authenticated.ts new file mode 100644 index 0000000000000000000000000000000000000000..d5f7ea0e9fe2bdecbd23a16f6c7fb3188e57cbf6 --- /dev/null +++ b/src/middlewares/auth/authenticated.ts @@ -0,0 +1,12 @@ +import {Request, Response, NextFunction} from "express"; + +const authenticated = () => (req : Request, res : Response, next: NextFunction ) =>{ + if(req.session!.user) + next(); + else{ + res.status(403); + res.json({message: "You have to login to see this page"}); + } +} + +export default authenticated; \ No newline at end of file diff --git a/src/middlewares/auth/complete.ts b/src/middlewares/auth/complete.ts new file mode 100644 index 0000000000000000000000000000000000000000..dedc5c8c0d95f6611bc2731264f4e851c15128dd --- /dev/null +++ b/src/middlewares/auth/complete.ts @@ -0,0 +1,29 @@ +import { Request, Response } from 'express'; +import {oauth2, scope} from '../../utils/auth' +import axios from "axios"; + + +const complete = () => async (req: Request, res: Response) => { + const tokenConfig = { + code: req.query.code, + scope: scope, + redirect_uri: "" + }; + + try { + const result = await oauth2().authorizationCode.getToken(tokenConfig); + const token = oauth2().accessToken.create(result); + await axios.get(`https://auth.sch.bme.hu/api/profile/?access_token=${token.token.access_token}`) + .then( (response) => { + req.session!.user = response.data; + }) + .catch(function (error) { + console.log(error); + }); + res.redirect('/'); + } catch (error) { + console.log('Access Token Error', error.message); + } +}; + +export default complete; \ No newline at end of file diff --git a/src/middlewares/auth/login.ts b/src/middlewares/auth/login.ts new file mode 100644 index 0000000000000000000000000000000000000000..356629f10a2f8bda13639201a837cdf7416b6fcd --- /dev/null +++ b/src/middlewares/auth/login.ts @@ -0,0 +1,13 @@ +import { Request, Response } from 'express'; +import {oauth2, scope} from '../../utils/auth' + + +const authorizationUri = oauth2().authorizationCode.authorizeURL({ + scope: scope, +}); + + +const login = () => (req: Request, res: Response) => res.redirect(authorizationUri); + + +export default login; \ No newline at end of file diff --git a/src/middlewares/auth/logout.ts b/src/middlewares/auth/logout.ts new file mode 100644 index 0000000000000000000000000000000000000000..791772f76771a64ddd2b04952bdeaa7f65afabda --- /dev/null +++ b/src/middlewares/auth/logout.ts @@ -0,0 +1,8 @@ +import { Request, Response } from 'express'; + +const logout = () => (req : Request, res : Response) =>{ + req.session!.destroy(() => console.log("user logged out.")); + res.redirect("/"); +} + +export default logout; \ No newline at end of file diff --git a/src/routes/auth.ts b/src/routes/auth.ts new file mode 100644 index 0000000000000000000000000000000000000000..b930250fa6704fd6b43c0f1191e7ff25326c00e6 --- /dev/null +++ b/src/routes/auth.ts @@ -0,0 +1,15 @@ +import { Application } from 'express'; +import login from '../middlewares/auth/login'; +import complete from '../middlewares/auth/complete'; +import logout from '../middlewares/auth/logout'; +import authenticated from '../middlewares/auth/authenticated' + + const authRout = (app: Application): void => { + app.get('/login', login() ); + + app.get('/complete', complete() ); + + app.get('/logout', authenticated(), logout() ); +} + +export default authRout; \ No newline at end of file diff --git a/src/utils/auth.ts b/src/utils/auth.ts new file mode 100644 index 0000000000000000000000000000000000000000..d00f20423b0e457c8a03b7a34e4b45915c5c9b0d --- /dev/null +++ b/src/utils/auth.ts @@ -0,0 +1,19 @@ +import simpleOauth2 from "simple-oauth2"; + +const oauth2 = () => simpleOauth2.create( + { + client: { + id: process.env.AUTH_ID || "", + secret: process.env.AUTH_SECRET_KEY || "" + }, + auth: { + tokenHost: 'https://auth.sch.bme.hu', + tokenPath: '/oauth2/token', + authorizePath: '/site/login' + } + } +); + +const scope : string[] = ['basic', 'mail', 'sn', 'givenName'] + +export {oauth2, scope}; \ No newline at end of file diff --git a/src/utils/env.ts b/src/utils/env.ts new file mode 100644 index 0000000000000000000000000000000000000000..0627d2453f2d58367b30824efb562de580e15e46 --- /dev/null +++ b/src/utils/env.ts @@ -0,0 +1,5 @@ +import { resolve } from "path" + +import { config } from "dotenv" + +config({ path: resolve(__dirname, "../../.env") }) \ No newline at end of file