Skip to content

Commit e45c104

Browse files
authored
Merge pull request #3 from orchestracities/container-test
v0.1
2 parents 39959f3 + 0a2471b commit e45c104

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+50236
-1
lines changed

.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
.env
2+
#automatic setup
3+
keycloak-connect-graphql/examples/config/config/*
4+
keycloak-connect-graphql/examples/config/realm-export
5+
keycloak
6+
17
# Logs
28
logs
39
*.log
@@ -102,3 +108,26 @@ dist
102108

103109
# TernJS port file
104110
.tern-port
111+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
112+
113+
# dependencies
114+
/node_modules
115+
/.pnp
116+
.pnp.js
117+
118+
# testing
119+
/coverage
120+
121+
# production
122+
/build
123+
124+
# misc
125+
.DS_Store
126+
.env.local
127+
.env.development.local
128+
.env.test.local
129+
.env.production.local
130+
131+
npm-debug.log*
132+
yarn-debug.log*
133+
yarn-error.log*

README.md

+56-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,56 @@
1-
# auth-management-ui
1+
# auth management ui
2+
3+
This Webapp is to provide an intuitive UI for [Anubis policy-api](https://github.com/orchestracities/anubis) using the language of [Material for you](https://m3.material.io/) (or Material v3) and the help of [MUI](https://mui.com/) in order to have an intuitive and a familiar look and feel for the end user.
4+
5+
![Interface](https://user-images.githubusercontent.com/34061179/161280350-a7a9fa46-9176-447c-b031-050f9e17f6a7.png)
6+
7+
## How to setup Docker
8+
9+
To start the docker compose and populate keycloack use the script:
10+
11+
```
12+
start.sh
13+
```
14+
The default user/password inside keycloack is admin/admin
15+
16+
## How to setup the Webapp
17+
18+
### The Webapp
19+
20+
To setup the webapp first of all let's create a .env file inside the main folder, the file should contain the string:
21+
22+
```
23+
REACT_APP_ANUBIS_API_URL=http://localhost:8085/
24+
```
25+
26+
Then let's start the package required with
27+
28+
```
29+
npm install
30+
```
31+
32+
after that we can start our application with
33+
34+
```
35+
npm start
36+
```
37+
38+
### Keycloack and graphql
39+
40+
To setup the connection between keycloack and graphql open a new window and move to the keycloak-connect-graphql folder
41+
42+
```
43+
cd keycloak-connect-graphql
44+
```
45+
46+
Then let's start the package required with
47+
48+
```
49+
npm install
50+
```
51+
52+
To run a simple example use:
53+
54+
```
55+
node examples/advancedAuth.js
56+
```

docker-compose.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: "3.7"
2+
services:
3+
policy-api:
4+
image: orchestracities/anubis-management-api:master
5+
networks:
6+
- envoymesh
7+
environment:
8+
- CORS_ALLOWED_ORIGINS=*
9+
- CORS_ALLOWED_METHODS=*
10+
- CORS_ALLOWED_HEADERS=*
11+
ports:
12+
- "8085:8000"
13+
14+
keycloak:
15+
image: jboss/keycloak:${KEYCLOAK_VERSION:-16.1.1}
16+
# if your platform is linux/arm64 use the following image
17+
# image: koolwithk/keycloak-arm:16.0.0
18+
ports:
19+
- "8080:8080"
20+
volumes:
21+
- ./keycloak/oc-custom.jar:/opt/jboss/keycloak/standalone/deployments/oc-custom.jar
22+
- ./keycloak/realm-export.json:/opt/jboss/realm-export.json
23+
environment:
24+
- KEYCLOAK_USER=admin
25+
- KEYCLOAK_PASSWORD=password
26+
- JAVA_TOOL_OPTIONS=-Dkeycloak.profile.feature.scripts=enabled -Dkeycloak.migration.action=import -Dkeycloak.migration.provider=singleFile -Dkeycloak.migration.file=/opt/jboss/realm-export.json -Dkeycloak.migration.strategy=OVERWRITE_EXISTING
27+
networks:
28+
- envoymesh
29+
30+
volumes:
31+
mongodata:
32+
33+
networks:
34+
envoymesh: {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const express = require('express')
2+
const { ApolloServer, gql } = require('apollo-server-express')
3+
const { configureKeycloak } = require('./lib/common')
4+
5+
const {
6+
KeycloakContext,
7+
KeycloakTypeDefs,
8+
KeycloakSchemaDirectives
9+
} = require('../')
10+
11+
const app = express()
12+
13+
const graphqlPath = '/graphql'
14+
15+
// perform the standard keycloak-connect middleware setup on our app
16+
configureKeycloak(app, graphqlPath)
17+
18+
const typeDefs = gql`
19+
type Query {
20+
greetings: [String] @hasRole(role: "developer")
21+
}
22+
23+
type Mutation {
24+
addGreeting(greeting: String!): String! @auth
25+
}
26+
`
27+
28+
const greetings = [
29+
'hello world!'
30+
]
31+
32+
const resolvers = {
33+
Query: {
34+
greetings: () => greetings
35+
},
36+
Mutation: {
37+
addGreeting: (obj, { greeting }, context, info) => {
38+
greetings.push(greeting)
39+
return greeting
40+
}
41+
}
42+
}
43+
44+
const options ={
45+
typeDefs: [KeycloakTypeDefs, typeDefs],
46+
schemaDirectives: KeycloakSchemaDirectives,
47+
resolvers,
48+
context: ({ req }) => {
49+
return {
50+
kauth: new KeycloakContext({ req })
51+
}
52+
}
53+
}
54+
55+
const server = new ApolloServer(options)
56+
57+
server.applyMiddleware({ app })
58+
59+
const port = 4000
60+
61+
app.listen({ port }, () =>
62+
console.log(`🚀 Server ready at http://localhost:${port}${server.graphqlPath}`)
63+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"realm": "master",
3+
"auth-server-url": "http://localhost:8080/auth",
4+
"ssl-required": "none",
5+
"resource": "keycloak-connect-graphql-public",
6+
"public-client": true,
7+
"use-resource-role-mappings": true,
8+
"confidential-port": 0
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const session = require('express-session')
4+
const Keycloak = require('keycloak-connect')
5+
6+
function configureKeycloak(app, graphqlPath) {
7+
8+
const keycloakConfig = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../config/keycloak.json')))
9+
10+
const memoryStore = new session.MemoryStore()
11+
12+
app.use(session({
13+
secret: process.env.SESSION_SECRET_STRING || 'this should be a long secret',
14+
resave: false,
15+
saveUninitialized: true,
16+
store: memoryStore
17+
}))
18+
19+
const keycloak = new Keycloak({
20+
store: memoryStore
21+
}, keycloakConfig)
22+
23+
// Install general keycloak middleware
24+
app.use(keycloak.middleware({
25+
admin: graphqlPath
26+
}))
27+
28+
// Protect the main route for all graphql services
29+
// Disable unauthenticated access
30+
app.use(graphqlPath, keycloak.middleware())
31+
32+
return { keycloak }
33+
}
34+
35+
module.exports = {
36+
configureKeycloak
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import express from 'express'
2+
import { ApolloServer, gql } from 'apollo-server-express'
3+
import { configureKeycloak } from '../lib/common'
4+
import cors from "cors"
5+
import {
6+
KeycloakContext,
7+
KeycloakTypeDefs,
8+
KeycloakSchemaDirectives
9+
} from '../../dist/index'
10+
11+
const app = express()
12+
13+
const graphqlPath = '/graphql'
14+
15+
// perform the standard keycloak-connect middleware setup on our app
16+
const { keycloak } = configureKeycloak(app, graphqlPath)
17+
18+
// Ensure entire GraphQL Api can only be accessed by authenticated users
19+
app.use(graphqlPath, keycloak.protect())
20+
app.use(cors());
21+
const typeDefs = `
22+
type Query {
23+
hello: String @hasRole(role: "developer")
24+
}
25+
`
26+
27+
const resolvers = {
28+
Query: {
29+
hello: (obj, args, context, info) => {
30+
// log some of the auth related info added to the context
31+
console.log(context.kauth.isAuthenticated())
32+
console.log(context.kauth.accessToken.content.preferred_username)
33+
34+
const name = context.kauth.accessToken.content.preferred_username || 'world'
35+
return `Hello ${name}`
36+
}
37+
}
38+
}
39+
40+
const server = new ApolloServer({
41+
typeDefs: [KeycloakTypeDefs, typeDefs],
42+
// See https://github.com/ardatan/graphql-tools/issues/1581
43+
schemaDirectives: KeycloakSchemaDirectives,
44+
resolvers,
45+
context: ({ req }) => {
46+
return {
47+
kauth: new KeycloakContext({ req : req as any })
48+
}
49+
}
50+
})
51+
52+
server.applyMiddleware({ app })
53+
54+
const port = 4000
55+
56+
app.listen({ port }, () =>
57+
console.log(`🚀 Server ready at http://localhost:${port}${server.graphqlPath}`)
58+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "./dist",
4+
"target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
5+
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6+
"strict": true, /* Enable all strict type-checking options. */
7+
"noImplicitAny": false,
8+
"allowJs": true,
9+
"types": ["node"], /* Type declaration files to be included in compilation. */
10+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
11+
},
12+
"include": ["./*.ts"],
13+
"exclude": []
14+
}

0 commit comments

Comments
 (0)