Skip to content

Commit ee157f5

Browse files
Major update towards version 2.0. Not complete yet, hopefully later this evening.
2 parents eaf5515 + 5cd9b07 commit ee157f5

File tree

21 files changed

+2948
-831
lines changed

21 files changed

+2948
-831
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
1+
node_modules
22
misc.txt
3+
bower_components

Gruntfile.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module.exports = function(grunt) {
2+
3+
// Project configuration.
4+
grunt.initConfig({
5+
pkg: grunt.file.readJSON('package.json'),
6+
// uglify: {
7+
// options: {
8+
// banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
9+
// },
10+
// build: {
11+
// src: 'src/<%= pkg.name %>.js',
12+
// dest: 'build/<%= pkg.name %>.min.js'
13+
// }
14+
// },
15+
requirejs: {
16+
compile: {
17+
options: {
18+
almond: true,
19+
dir: 'build',
20+
appDir: 'src',
21+
baseUrl: '.',
22+
modules: [{name: 'jso'}],
23+
optimize: "none",
24+
paths: {
25+
underscore: '../vendor/underscore',
26+
jquery : '../vendor/jquery',
27+
backbone : '../vendor/backbone'
28+
},
29+
wrap: {
30+
startFile: 'tools/wrap.start',
31+
endFile: 'tools/wrap.end'
32+
},
33+
preserveLicenseComments: false
34+
}
35+
}
36+
}
37+
});
38+
39+
40+
// grunt.loadNpmTasks('grunt-contrib-uglify');
41+
grunt.loadNpmTasks('grunt-requirejs');
42+
43+
// Default task(s).
44+
grunt.registerTask('default', ['requirejs']);
45+
46+
};

LICENCE.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Licence: Simplified BSD Licence
2+
3+
Copyright (c) 2013, Andreas Åkre Solberg, UNINETT AS
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
2. Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README-Phonegap.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
# Using JSO with Phonegap and ChildBrowser
22

3+
4+
**WARNING: JSO2 beta is not ready for integration with Phonegap and Childbrowser just yet. Stay tuned for updates on this. This document is old and documents integration with phonegap using JSO verson 1**
5+
6+
7+
38
Using JSO to perform OAuth 2.0 authorization in WebApps running on mobile devices in hybrid environment is an important deployment scenario for JSO.
49

510
Here is a detailed instruction on setting up JSO with Phonegap for iOS and configure OAuth 2.0 with Google. You may use it with Facebook or other OAuth providers as well.
611

712

813
# Preparations
914

