@@ -10,14 +10,33 @@ import {
1010 HttpMethods ,
1111 OAuthResponseDto
1212} from "../contracts" ;
13+ import { STORAGE_OAUTH_KEY } from "../constants" ;
14+ import { isOAuthResponse } from "../helpers" ;
1315
1416const IdentityEventEmitter : { new ( ) : StrictEventEmitter < EventEmitter , IdentityMechanismEvents > } = EventEmitter ;
1517export class OAuthIdentity extends IdentityEventEmitter implements IdentityMechanism {
1618 constructor ( protected readonly configuration : OAuthIdentityConfiguration ) {
1719 super ( ) ;
20+
21+ if ( this . configuration . storage == null ) {
22+ return ;
23+ }
24+
25+ const storageKey = this . configuration . storageKey != null ? this . configuration . storageKey : STORAGE_OAUTH_KEY ;
26+ const storageOAuthItem = this . configuration . storage . getItem ( storageKey ) ;
27+
28+ if ( storageOAuthItem == null ) {
29+ return ;
30+ }
31+
32+ const parsedItem : unknown = JSON . parse ( storageOAuthItem ) ;
33+
34+ if ( isOAuthResponse ( parsedItem ) ) {
35+ this . oAuth = parsedItem ;
36+ }
1837 }
1938
20- private loginData : OAuthResponseDto | undefined ;
39+ private oAuth : OAuthResponseDto | undefined ;
2140 private renewalTimeoutId : number | undefined ;
2241 /**
2342 * Value is set in seconds.
@@ -44,11 +63,11 @@ export class OAuthIdentity extends IdentityEventEmitter implements IdentityMecha
4463 }
4564
4665 this . emit ( "login" ) ;
47- this . setLoginData ( ( await response . json ( ) ) as OAuthResponseDto ) ;
66+ this . setOAuthData ( ( await response . json ( ) ) as OAuthResponseDto ) ;
4867 }
4968
5069 public async logout ( ) : Promise < void > {
51- if ( this . loginData == null ) {
70+ if ( this . oAuth == null ) {
5271 throw new Error ( "Identity: login data is not set yet." ) ;
5372 }
5473
@@ -59,21 +78,27 @@ export class OAuthIdentity extends IdentityEventEmitter implements IdentityMecha
5978 headers : { ...( this . configuration . headers || { } ) } ,
6079 body : queryString . stringify ( {
6180 grant_type : "refresh_token" ,
62- refresh_token : this . loginData . refresh_token
81+ refresh_token : this . oAuth . refresh_token
6382 } )
6483 } ) ;
6584
6685 const responseStatus = `${ response . status } ` [ 0 ] ;
6786 if ( responseStatus !== "2" ) {
6887 throw new Error ( "Failed to logout." ) ;
6988 }
70- this . loginData = undefined ;
89+ this . oAuth = undefined ;
7190 clearTimeout ( this . renewalTimeoutId ) ;
91+
7292 this . emit ( "logout" ) ;
93+
94+ if ( this . configuration . storage == null ) {
95+ return ;
96+ }
97+ this . configuration . storage . clear ( ) ;
7398 }
7499
75100 public async authenticateRequest ( request : QueuedRequest ) : Promise < QueuedRequest > {
76- if ( this . loginData == null ) {
101+ if ( this . oAuth == null ) {
77102 throw new Error ( "Identity: login data is not set yet." ) ;
78103 }
79104
@@ -82,7 +107,7 @@ export class OAuthIdentity extends IdentityEventEmitter implements IdentityMecha
82107 }
83108
84109 const authHeader : { [ index : string ] : string } = {
85- Authorization : `${ this . loginData . token_type } ${ this . loginData . access_token } `
110+ Authorization : `${ this . oAuth . token_type } ${ this . oAuth . access_token } `
86111 } ;
87112
88113 request . headers = {
@@ -111,22 +136,27 @@ export class OAuthIdentity extends IdentityEventEmitter implements IdentityMecha
111136 throw new Error ( "Failed renew token." ) ;
112137 }
113138
114- this . setLoginData ( ( await response . json ( ) ) as OAuthResponseDto ) ;
139+ this . setOAuthData ( ( await response . json ( ) ) as OAuthResponseDto ) ;
115140 }
116141
117- private setLoginData ( loginData : OAuthResponseDto ) : void {
118- if ( loginData . expires_in == null ) {
142+ private setOAuthData ( oAuthData : OAuthResponseDto ) : void {
143+ if ( oAuthData . expires_in == null ) {
119144 throw Error ( "Not supported without expiration time." ) ;
120145 }
121146
122- this . loginData = loginData ;
147+ this . oAuth = oAuthData ;
148+
149+ if ( this . configuration . storage != null ) {
150+ const storageKey = this . configuration . storageKey != null ? this . configuration . storageKey : STORAGE_OAUTH_KEY ;
151+ this . configuration . storage . setItem ( storageKey , JSON . stringify ( oAuthData ) ) ;
152+ }
123153
124154 // If response do not have `refresh_token` we are not using renewal mechanism.
125- if ( loginData . refresh_token == null ) {
155+ if ( oAuthData . refresh_token == null ) {
126156 return ;
127157 }
128158
129- const refreshToken = loginData . refresh_token ;
159+ const refreshToken = oAuthData . refresh_token ;
130160
131161 // If response has `refresh_token` but we do not want to use renewal mechanism.
132162 if ( this . configuration . tokenRenewalEnabled === false ) {
@@ -138,7 +168,7 @@ export class OAuthIdentity extends IdentityEventEmitter implements IdentityMecha
138168 this . renewalTimeoutId = undefined ;
139169 }
140170
141- const timeoutNumber = this . renewalTime ( loginData . expires_in ) ;
171+ const timeoutNumber = this . renewalTime ( oAuthData . expires_in ) ;
142172 this . renewalTimeoutId = window . setTimeout ( ( ) => this . renewToken ( refreshToken ) , timeoutNumber ) ;
143173 }
144174
0 commit comments