├── Demos ├── login.php ├── index.html ├── jquery.browserid.js └── login.class.php ├── Source ├── login.php ├── jquery.browserid-min.js ├── jquery.browserid.js └── login.class.php ├── Docs └── BrowserID.md └── README.md /Demos/login.php: -------------------------------------------------------------------------------- 1 | verify_assertion()) { 8 | 9 | echo json_encode(array('status'=>'okay', 'email'=>$browserID->getEmail())); 10 | 11 | } else { 12 | 13 | echo json_encode(array('status'=>'failure','reason'=>$browserID->getReason())); 14 | } 15 | 16 | ?> -------------------------------------------------------------------------------- /Source/login.php: -------------------------------------------------------------------------------- 1 | verify_assertion()) { 8 | 9 | echo json_encode(array('status'=>'okay', 'email'=>$browserID->getEmail())); 10 | 11 | } else { 12 | 13 | echo json_encode(array('status'=>'failure','reason'=>$browserID->getReason())); 14 | } 15 | 16 | ?> -------------------------------------------------------------------------------- /Source/jquery.browserid-min.js: -------------------------------------------------------------------------------- 1 | (function(e){var t={assertion:null,email:null,login_button_class:"browserid-login",logout_button_class:"browserid-logout",onlogin:null,onfail:null,onlogout:null,server:"login.php"},n={init:function(n){typeof n=="object"&&e.extend(t,n);this.addClass(t.login_button_class).on("click.login",r)},getEmail:function(){return t.email}},r=function(){var n=e(this);navigator.id.get(function(e){if(e){t.assertion=e;s.apply(n)}else typeof t.onfail=="function"&&t.onfail.apply(this)})},i=function(){navigator.id.logout();t.assertion=null;t.email=null;e(this).removeClass(t.logout_button_class).removeClass(t.login_button_class);n.init.apply(e(this));typeof t.onlogout=="function"&&t.onlogout.apply(this)},s=function(){var n=this;e.getJSON(t.server,{assertion:t.assertion},function(e){if(e.status&&e.status=="okay"){t.email=e.email;n.removeClass(t.login_button_class).addClass("options.logout_button_class").text("Logout ("+e.email+")");n.off("click.login").on("click.logout",i);typeof t.onlogin=="function"&&t.onlogin.apply(this)}else e.status!=="okay"&&typeof t.onfail=="function"&&t.onfail.apply(e)})};e.fn.browserID=function(e){if(n[e])return n[e].apply(this,Array.prototype.slice.call(arguments,1));if(typeof e=="object"||!e)return n.init.apply(this,arguments)}})(jQuery); -------------------------------------------------------------------------------- /Docs/BrowserID.md: -------------------------------------------------------------------------------- 1 | Class: BrowserID {#BrowserID} 2 | ============================== 3 | 4 | This is a jquery plugin for the BrowserID Protocol. BrowserID is a new way for users to log into web sites using their email address. 5 | It aims to provide a secure way of proving your identity to servers across the internet, without having to create separate usernames and passwords each time. 6 | Instead of a new username, it uses your email address as you identity which allows it to be descentralized since anyone can send you an 7 | email verification message. 8 | 9 | ### Syntax: 10 | ``` 11 | $('').browserid([options]); 12 | ``` 13 | ### Arguments: 14 | 15 | - options `object` - The options for the BrowserID instance. 16 | 17 | ####Example options : 18 | ``` 19 | var options = { 20 | onlogin : function(response){ ... }, 21 | onfail : function(response){ ... }, 22 | onlogout : function(response){ ... }, 23 | server : 'login.php' /* this is the verifier server url - attached in login.php */ 24 | } 25 | ``` 26 | ### Arguments 27 | 28 | - `object` The verifier will check that the assertion was meant for your website and is valid 29 | returns => {status: 'okay','email': 'user@mozilla.com'}, 30 | otherwise returns {status: 'failure','reason': 'audience missmatch'}, 31 | 32 | -------------------------------------------------------------------------------- /Demos/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Demo for jquery-browserID plugin 5 | 6 | 19 | 20 | 21 | 22 |

Demo for jquery-browserID plugin

23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 42 | 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | BrowserID 2 | ========= 3 | 4 | This is a jQuery client library for the BrowserID Protocol. BrowserID is a new way for users to log into web sites using their email address. 5 | It aims to provide a secure way of proving your identity to servers across the internet, without having to create separate usernames and passwords each time. 6 | Instead of a new username, it uses your email address as you identity which allows it to be descentralized since anyone can send you an 7 | email verification message. 8 | 9 | ![Screenshot](https://developer.mozilla.org/@api/deki/files/6051/=browserid-enter-email.png) 10 | 11 | #[DEMO](http://htmlpreview.github.com/?https://github.com/altryne/browserID-jQuery/blob/master/Demos/index.html) 12 | How to Use 13 | ---------- 14 | 15 | ###Include the BrowserID include.js library in your site by adding the following script tags to your pages: 16 | ``` 17 | 18 | 19 | 20 | ``` 21 | 22 | ###Adding a login button: 23 | 24 | ``` 25 | 26 | ``` 27 | 28 | ###on DOM ready initiate 29 | ``` 30 | $('').browserID([options]); 31 | ``` 32 | 33 | ### Arguments: 34 | 35 | - options `object` - The options for the BrowserID instance. 36 | 37 | ####Example options : 38 | ``` 39 | var options = { 40 | onlogin : function(response){ ... }, 41 | onfail : function(response){ ... }, 42 | onlogout : function(response){ ... }, 43 | server : 'login.php' /* this is the verifier server url - attached in login.php */ 44 | } 45 | ``` 46 | 47 | References: 48 | 49 | - https://browserid.org/ 50 | - https://developer.mozilla.org/en/BrowserID 51 | - https://browserid.org/developers 52 | - http://identity.mozilla.com/post/7616727542/introducing-browserid-a-better-way-to-sign-in 53 | - http://identity.mozilla.com/post/17207734786/id-provider-support-now-live-on-browserid 54 | - https://github.com/mozilla/browserid/wiki 55 | - https://github.com/thinkphp/browserID-MooTools 56 | -------------------------------------------------------------------------------- /Demos/jquery.browserid.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Description : This is a jQuery client library for the BrowserID Protocol. 3 | * Created by Alex Wolkov (alexw.me) . 4 | */ 5 | 6 | (function ($) { 7 | var options = { 8 | assertion : null, 9 | email : null, 10 | login_button_class : 'browserid-login', 11 | logout_button_class : 'browserid-logout', 12 | onlogin : null, 13 | onfail : null, 14 | onlogout : null, 15 | server : 'login.php' 16 | } 17 | var methods = { 18 | init : function (settings) { 19 | if(typeof settings === 'object'){ 20 | $.extend(options, settings); 21 | } 22 | this.addClass(options.login_button_class).on('click.login', _login); 23 | }, 24 | getEmail:function () { 25 | return options.email; 26 | } 27 | } 28 | var _login = function () { 29 | var $el = $(this); 30 | navigator.id.get(function (assertion) { 31 | if (assertion) { 32 | options.assertion = assertion; 33 | //got an assertion, now send it up to the server for verification 34 | _verify_assertion.apply($el); 35 | }else{ 36 | if(typeof options.onfail === 'function') { 37 | options.onfail.apply(this); 38 | } 39 | } 40 | }); 41 | } 42 | 43 | var _logout = function () { 44 | navigator.id.logout(); 45 | options.assertion = null; 46 | options.email = null; 47 | $(this).removeClass(options.logout_button_class).removeClass(options.login_button_class); 48 | 49 | methods.init.apply($(this)); 50 | if(typeof options.onlogout === 'function') { 51 | options.onlogout.apply(this); 52 | } 53 | } 54 | 55 | var _verify_assertion = function(){ 56 | var $el = this; 57 | 58 | $.getJSON(options.server, {assertion:options.assertion}, function (response) { 59 | if(response.status && response.status == 'okay'){ 60 | options.email = response.email; 61 | $el.removeClass(options.login_button_class).addClass('options.logout_button_class').text('Logout ('+ response.email +')'); 62 | $el.off('click.login').on('click.logout',_logout); 63 | 64 | if(typeof options.onlogin === 'function') { 65 | options.onlogin.apply(this); 66 | } 67 | }else if(response.status !== 'okay'){ 68 | if(typeof options.onfail === 'function') { 69 | options.onfail.apply(response); 70 | } 71 | } 72 | }) 73 | } 74 | 75 | $.fn.browserID = function (method) { 76 | 77 | // Method calling logic 78 | if (methods[method]) { 79 | 80 | return methods[ method ].apply(this, Array.prototype.slice.call(arguments, 1)); 81 | } else if (typeof method === 'object' || !method) { 82 | 83 | return methods.init.apply(this, arguments); 84 | } 85 | } 86 | })(jQuery); 87 | -------------------------------------------------------------------------------- /Source/jquery.browserid.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Description : This is a jQuery client library for the BrowserID Protocol. 3 | * Created by Alex Wolkov (alexw.me) . 4 | */ 5 | 6 | (function ($) { 7 | var options = { 8 | assertion : null, 9 | email : null, 10 | login_button_class : 'browserid-login', 11 | logout_button_class : 'browserid-logout', 12 | onlogin : null, 13 | onfail : null, 14 | onlogout : null, 15 | server : 'login.php' 16 | } 17 | var methods = { 18 | init : function (settings) { 19 | if(typeof settings === 'object'){ 20 | $.extend(options, settings); 21 | } 22 | this.addClass(options.login_button_class).on('click.login', _login); 23 | }, 24 | getEmail:function () { 25 | return options.email; 26 | } 27 | } 28 | var _login = function () { 29 | var $el = $(this); 30 | navigator.id.get(function (assertion) { 31 | if (assertion) { 32 | options.assertion = assertion; 33 | //got an assertion, now send it up to the server for verification 34 | _verify_assertion.apply($el); 35 | }else{ 36 | if(typeof options.onfail === 'function') { 37 | options.onfail.apply(this); 38 | } 39 | } 40 | }); 41 | } 42 | 43 | var _logout = function () { 44 | navigator.id.logout(); 45 | options.assertion = null; 46 | options.email = null; 47 | $(this).removeClass(options.logout_button_class).removeClass(options.login_button_class); 48 | 49 | methods.init.apply($(this)); 50 | if(typeof options.onlogout === 'function') { 51 | options.onlogout.apply(this); 52 | } 53 | } 54 | 55 | var _verify_assertion = function(){ 56 | var $el = this; 57 | 58 | $.getJSON(options.server, {assertion:options.assertion}, function (response) { 59 | if(response.status && response.status == 'okay'){ 60 | options.email = response.email; 61 | $el.removeClass(options.login_button_class).addClass(options.logout_button_class).text('Logout ('+ response.email +')'); 62 | $el.off('click.login').on('click.logout',_logout); 63 | 64 | if(typeof options.onlogin === 'function') { 65 | options.onlogin.apply(this); 66 | } 67 | }else if(response.status !== 'okay'){ 68 | if(typeof options.onfail === 'function') { 69 | options.onfail.apply(response); 70 | } 71 | } 72 | }) 73 | } 74 | 75 | $.fn.browserID = function (method) { 76 | 77 | // Method calling logic 78 | if (methods[method]) { 79 | 80 | return methods[ method ].apply(this, Array.prototype.slice.call(arguments, 1)); 81 | } else if (typeof method === 'object' || !method) { 82 | 83 | return methods.init.apply(this, arguments); 84 | } 85 | } 86 | })(jQuery); 87 | -------------------------------------------------------------------------------- /Demos/login.class.php: -------------------------------------------------------------------------------- 1 | audience = $audience; 51 | $this->assertion = $assertion; 52 | } 53 | 54 | 55 | /** 56 | * Get email address of the user 57 | * @param None 58 | * @return String return email address 59 | * @public access 60 | */ 61 | public function getEmail() { 62 | 63 | return $this->email; 64 | } 65 | 66 | /** 67 | * Get expiration timestamp 68 | * @param None 69 | * @return integer expiration timestamp 70 | * @public access 71 | */ 72 | public function getExpires() { 73 | 74 | return $this->expires; 75 | } 76 | 77 | /** 78 | * Get the entity who issued the assertion 79 | * @param None 80 | * @return String the entity who issued the assertion 81 | * @public access 82 | */ 83 | public function getIssuer() { 84 | 85 | return $this->issuer; 86 | } 87 | 88 | 89 | /** 90 | * Get the reason if any! 91 | * @param None 92 | * @return String the reason why the assertion is failed 93 | * @public access 94 | */ 95 | public function getReason() { 96 | 97 | return $this->reason; 98 | } 99 | 100 | /** 101 | * Makes an HTTP POST Request to verification endpoint 102 | * @param String Endpoint Server 103 | * @param Array the data to be sent to the endpoint 104 | * @return Object returns an object verification response 105 | * @private access 106 | */ 107 | private function _requestPOST($url, $data) { 108 | 109 | $ch = curl_init(); 110 | 111 | curl_setopt($ch, CURLOPT_URL,$url); 112 | curl_setopt($ch, CURLOPT_POST, true); 113 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 114 | curl_setopt($ch, CURLOPT_HEADER, false); 115 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 116 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 117 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 118 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 119 | curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 120 | 121 | $response = curl_exec($ch); 122 | 123 | $infos = curl_getinfo($ch); 124 | 125 | curl_close($ch); 126 | 127 | if(false === $response) { 128 | throw new Exception(sprintf("Faild to connect to the %s verifier", $url)); 129 | } 130 | 131 | $json_decoded = json_decode($response); 132 | 133 | if(!$json_decoded) { 134 | throw new Exception(sprintf("JSON Response from %s is not valid", $url)); 135 | } 136 | 137 | //for debug 138 | //echo"
"
139 |                   //print_r($infos); 
140 |                   //echo"
" 141 | 142 | return $json_decoded; 143 | } 144 | 145 | /** 146 | * With this method you must verify the assertion is authentic and extract the email address from it. 147 | * @public access 148 | * @return Object - returns an object as response from service with the following attributes: 149 | * 1)status Okay 150 | * 2)email mergesortv@gmail.com 151 | * 3)audience https://mysite.com 152 | * 4)expires 1308859352261 153 | * 5)issuer "login.persona.org" 154 | */ 155 | public function verify_assertion() { 156 | 157 | $params = json_encode(array('assertion'=>$this->assertion, 158 | 'audience'=>$this->audience)); 159 | 160 | $output = $this->_requestPOST(self::endpoint, $params); 161 | 162 | //for debug 163 | //print_r($output); 164 | 165 | if(isset($output->status) && $output->status == 'okay') { 166 | 167 | $this->email = $output->email; 168 | $this->expires = $output->expires; 169 | $this->issuer = $output->issuer; 170 | 171 | return true; 172 | 173 | } else { 174 | 175 | $this->reason = $output->reason; 176 | 177 | return false; 178 | } 179 | } 180 | } 181 | 182 | ?> 183 | -------------------------------------------------------------------------------- /Source/login.class.php: -------------------------------------------------------------------------------- 1 | audience = $audience; 51 | $this->assertion = $assertion; 52 | } 53 | 54 | 55 | /** 56 | * Get email address of the user 57 | * @param None 58 | * @return String return email address 59 | * @public access 60 | */ 61 | public function getEmail() { 62 | 63 | return $this->email; 64 | } 65 | 66 | /** 67 | * Get expiration timestamp 68 | * @param None 69 | * @return integer expiration timestamp 70 | * @public access 71 | */ 72 | public function getExpires() { 73 | 74 | return $this->expires; 75 | } 76 | 77 | /** 78 | * Get the entity who issued the assertion 79 | * @param None 80 | * @return String the entity who issued the assertion 81 | * @public access 82 | */ 83 | public function getIssuer() { 84 | 85 | return $this->issuer; 86 | } 87 | 88 | 89 | /** 90 | * Get the reason if any! 91 | * @param None 92 | * @return String the reason why the assertion is failed 93 | * @public access 94 | */ 95 | public function getReason() { 96 | 97 | return $this->reason; 98 | } 99 | 100 | /** 101 | * Makes an HTTP POST Request to verification endpoint 102 | * @param String Endpoint Server 103 | * @param Array the data to be sent to the endpoint 104 | * @return Object returns an object verification response 105 | * @private access 106 | */ 107 | private function _requestPOST($url, $data) { 108 | 109 | $ch = curl_init(); 110 | 111 | curl_setopt($ch, CURLOPT_URL,$url); 112 | curl_setopt($ch, CURLOPT_POST, true); 113 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 114 | curl_setopt($ch, CURLOPT_HEADER, false); 115 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 116 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 117 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 118 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 119 | curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 120 | 121 | $response = curl_exec($ch); 122 | 123 | $infos = curl_getinfo($ch); 124 | 125 | curl_close($ch); 126 | 127 | if(false === $response) { 128 | throw new Exception(sprintf("Faild to connect to the %s verifier", $url)); 129 | } 130 | 131 | $json_decoded = json_decode($response); 132 | 133 | if(!$json_decoded) { 134 | throw new Exception(sprintf("JSON Response from %s is not valid", $url)); 135 | } 136 | 137 | //for debug 138 | //echo"
"
139 |                   //print_r($infos); 
140 |                   //echo"
" 141 | 142 | return $json_decoded; 143 | } 144 | 145 | /** 146 | * With this method you must verify the assertion is authentic and extract the email address from it. 147 | * @public access 148 | * @return Object - returns an object as response from service with the following attributes: 149 | * 1)status Okay 150 | * 2)email mergesortv@gmail.com 151 | * 3)audience https://mysite.com 152 | * 4)expires 1308859352261 153 | * 5)issuer "login.persona.org" 154 | */ 155 | public function verify_assertion() { 156 | 157 | $params = json_encode(array('assertion'=>$this->assertion, 158 | 'audience'=>$this->audience)); 159 | 160 | $output = $this->_requestPOST(self::endpoint, $params); 161 | 162 | //for debug 163 | //print_r($output); 164 | 165 | if(isset($output->status) && $output->status == 'okay') { 166 | 167 | $this->email = $output->email; 168 | $this->expires = $output->expires; 169 | $this->issuer = $output->issuer; 170 | 171 | return true; 172 | 173 | } else { 174 | 175 | $this->reason = $output->reason; 176 | 177 | return false; 178 | } 179 | } 180 | } 181 | 182 | ?> 183 | --------------------------------------------------------------------------------