Fakultas Ilmu Komputer UI

Commit 09722ac4 authored by Muhamad Istiady Kartadibrata's avatar Muhamad Istiady Kartadibrata
Browse files

fix getuser endpoint

parent ba1e9c36
......@@ -3,13 +3,15 @@
const express = require('express');
const bodyParser = require('body-parser');
const axios = require("axios");
var parser = require('xml2json-light');
var cors = require('cors')
var convert = require('xml-js');
// Constants
const PORT = 8000;
// App
const app = express();
app.use(cors())
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
......@@ -22,16 +24,46 @@ app.get('/', (req, res) => {
})
})
app.get("/user/:ticketId", async (req, res, next) => {
const ticket = req.params.ticketId
const URL_REDIRECT = "http://localhost:8000"
app.post("/user", async (req, res, next) => {
const ticket = req.query.ticketId
const URL_REDIRECT = "http://localhost:3001"
try {
const data = await axios.post(
let {data} = await axios.post(
`https://sso.ui.ac.id/cas2/serviceValidate?ticket=${ticket}&service=${URL_REDIRECT}`
)
var json = parser.xml2json(data.data)
console.log(json)
res.send(data)
data = data.replace(/>\s*/g, '>'); // Replace "> " with ">"
data = data.replace(/\s*</g, '<'); // Replace "< " with "<"
data = data.replace(
// Replace out the new line character.
new RegExp( "\\n", "g" ),
""
);
var json = convert.xml2json(data)
let result = {}
json = JSON.parse(json)
if(json.elements[0].elements[0].name==="cas:authenticationSuccess"){
const element = json.elements[0].elements[0].elements
const username = element[0].elements[0].text
const role = element[1].elements[2].elements[0].text
const name = element[1].elements[3].elements[0].text
const npm = element[1].elements[4].elements[0].text
result['message'] = 'success'
result['data']={'username':username,'role':role,'name':name,'npm':npm}
res.send(result)
}
else{
result['message'] = 'invalid ticket'
res.send(result)
}
} catch (error) {
console.log(error)
}
......
......@@ -71,6 +71,15 @@
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
"integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
},
"cors": {
"version": "2.8.5",
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
"requires": {
"object-assign": "^4",
"vary": "^1"
}
},
"debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
......@@ -248,6 +257,11 @@
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
"integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
},
"object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
"integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM="
},
"on-finished": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
......
......@@ -10,6 +10,7 @@
"dependencies": {
"axios": "^0.20.0",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.16.1",
"xml-js": "^1.6.11",
"xml2json-light": "^1.0.6"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment