Skip to content
Open

Pr #73

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
/postgres-keycloak-data
2024__yandex-architecture-sso.code-workspace

.idea

node_modules
1 change: 1 addition & 0 deletions API/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/node_modules
13 changes: 13 additions & 0 deletions API/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:20.11.1-slim
# App directory
WORKDIR /app

# App dependencies
COPY package*.json ./
RUN npm install --production

# Copy app source code
COPY . .

# Start the app
ENTRYPOINT [ "npm", "run", "start"]
71 changes: 71 additions & 0 deletions API/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const express = require('express')
const Keycloak = require('keycloak-connect');
const cors = require('cors')
const Jwt = require("jsonwebtoken");

const keycloakConfig = {
clientId: process.env.CLIENT_ID,
bearerOnly: true,
serverUrl: process.env.KEYCLOAK_SERVER_URL,
realm: process.env.REALM,
credentials: {
secret: process.env.CLIENT_SECRET,
},
};

const keycloak = new Keycloak({}, keycloakConfig);

const app = express()
const port = 8000

app.use(cors())

app.get('/', (req, res) => {
res.send('Hello World!')
})

//route protected with Keycloak
app.get(
'/reports',
async function(req, res) {
const token = req.headers?.authorization?.split(" ")[1];
const check = await keycloak.grantManager
.validateAccessToken(token);

if (!check) {
return res.status(401).send();
}

await keycloak.grantManager
.validateAccessToken(token)

const decoded = Jwt.decode(token);

if (!decoded.realm_access.roles.includes('prothetic_user')) {
return res.status(401).send();
}

res.json({
data: {
reports: [
{
id: 1,
reportData: {
name: 'TestReport'
}
},
{
id: 2,
reportData: {
name: 'TestReport'
}
}
]
}
});
}
);

app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Loading