8000 add a feature to use redirect instead of popup -add a demo for this new feature by clakech · Pull Request #2 · angular-oauth/angular-oauth · GitHub
[go: up one dir, main page]

Skip to content

add a feature to use redirect instead of popup -add a demo for this new feature #2

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

Open
wants to merge 1 commit into
base: master
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
33 changes: 33 additions & 0 deletions example/demo_redirect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!doctype html>
<html lang="en" ng-app="demoRedirect" ng-controller="DemoRedirectCtrl">
<head>
<meta charset="utf-8">
<title>Client-side authentication demo with redirect</title>
</head>
<body>

<div>
<div ng-show="accessToken" style="display: none">
<h1>You're authenticated!</h1>

<h2>Access token</h2>
<pre>{{accessToken}}</pre>

<div ng-show="expiresIn">
<h2>Expiry</h2>
<p>Expires in {{expiresIn}} seconds.</p>
</div>

<div ng-show="accessTokenVerified">
<h2>Access token verified</h2>
</div>
</div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/angular.js"></script>

<script src="js/demo.js"></script>
<script src="../src/js/googleOauth.js"></script>
<script src="../src/js/angularOauth.js"></script>
</body>
</html>
44 changes: 44 additions & 0 deletions example/js/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,48 @@ angular.module('demo', ['googleOauth']).
alert("Failed to get token from popup.");
});
};
});

angular.module('demoRedirect', ['googleOauth']).

config(function(TokenProvider) {
// Demo configuration for the "angular-oauth demo" project on Google.
// Log in at will!

// Sorry about this way of getting a relative URL, powers that be.
var baseUrl = document.URL.replace('example/demo_redirect.html', '');

TokenProvider.extendConfig({
clientId: '191261111313.apps.googleusercontent.com',
redirectUri: baseUrl + 'src/oauth2callback.html', // allow lunching demo from a mirror
scopes: ["https://www.googleapis.com/auth/userinfo.email"]
});
}).
controller('DemoRedirectCtrl', function($scope, $window, $location, Token) {

$scope.$on("$routeChangeStart", function () {
$scope.login();
});

$scope.login = function() {
$scope.accessToken = Token.get();
if ($scope.accessToken) {
// Success getting token.
// Verify the token before setting it, to avoid the confused deputy problem.
Token.verifyAsync($scope.accessToken).
then(function(data) {
// alert("verify token.");
$scope.accessTokenVerified = true;
}, function() {
alert("Failed to verify token.");
});
} else {
Token.extendConfig({state: $location.absUrl()});
var extraParams = {};
Token.getTokenWithRedirect(extraParams);
}
}

$scope.login();

});
59 changes: 58 additions & 1 deletion src/js/angularOauth.js
D51C
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ angular.module('angularOauth', []).
response_type: RESPONSE_TYPE,
client_id: config.clientId,
redirect_uri: config.redirectUri,
scope: config.scopes.join(" ")
scope: config.scopes.join(" "),
state: config.state
}
};

Expand Down Expand Up @@ -184,6 +185,18 @@ angular.module('angularOauth', []).
// TODO: reject deferred if the popup was closed without a message being delivered + maybe offer a timeout

return deferred.promise;
},

extendConfig: function(configExtension) {
config = angular.extend(config, configExtension);
},

getTokenWithRedirect: function(extraParams) {

var params = angular.extend(getParams(), extraParams),
url = config.authorizationEndpoint + '?' + objectToQueryString(params);

$window.open(url, "_self");
}
}
}
Expand Down Expand Up @@ -224,3 +237,47 @@ angular.module('angularOauth', []).
window.opener.postMessage(params, "*");
window.close();
});

angular.module('angularOauthRedirect', ['angularOauth']).config(['TokenProvider', function (TokenProvider) {

// can't find any other way to avoid exception if I want to be able to use Token.set inside RedirectCtrl
TokenProvider.extendConfig({
clientId: 'test',
authorizationEndpoint: 'test',
redirectUri: 'test',
scopes: ['test'],
verifyFunc: 'test'
});

}]).

controller('RedirectCtrl', function($scope, $location, $window, $log, Token) {

/**
* TODO duplicate from CallbackCtrl
*
* Parses an escaped url query string into key-value pairs.
*
* (Copied from Angular.js in the AngularJS project.)
*
* @returns Object.<(string|boolean)>
*/
function parseKeyValue(/**string*/keyValue) {
var obj = {}, key_value, key;
angular.forEach((keyValue || "").split('&'), function(keyValue){
if (keyValue) {
key_value = keyValue.split('=');
key = decodeURIComponent(key_value[0]);
obj[key] = angular.isDefined(key_value[1]) ? decodeURIComponent(key_value[1]) : true;
}
});
return obj;
}

var queryString = $location.path().substring(1); // preceding slash omitted
var params = parseKeyValue(queryString);

Token.set(params.access_token);

$window.open(params.state, "_self");
});
2 changes: 1 addition & 1 deletion src/oauth2callback.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="en" ng-app="angularOauth" ng-controller="CallbackCtrl">
<html lang="en" ng-app="angularOauthRedirect" ng-controller="RedirectCtrl">
<head>
<meta charset="utf-8">
<title>Receiving authentication</title>
Expand Down
0