Skip to content

Commit

Permalink
chore: move browser-extension-oauth2 to monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
shuowu committed Jul 3, 2020
1 parent 329fc07 commit 41439f7
Show file tree
Hide file tree
Showing 25 changed files with 3,297 additions and 73 deletions.
131 changes: 126 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/browser-extension-oauth2/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
["@babel/env", {"modules": false}]
]
}
21 changes: 21 additions & 0 deletions packages/browser-extension-oauth2/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"env": {
"browser": true,
"es6": true
},
"extends": ["eslint:recommended"],
"parser": "babel-eslint",
"plugins": [
"prettier"
],
"globals": {
"browser": "readonly"
},
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"rules": {
"semi": "error"
}
}
2 changes: 2 additions & 0 deletions packages/browser-extension-oauth2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
21 changes: 21 additions & 0 deletions packages/browser-extension-oauth2/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Shuo Wu

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.
64 changes: 64 additions & 0 deletions packages/browser-extension-oauth2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# browser-extension-oauth2

This module provides wrapper around browser.identity API for Oauth2.0 implicit flow. This module not only handles retrieving and renewing access token from authorization endpoints and properly cache it in `browser.storage.local`, but also provide enhanced fetch functon, `callApi`, to access protected resources with access token properly binded in headers.

## Installation

Please follow either of following options to use this module in your extension project.

### Build extension with bundler like webpack

```bash
npm install browser-extension-oauth2
```

### Add bundle file directly in `manifest.json`

- Clone repo

- Install dependencies

```bash
npm install
```

- Build bundle

```bash
npm run build
```

- Add `index.js` from dist folder to your extension project. Then

## Development

```bash
npm install
npm run dev
```

## Examples

```js
import Oauth2 from 'browser-extension-oauth2'

// Initial oauth2 instance
const oauth2 = new Oauth2({
provider: '{provide name}', // Provider name, this will be used in redirectUrl and as storage key
authorization_endpoint: '{oauth authorization_endpoint}',
client_id: '{registered client id in idp}',
scopes: ['{scope}'], // Scopes for api access
api_base_url: '{api base url}' // Optional, only relative paths need to be provided if callApi method if this config exist
});

// Call resource api, this method follows `fetch` API input with addtional renew token logic provided.
// If no access token available, prompt will popup to ask for user's consent.
oauth2.callApi('/protected-resource')
.then(data => {
// Handle resource
});
```

### License

MIT
Loading

0 comments on commit 41439f7

Please sign in to comment.