-
Notifications
You must be signed in to change notification settings - Fork 0
/
cordova.oauth2.js
137 lines (128 loc) · 5.47 KB
/
cordova.oauth2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* cordova.oauth2.js - v0.1.1
*
* jQuery plugin to do Oauth2 login using either authorization code
* grant or implicit grant method in a Cordova application
*
* Usage:
* $.oauth2(options, successCallback, errorCallback);
*
* $.oauth2({
* auth_url: '', // required
* response_type: '', // required
* token_url: '', // required if response_type = 'code'
* logout_url: '', // recommended if available
* client_id: '', // required
* client_secret: '', // required if response_type = 'code'
* redirect_uri: '', // required - some dummy url
* other_params: {} // optional params object for scope, state, display...
* }, function(token, response){
* // do something with token and response
* }, function(error){
* // do something with error
* });
*
*
*
*
*/
(function($){
$.oauth2 = function (options, successCallback, errorCallback) {
// checks if all the required oauth2 params are defined
var checkOauth2Params = function(options){
var missing = "";
if(!options.client_id) {missing += " client_id"}
if(!options.auth_url) {missing += " auth_url"}
if(!options.response_type) {missing += " response_type"}
if(!options.client_secret && options.response_type == "code") {missing += " client_secret"}
if(!options.token_url && options.response_type == "code") {missing += " token_url"}
if(!options.redirect_uri) {missing += " redirect_uri"}
if(missing){
var error_msg = "Oauth2 parameters missing:" + missing;
errorCallback(error_msg, {error:error_msg});
return false;
} else {
return true;
}
}
// performs logout after oauth redirect
var oauth2Logout = function(options){
if(options.logout_url){
var s = document.createElement("script");
s.src = options.logout_url;
$("head").append(s);
}
}
// String prototype to parse and get url params
String.prototype.getParam = function( str ){
str = str.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regex = new RegExp( "[\\?&]*"+str+"=([^&#]*)" );
var results = regex.exec( this );
if( results == null ){
return "";
} else {
return results[1];
}
}
// if params missing return
if(!checkOauth2Params(options)) return;
// build oauth login url
var paramObj = {
client_id: options.client_id,
redirect_uri: options.redirect_uri,
response_type: options.response_type
};
$.extend(paramObj, options.other_params);
var login_url = options.auth_url + '?' + $.param(paramObj);
// open Cordova inapp-browser with login url
var loginWindow = window.open(login_url, '_blank', 'location=yes');
// check if redirect url has code, access_token or error
$(loginWindow).on('loadstart', function(e) {
var url = e.originalEvent.url;
// if authorization code method check for code/error in url param
if(options.response_type == "code"){
url = url.split("#")[0];
var code = url.getParam("code");
var error = url.getParam("error");
if (code || error){
loginWindow.close();
oauth2Logout(options);
if (code) {
$.ajax({
url: options.token_url,
data: {code:code, client_id:options.client_id, client_secret:options.client_secret, redirect_uri:options.redirect_uri, grant_type:"authorization_code"},
type: 'POST',
success: function(data){
var access_token;
if((data instanceof Object)){
access_token = data.access_token;
} else {
access_token = data.getParam("access_token");
}
successCallback(access_token, data);
},
error: function(error){
errorCallback(error, error);
}
});
} else if (error) {
errorCallback(error, url.split("?")[1]);
}
}
// if implicit method check for acces_token/error in url hash fragment
} else if(options.response_type == "token") {
var access_token = url.getParam("access_token");
var error = url.getParam("error");
if(access_token || error){
loginWindow.close();
oauth2Logout(options);
if(access_token){
successCallback(access_token, url.split("#")[1]);
} else if(error){
errorCallback(error, url.split("#")[1]);
}
}
}
});
};
}(jQuery));