forked from m2sd/workbox-plugin-firebase-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Michael Marcenich <[email protected]>
- Loading branch information
0 parents
commit 4bb2781
Showing
11 changed files
with
5,173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# editorconfig.org | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"es6": true | ||
}, | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/eslint-recommended", | ||
"plugin:@typescript-eslint/recommended", | ||
"plugin:prettier/recommended", | ||
"prettier/@typescript-eslint" | ||
], | ||
"globals": { | ||
"Atomics": "readonly", | ||
"SharedArrayBuffer": "readonly" | ||
}, | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"ecmaVersion": 2018, | ||
"sourceType": "module" | ||
}, | ||
"plugins": [ | ||
"@typescript-eslint", | ||
"prettier" | ||
], | ||
"rules": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
|
||
# Diagnostic reports (https://nodejs.org/api/report.html) | ||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
*.lcov | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# Snowpack dependency directory (https://snowpack.dev/) | ||
web_modules/ | ||
|
||
# TypeScript cache | ||
*.tsbuildinfo | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Microbundle cache | ||
.rpt2_cache/ | ||
.rts2_cache_cjs/ | ||
.rts2_cache_es/ | ||
.rts2_cache_umd/ | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
.env.test | ||
|
||
# parcel-bundler cache (https://parceljs.org/) | ||
.cache | ||
.parcel-cache | ||
|
||
# Next.js build output | ||
.next | ||
out | ||
|
||
# Nuxt.js build / generate output | ||
.nuxt | ||
dist | ||
|
||
# Gatsby files | ||
.cache/ | ||
# Comment in the public line in if your project uses Gatsby and not Next.js | ||
# https://nextjs.org/blog/next-9-1#public-directory-support | ||
# public | ||
|
||
# vuepress build output | ||
.vuepress/dist | ||
|
||
# Serverless directories | ||
.serverless/ | ||
|
||
# FuseBox cache | ||
.fusebox/ | ||
|
||
# DynamoDB Local files | ||
.dynamodb/ | ||
|
||
# TernJS port file | ||
.tern-port | ||
|
||
# Stores VSCode versions used for testing VSCode extensions | ||
.vscode-test | ||
|
||
# yarn v2 | ||
|
||
.yarn/cache | ||
.yarn/unplugged | ||
.yarn/build-state.yml | ||
.pnp.* | ||
|
||
/lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"semi": false, | ||
"arrowParens": "avoid", | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Michael Marcenich | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# Workbox plugin for firebase auth | ||
|
||
This is a simple plugin for workbox strategies which adds an `Authorization: Bearer` header with the return value from [`firebase.User.getIdToken(true)`](https://firebase.google.com/docs/reference/js/firebase.User#getidtoken) to the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) if a firebase User is authenticated (i.e. [`firebase.auth.Auth.onAuthStateChanged()`](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onauthstatechanged) returns a [`firebase.User`](https://firebase.google.com/docs/reference/js/firebase.User)). | ||
|
||
This way you are able to cache authorized routes without relying on a session cookie or an API token. | ||
|
||
## Usage | ||
|
||
### Module | ||
|
||
Use the module if: | ||
|
||
- you are building your service worker [using a bundler](https://developers.google.com/web/tools/workbox/guides/using-bundlers). | ||
- you are generating your service worker with [workbox-build](https://developers.google.com/web/tools/workbox/modules/workbox-build) | ||
|
||
1. Add the dependency: | ||
|
||
```sh | ||
npm i workbox-firebase-auth // or yarn add workbox-firebase-auth | ||
``` | ||
|
||
2. Import the plugin and use it for your strategies | ||
|
||
Example: | ||
|
||
```js | ||
import {registerRoute} from 'workbox-routing/registerRoute.mjs'; | ||
import {NetworkFirst} from 'workbox-strategies/NetworkFirst.mjs'; | ||
import FirebaseAuthPlugin from 'workbox-plugin-firebase-auth'; | ||
|
||
registerRoute( | ||
/\.(?:png|gif|jpg|jpeg|svg)$/, | ||
new NetworkFirst({ | ||
cacheName: 'authorizedApi', | ||
plugins: [ | ||
new FirebaseAuthPlugin({ | ||
firebase: { | ||
config: { /* your firebase config */ } | ||
} | ||
}), | ||
], | ||
}), | ||
); | ||
``` | ||
|
||
### CDN | ||
|
||
If you are using [workbox-sw](https://developers.google.com/web/tools/workbox/modules/workbox-sw) to import workbox, you can use the [unpkg CDN](https://unpkg.com/) to import the plugin. | ||
It will then be available under the global variable `WorkboxPluginFirebaseAuth`. | ||
|
||
Example: | ||
|
||
```js | ||
importScripts( | ||
'https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js', | ||
'https://unpkg.com/workbox-plugin-firebase-auth' | ||
) | ||
|
||
workbox.routing.registerRoute( | ||
/\.(?:png|gif|jpg|jpeg|svg)$/, | ||
new workbox.strategies.CacheFirst({ | ||
cacheName: 'authorizedApi', | ||
plugins: [ | ||
new WorkboxPluginFirebaseAuth({ | ||
firebase: { | ||
config: { /* your firebase config */ } | ||
} | ||
}), | ||
], | ||
}), | ||
) | ||
``` | ||
|
||
## Options | ||
|
||
If your service worker is served from firebase hosting, associated with the firebase app you use to authorize users, you can omit configuration altogether. | ||
Otherwise the [`firebase.config`](#firebaseconfig) parameter is **REQUIRED**. | ||
|
||
### firebase | ||
|
||
This key is used to configure the firebase instance. | ||
|
||
#### firebase.config | ||
|
||
**Required:** If your service worker is NOT served from firebase hosting or if you use a different app to authorize users. | ||
**Type:** `object` | ||
|
||
The [firebase config object](https://firebase.google.com/docs/web/setup?authuser=0#config-object) from the app that you use to authorize your users. | ||
|
||
#### firebase.version | ||
|
||
**Type:** `string` | ||
**Default:** `7.14.2` | ||
|
||
This key can be used to specify the firebase version to use. | ||
|
||
> **Note:** This is currently a hard coded fallback and will manually be updated. | ||
> In the future this should use the version form the dependency used for development. | ||
> Sadly I haven't figured out how to achieve this yet (PRs welcome :sweat_smile:) | ||
### awaitResponse | ||
|
||
**Type:** `boolean` | ||
**Default:** `false` | ||
|
||
If true the plugin will await the fetch to go through and check if the response has a 401 status before attaching the authorization and resending the request. | ||
Be aware that this happens before the response is passed to the caching strategy, please plan accordingly (e.g. a cache first strategy might serve authorized content to non authorized users). | ||
|
||
> **Note:** Please make sure your server responds to unauthorized requests with a 401 status code, so that the plugin can correctly identify authorization failures. | ||
### constraints | ||
|
||
This key can be used to specify additional constraints on top of the route matcher. | ||
|
||
#### constraints.types | ||
|
||
**Type:** `string | string[]` | ||
**Default:** `['*']` | ||
|
||
This can be used to authorize only requests that accept certain types of responses (e.g. `application/json`) | ||
|
||
> **Note:** This simply matches the entries from the `Accept` request header against the passed array/string. | ||
> As of yet group matching is not supported (e.g. `text/*` will not match `text/html`) | ||
#### constraints.https | ||
|
||
**Type:** `boolean` | ||
**Default:** `false` | ||
|
||
Only allow requests from secure origins (`https://` or `localhost`) to be authorized. | ||
|
||
#### constraints.ignorePaths | ||
|
||
**Type:** `(string | RegExp)[]` | ||
**Default:** `[]` | ||
|
||
Paths to ignore when authorizing requests. | ||
|
||
> **Note:** Checks against the pathname of the request (e.g. `/api/some-resource`) | ||
> If the argument is a `string` a request will be ignored if the pathname starts with that `string`. | ||
> If the argument is a `RegExp` the leading `'/'` will be stripped from the pathname and it will be checked against the `RegExp`. |
Oops, something went wrong.