Skip to content

Commit

Permalink
added express example
Browse files Browse the repository at this point in the history
  • Loading branch information
vzakharchenko committed Apr 30, 2021
1 parent b7af528 commit 1cb5510
Show file tree
Hide file tree
Showing 16 changed files with 2,698 additions and 10 deletions.
18 changes: 18 additions & 0 deletions .circleci/config.yml
Expand Up @@ -66,3 +66,21 @@ jobs:
- run:
name: lint example/keycloak-cloudfront-portal cloudfront
command: cd example/keycloak-cloudfront-portal && npm run lint
- run:
name: lint example/chain-service-calls/frontend
command: cd example/chain-service-calls/frontend && npm run lint && npm i
- run:
name: lint example/chain-service-calls/service1
command: cd example/chain-service-calls/service1 && npm run lint && npm i
- run:
name: lint example/chain-service-calls/service2
command: cd example/chain-service-calls/service2 && npm run lint && npm i
- run:
name: lint example/chain-service-calls/service3
command: cd example/chain-service-calls/service3 && npm run lint && npm i
- run:
name: lint example/express/frontend
command: cd example/express/frontend && npm run lint && npm i
- run:
name: lint example/express/express-service
command: cd example/express/express-service && npm run lint && npm i
4 changes: 4 additions & 0 deletions .github/workflows/nodejs.yml
Expand Up @@ -41,5 +41,9 @@ jobs:
- run: cd example/keycloak-cloudfront-portal/lambda-edge-example && npm i && npm run build
- run: cd example/keycloak-cloudfront-portal && npm i && npm run build
- run: cd example/keycloak-cloudfront-portal && npm run lint
- run: cd example/chain-service-calls/frontend && npm run lint && npm i
- run: cd example/chain-service-calls/service1 && npm run lint && npm i
- run: cd example/chain-service-calls/service2 && npm run lint && npm i
- run: cd example/chain-service-calls/service3 && npm run lint && npm i


30 changes: 30 additions & 0 deletions README.md
Expand Up @@ -27,6 +27,7 @@ npm install keycloak-lambda-authorizer -S
```
# Examples
- [Serverless example (Api gateway with lambda authorizer)](example/keycloak-authorizer/README.md)
- [Example of expressjs middleware](example/express)
- [Example of calling a chain of micro services, where each service is protected by its secured client](example/chain-service-calls)
- [CloudFront with Lambda:Edge example](example/keycloak-cloudfront/README.md)
- [CloudFront with portal authorization (switching between security realms)](example/keycloak-cloudfront-portal)
Expand Down Expand Up @@ -729,6 +730,35 @@ keycloakJson,
});
}
```
## 15. ExpressJS middleware

```
const fs = require('fs');
const { middlewareAdapter } = require('keycloak-lambda-authorizer');
function getKeycloakJSON() {
return JSON.parse(fs.readFileSync(`${__dirname}/keycloak.json`, 'utf8'));
}
const app = express();
app.get('/expressServiceApi', middlewareAdapter(
getKeycloakJSON(),
{
enforce: {
enabled: true,
resource: {
name: 'service-api',
},
},
},
).middleware,
async (request, response) => {
response.json({
message: `Hi ${request.jwt.payload.preferred_username}. Your function executed successfully!`,
});
});
```


# If you find these useful, please [Donate](https://secure.wayforpay.com/button/b18610f33a01c)!
10 changes: 0 additions & 10 deletions example/chain-service-calls/README.md
Expand Up @@ -14,10 +14,6 @@ sh bin/standalone.sh -c standalone.xml -b 0.0.0.0 -Djboss.bind.address.manageme
```
Open the Keycloak admin console, click on Add Realm, click on import 'Select file', select example-realm-export.json and click Create.

## 2. Run Serverless offline (Client Id and Secret credential Type)

```
## 2. Run Services Locally
- Service1
```bash
Expand Down Expand Up @@ -59,12 +55,6 @@ users:

## 6. Results

| User | Password | Service 1 Role 1 | Service 1 Role 2 | Service 2 Role | Service 3 Role |
|:----------|:-----------|:-----------------|:-----------------|:---------------|:---------------|
| user | user | X | X | X | X |
| user1 | user1 | - | - | X | X |
| user2 | user2 | X | - | - | X |

| User | Result | Description |
|:----------|:-------------------------------------------------------------------------------------------------------|:------------------------------------------------------|
| User | ![](../../docs/userChain.png) | All Access |
Expand Down
48 changes: 48 additions & 0 deletions example/express/README.md
@@ -0,0 +1,48 @@
# Example expressjs middleware
![](../../keycloak-cross-client-authentication3.png)

## 1. Start Keycloak

### Docker
Using the image from https://hub.docker.com/r/jboss/keycloak/
```
docker run -p 8090:8080 -e JAVA_OPTS="-Dkeycloak.profile.feature.scripts=enabled -Dkeycloak.profile.feature.upload_scripts=enabled -server -Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true" -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin -v `pwd`/example/express:/express -e KEYCLOAK_IMPORT=/express/example-realm-export.json jboss/keycloak
```
### Standard
```
sh bin/standalone.sh -c standalone.xml -b 0.0.0.0 -Djboss.bind.address.management=0.0.0.0 --debug 8190 -Djboss.http.port=8090
```
Open the Keycloak admin console, click on Add Realm, click on import 'Select file', select example-realm-export.json and click Create.

## 2. Run Services Locally
- Express Service
```bash
cd express-service
npm i
npm run start
```

## 3. Run UI locally

```bash
cd frontend
npm i
npm run start
```

## 4. Open UI
[http://localhost:3001](http://localhost:3001)

users:

| User | Password | Service Role |
|:----------|:-----------|:-----------------|
| user | user | X |
| user1 | user1 | - |

## 6. Results

| User | Result | Description |
|:----------|:-------------------------------------------------------------------------------------------------------|:------------------------------------------------------|
| User | Hi user. Your function executed successfully! | All Access |
| User1 | Request failed with status code 403 | User has not access to express-service |

0 comments on commit 1cb5510

Please sign in to comment.