-
Notifications
You must be signed in to change notification settings - Fork 39
Protecting endpoints with a middleware #45
base: development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ jspm_packages | |
|
||
# Serverless directories | ||
.serverless | ||
*-function.json | ||
|
||
.DS_Store |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
service: codingcoach-api | ||
|
||
# You can pin your service to only deploy with a specific Serverless version | ||
# Check out our docs for more details | ||
# frameworkVersion: "=X.X.X" | ||
|
||
provider: | ||
name: azure | ||
location: West US | ||
|
||
plugins: | ||
- serverless-azure-functions | ||
|
||
# you can add packaging information here | ||
#package: | ||
# include: | ||
# - include-me.js | ||
# - include-me-dir/** | ||
# exclude: | ||
# - exclude-me.js | ||
# - exclude-me-dir/** | ||
|
||
functions: | ||
users: ${file(src/handlers/users/config.yml):users} | ||
users-add: ${file(src/handlers/users/config.yml):usersadd} | ||
hello: ${file(src/handlers/hello/config.yml):hello} | ||
|
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
module.exports = { | ||
auth0: { | ||
DOMAIN: process.env.AUTH0_DOMAIN, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another question I have is that every time I deploy... these values disappear in azure :( and I need to add it again, if I deploy a single function everything works as expected. Anyone knows how to prevent that? Or how can we dynamically create those settings? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @crysfel, am not sure if these can be handled in the local.settings.json There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could set these variables in the serverless.yml |
||
CLIENT_ID: process.env.AUTH0_CLIENT_ID, | ||
CLIENT_SECRET: process.env.AUTH0_CLIENT_SECRET, | ||
CERTIFICATE: process.env.AUTH0_CERTIFICATE, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These data is defined in azure as The value of this variable is something like:
It looks like when saving this value in the applications settings azure removes the Anyone can help me with this? |
||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
hello: | ||
handler: src/handlers/hello/handler.hello | ||
events: | ||
- http: true | ||
x-azure-settings: | ||
authLevel : anonymous | ||
- http: true | ||
x-azure-settings: | ||
direction: out | ||
name: res |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
users: | ||
handler: src/handlers/users/list.list | ||
events: | ||
- http: true | ||
x-azure-settings: | ||
authLevel : anonymous | ||
- http: true | ||
x-azure-settings: | ||
direction: out | ||
name: res | ||
usersadd: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to make this as
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is done in the function.json, there should be a function.json per function handler - since you are using serverless, there should be some config in the handler in the serverless.yml that allows you to specify this |
||
handler: src/handlers/users/store.add | ||
events: | ||
- http: true | ||
x-azure-settings: | ||
authLevel : anonymous | ||
- http: true | ||
x-azure-settings: | ||
direction: out | ||
name: res |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict'; | ||
|
||
const fetch = require('node-fetch'); | ||
const config = require('../../config/constants.js'); | ||
const authMiddleware = require('../../middlewares/auth0.js'); | ||
|
||
// Get an access token for the Auth0 Admin API | ||
function getAdminAccessToken() { | ||
const options = { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
client_id: config.auth0.CLIENT_ID, | ||
client_secret: config.auth0.CLIENT_SECRET, | ||
audience: `${config.auth0.DOMAIN}/api/v2/`, | ||
grant_type: 'client_credentials', | ||
}), | ||
}; | ||
|
||
return fetch(`${config.auth0.DOMAIN}/oauth/token`, options) | ||
.then((response) => response.json()); | ||
} | ||
|
||
|
||
// Get the user's profile from auth0 | ||
function getUserProfile(accessToken, userID) { | ||
const options = { | ||
headers: { | ||
'Authorization': `Bearer ${accessToken}` | ||
} | ||
} | ||
return fetch(`${config.auth0.DOMAIN}/api/v2/users/${userID}`, options) | ||
.then(response => response.json()); | ||
} | ||
|
||
module.exports.add = authMiddleware(async (context, request) => { | ||
let res = {}; | ||
try { | ||
const data = await getAdminAccessToken(); | ||
const user = await getUserProfile(data.access_token, request.user.sub); | ||
|
||
// @TODO: check if the current user already exist in the database, | ||
// if not, we need to add the new record. | ||
|
||
res = { | ||
body: { | ||
success: true, | ||
message: 'User successfully saved', | ||
user, | ||
}, | ||
}; | ||
} catch (error) { | ||
res = { | ||
status: 500, | ||
body: { | ||
success: false, | ||
error, | ||
}, | ||
}; | ||
} | ||
|
||
context.res = { | ||
...res, | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
}; | ||
context.done() | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we should commit these files and if so.... is there a way to put them somewhere else other than the root folder?
These files are created automatically when deploying with the serverless framework.