Skip to content

Commit 0c280c8

Browse files
committed
adding roles sample
1 parent e5e55d1 commit 0c280c8

30 files changed

Lines changed: 1515 additions & 1 deletion

.github/ISSUE_TEMPLATE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [ ] 1-2) Sign-in with Azure AD B2C
1414
- [ ] 2-1) Acquire a Token and call Microsoft Graph
1515
- [ ] 3) Deploy to Azure Storage and App Service
16+
- [ ] 4-1) Use App Roles for Role-based Access Control
1617
```
1718

1819
## This issue is for a

2-Authorization/1-call-graph/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ This sample also demonstrates how to use the [Microsoft Graph JavaScript SDK](ht
3939
| `App/cache.json` | Stores MSAL Node token cache data. |
4040
| `App/app.js` | Application entry point. |
4141
| `App/utils/graphManager.js` | Handles calls to Microsoft Graph using Graph JS SDK. |
42-
| `App/utils/cacheManager.js` | Handles calls to protected APIs using Axios package. |
42+
| `App/utils/fetchManager.js` | Handles calls to protected APIs using Axios package. |
4343

4444
## Prerequisites
4545

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License.
4+
*/
5+
6+
const express = require('express');
7+
const session = require('express-session');
8+
const path = require('path');
9+
10+
const router = require('./routes/router');
11+
const SERVER_PORT = process.env.PORT || 4000;
12+
13+
// initialize express
14+
const app = express();
15+
16+
app.set('views', path.join(__dirname, './views'));
17+
app.set('view engine', 'ejs');
18+
19+
app.use('/css', express.static(path.join(__dirname, 'node_modules/bootstrap/dist/css')));
20+
app.use('/js', express.static(path.join(__dirname, 'node_modules/bootstrap/dist/js')));
21+
22+
app.use(express.urlencoded({ extended: false }));
23+
24+
app.use(express.static(path.join(__dirname, './public')));
25+
26+
app.use(express.json());
27+
28+
/**
29+
* Using express-session middleware. Be sure to familiarize yourself with available options
30+
* and set as desired. Visit: https://www.npmjs.com/package/express-session
31+
*/
32+
app.use(session({
33+
secret: 'ENTER_YOUR_SECRET_HERE',
34+
resave: false,
35+
saveUninitialized: false
36+
}));
37+
38+
app.use(router);
39+
40+
app.listen(SERVER_PORT, () => console.log(`Msal Node Auth Code Sample app listening on port ${SERVER_PORT}!`));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"credentials": {
3+
"clientId": "Enter_the_Application_Id_Here",
4+
"tenantId": "Enter_the_Tenant_Id_Here",
5+
"clientSecret": "Enter_the_Client_Secret_Here"
6+
},
7+
"settings": {
8+
"homePageRoute": "/home",
9+
"redirectUri": "/redirect",
10+
"postLogoutRedirectUri": "/"
11+
},
12+
"accessMatrix": {
13+
"todolist": {
14+
"path": "/todolist",
15+
"methods": ["GET", "POST", "DELETE"],
16+
"roles": ["TaskUser", "TaskAdmin"]
17+
},
18+
"dashboard": {
19+
"path": "/dashboard",
20+
"methods": ["GET"],
21+
"roles": ["TaskAdmin"]
22+
}
23+
}
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const lowdb = require('lowdb');
2+
const FileSync = require('lowdb/adapters/FileSync');
3+
const adapter = new FileSync('./data/db.json');
4+
const db = lowdb(adapter);
5+
6+
exports.getAllTodos = (req, res) => {
7+
const todos = db.get('todos')
8+
.value();
9+
10+
res.render('dashboard', { isAuthenticated: req.session.isAuthenticated, todos: todos });
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
exports.getHomePage = (req, res, next) => {
2+
res.render('home', { isAuthenticated: req.session.isAuthenticated });
3+
}
4+
5+
exports.getIdPage = (req, res, next) => {
6+
const claims = {
7+
name: req.session.account.idTokenClaims.name,
8+
preferred_username: req.session.account.idTokenClaims.preferred_username,
9+
oid: req.session.account.idTokenClaims.oid,
10+
sub: req.session.account.idTokenClaims.sub
11+
};
12+
13+
res.render('id', { isAuthenticated: req.session.isAuthenticated, claims: claims });
14+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const lowdb = require('lowdb');
2+
const FileSync = require('lowdb/adapters/FileSync');
3+
const adapter = new FileSync('./data/db.json');
4+
const db = lowdb(adapter);
5+
6+
exports.getTodos = (req, res) => {
7+
const owner = req.session.account.idTokenClaims['preferred_username'];
8+
9+
const todos = db.get('todos')
10+
.filter({ owner: owner })
11+
.value();
12+
13+
res.render('todolist', { isAuthenticated: req.session.isAuthenticated, todos: todos });
14+
}
15+
16+
exports.postTodo = (req, res) => {
17+
db.get('todos').push(req.body).write();
18+
res.redirect('/todolist');
19+
}
20+
21+
exports.deleteTodo = (req, res) => {
22+
const id = req.params.id;
23+
const owner = req.session.account.idTokenClaims['preferred_username'];
24+
25+
db.get('todos')
26+
.remove({ owner: owner, id: id })
27+
.write();
28+
29+
res.redirect('/todolist');
30+
}

4-AccessControl/1-app-roles/App/data/cache.json

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"todos": []
3+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "msal-node-tutorial-app-roles",
3+
"version": "1.0.0",
4+
"description": "A Node.js & Express web app using Azure AD App Roles to implement Role-based Access Control (RBAC)",
5+
"main": "app.js",
6+
"scripts": {
7+
"start": "node app.js",
8+
"dev": "nodemon app.js"
9+
},
10+
"author": "derisen",
11+
"license": "MIT",
12+
"dependencies": {
13+
"@azure/msal-node": "^1.1.0",
14+
"@microsoft/microsoft-graph-client": "^2.2.1",
15+
"axios": "^0.21.1",
16+
"bootstrap": "^4.5.3",
17+
"ejs": "^3.0.1",
18+
"express": "^4.17.1",
19+
"express-session": "^1.17.1",
20+
"isomorphic-fetch": "^3.0.0",
21+
"lowdb": "^1.0.0",
22+
"msal-express-wrapper": "git+https://github.com/Azure-Samples/msal-express-wrapper.git",
23+
"nanoid": "^3.1.23"
24+
},
25+
"devDependencies": {
26+
"nodemon": "^2.0.2"
27+
}
28+
}

0 commit comments

Comments
 (0)