Skip to content

Commit

Permalink
Merge pull request #12 from vzakharchenko/resourceHandler
Browse files Browse the repository at this point in the history
Resource Handler
  • Loading branch information
vzakharchenko committed Jun 26, 2020
2 parents 497d53c + db02092 commit 262cd18
Show file tree
Hide file tree
Showing 25 changed files with 99 additions and 76 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,28 @@ keycloakJson,
}
}
);

```
## 14. Resource Handler

```js
const keycloakJSON = ...; // read Keycloak.json
apigateway.awsHandler(event, keycloakJSON, {
enforce: {
enabled: true,
resource: {
name: 'SOME_RESOURCE',
uri: 'RESOURCE_URI',
matchingUri: true,
},
resourceHandler:(resourceJson, options)=>{
console.log('resource: ' + JSON.stringify(resourceJson));
}
},
}).then((token)=>{
// Success
}).catch((e)=>{
// Failed
});
}
```
18 changes: 18 additions & 0 deletions __tests__/src/umaConfigurationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ describe('testing umaConfiguration', () => {
});
});

test('test enforceResource Handler success', async () => {
await enforce(token, {
cache,
keycloakJson,
enforce: {
enabled: true,
resource: {
name: 'resource',
uri: '/test',
matchingUri: true,
},
resourceHandler: (resourceJson) => {
expect(resourceJson).toEqual(['resourceId']);
},
},
});
});

test('test enforceResource denied', async () => {
try {
await enforce(token, {
Expand Down
2 changes: 0 additions & 2 deletions example/keycloak-authorizer/serverless-jwks/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function getKeycloakJSON() {
}));
}


function getToken(event) {
const tokenString = event.authorizationToken || event.headers.Authorization;
if (!tokenString) {
Expand All @@ -27,7 +26,6 @@ function getToken(event) {
return jsonwebtoken.decode(tokenStringValue);
}


export function hello(event, context, callback) {
const token = getToken(event);
callback(null, {
Expand Down
2 changes: 0 additions & 2 deletions example/keycloak-authorizer/serverless/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function getKeycloakJSON() {
}));
}


function getToken(event) {
const tokenString = event.authorizationToken || event.headers.Authorization;
if (!tokenString) {
Expand All @@ -27,7 +26,6 @@ function getToken(event) {
return jsonwebtoken.decode(tokenStringValue);
}


export function hello(event, context, callback) {
const token = getToken(event);
callback(null, {
Expand Down
18 changes: 9 additions & 9 deletions example/keycloak-authorizer/serverless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
"author": "vzakharchenko",
"license": "Apache-2.0",
"devDependencies": {
"@babel/core": "^7.9.6",
"@babel/plugin-proposal-decorators": "^7.8.3",
"@babel/plugin-proposal-object-rest-spread": "^7.9.6",
"@babel/plugin-transform-async-to-generator": "^7.8.3",
"@babel/plugin-transform-runtime": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@babel/core": "^7.10.3",
"@babel/plugin-proposal-decorators": "^7.10.3",
"@babel/plugin-proposal-object-rest-spread": "^7.10.3",
"@babel/plugin-transform-async-to-generator": "^7.10.1",
"@babel/plugin-transform-runtime": "^7.10.3",
"@babel/preset-env": "^7.10.3",
"@babel/preset-es2015": "^7.0.0-beta.53",
"@babel/register": "^7.9.0",
"@babel/register": "^7.10.3",
"babel-loader": "^8.1.0",
"@babel/runtime": "^7.9.6",
"@babel/runtime": "^7.10.3",
"copy-webpack-plugin": "*",
"eslint": "*",
"eslint-config-airbnb": "*",
Expand All @@ -33,7 +33,7 @@
"serverless-offline": "*",
"serverless-webpack": "*",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
"webpack-cli": "^3.3.12"
},
"dependencies": {
"keycloak-lambda-authorizer": "*"
Expand Down
4 changes: 0 additions & 4 deletions example/keycloak-authorizer/ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ app.use(session({
store: memoryStore,
}));


const keycloak = new Keycloak({
store: memoryStore,
});

app.use(keycloak.middleware());


app.use(bodyParser.urlencoded({ extended: true }));

app.engine('.hbs', exphbs({
Expand Down Expand Up @@ -69,7 +67,6 @@ async function clientToRPTExchange(request, clientId) {
}
}


app.post('/lambda', keycloak.protect(), keycloak.enforcer(['uiResource']), async (request, response) => {
try {
const lambdaJWT = await clientToRPTExchange(request, 'lambda');
Expand All @@ -94,7 +91,6 @@ app.post('/lambdaJwks', keycloak.protect(), keycloak.enforcer(['uiResource']), a
}
});


app.get('/', keycloak.protect(), keycloak.enforcer(['uiResource']), (request, response) => {
renderUI(request, response, '', '');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class KeycloakCloudFrontExampleStack extends cdk.Stack {
constructor(parent, id, props) {
super(parent, id, props);


const role = iam.Role.fromRoleArn(this, `Role ${bucketName}`, roleArn, { mutable: false });
const bucket = new s3.Bucket(this, 'lambda-edge-bucket', {
accessControl: s3.BucketAccessControl.AUTHENTICATED_READ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
tenantOptions,
} from './Tentants';


lamdaEdge.routes.addJwksEndpoint('/cert', publicKey.key);

function tenantResponseHandler(request, options) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const publicKey = '-----BEGIN CERTIFICATE-----\n'
+ 'MIIDgzCCAmugAwIBAgIJAJTi4Mu+7fIMMA0GCSqGSIb3DQEBCwUAMFgxCzAJBgNV\n'
+ 'BAYTAlVTMQ8wDQYDVQQIDAZEZW5pYWwxFDASBgNVBAcMC1NwcmluZ2ZpZWxkMQww\n'
Expand Down
1 change: 0 additions & 1 deletion example/keycloak-cloudfront-portal/lambdaEdgeProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ function transformResponse(response, res, next) {
}
}


module.exports.middleware = async (req, res, next) => {
const cb = function callback(error, r) {
if (error) {
Expand Down
30 changes: 15 additions & 15 deletions example/keycloak-cloudfront-portal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,44 @@
"author": "vzakharchenko",
"license": "Apache-2.0",
"devDependencies": {
"@babel/core": "^7.10.2",
"@babel/core": "^7.10.3",
"@babel/plugin-proposal-class-properties": "^7.10.1",
"@babel/plugin-proposal-decorators": "^7.10.1",
"@babel/plugin-proposal-decorators": "^7.10.3",
"@babel/plugin-proposal-do-expressions": "^7.10.1",
"@babel/plugin-proposal-export-default-from": "^7.10.1",
"@babel/plugin-proposal-function-sent": "^7.10.1",
"@babel/plugin-proposal-object-rest-spread": "^7.10.1",
"@babel/plugin-proposal-optional-chaining": "^7.10.1",
"@babel/plugin-proposal-object-rest-spread": "^7.10.3",
"@babel/plugin-proposal-optional-chaining": "^7.10.3",
"@babel/plugin-proposal-partial-application": "^7.10.1",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-transform-object-assign": "^7.10.1",
"@babel/plugin-transform-react-jsx": "^7.10.1",
"@babel/plugin-transform-runtime": "^7.10.1",
"@babel/preset-env": "^7.10.2",
"@babel/plugin-transform-object-assign": "^7.10.3",
"@babel/plugin-transform-react-jsx": "^7.10.3",
"@babel/plugin-transform-runtime": "^7.10.3",
"@babel/preset-env": "^7.10.3",
"@babel/preset-flow": "^7.10.1",
"@babel/preset-react": "^7.10.1",
"@babel/register": "^7.10.1",
"@babel/register": "^7.10.3",
"@material-ui/styles": "^4.10.0",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.1.0",
"babel-plugin-dynamic-import-node": "^2.3.3",
"css-loader": "^3.5.3",
"eslint": "^7.1.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"css-loader": "^3.6.0",
"eslint": "^7.3.1",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-react": "^7.20.0",
"file-loader": "^6.0.0",
"html-webpack-plugin": "^4.3.0",
"lambda-edge-example": "./lambda-edge-example",
"progress-bar-webpack-plugin": "^2.1.0",
"style-loader": "^1.2.1",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0"
},
"dependencies": {
"@babel/polyfill": "^7.10.1",
"@material-ui/core": "^4.10.1",
"@material-ui/core": "^4.10.2",
"@material-ui/icons": "^4.9.1",
"axios": "^0.19.2",
"browserify": "^16.5.1",
Expand Down
2 changes: 0 additions & 2 deletions example/keycloak-cloudfront-portal/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ async function fetchData(url, method = 'GET', headers) {
return ret.data;
}


export default
class App extends React.Component {
// eslint-disable-next-line class-methods-use-this


async componentDidMount() {
const tenants = getTenants();
// eslint-disable-next-line no-plusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class Header extends React.Component {
this.setState({ anchorEl: event.currentTarget });
};


handleClose = () => {
this.setState({ anchorEl: null });
};
Expand Down
1 change: 0 additions & 1 deletion example/keycloak-cloudfront-portal/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import ReactDOM from 'react-dom';
import App from './components/App';
import Header from './components/Header';


const Index = () => (
<div>
<Header />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class KeycloakCloudFrontExampleStack extends cdk.Stack {
constructor(parent, id, props) {
super(parent, id, props);


const role = iam.Role.fromRoleArn(this, `Role ${bucketName}`, roleArn, { mutable: false });
const bucket = new s3.Bucket(this, 'lambda-edge-bucket', {
accessControl: s3.BucketAccessControl.AUTHENTICATED_READ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const tenant1Options = {
},
};


export const tenant2KeycloakJson = {
realm: 'Tenant2',
'auth-server-url': 'http://localhost:8090/auth',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const publicKey = '-----BEGIN CERTIFICATE-----\n'
+ 'MIIDgzCCAmugAwIBAgIJAJTi4Mu+7fIMMA0GCSqGSIb3DQEBCwUAMFgxCzAJBgNV\n'
+ 'BAYTAlVTMQ8wDQYDVQQIDAZEZW5pYWwxFDASBgNVBAcMC1NwcmluZ2ZpZWxkMQww\n'
Expand Down
1 change: 0 additions & 1 deletion example/keycloak-cloudfront/lambdaEdgeProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ function transformResponse(response, res, next) {
}
}


module.exports.middleware = async (req, res, next) => {
const cb = function callback(error, r) {
if (error) {
Expand Down
52 changes: 26 additions & 26 deletions example/keycloak-cloudfront/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,50 @@
"author": "vzakharchenko",
"license": "Apache-2.0",
"devDependencies": {
"@babel/core": "^7.9.6",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-decorators": "^7.8.3",
"@babel/plugin-proposal-do-expressions": "^7.8.3",
"@babel/plugin-proposal-export-default-from": "^7.8.3",
"@babel/plugin-proposal-function-sent": "^7.8.3",
"@babel/plugin-proposal-object-rest-spread": "^7.9.6",
"@babel/plugin-proposal-optional-chaining": "^7.9.0",
"@babel/plugin-proposal-partial-application": "^7.8.3",
"@babel/core": "^7.10.3",
"@babel/plugin-proposal-class-properties": "^7.10.1",
"@babel/plugin-proposal-decorators": "^7.10.3",
"@babel/plugin-proposal-do-expressions": "^7.10.1",
"@babel/plugin-proposal-export-default-from": "^7.10.1",
"@babel/plugin-proposal-function-sent": "^7.10.1",
"@babel/plugin-proposal-object-rest-spread": "^7.10.3",
"@babel/plugin-proposal-optional-chaining": "^7.10.3",
"@babel/plugin-proposal-partial-application": "^7.10.1",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-transform-object-assign": "^7.8.3",
"@babel/plugin-transform-react-jsx": "^7.9.4",
"@babel/plugin-transform-runtime": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@babel/preset-flow": "^7.9.0",
"@babel/preset-react": "^7.9.4",
"@babel/register": "^7.9.0",
"@material-ui/styles": "^4.9.14",
"@babel/plugin-transform-object-assign": "^7.10.3",
"@babel/plugin-transform-react-jsx": "^7.10.3",
"@babel/plugin-transform-runtime": "^7.10.3",
"@babel/preset-env": "^7.10.3",
"@babel/preset-flow": "^7.10.1",
"@babel/preset-react": "^7.10.1",
"@babel/register": "^7.10.3",
"@material-ui/styles": "^4.10.0",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.1.0",
"babel-plugin-dynamic-import-node": "^2.3.3",
"css-loader": "^3.5.3",
"eslint": "^7.0.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"css-loader": "^3.6.0",
"eslint": "^7.3.1",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-react": "^7.20.0",
"file-loader": "^6.0.0",
"html-webpack-plugin": "^4.3.0",
"lambda-edge-example": "./lambda-edge-example",
"progress-bar-webpack-plugin": "^2.1.0",
"style-loader": "^1.2.1",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0"
},
"dependencies": {
"@babel/polyfill": "^7.8.7",
"@material-ui/core": "^4.9.14",
"@babel/polyfill": "^7.10.1",
"@material-ui/core": "^4.10.2",
"@material-ui/icons": "^4.9.1",
"axios": "^0.19.2",
"browserify": "^16.5.1",
"js-cookie": "^2.2.1",
"keycloak-lambda-cloudfront-ui": "^0.1.4",
"mobx-utils": "^5.5.7",
"keycloak-lambda-cloudfront-ui": "^0.1.6",
"mobx-utils": "^5.6.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"typeface-roboto": "0.0.75",
Expand Down
2 changes: 0 additions & 2 deletions example/keycloak-cloudfront/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ async function fetchData(url, method = 'GET', headers) {
return ret.data;
}


export default
class App extends React.Component {
// eslint-disable-next-line class-methods-use-this


async componentDidMount() {
const tenants = getTenants();
// eslint-disable-next-line no-plusplus
Expand Down
1 change: 0 additions & 1 deletion example/keycloak-cloudfront/src/components/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class Header extends React.Component {
this.setState({ anchorEl: event.currentTarget });
};


handleClose = () => {
this.setState({ anchorEl: null });
};
Expand Down
Loading

0 comments on commit 262cd18

Please sign in to comment.