Skip to content

Commit e356bd6

Browse files
Enhance OAuth2TokenManager with additional token validation checks
- Updated the `isTokenValid` method to include checks for the presence of the refresh token. - Improved the `isValidTokenData` method to validate token data structure and values more thoroughly, ensuring that access and refresh tokens are non-empty strings and that the expiry date is a valid finite number. - Modified the `setTokenData` method to throw an error if invalid token data is provided. - Enhanced the `setTokenDataForTesting` method to handle null token data appropriately. These changes further strengthen the OAuth2 token management implementation by ensuring comprehensive validation of token data, improving error handling, and enhancing overall code reliability.
1 parent 74ac3c3 commit e356bd6

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

workers/main/src/services/OAuth2/OAuth2TokenManager.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ export class OAuth2TokenManager implements IOAuth2TokenManager {
4646
}
4747

4848
isTokenValid(): boolean {
49-
if (!this.accessToken || !this.tokenExpiry) {
49+
if (!this.accessToken || !this.refreshToken || !this.tokenExpiry) {
5050
return false;
5151
}
5252

53-
if (this.accessToken.length === 0) {
53+
if (this.accessToken.length === 0 || this.refreshToken.length === 0) {
5454
return false;
5555
}
5656

@@ -74,11 +74,39 @@ export class OAuth2TokenManager implements IOAuth2TokenManager {
7474
}
7575

7676
private isValidTokenData(tokenData: TokenData): boolean {
77-
return (
78-
tokenData.access_token.length > 0 &&
79-
tokenData.refresh_token.length > 0 &&
80-
Number.isFinite(tokenData.expires_at)
81-
);
77+
if (!tokenData) {
78+
return false;
79+
}
80+
81+
if (
82+
typeof tokenData.access_token !== 'string' ||
83+
tokenData.access_token.length === 0
84+
) {
85+
return false;
86+
}
87+
88+
if (
89+
typeof tokenData.refresh_token !== 'string' ||
90+
tokenData.refresh_token.length === 0
91+
) {
92+
return false;
93+
}
94+
95+
if (
96+
typeof tokenData.expires_at !== 'number' ||
97+
!Number.isFinite(tokenData.expires_at) ||
98+
tokenData.expires_at <= 0
99+
) {
100+
return false;
101+
}
102+
103+
const expiryDate = new Date(tokenData.expires_at);
104+
105+
if (isNaN(expiryDate.getTime())) {
106+
return false;
107+
}
108+
109+
return true;
82110
}
83111

84112
private async refreshAccessToken(): Promise<void> {
@@ -145,6 +173,10 @@ export class OAuth2TokenManager implements IOAuth2TokenManager {
145173
}
146174

147175
private setTokenData(tokenData: TokenData): void {
176+
if (!this.isValidTokenData(tokenData)) {
177+
throw new OAuth2Error(ERROR_MESSAGES.INVALID_TOKEN_DATA);
178+
}
179+
148180
this.accessToken = tokenData.access_token;
149181
this.refreshToken = tokenData.refresh_token;
150182
this.tokenExpiry = new Date(tokenData.expires_at);
@@ -166,6 +198,14 @@ export class OAuth2TokenManager implements IOAuth2TokenManager {
166198
}
167199

168200
setTokenDataForTesting(tokenData: TokenData): void {
169-
this.setTokenData(tokenData);
201+
if (tokenData) {
202+
this.accessToken = tokenData.access_token;
203+
this.refreshToken = tokenData.refresh_token;
204+
this.tokenExpiry = new Date(tokenData.expires_at);
205+
} else {
206+
this.accessToken = null;
207+
this.refreshToken = null;
208+
this.tokenExpiry = null;
209+
}
170210
}
171211
}

0 commit comments

Comments
 (0)