Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/idandaccess/azure-functions-auth
Authentication and Authorization for Azure Functions (with OAuth 2.0 and JWT)
https://github.com/idandaccess/azure-functions-auth
authentication authorization azure azure-functions identity-management javascript jwt oauth2
Last synced: about 13 hours ago
JSON representation
Authentication and Authorization for Azure Functions (with OAuth 2.0 and JWT)
- Host: GitHub
- URL: https://github.com/idandaccess/azure-functions-auth
- Owner: idandaccess
- License: mit
- Created: 2017-11-08T19:30:28.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-02-23T22:23:23.000Z (11 months ago)
- Last Synced: 2024-03-14T18:03:19.042Z (10 months ago)
- Topics: authentication, authorization, azure, azure-functions, identity-management, javascript, jwt, oauth2
- Language: JavaScript
- Size: 470 KB
- Stars: 18
- Watchers: 2
- Forks: 6
- Open Issues: 39
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://idandaccess.visualstudio.com/azure-functions-auth/_apis/build/status/idandaccess.azure-functions-auth?branchName=master)](https://idandaccess.visualstudio.com/azure-functions-auth/_build/latest?definitionId=2&branchName=master)
# azure-functions-auth
Authentication and Authorization for Azure Functions (with OAuth 2.0 and JWT)## Configuration
```js
const validateJwt = require('azure-functions-auth')({
clientId: '',
clientSecret: '',
domain: '',
algorithms: ['RS256'],
});
```## Usage
### Callback Style
```js
module.exports = validateJwt(function(context, req) {
if (req.user) {
context.res = {
body: req.user
};
}
else {
context.res = {
status: 400,
body: "The user property is missing"
};
}
context.done();
});
```
In case of an invalid JWT `context.res` gets populated accordingly and `context.done()` gets called.### Async Style
```js
const main = (context, req) => {
context.log('token is valid. (you shouldn\'t log like that in production code)')
return new Promise(resolve => {
resolve('the function will return this exact string as body with a status code of 200')
}).then(asyncResult =>{
return asyncResult
})
}
module.exports = validateJwt(main, true)
```
In case of an invalid JWT a specific error and status code get returned. Make sure to have your function host is configured to use function's return value.```json
{
"bindings": [
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
```
Regarding the http output your `function.json` should look like the above.```js
module.exports = {
run: validateJwt(main, true),
main
}
```
In order to do tests, of course you still can export your functions.### Calling your function
Now when you make a call to the Http endpoint you'll need to add an Authorization header, e.g.:
```bash
GET https://functionsad5bb49d.azurewebsites.net/api/my-http-function?...
Authorization: Bearer the-access-token
```## Attribution
This code is based on [https://github.com/sandrinodimattia/azure-functions-auth0](https://github.com/sandrinodimattia/azure-functions-auth0)