10-
* Install XCode from App Store, and iOS development kit
11-
* Install [Phonegap 2.0, Cordova 2.0](http://phonegap.com/download)
15+
# * Install XCode from App Store, and iOS development kit
16+
# * Install [Phonegap 2.0, Cordova 2.0](http://phonegap.com/download)
17+
18+
19+
Install Cordova CLI:
20+
21+
sudo npm install -g cordova
22+
23+
24+
1225

1326

1427
# Setup App
1528

1629
To create a new App
1730

18-
./create /Users/andreas/Sites/cordovatest no.erlang.test "CordovaJSOTest"
31+
cordova create jsodemo no.uninett.jso-demo JSOdemo
32+
cd jsodemo/
33+
cordova platform add ios
34+
35+
36+
1937

2038
# Install ChildBrowser
2139

README.md

Lines changed: 59 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# JSO - a Javascript OAuth Library
22

3+
<<<<<<< HEAD
34
**UPDATE March 20th 2013: I just commited [JSO version 2.0, a redesigned library](https://github.com/andreassolberg/jso/tree/version2). It is only in beta state yet, but feel free to test it.**
45

56

67
This library was written by Andreas Åkre Solberg (UNINETT AS) in March 2012.
8+
=======
9+
The initial version 1 of this library was written by Andreas Åkre Solberg (UNINETT AS) in March 2012. **This is the beta release of JSO2, and redesigned and not well-tested version of the same library.** [Return to the stable version 1 of JSO](https://github.com/andreassolberg/jso)
10+
11+
>>>>>>> version2
712
813
* [Read the blog of Andreas Åkre Solberg](http://rnd.feide.no)
914
* [Follow Andreas Åkre Solberg on twitter](https://twitter.com/erlang)
@@ -20,21 +25,32 @@ If you want to use JSO together with Phonegap to support OAuth 2.0 in a hybrid w
2025

2126
* [JSO Phonegap Guide](README-Phonegap.md)
2227

28+
## Contributors
29+
30+
* [Robbie MacKay](https://github.com/rjmackay)
31+
2332

2433
## Licence
2534

35+
<<<<<<< HEAD
2636
UNINETT holds the copyright of the JSO library. The software can be used free of charge for both non-commercial and commercial projects.
2737

2838
The software is dual-licenced with *The GNU Lesser General Public License, version 2.1 (LGPL-2.1)* and *version 3.0*; meaning that you can select which of these two versions depending on your needs.
2939

3040
* <http://opensource.org/licenses/lgpl-2.1>
3141
* <http://opensource.org/licenses/LGPL-3.0>
42+
=======
43+
UNINETT holds the copyright of the JSO library. The software can be used free of charge for both non-commercial and commercial projects. The software is licenced with *Simplified BSD License*.
44+
45+
* <http://opensource.org/licenses/BSD-2-Clause>
46+
>>>>>>> version2
3247
3348

3449
## Features
3550

36-
* Implements OAuth 2.0 Implicit Flow. All you need is a single javascript file.
37-
* Supports the `bearer` access token type.
51+
* Implements OAuth 2.0 Implicit Flow.
52+
* AMD Loading
53+
* Supports the `Bearer` access token type.
3854
* No server component needed.
3955
* Adds a jQuery plugin extending the `$.ajax()` function with OAuth capabilities.
4056
* Can handle multilple providers at once.
@@ -45,7 +61,8 @@ The software is dual-licenced with *The GNU Lesser General Public License, versi
4561

4662
## Dependencies
4763

48-
JSO makes use of jQuery, mostly to plugin and make use of the `$.ajax()` function. If there is an interest for making JSO independent from jQuery, I can do that.
64+
JSO may make use of jQuery, mostly to plugin and make use of the `ajax()` function.
65+
4966

5067
## Browser support
5168

@@ -56,167 +73,83 @@ JSO uses JSON serialization functions (stringify and parse). These are supported
5673

5774
## Configure
5875

59-
First, you must configure your OAuth providers. You do that by calling `jso_configure` with a configuration object as a parameter.
6076

61-
The object is a key, value set of providers, where the providerID is an internal identifier of the provider that is used later, when doing protected calls.
77+
First, load JSO with requirejs:
78+
79+
```javascript
80+
var
81+
JSO = require('bower_components/jso/build/jso'),
82+
jQuery = require('jquery');
83+
OAuth.enablejQuery($);
84+
```
85+
86+
Loading jQuery is optional. If you load jQuery and want the `ajax()` function, you should run the enablejQuery function.
6287

63-
In this example, we set the provider identifier to be `facebook`.
88+
Next is configuring an OAuth object with the configuration of an OAuth Provider.
6489

6590
```javascript
66-
jso_configure({
67-
"facebook": {
68-
client_id: "xxxxxxxxxx",
69-
redirect_uri: "http://localhost/~andreas/jso/",
70-
authorization: "https://www.facebook.com/dialog/oauth",
71-
presenttoken: "qs"
72-
}
91+
var jso = new JSO({
92+
providerID: "google",
93+
client_id: "541950296471.apps.googleusercontent.com",
94+
redirect_uri: "http://bridge.uninett.no/jso/index.html",
95+
authorization: "https://accounts.google.com/o/oauth2/auth",
96+
scopes: { request: ["https://www.googleapis.com/auth/userinfo.profile"]}
7397
});
7498
```
7599

100+
Here is some of the parameters:
101+
102+
76103
* `client_id`: The client idenfier of your client that as trusted by the provider. As JSO uses the implicit grant flow, there is now use for a
77104
* `redirect_uri`: OPTIONAL (may be needed by the provider). The URI that the user will be redirected back to when completed. This shuold be the same URL that the page is presented on.
78105
* `presenttoken`: OPTIONAL How to present the token with the protected calls. Values can be `qs` (in query string) or `header` (default; in authorization header).
79106
* `default_lifetime` : OPTIONAL Seconds with default lifetime of an access token. If set to `false`, it means permanent.
80107
* `permanent_scope`: A scope that indicates that the lifetime of the access token is infinite. (not yet tested.)
81108
* `isDefault`: Some OAuth providers does not support the `state` parameter. When this parameter is missing, the consumer does not which provider that is sending the access_token. If you only provide one provider config, or set isDefault to `true` for one of them, the consumer will assume this is the provider that sent the token.
82109
* `scope`: For providers that does not support `state`: If state was not provided, and default provider contains a scope parameter we assume this is the one requested... Set this as the same list of scopes that you provide to `ensure_tokens`.
110+
* `scopes.request`: Control what scopes are requested in the authorization request.
83111

84112

85-
The second optional parameter, options, of `jso_configure(providerconfig, options)` allows you to configure these global settings:
86-
87-
* `debug`: Default value is `false`. If you enable debugging, JSO will log a bunch of things to the console, using `console.log` - if not, JSO will not log anything.
88-
89-
90-
## Authorization
91-
92-
This OPTIONAL step involves an early ensurance that all neccessary access tokens have been retreived.
93-
94-
95-
`jso_ensureTokens` can be used to force user authentication before you really need it; and the reason why you would typically do that is to make it easier to recover the state when you return. Typically if you need an OAuth token in the middle of a complex transaction it would be really difficult if the user is redirected away during that transaction, instead you can use `jso_ensureTokens` before starting with the transaction.
96113

97-
Using `jso_ensureTokens` is completely optional, and when you do not want to make sure that you have sufficient tokens before you really need it, then you can call `$.oajax` right away and it will redirect you for authenticationo - if needed.
114+
## Callback
98115

99-
100-
101-
102-
By doing a call like this early in your code:
116+
At the endpoint where the OAuth provider is redirecting back the user with the access token response, you need to run the callback(). This allows JSO to collect and parse the response.
103117

104118
```javascript
105-
// Make sure that you have
106-
jso_ensureTokens({
107-
"facebook": ["read_stream"],
108-
"google": ["https://www.googleapis.com/auth/userinfo.profile"]
109-
});
119+
jso.callback();
110120
```
111121

112-
the library will check its cached tokens, and if it does not have the specified tokens/scopes, it will start a new authorization process.
113-
114-
When this code is completed, you know that you have valid tokens for your use cases.
122+
Be aware to run the `callback()` function before your *router*, and before `o.getToken()` or `o.ajax()`.
115123

116-
The `jso_ensureTokens` function takes an object as input, with the providerids as keys, and the values are eigther `false` or an array of required scopes. A value of `false` mean that we do not care about scopes, but we want a valid token.
124+
The redirect_uri may very well be the same page that initates the authorization request.
117125

118126

119127
## OAuth protected data requests
120128

121-
To get data, you eigther use the `jso_getToken("facebook")` function, that returns a valid access token (or `null`), or you may use the `$.oajax()` function.
122129

123-
The `$.oajax()` function works very similar to `$.ajax()` ([see documentation](http://api.jquery.com/jQuery.ajax/)), actually the settings parameters are bypassed to the real `$.ajax()` function.
124-
125-
In addition to the settings properties allowed by `$.ajax()`, these properties are allowed:
126-
127-
* jso_provider: The providerid of the OAuth provider to use.
128-
* jso_allowia: Allow userinteraction? If you have prepared the tokens, using `jso_ensureTokens()` you might set this value to `false` (default) and it will trow an exception instead of starting a new authorization process.
129-
* jso_scopes: If this specific call requires one or more scopes, provide it here. It will be used to find a suitable token, if multiple exists.
130-
131-
Here is an example of retrieving the Facebook newsstream using OAuth:
130+
You may use the `o.ajax()` function to perform OAuth protected API calls.
132131

133132
```javascript
134-
$.oajax({
135-
url: "https://graph.facebook.com/me/home",
136-
jso_provider: "facebook",
137-
jso_scopes: ["read_stream"],
138-
jso_allowia: true,
133+
jso.ajax({
134+
url: "https://www.googleapis.com/oauth2/v1/userinfo",
135+
oauth: {
136+
scopes: {
137+
request: ["https://www.googleapis.com/auth/userinfo.email"],
138+
require: ["https://www.googleapis.com/auth/userinfo.email"]
139+
}
140+
},
139141
dataType: 'json',
140142
success: function(data) {
141-
console.log("Response (facebook):");
143+
console.log("Response (google):");
142144
console.log(data);
145+
$(".loader-hideOnLoad").hide();
143146
}
144147
});
145148
```
146149

147-
## jQuery or not jQuery
148-
149-
If you load jQuery before the JSO library, it will discover and add the `$.oajax` function. However, loading jQuery is optional, and if you do not load jQuery JSO will not complain, but neigther will if offer the easy to use `$.oajax` function.
150-
151-
If you do not use jQuery, you probably want to use the `jso_getToken(providerid, scopes)` function.
152-
153-
```javascript
154-
var accesstoken = jso_getToken("facebook", "read_stream");
155-
156-
var authzheader = "Authorization: Authorization " + accesstoken;
157-
// Perform the Cross site AJAX request using this custom header with your
158-
// preferred AJAX library.
159-
```
160-
161-
162-
163-
## Using JSO With Phonegap
164-
165-
Normal use of JSO involves JSO redirecting to the OAuth authorization endpoint for authentication and authorization, then the user is redirected back to the callback url where JSO autoamtically inspects the hash for an access token, and caches it.
166-
167-
When using JSO with phonegap (or similar libraries), you would not perform a normal redirect, but instead open a *childbrowser*. And when the user returns you would need to tell JSO what URL the childbrowser ended up on.
168-
169-
170-
**Register a custom URL redirect handler**
171-
172-
```javascript
173-
jso_registerRedirectHandler(function(url) {
174-
console.log("About to redirect the user to ", url);
175-
console.log("Instead we can do whatever we want, such as opening a child browser");
176-
177-
// Open a child browser or similar.
178-
});
179-
```
180-
*Please help! I have not used phonegap my self, and if someone could provide exact code examples for use with phonegap I would appreciate that.*
181-
182-
183-
**Tell JSO about the return URL**
184-
185-
Use the following function providing the url of the callback page, including the parameters in the hash: `jso_checkfortoken(providerid, url)`
186-
187-
The provided parameters might be like this:
188-
189-
* `jso_checkfortoken('facebook', 'https://yourservice.org/callback#accesstoken=lsdkfjldkfj')`
190-
191-
192-
193-
194-
## Some convenient debugging functions
195-
196-
For debugging, open the javascript console. And you might type:
197-
198-
199-
```javascript
200-
jso_dump();
201-
```
202-
203-
to list all cached tokens, and
204-
205-
```javascript
206-
jso_wipe();
207-
```
208-
209-
to remove all tokens.
210-
211-
212-
213-
## Upgrade
214-
215-
This section will contain useful information if you have been using JSO already, and would like to update to the latest version. API and configuration changes will be listed here.
216-
217-
218-
150+
`o.ajax()` wraps the `$.ajax()` function with one single additional optional option property `oauth`.
219151

152+
Currently, only the `scopes` property is included. It adds to the scopes property in the initial provider configuration.
220153

221154

222155

0 commit comments

Comments
 (0)