Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added auth through Laravel Passport auth server #335

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Each web service API acts independently in this library. However, when configur
$cordovaOauth.fiveHundredsPx(string sdkKey, object options);
$cordovaOauth.netatmo(object options);
$cordovaOauth.trakttv(string clientId, string clientSecret, string state);
$cordovaOauth.passport(clientId, appScope, options);

Each API call returns a promise. The success callback will provide a response object and the error
callback will return a string.
Expand All @@ -117,6 +118,17 @@ To authenticate with Twitter, Withings, Magento and Xing an additional library i

As of Apache Cordova 5.0.0, the [whitelist plugin](https://www.thepolyglotdeveloper.com/2015/05/whitelist-external-resources-for-use-in-ionic-framework/) must be used in order to reach external web services.

To Authenticate with the Passport plugin using your own Auth server with [Laravel Passport](https://laravel.com/docs/master/passport) you must pass in your site url and client secret for the client you are using

```javascript
// Notice in the target_url there is no trailing slash
$cordovaOauth.passport('CLIENT_ID_HERE',['YOUR_SCOPES_HERE'], {client_secret: 'YOU_CLIENT_SECRET',target_url: 'http(s)://your-site.com'}).then(function(result) {
// Handle your auth here
}, function(error) {
// Handle your error here
});
```

### Important Note About Testing

This library will **NOT** work with a web browser, ionic serve, ionic live-reload, or ionic view. It **MUST** be used via installing to a device or simulator.
Expand Down
81 changes: 77 additions & 4 deletions dist/ng-cordova-oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,8 @@
'oauth.xing',
'oauth.netatmo',
'oauth.trakttv',
'oauth.yahoo'])
'oauth.yahoo',
'oauth.laravelPassport'])
.factory("$cordovaOauth", cordovaOauth);

function cordovaOauth(
Expand All @@ -1047,7 +1048,7 @@
$ngCordovaTwitter, $ngCordovaMeetup, $ngCordovaSalesforce, $ngCordovaStrava, $ngCordovaWithings, $ngCordovaFoursquare, $ngCordovaMagento,
$ngCordovaVkontakte, $ngCordovaOdnoklassniki, $ngCordovaImgur, $ngCordovaSpotify, $ngCordovaUber, $ngCordovaWindowslive, $ngCordovaYammer,
$ngCordovaVenmo, $ngCordovaStripe, $ngCordovaRally, $ngCordovaFamilySearch, $ngCordovaEnvato, $ngCordovaWeibo, $ngCordovaJawbone, $ngCordovaUntappd,
$ngCordovaDribble, $ngCordovaPocket, $ngCordovaMercadolibre, $ngCordovaXing, $ngCordovaNetatmo, $ngCordovaTraktTv, $ngCordovaYahoo) {
$ngCordovaDribble, $ngCordovaPocket, $ngCordovaMercadolibre, $ngCordovaXing, $ngCordovaNetatmo, $ngCordovaTraktTv, $ngCordovaYahoo, $ngCordovaLaravelPassport) {

return {
azureAD: $ngCordovaAzureAD.signin,
Expand Down Expand Up @@ -1090,7 +1091,8 @@
xing: $ngCordovaXing.signin,
netatmo: $ngCordovaNetatmo.signin,
trakttv: $ngCordovaTraktTv.signin,
yahoo: $ngCordovaYahoo.signin
yahoo: $ngCordovaYahoo.signin,
passport: $ngCordovaLaravelPassport
};
}

Expand Down Expand Up @@ -1136,10 +1138,81 @@
'$ngCordovaXing',
'$ngCordovaNetatmo',
'$ngCordovaTraktTv',
'$ngCordovaYahoo'
'$ngCordovaYahoo',
'$ngCordovaLaravelPassport'
];
})();

(function() {
'use strict';

angular.module('oauth.laravelPassport', ['oauth.utils'])
.factory('$ngCordovaLaravelPassport', laravelPassport);

function laravelPassport($q, $http, $cordovaOauthUtility) {
return { signin: oauthLaravelPassport };

/*
* Sign into the Laravel Passport service
*
* @param string clientId
* @param array appScope
* @param object options
* @return promise
*/
function oauthLaravelPassport(clientId, appScope, options) {
var deferred = $q.defer();
if(window.cordova) {
if($cordovaOauthUtility.isInAppBrowserInstalled()) {
var redirect_uri = "http://localhost/callback";
var target_url = '';
var client_secret = '';
if(options !== undefined) {
if(options.hasOwnProperty("redirect_uri")) {
redirect_uri = options.redirect_uri;
}
if(options.hasOwnProperty("target_url")) {
target_url = options.target_url;
}
if(options.hasOwnProperty('client_secret')) {
client_secret = options.client_secret;
}
}

var browserRef = window.cordova.InAppBrowser.open(target_url + '/oauth/authorize?response_type=code&client_id=' + clientId + '&redirect_uri='+ redirect_uri +'&scope=' + appScope.join(' '), '_blank', 'location=no,clearsessioncache=yes,clearcache=yes');
browserRef.addEventListener("loadstart", function(event) {
if((event.url).indexOf(redirect_uri) === 0) {
var requestToken = (event.url).split("code=")[1];

$http({method: "post", headers: {'Content-Type': 'application/x-www-form-urlencoded'}, url: target_url+ '/oauth/token', data: "client_id=" + clientId + "&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri + "&grant_type=authorization_code" + "&code=" + requestToken })
.then(function(data) {
deferred.resolve(data);
})
.catch(function(data, status) {
deferred.reject(data);
})
.finally(function() {
setTimeout(function() {
browserRef.close();
}, 10);
});
}
});
browserRef.addEventListener('exit', function(event) {
deferred.reject("The sign in flow was canceled");
});
} else {
deferred.reject("Could not find InAppBrowser plugin");
}
} else {
deferred.reject("Cannot authenticate via a web browser");
}
return deferred.promise;
}
}
laravelPassport.$inject = ['$q', '$http', '$cordovaOauthUtility'];
})();

(function() {
'use strict';

Expand Down
6 changes: 3 additions & 3 deletions dist/ng-cordova-oauth.min.js

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions src/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
'oauth.xing',
'oauth.netatmo',
'oauth.trakttv',
'oauth.yahoo'])
'oauth.yahoo',
'oauth.laravelPassport'])
.factory("$cordovaOauth", cordovaOauth);

