').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 |
--------------------------------------------------------------------------------