-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathendpoint.js
118 lines (100 loc) · 3.42 KB
/
endpoint.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
'use strict';
var endpointClient = angular.module('oauth.endpoint', []);
endpointClient.factory('Endpoint', ['$rootScope', 'AccessToken', '$q', '$http', function($rootScope, AccessToken, $q, $http) {
var service = {};
var buildOauthUrl = function (path, params) {
var oAuthScope = (params.scope) ? encodeURIComponent(params.scope) : '',
state = (params.state) ? encodeURIComponent(params.state) : '',
authPathHasQuery = (params.authorizePath.indexOf('?') == -1) ? false : true,
appendChar = (authPathHasQuery) ? '&' : '?', //if authorizePath has ? already append OAuth2 params
nonceParam = (params.nonce) ? '&nonce=' + params.nonce : '',
responseType = encodeURIComponent(params.responseType);
return params.site +
path +
appendChar + 'response_type=' + responseType + '&' +
'client_id=' + encodeURIComponent(params.clientId) + '&' +
'redirect_uri=' + encodeURIComponent(params.redirectUri) + '&' +
'scope=' + oAuthScope + '&' +
'state=' + state + nonceParam;
};
var extendValidity = function (tokenInfo) {
AccessToken.updateExpiry(tokenInfo.expires);
};
/*
* Defines the authorization URL
*/
service.set = function(configuration) {
this.config = configuration;
return this.get();
};
/*
* Returns the authorization URL
*/
service.get = function(overrides) {
var params = angular.extend( {}, service.config, overrides);
return buildOauthUrl(params.authorizePath, params);
};
/*
* Redirects the app to the authorization URL
*/
service.redirect = function(overrides) {
var targetLocation = this.get(overrides);
$rootScope.$broadcast('oauth:logging-in');
window.location.replace(targetLocation);
};
/*
* Alias for 'redirect'
*/
service.login = function() {
service.redirect();
};
/*
* Check the validity of the token if a session path is available
*/
service.checkValidity = function() {
var params = service.config;
if( params.sessionPath ) {
var token = AccessToken.get();
if( !token ) {
return $q.reject("No token configured");
}
var path = params.site + params.sessionPath;
var options = null;
var sessionPathHasQuery = (params.sessionPath.indexOf('?') == -1) ? false : true;
if (sessionPathHasQuery) {
path += "token=" + token.access_token;
} else {
options = { headers: headers() };
}
var promise = $http.get(path, options);
return promise.then( function(httpResponse) {
var tokenInfo = httpResponse.data;
if(tokenInfo.valid) {
extendValidity(tokenInfo);
return true;
} else {
return $q.reject("Server replied: token is invalid.");
}
});
} else {
return $q.reject("You must give a :session-path param in order to validate the token.")
}
};
var headers = function() {
return { Authorization: 'Bearer ' + AccessToken.get().access_token };
};
/*
* Destroys the session, sends the user to the logout url if set.
* First broadcasts 'logging-out' and then 'logout' when finished.
*/
service.logout = function() {
var params = service.config;
AccessToken.destroy();
$rootScope.$broadcast('oauth:logging-out');
if( params.logoutPath ) {
window.location.replace(buildOauthUrl(params.logoutPath, params));
}
$rootScope.$broadcast('oauth:logout');
};
return service;
}]);