function cordovaOauth(
Expand All @@ -53,7 +54,7 @@
$ngCordovaTwitter, $ngCordovaMeetup, $ngCordovaSalesforce, $ngCordovaStrava, $ngCordovaWithings, $ngCordovaFoursquare, $ngCordovaMagento,
$ngCordovaVkontakte, $ngCordovaOdnoklassniki, $ngCordovaImgur, $ngCordovaSpotify, $ngCordovaUber, $ngCordovaWindowslive, $ngCordovaYammer,
$ngCordovaVenmo, $ngCordovaStripe, $ngCordovaRally, $ngCordovaFamilySearch, $ngCordovaEnvato, $ngCordovaWeibo, $ngCordovaJawbone, $ngCordovaUntappd,
$ngCordovaDribble, $ngCordovaPocket, $ngCordovaMercadolibre, $ngCordovaXing, $ngCordovaNetatmo, $ngCordovaTraktTv, $ngCordovaYahoo) {
$ngCordovaDribble, $ngCordovaPocket, $ngCordovaMercadolibre, $ngCordovaXing, $ngCordovaNetatmo, $ngCordovaTraktTv, $ngCordovaYahoo, $ngCordovaLaravelPassport) {

return {
azureAD: $ngCordovaAzureAD.signin,
Expand Down Expand Up @@ -96,7 +97,8 @@
xing: $ngCordovaXing.signin,
netatmo: $ngCordovaNetatmo.signin,
trakttv: $ngCordovaTraktTv.signin,
yahoo: $ngCordovaYahoo.signin
yahoo: $ngCordovaYahoo.signin,
passport: $ngCordovaLaravelPassport
};
}

Expand Down Expand Up @@ -142,6 +144,7 @@
'$ngCordovaXing',
'$ngCordovaNetatmo',
'$ngCordovaTraktTv',
'$ngCordovaYahoo'
'$ngCordovaYahoo',
'$ngCordovaLaravelPassport'
];
})();
69 changes: 69 additions & 0 deletions src/oauth.laravelPassport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
(function() {
'use strict';

angular.module('oauth.laravelPassport', ['oauth.utils'])
.factory('$ngCordovaLaravelPassport', laravelPassport);

function laravelPassport($q, $http, $cordovaOauthUtility) {
return { signin: oauthLaravelPassport };

/*
* Sign into the Laravel Passport service
*
* @param string clientId
* @param array appScope
* @param object options
* @return promise
*/
function oauthLaravelPassport(clientId, appScope, options) {
var deferred = $q.defer();
if(window.cordova) {
if($cordovaOauthUtility.isInAppBrowserInstalled()) {
var redirect_uri = "http://localhost/callback";
var target_url = '';
var client_secret = '';
if(options !== undefined) {
if(options.hasOwnProperty("redirect_uri")) {
redirect_uri = options.redirect_uri;
}
if(options.hasOwnProperty("target_url")) {
target_url = options.target_url;
}
if(options.hasOwnProperty('client_secret')) {
client_secret = options.client_secret;
}
}

var browserRef = window.cordova.InAppBrowser.open(target_url + '/oauth/authorize?response_type=code&client_id=' + clientId + '&redirect_uri='+ redirect_uri +'&scope=' + appScope.join(' '), '_blank', 'location=no,clearsessioncache=yes,clearcache=yes');
browserRef.addEventListener("loadstart", function(event) {
if((event.url).indexOf(redirect_uri) === 0) {
var requestToken = (event.url).split("code=")[1];

$http({method: "post", headers: {'Content-Type': 'application/x-www-form-urlencoded'}, url: target_url+ '/oauth/token', data: "client_id=" + clientId + "&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri + "&grant_type=authorization_code" + "&code=" + requestToken })
.then(function(data) {
deferred.resolve(data);
})
.catch(function(data, status) {
deferred.reject(data);
})
.finally(function() {
setTimeout(function() {
browserRef.close();
}, 10);
});
}
});
browserRef.addEventListener('exit', function(event) {
deferred.reject("The sign in flow was canceled");
});
} else {
deferred.reject("Could not find InAppBrowser plugin");
}
} else {
deferred.reject("Cannot authenticate via a web browser");
}
return deferred.promise;
}
}
laravelPassport.$inject = ['$q', '$http', '$cordovaOauthUtility'];
})();