├── .gitignore ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.txt ├── README.md ├── injection.js ├── loading.gif ├── oauth2.html ├── oauth2.js └── script.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | Change Log 2 | ========== 3 | 4 | Version 2.0.0 *(2013-06-23)* 5 | ---------------------------- 6 | Release updated version with new API. -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Contributing 2 | ============ 3 | If you would like to contribute please fork the chrome-ex-oauth2 project from GitHub and send a pull request. 4 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2012 JJ Ford 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Chrome Extension OAuth2 Library 2 | =============================== 3 | Provides a simple way to retrieve an OAuth2 token for API authorization within a Google Chrome Extension. 4 | 5 | 6 | How To Use 7 | ---------- 8 | 1. Add the following to your extension manifest: 9 | 10 | Note: The `permissions` url and `content_script` > `matches` URL are determined by the API you are requesting authorization for. 11 | 12 | ```text 13 | { 14 | ... 15 | ... 16 | 17 | "permissions":{ 18 | "https://github.com/login/oauth/access_token", 19 | "tabs" 20 | }, 21 | 22 | "content_scripts":[{ 23 | "matches":["https://github.com/robots.txt*"], 24 | "js":["libs/chrome-ex-oauth2/injection.js"], 25 | "run_at":"document_start" 26 | }], 27 | 28 | ... 29 | ... 30 | } 31 | ``` 32 | 33 | 2. Add application and API information to `libs/chrome-ex-auth/oauth2.js`: 34 | 35 | ```javascript 36 | (function() { 37 | window.oauth2 = { 38 | 39 | access_token_url: "{your-access-token-url}", 40 | authorization_url: "{your-authorization-url}", 41 | client_id: "{your-client-id}", 42 | client_secret: "{your-client-secret}", 43 | redirect_url: "{your-redirect-url}", 44 | scopes: [{your-array-of-scopes}], 45 | 46 | ... 47 | ... 48 | 49 | })(); 50 | 51 | ``` 52 | 53 | 54 | 3. Include the authorization script in your project: 55 | 56 | ```html 57 | 58 | ... 59 | ... 60 | 61 | ... 62 | ... 63 | 64 | 65 | 66 | ... 67 | ... 68 | 69 | 70 | ``` 71 | 72 | 4. To authorize the application from your script: 73 | 74 | ```javascript 75 | window.oauth2.start(); 76 | ``` 77 | 78 | 5. Include attribution to library. 79 | 80 | API 81 | --- 82 | 83 | >**start()** 84 | >

85 | >Starts the authorization process. 86 | 87 |
88 | 89 | >**getToken()** 90 | >

91 | >Retrieves the applications authorization token from the browsers local storage. 92 | 93 |
94 | 95 | >**clearToken()** 96 | >

97 | >Clears the applications token from the browsers local storage. 98 | 99 | -- 100 | This library has only been tested with the [GitHub API v3](http://developer.github.com/v3/) 101 | -------------------------------------------------------------------------------- /injection.js: -------------------------------------------------------------------------------- 1 | window.location = chrome.extension.getURL('libs/chrome-ex-oauth2/oauth2.html') + window.location.href.substring(window.location.href.indexOf('?')); -------------------------------------------------------------------------------- /loading.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jjNford/chrome-ex-oauth2/88c087797a2b4fb24d2e56f66d89533975bf6be3/loading.gif -------------------------------------------------------------------------------- /oauth2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Authorizing 6 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /oauth2.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | window.oauth2 = { 3 | 4 | access_token_url: "https://github.com/login/oauth/access_token", 5 | authorization_url: "https://github.com/login/oauth/authorize", 6 | client_id: "911fa741a8b8dac7d28c", 7 | client_secret: "e13f2f8ba4d9892eb231b4fcf3257013736327d1", 8 | redirect_url: "https://github.com/robots.txt", 9 | scopes: [], 10 | 11 | key: "oauth2_token", 12 | 13 | /** 14 | * Starts the authorization process. 15 | */ 16 | start: function() { 17 | window.close(); 18 | var url = this.authorization_url + "?client_id=" + this.client_id + "&redirect_uri=" + this.redirect_url + "&scopes="; 19 | for(var i in this.scopes) { 20 | url += this.scopes[i]; 21 | } 22 | chrome.tabs.create({url: url, active: true}); 23 | }, 24 | 25 | /** 26 | * Finishes the oauth2 process by exchanging the given authorization code for an 27 | * authorization token. The authroiztion token is saved to the browsers local storage. 28 | * If the redirect page does not return an authorization code or an error occures when 29 | * exchanging the authorization code for an authorization token then the oauth2 process dies 30 | * and the authorization tab is closed. 31 | * 32 | * @param url The url of the redirect page specified in the authorization request. 33 | */ 34 | finish: function(url) { 35 | 36 | function removeTab() { 37 | chrome.tabs.getCurrent(function(tab) { 38 | chrome.tabs.remove(tab.id); 39 | }); 40 | }; 41 | 42 | if(url.match(/\?error=(.+)/)) { 43 | removeTab(); 44 | } else { 45 | var code = url.match(/\?code=([\w\/\-]+)/)[1]; 46 | 47 | var that = this; 48 | var data = new FormData(); 49 | data.append('client_id', this.client_id); 50 | data.append('client_secret', this.client_secret); 51 | data.append('code', code); 52 | 53 | // Send request for authorization token. 54 | var xhr = new XMLHttpRequest(); 55 | xhr.addEventListener('readystatechange', function(event) { 56 | if(xhr.readyState == 4) { 57 | if(xhr.status == 200) { 58 | if(xhr.responseText.match(/error=/)) { 59 | removeTab(); 60 | } else { 61 | var token = xhr.responseText.match(/access_token=([^&]*)/)[1]; 62 | window.localStorage.setItem(that.key, token); 63 | removeTab(); 64 | } 65 | } else { 66 | removeTab(); 67 | } 68 | } 69 | }); 70 | xhr.open('POST', this.access_token_url, true); 71 | xhr.send(data); 72 | } 73 | }, 74 | 75 | /** 76 | * Retreives the authorization token from local storage. 77 | * 78 | * @return Authorization token if it exists, null if not. 79 | */ 80 | getToken: function() { 81 | return window.localStorage.getItem(this.key); 82 | }, 83 | 84 | /** 85 | * Clears the authorization token from the local storage. 86 | */ 87 | clearToken: function() { 88 | delete window.localStorage.removeItem(this.key); 89 | } 90 | } 91 | })(); -------------------------------------------------------------------------------- /script.js: -------------------------------------------------------------------------------- 1 | window.oauth2.finish(window.location.href); --------------------------------------------------------------------------------