|
1 | 1 | /** |
2 | 2 | * angular-oauth2 - Angular OAuth2 |
3 | | - * @version v4.0.0 |
| 3 | + * @version v4.1.0 |
4 | 4 | * @link https://github.com/seegno/angular-oauth2 |
5 | 5 | * @license MIT |
6 | 6 | */ |
|
14 | 14 | } |
15 | 15 | })(this, function(angular, ngCookies, queryString) { |
16 | 16 | var ngModule = angular.module("angular-oauth2", [ ngCookies ]).config(oauthConfig).factory("oauthInterceptor", oauthInterceptor).provider("OAuth", OAuthProvider).provider("OAuthToken", OAuthTokenProvider); |
17 | | - function oauthConfig($httpProvider) { |
18 | | - $httpProvider.interceptors.push("oauthInterceptor"); |
19 | | - } |
20 | | - oauthConfig.$inject = [ "$httpProvider" ]; |
21 | 17 | function oauthInterceptor($q, $rootScope, OAuthToken) { |
22 | 18 | return { |
23 | 19 | request: function request(config) { |
|
40 | 36 | }; |
41 | 37 | } |
42 | 38 | oauthInterceptor.$inject = [ "$q", "$rootScope", "OAuthToken" ]; |
| 39 | + function oauthConfig($httpProvider) { |
| 40 | + $httpProvider.interceptors.push("oauthInterceptor"); |
| 41 | + } |
| 42 | + oauthConfig.$inject = [ "$httpProvider" ]; |
43 | 43 | var _createClass = function() { |
44 | 44 | function defineProperties(target, props) { |
45 | 45 | for (var i = 0; i < props.length; i++) { |
|
70 | 70 | }; |
71 | 71 | var requiredKeys = [ "baseUrl", "clientId", "grantPath", "revokePath" ]; |
72 | 72 | function OAuthProvider() { |
73 | | - var config; |
74 | | - this.configure = function(params) { |
75 | | - if (config) { |
76 | | - throw new Error("Already configured."); |
77 | | - } |
| 73 | + var _this = this; |
| 74 | + var sanitizeConfigParams = function sanitizeConfigParams(params) { |
78 | 75 | if (!(params instanceof Object)) { |
79 | 76 | throw new TypeError("Invalid argument: `config` must be an `Object`."); |
80 | 77 | } |
81 | | - config = angular.extend({}, defaults, params); |
| 78 | + var config = angular.extend({}, defaults, params); |
82 | 79 | angular.forEach(requiredKeys, function(key) { |
83 | 80 | if (!config[key]) { |
84 | 81 | throw new Error("Missing parameter: " + key + "."); |
|
95 | 92 | } |
96 | 93 | return config; |
97 | 94 | }; |
| 95 | + this.configure = function(params) { |
| 96 | + _this.defaultConfig = sanitizeConfigParams(params); |
| 97 | + }; |
98 | 98 | this.$get = function($http, OAuthToken) { |
99 | 99 | var OAuth = function() { |
100 | | - function OAuth() { |
| 100 | + function OAuth(config) { |
101 | 101 | _classCallCheck(this, OAuth); |
102 | | - if (!config) { |
103 | | - throw new Error("`OAuthProvider` must be configured first."); |
104 | | - } |
| 102 | + this.config = config; |
105 | 103 | } |
106 | 104 | _createClass(OAuth, [ { |
| 105 | + key: "configure", |
| 106 | + value: function configure(params) { |
| 107 | + this.config = sanitizeConfigParams(params); |
| 108 | + } |
| 109 | + }, { |
107 | 110 | key: "isAuthenticated", |
108 | 111 | value: function isAuthenticated() { |
109 | 112 | return !!OAuthToken.getToken(); |
|
112 | 115 | key: "getAccessToken", |
113 | 116 | value: function getAccessToken(data, options) { |
114 | 117 | data = angular.extend({ |
115 | | - client_id: config.clientId, |
| 118 | + client_id: this.config.clientId, |
116 | 119 | grant_type: "password" |
117 | 120 | }, data); |
118 | | - if (null !== config.clientSecret) { |
119 | | - data.client_secret = config.clientSecret; |
| 121 | + if (null !== this.config.clientSecret) { |
| 122 | + data.client_secret = this.config.clientSecret; |
120 | 123 | } |
121 | 124 | data = queryString.stringify(data); |
122 | 125 | options = angular.extend({ |
|
125 | 128 | "Content-Type": "application/x-www-form-urlencoded" |
126 | 129 | } |
127 | 130 | }, options); |
128 | | - return $http.post("" + config.baseUrl + config.grantPath, data, options).then(function(response) { |
| 131 | + return $http.post("" + this.config.baseUrl + this.config.grantPath, data, options).then(function(response) { |
129 | 132 | OAuthToken.setToken(response.data); |
130 | 133 | return response; |
131 | 134 | }); |
|
134 | 137 | key: "getRefreshToken", |
135 | 138 | value: function getRefreshToken(data, options) { |
136 | 139 | data = angular.extend({ |
137 | | - client_id: config.clientId, |
| 140 | + client_id: this.config.clientId, |
138 | 141 | grant_type: "refresh_token", |
139 | 142 | refresh_token: OAuthToken.getRefreshToken() |
140 | 143 | }, data); |
141 | | - if (null !== config.clientSecret) { |
142 | | - data.client_secret = config.clientSecret; |
| 144 | + if (null !== this.config.clientSecret) { |
| 145 | + data.client_secret = this.config.clientSecret; |
143 | 146 | } |
144 | 147 | data = queryString.stringify(data); |
145 | 148 | options = angular.extend({ |
|
148 | 151 | "Content-Type": "application/x-www-form-urlencoded" |
149 | 152 | } |
150 | 153 | }, options); |
151 | | - return $http.post("" + config.baseUrl + config.grantPath, data, options).then(function(response) { |
| 154 | + return $http.post("" + this.config.baseUrl + this.config.grantPath, data, options).then(function(response) { |
152 | 155 | OAuthToken.setToken(response.data); |
153 | 156 | return response; |
154 | 157 | }); |
|
158 | 161 | value: function revokeToken(data, options) { |
159 | 162 | var refreshToken = OAuthToken.getRefreshToken(); |
160 | 163 | data = angular.extend({ |
161 | | - client_id: config.clientId, |
| 164 | + client_id: this.config.clientId, |
162 | 165 | token: refreshToken ? refreshToken : OAuthToken.getAccessToken(), |
163 | 166 | token_type_hint: refreshToken ? "refresh_token" : "access_token" |
164 | 167 | }, data); |
165 | | - if (null !== config.clientSecret) { |
166 | | - data.client_secret = config.clientSecret; |
| 168 | + if (null !== this.config.clientSecret) { |
| 169 | + data.client_secret = this.config.clientSecret; |
167 | 170 | } |
168 | 171 | data = queryString.stringify(data); |
169 | 172 | options = angular.extend({ |
170 | 173 | headers: { |
171 | 174 | "Content-Type": "application/x-www-form-urlencoded" |
172 | 175 | } |
173 | 176 | }, options); |
174 | | - return $http.post("" + config.baseUrl + config.revokePath, data, options).then(function(response) { |
| 177 | + return $http.post("" + this.config.baseUrl + this.config.revokePath, data, options).then(function(response) { |
175 | 178 | OAuthToken.removeToken(); |
176 | 179 | return response; |
177 | 180 | }); |
178 | 181 | } |
179 | 182 | } ]); |
180 | 183 | return OAuth; |
181 | 184 | }(); |
182 | | - return new OAuth(); |
| 185 | + return new OAuth(this.defaultConfig); |
183 | 186 | }; |
184 | 187 | this.$get.$inject = [ "$http", "OAuthToken" ]; |
185 | 188 | } |
|
0 commit comments