Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vzakharchenko/keycloak-api-gateway

Login flow gateway through Keycloak for static Web resources(bundle.js, images, pdf etc...)
https://github.com/vzakharchenko/keycloak-api-gateway

api-gateway aws aws-lambda-edge express frontend keycloak lamda-edge middleware multi-tenant-application

Last synced: 18 days ago
JSON representation

Login flow gateway through Keycloak for static Web resources(bundle.js, images, pdf etc...)

Awesome Lists containing this project

README

        

# keycloak-api-gateway

- [![Node.js 10.x, 12.x, 13.x, 14.x, 15.x CI](https://github.com/vzakharchenko/keycloak-api-gateway/actions/workflows/nodejs.yml/badge.svg)](https://github.com/vzakharchenko/keycloak-api-gateway/actions/workflows/nodejs.yml)
- [![npm version](https://badge.fury.io/js/keycloak-api-gateway.svg)](https://badge.fury.io/js/keycloak-api-gateway)
- [![Coverage Status](https://coveralls.io/repos/github/vzakharchenko/keycloak-api-gateway/badge.svg?branch=master)](https://coveralls.io/github/vzakharchenko/keycloak-api-gateway?branch=master)
- [![Maintainability](https://api.codeclimate.com/v1/badges/7d57b4bb709970045ad3/maintainability)](https://codeclimate.com/github/vzakharchenko/keycloak-api-gateway/maintainability)
- [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=vzakharchenko_keycloak-api-gateway&metric=security_rating)](https://sonarcloud.io/dashboard?id=vzakharchenko_keycloak-api-gateway)
- [![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=vzakharchenko_keycloak-api-gateway&metric=sqale_index)](https://sonarcloud.io/dashboard?id=vzakharchenko_keycloak-api-gateway)
- [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=vzakharchenko_keycloak-api-gateway&metric=bugs)](https://sonarcloud.io/dashboard?id=vzakharchenko_keycloak-api-gateway)

Login flow gateway through [Keycloak](https://www.keycloak.org/) for static Web resources(bundle.js, images, pdf etc...)

## Features
- lambda@edge or expressjs middleware
- authorization based on realm/client role and security resources
- protect frontend static web resources(bundle.js and other files)
- support Multi-Tenancy
- change behaviour of static web resource (dynamically replace to another resource)
- dynamically create web resources.
- redirect to another services
- work as Express middleware
- easily transform to aws lambda@edge

# Examples
- [Single Tenant ReactJS Application (Authorization based on Client Role)](./examples/reactJSExample)
- [Multi-tenant ReactJS Application with Tenant selector (Authorization based on Realm Role)](./examples/multiTenantReactJSExample)
- [Cross-tenant ReactJS Application with Tenant selector and approval proccess (Authorization based on security resources)](./examples/crossTenantReactJSExample)
- [Custom Storage example](./examples/customStorageExample)

# Installation

```
npm i keycloak-api-gateway -S
```

**Import Adapter**
```js
import { KeycloakApiGateWayAdapter } from 'keycloak-api-gateway/dist';
```
or
```js
const { KeycloakApiGateWayAdapter } = require('keycloak-api-gateway/dist');
```

# Configuration for Single-Tenant Application
![](./imgs/keycloak-api-gateway.png)
```json
{
"defaultAdapterOptions": {
"keycloakJson": {
"realm": "express-example",
"auth-server-url": "http://localhost:8090/auth/",
"ssl-required": "external",
"resource": "express-example",
"credentials": {
"secret": "express-example"
},
"use-resource-role-mappings": true,
"confidential-port": 0
}
},
"storageType": "InMemoryDB",
"keys": {
"privateKey": {
"key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQD40tysViQSnd3E\nIe5+6hDM/7ixHND8UoxYAKWwnA2/PdH2lq/pzjOo1t1Jt6ZbZx2l3cNUDt7FQXHL\nvZeEn0w75/LVe/gIeoKJIUTWrXyVOrrPn50oWiaKX5pnMCLWUwk1usRwnP7o26SH\nURTebSfBI7kQfh22aiv68qgGvo4lMWISVrWNCNej4oItLafRzvgBBD7GvJhqvPIW\nTMFyqDzGRtVk8nYi9x3Wwp72eUW9aY/j/akPTLdU5a+uAjlQYDrPa0wkg+/2KIhx\nGD/ffyggjvUaopzOEbnNGyBVXiOS3rQwwQnXNq+ip0xVecYVDJBlpOdQAxE77fUl\nRrw5DzKtAgMBAAECggEASLuyf7nKX5q/2XYltfmLobDadwM6X5dtqMe/pylmp1FV\nz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SqGSIb3DQEBCwUAA4IBAQDhKnZDt5VwTroWcTtX\nLSqIDtLLHiZxk6PIE8X9DG+rU//4Rfd+MFHClcKWiyLgYZPdgPaXSDXPiyfxlb7v\njOA0F0PXbEpR/RmjM5A+x3gljSufrWgedEC6rFFEg5Ju1IY+/7nJYkvd3ICMiLB3\ngOczMEp/tI7m89DS+bJAGG8AIYeBjj+3OjuGdEFtXpkt1ri33LYC4wK+rjqkBMyi\njqwex5bEkloSuyWP/IIDa8OpBWUM17H9ZswG74kQr5/wsvvTxc/JvRmMtNrbUyKa\n2JKXA1IJgNPP4/v2FxiGTibidZVf0fyXVqarU5Ngj/fVQyn7EBg+VGqPintiL5xU\ngUsi\n-----END CERTIFICATE-----\n"
}
}
}

```
example: [Single Tenant ReactJS Application](./examples/reactJSExample)
# Configuration for Multi-Tenant Application
![](/imgs/multi-tenant-keycloak-api-gateway.png)
```js
{
multiTenantJson(tenantName) {
return {
"realm": tenantName,
"auth-server-url": "http://localhost:8090/auth/",
"ssl-required": "external",
"resource": "multiTenantreactJsExample"
}
},
multiTenantAdapterOptions: {
"multiTenantAdapterOptions": {},
keys:{
privateKey:{
key: "-----BEGIN PRIVATE KEY-----\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDe8j3ZTRd2Rpaz\niLG33ItRWrL/p5wS+IEM3c4WFDzJvLKSTafFS78l3EuJz5bXuOQtKd6TrpXLkCG/\n1CQqaD7EPiMK11AQ3/mmALO6PSsXtFqTBk50NgUU64XmLt7nLG9FFgfK0Ez0p2HV\nTTkzbTCczUyktPte596b/7YMCFT7OozNjvCuSpNEFrGvSd3W1TN9nvlSy/YDb5x6\n+d8AWGGgyv7e/ziOLhvkD9XpsxApXbK6VxS6LHaZs5rYz5oIc10Q3Oizw5lSmW6P\nj9Izu0NxySQNNIa1RUaZnxUYratO0J8sqn0UwH6S6opUwYWmrRMJ+Ke6tWIzjlEx\n8hh+V/55AgMBAAECggEBALeY4dobpGCgjOZ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1si0IxfHKzAfBgNV\nHSMEGDAWgBS+MiDLDReuMpw4d2iF1si0IxfHKzAPBgNVHRMBAf8EBTADAQH/MA0G\nCSqGSIb3DQEBCwUAA4IBAQBaozLIBUH1aMCRL5LtVc9GaJt9NNT4O1dWJFShOfnH\nFqZ6VHfcBKrEDwLerG2PgGiDZABkCZe31KvQQk3eib3uGZnIWyaCr8uE3b/GOUds\nKpqTbjrSlYfP0rTi4CqT2PNLDkC78FtL4m7J8OF1nwFDIExaBz+75K0wDnGMOv84\nPLwjX2TrtQ53IVvkMgxVGC9gyK7SsuOHTDKzHwai5R00gs7m5+AHrh8fxb+aOyz5\ntz9OHtBMWO8qMcdblooUcs6CqgkwPVEbZtaJBzVNDgeVHC1NklpK6294e7ZIGv8c\nv2oAw8mMhvD4txqXW6S8oelZOKN4pcpSELTL6Kj/+L/D\n-----END CERTIFICATE-----\n"
}
}
},
storageType: "InMemoryDB",
keys: {
privateKey: {
key: "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQD40tysViQSnd3E\nIe5+6hDM/7ixHND8UoxYAKWwnA2/PdH2lq/pzjOo1t1Jt6ZbZx2l3cNUDt7FQXHL\nvZeEn0w75/LVe/gIeoKJIUTWrXyVOrrPn50oWiaKX5pnMCLWUwk1usRwnP7o26SH\nURTebSfBI7kQfh22aiv68qgGvo4lMWISVrWNCNej4oItLafRzvgBBD7GvJhqvPIW\nTMFyqDzGRtVk8nYi9x3Wwp72eUW9aY/j/akPTLdU5a+uAjlQYDrPa0wkg+/2KIhx\nGD/ffyggjvUaopzOEbnNGyBVXiOS3rQwwQnXNq+ip0xVecYVDJBlpOdQAxE77fUl\nRrw5DzKtAgMBAAECggEASLuyf7nKX5q/2XYltfmLobDadwM6X5dtqMe/pylmp1FV\nz6PqlgiNdzwfgU3qletFclepoieanMRtlCW+Zaj+6r/5bsgHD8tn3tfXvH0H3sNF\nGi3JDaOUgnxBsQoUFNw+4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YZPdgPaXSDXPiyfxlb7v\njOA0F0PXbEpR/RmjM5A+x3gljSufrWgedEC6rFFEg5Ju1IY+/7nJYkvd3ICMiLB3\ngOczMEp/tI7m89DS+bJAGG8AIYeBjj+3OjuGdEFtXpkt1ri33LYC4wK+rjqkBMyi\njqwex5bEkloSuyWP/IIDa8OpBWUM17H9ZswG74kQr5/wsvvTxc/JvRmMtNrbUyKa\n2JKXA1IJgNPP4/v2FxiGTibidZVf0fyXVqarU5Ngj/fVQyn7EBg+VGqPintiL5xU\ngUsi\n-----END CERTIFICATE-----\n"
}
}
}
```

Example: [Multi-tenant ReactJS Application with Tenant selector](./examples/multiTenantReactJSExample)

# Configuration for Cross-Tenant Application
[Cross-tenant ReactJS Application with Tenant selector and approval proccess](./examples/crossTenantReactJSExample)

# Url Protection Level

| Access Level | Page Handlers | Description |
|--------------|-------------------------------|-----------------------------------------------------------------|
| public | PublicUrlPageHandler.ts | Get resource without authentication and authorization |
| multi-tenant | MultiTenantUrlPageHandler.ts | Get Multi-tenant resource with authentication and authorization |
| single | SingleTenantUrlPageHandler.ts | Default Tenant authentication and authorization |

- default public urls
| Public Urls |
|-------------------------------------------|
| (.*)(/public)(.*) |
| (.*)(.(jpg \| jpeg \| png \| gif \| bmp)) |
| (.*)(.(ico \| tiff)) |
| (.* )(.(css)) |

- default tenant urls

| Single tenant Urls |
|-------------------------------------------|
| / |
| /index.html |

# Custom Page Handlers

- Existing page handlers

| Page Handler | Description |
|----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| PublicUrlPageHandler | Handler for public web resources |
| SingleTenantUrlPageHandler | Handler for default tenant web resources |
| MultiTenantUrlPageHandler | Handler for multi-tenant web resources |
| TokenPageHandler | Handler for /token path. Return single tenant access_token if use single tenant authentication Return multi-tenant access_token if use multi-tenant authentication |
| TenantInternalPage | Internal Tenant selector. If open /tenants/ then redirect to login page |
| TenantExternalPage | External Tenant selector. Redirect to external Service to select tenant. |

- Add Custom Page Handler
```js
const keycloakApiGateWayAdapter = new adapter.KeycloakApiGateWayAdapter(
options
)
.addCustomPageHandler(new PublicUrlPageHandler('icon.ico', 15000));
```

- Delete custom Page Handler
```js
const keycloakApiGateWayAdapter = new adapter.KeycloakApiGateWayAdapter(
options
)
.deleteCustomPageHandler('icon.ico');
```

- Replace custom Page Handler

```
const keycloakApiGateWayAdapter = new adapter.KeycloakApiGateWayAdapter(
options
)
.addCustomPageHandler(new TenantExternalPage("/", {
redirectedUrl: "http://localhost:8082",
applicationName: 'multiTenantreactJsExample'
}, 0))
.addCustomPageHandler(new TenantExternalPage("/index.html", {
redirectedUrl: "http://localhost:8082",
applicationName: 'multiTenantreactJsExample'
}, 32000))
```

# KeycloakApiGateWayAdapter OPTIONS

```js
multiTenantJson: (tenant: string)=>{
return KeycloakJSONForTenant(tenant)
};
multiTenantAdapterOptions: {...};
defaultAdapterOptions: {
keycloakJson,
...
}
storageType: 'DynamoDB',
identityProviders: {
"multiTenant":"IdentityProvider Alias",
"singleTenant":"IdentityProvider Alias"
},
storageTypeSettings: {
tableName,
region,
apiVersion,
}
keys: RSA KEYS,
```
where
- **defaultAdapterOptions** authentication and authorization for single-tenant application ([structure](https://github.com/vzakharchenko/keycloak-lambda-authorizer#option-structure))
- **multiTenantAdapterOptions** authentication and authorization for multi-tenant application ([structure](https://github.com/vzakharchenko/keycloak-lambda-authorizer#option-structure))
- **multiTenantJson** tenant Keycloak.json resolver
- **storageType** place where store session data(user access and refresh tokens)
- DynamoDB store in AWS DynamoDB
- InMemoryDB store in file
- own implementation of StorageDB ([example](./examples/customStorageExample))
- **identityProviders** Identity Provider Alias name.
- multiTenant - Identity Provider for Multitenant application. need use the same alias name between tenants. can be overridden by request parameter kc_idp_hint
- singleTenant - Identity Provider for application.
- **storageTypeSettings** configuration for storageType
- keys RSA keys which can be used for sign/verify sessionId and also can be used for "Signed JWT" client authentication

```js
const keycloakApiGateWayAdapter = new KeycloakApiGateWayAdapter(
{
"defaultAdapterOptions": AUTHENTICATION AND AUTHORIZATION OPTIONS,
"storageType": "InMemoryDB",
"keys": {
"privateKey": {
"key": PRIVATE KEY
},
"publicKey": {
"key": CERTIFICATE OR PUBLIC KEY
}
}
}
);
```
# **[AWS Lambda@edge](https://aws.amazon.com/ru/lambda/edge/) middleWare**

```js
module.exports.handler =
async (awsEvent) => {
return await keycloakApiGateWayAdapter
.awsLambdaEdgeAdapter()
.handler(awsEvent);
};

```

# **[ExpressJS](https://www.npmjs.com/package/express) Server**

```js
const middlewareServer = express();
middlewareServer.use(cookieParser());
middlewareServer.use(async (req, res, next) => {
await keycloakApiGateWayAdapter.expressMiddleWare().middleWare(req, res, next);
});
middlewareServer.use(express.static('./static'));

middlewareServer.listen(8080, () => {
console.info('HTTP server listening on port 8080');
});
```

# **[https://www.npmjs.com/package/@craco/craco](https://www.npmjs.com/package/@craco/craco) Development Server**
[craco.config.js](./development/craco.config.js)
```js

module.exports = {
devServer: (devServerConfig) => {
keycloakApiGateWayAdapter.webPackDevServerMiddleWare().applyMiddleWare(devServerConfig);
return devServerConfig;
}
};
```

# **[Webpack](https://www.npmjs.com/package/webpack) Development Server**

```js

const config = {
mode: env,
...,
devServer:{},
...
};
keycloakApiGateWayAdapter
.webPackDevServerMiddleWare()
.applyMiddleWare(config.devServer);

module.exports = config;
```

# Session Token RSA Keys Generation
```
openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=clientId" -keyout server.key -out server.crt
```
# Signed JWT Token RSA Generation
```
openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=clientId" -keyout server.key -out server.crt

```