11/**
22 * angular-oauth2 - Angular OAuth2
3- * @version v3.0.1
3+ * @version v3.1.0
44 * @link https://github.com/seegno/angular-oauth2
55 * @license MIT
66 */
77( function ( root , factory ) {
88 if ( typeof define === "function" && define . amd ) {
9- define ( [ "angular" , "query-string" ] , factory ) ;
9+ define ( [ "angular" , "angular-cookies" , " query-string" ] , factory ) ;
1010 } else if ( typeof exports === "object" ) {
11- module . exports = factory ( require ( "angular" ) , require ( "query-string" ) ) ;
11+ module . exports = factory ( require ( "angular" ) , require ( "angular-cookies" ) , require ( " query-string") ) ;
1212 } else {
13- root . angularOAuth2 = factory ( root . angular , root . queryString ) ;
13+ root . angularOAuth2 = factory ( root . angular , "ngCookies" , root . queryString ) ;
1414 }
15- } ) ( this , function ( angular , queryString ) {
16- var ngModule = angular . module ( "angular-oauth2" , [ " ngCookies" ] ) . config ( oauthConfig ) . factory ( "oauthInterceptor" , oauthInterceptor ) . provider ( "OAuth" , OAuthProvider ) . provider ( "OAuthToken" , OAuthTokenProvider ) ;
15+ } ) ( this , function ( angular , ngCookies , queryString ) {
16+ var ngModule = angular . module ( "angular-oauth2" , [ ngCookies ] ) . config ( oauthConfig ) . factory ( "oauthInterceptor" , oauthInterceptor ) . provider ( "OAuth" , OAuthProvider ) . provider ( "OAuthToken" , OAuthTokenProvider ) ;
1717 function oauthConfig ( $httpProvider ) {
1818 $httpProvider . interceptors . push ( "oauthInterceptor" ) ;
1919 }
2020 oauthConfig . $inject = [ "$httpProvider" ] ;
21+ function oauthInterceptor ( $q , $rootScope , OAuthToken ) {
22+ return {
23+ request : function ( config ) {
24+ config . headers = config . headers || { } ;
25+ if ( ! config . headers . hasOwnProperty ( "Authorization" ) && OAuthToken . getAuthorizationHeader ( ) ) {
26+ config . headers . Authorization = OAuthToken . getAuthorizationHeader ( ) ;
27+ }
28+ return config ;
29+ } ,
30+ responseError : function ( rejection ) {
31+ if ( 400 === rejection . status && rejection . data && ( "invalid_request" === rejection . data . error || "invalid_grant" === rejection . data . error ) ) {
32+ OAuthToken . removeToken ( ) ;
33+ $rootScope . $emit ( "oauth:error" , rejection ) ;
34+ }
35+ if ( 401 === rejection . status && ( rejection . data && "invalid_token" === rejection . data . error ) || rejection . headers ( "www-authenticate" ) && 0 === rejection . headers ( "www-authenticate" ) . indexOf ( "Bearer" ) ) {
36+ $rootScope . $emit ( "oauth:error" , rejection ) ;
37+ }
38+ return $q . reject ( rejection ) ;
39+ }
40+ } ;
41+ }
42+ oauthInterceptor . $inject = [ "$q" , "$rootScope" , "OAuthToken" ] ;
2143 var _prototypeProperties = function ( child , staticProps , instanceProps ) {
2244 if ( staticProps ) Object . defineProperties ( child , staticProps ) ;
2345 if ( instanceProps ) Object . defineProperties ( child . prototype , instanceProps ) ;
7395 configurable : true
7496 } ,
7597 getAccessToken : {
76- value : function getAccessToken ( user , options ) {
77- if ( ! user || ! user . username || ! user . password ) {
78- throw new Error ( "`user` must be an object with `username` and `password` properties." ) ;
79- }
80- var data = {
98+ value : function getAccessToken ( data , options ) {
99+ data = angular . extend ( {
81100 client_id : config . clientId ,
82- grant_type : "password" ,
83- username : user . username ,
84- password : user . password
85- } ;
101+ grant_type : "password"
102+ } , data ) ;
86103 if ( null !== config . clientSecret ) {
87104 data . client_secret = config . clientSecret ;
88105 }
89106 data = queryString . stringify ( data ) ;
90107 options = angular . extend ( {
91108 headers : {
109+ Authorization : undefined ,
92110 "Content-Type" : "application/x-www-form-urlencoded"
93111 }
94112 } , options ) ;
102120 configurable : true
103121 } ,
104122 getRefreshToken : {
105- value : function getRefreshToken ( ) {
106- var data = {
123+ value : function getRefreshToken ( data , options ) {
124+ data = angular . extend ( {
107125 client_id : config . clientId ,
108126 grant_type : "refresh_token" ,
109127 refresh_token : OAuthToken . getRefreshToken ( )
110- } ;
128+ } , data ) ;
111129 if ( null !== config . clientSecret ) {
112130 data . client_secret = config . clientSecret ;
113131 }
114132 data = queryString . stringify ( data ) ;
115- var options = {
133+ options = angular . extend ( {
116134 headers : {
135+ Authorization : undefined ,
117136 "Content-Type" : "application/x-www-form-urlencoded"
118137 }
119- } ;
138+ } , options ) ;
120139 return $http . post ( "" + config . baseUrl + "" + config . grantPath , data , options ) . then ( function ( response ) {
121140 OAuthToken . setToken ( response . data ) ;
122141 return response ;
127146 configurable : true
128147 } ,
129148 revokeToken : {
130- value : function revokeToken ( ) {
131- var data = queryString . stringify ( {
132- token : OAuthToken . getRefreshToken ( ) ? OAuthToken . getRefreshToken ( ) : OAuthToken . getAccessToken ( )
133- } ) ;
134- var options = {
149+ value : function revokeToken ( data , options ) {
150+ var refreshToken = OAuthToken . getRefreshToken ( ) ;
151+ data = angular . extend ( {
152+ client_id : config . clientId ,
153+ token : refreshToken ? refreshToken : OAuthToken . getAccessToken ( ) ,
154+ token_type_hint : refreshToken ? "refresh_token" : "access_token"
155+ } , data ) ;
156+ if ( null !== config . clientSecret ) {
157+ data . client_secret = config . clientSecret ;
158+ }
159+ data = queryString . stringify ( data ) ;
160+ options = angular . extend ( {
135161 headers : {
136162 "Content-Type" : "application/x-www-form-urlencoded"
137163 }
138- } ;
164+ } , options ) ;
139165 return $http . post ( "" + config . baseUrl + "" + config . revokePath , data , options ) . then ( function ( response ) {
140166 OAuthToken . removeToken ( ) ;
141167 return response ;
240266 } ;
241267 this . $get . $inject = [ "$cookies" ] ;
242268 }
243- function oauthInterceptor ( $q , $rootScope , OAuthToken ) {
244- return {
245- request : function ( config ) {
246- if ( OAuthToken . getAuthorizationHeader ( ) ) {
247- config . headers = config . headers || { } ;
248- config . headers . Authorization = OAuthToken . getAuthorizationHeader ( ) ;
249- }
250- return config ;
251- } ,
252- responseError : function ( rejection ) {
253- if ( 400 === rejection . status && rejection . data && ( "invalid_request" === rejection . data . error || "invalid_grant" === rejection . data . error ) ) {
254- OAuthToken . removeToken ( ) ;
255- $rootScope . $emit ( "oauth:error" , rejection ) ;
256- }
257- if ( 401 === rejection . status && ( rejection . data && "invalid_token" === rejection . data . error ) || rejection . headers ( "www-authenticate" ) && 0 === rejection . headers ( "www-authenticate" ) . indexOf ( "Bearer" ) ) {
258- $rootScope . $emit ( "oauth:error" , rejection ) ;
259- }
260- return $q . reject ( rejection ) ;
261- }
262- } ;
263- }
264- oauthInterceptor . $inject = [ "$q" , "$rootScope" , "OAuthToken" ] ;
265269 return ngModule ;
266270} ) ;
0 commit comments