-
Notifications
You must be signed in to change notification settings - Fork 1
Add OAuth2 token management and storage implementation #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
29d31e9
Add OAuth2 token management and storage implementation
anatolyshipitz 25a6345
Refactor OAuth2TokenManager and remove unused types
anatolyshipitz 6e28da6
Add OAuth2Error class and integrate into token management
anatolyshipitz 2c5cc80
Enhance OAuth2TokenManager tests for token retrieval and error handling
anatolyshipitz e871c4e
Refactor OAuth2TokenManager and enhance error handling
anatolyshipitz d4d0f2e
Merge branch 'main' into feature/65031_oauth_service
anatolyshipitz 60c0b1c
Enhance OAuth2TokenManager with improved error handling and constants
anatolyshipitz 74ac3c3
Enhance OAuth2TokenManager with token validation and error handling i…
anatolyshipitz e356bd6
Enhance OAuth2TokenManager with additional token validation checks
anatolyshipitz 449abb4
Refactor OAuth2 token management interfaces and consolidate types
anatolyshipitz 7092850
Refactor OAuth2 token loading to support asynchronous operations
anatolyshipitz cf78db5
Enhance OAuth2Error class with error code functionality
anatolyshipitz a939883
Enhance setTokenDataForTesting method in OAuth2TokenManager
anatolyshipitz 6379c43
Add unit tests for FileTokenStorage and OAuth2TokenRefreshProvider
anatolyshipitz 8881bea
Add unit tests for OAuth2TokenManager and OAuth2TokenRefreshProvider
anatolyshipitz f6845a9
Add edge case unit tests for OAuth2TokenManager
anatolyshipitz 3abdcc5
Add unit tests for token refresh handling in OAuth2TokenManager
anatolyshipitz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { OAuth2Error } from './OAuth2Error'; | ||
|
|
||
| describe('OAuth2Error', () => { | ||
| it('should set the message, name, and default error code', () => { | ||
| const err = new OAuth2Error('test message'); | ||
|
|
||
| expect(err.message).toBe('test message'); | ||
| expect(err.name).toBe('OAuth2Error'); | ||
| expect(err.code).toBe('UNKNOWN_OAUTH2_ERROR'); | ||
| }); | ||
|
|
||
| it('should set the message, name, and custom error code', () => { | ||
| const err = new OAuth2Error('test message', 'CUSTOM_ERROR'); | ||
|
|
||
| expect(err.message).toBe('test message'); | ||
| expect(err.name).toBe('OAuth2Error'); | ||
| expect(err.code).toBe('CUSTOM_ERROR'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { AppError } from './AppError'; | ||
|
|
||
| export class OAuth2Error extends AppError { | ||
| public readonly code: string; | ||
|
|
||
| constructor(message: string, code: string = 'UNKNOWN_OAUTH2_ERROR') { | ||
| super(message, 'OAuth2Error'); | ||
| this.code = code; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| export * from './AppError'; | ||
| export * from './FileUtilsError'; | ||
| export * from './FinAppRepositoryError'; | ||
| export * from './OAuth2Error'; | ||
| export * from './QuickBooksRepositoryError'; | ||
| export * from './SlackRepositoryError'; | ||
| export * from './TargetUnitRepositoryError'; |
99 changes: 99 additions & 0 deletions
99
workers/main/src/services/OAuth2/FileTokenStorage.basic.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { promises as fs } from 'fs'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { OAuth2Error } from '../../common/errors'; | ||
| import { FileTokenStorage } from './FileTokenStorage'; | ||
| import { TokenData } from './types'; | ||
|
|
||
| vi.mock('fs', () => ({ | ||
| promises: { | ||
| mkdir: vi.fn(), | ||
| writeFile: vi.fn(), | ||
| readFile: vi.fn(), | ||
| unlink: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| describe('FileTokenStorage - Basic', () => { | ||
| let fileTokenStorage: FileTokenStorage; | ||
| let mockMkdir: ReturnType<typeof vi.fn>; | ||
| let mockWriteFile: ReturnType<typeof vi.fn>; | ||
|
|
||
| const mockTokenData: TokenData = { | ||
| access_token: 'test-access-token', | ||
| refresh_token: 'test-refresh-token', | ||
| expires_at: Date.now() + 3600000, | ||
| token_type: 'Bearer', | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| mockMkdir = vi.mocked(fs.mkdir); | ||
| mockWriteFile = vi.mocked(fs.writeFile); | ||
|
|
||
| mockMkdir.mockResolvedValue(undefined); | ||
| mockWriteFile.mockResolvedValue(undefined); | ||
|
|
||
| fileTokenStorage = new FileTokenStorage('test-service'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('constructor', () => { | ||
| it('should create instance with default service name', () => { | ||
| const storage = new FileTokenStorage(); | ||
|
|
||
| expect(storage).toBeInstanceOf(FileTokenStorage); | ||
| }); | ||
|
|
||
| it('should create instance with custom service name', () => { | ||
| const storage = new FileTokenStorage('custom-service'); | ||
|
|
||
| expect(storage).toBeInstanceOf(FileTokenStorage); | ||
| }); | ||
|
|
||
| it('should create instance with custom token file path', () => { | ||
| const customPath = '/custom/path/token.json'; | ||
| const storage = new FileTokenStorage('test-service', customPath); | ||
|
|
||
| expect(storage).toBeInstanceOf(FileTokenStorage); | ||
| }); | ||
| }); | ||
|
|
||
| describe('save', () => { | ||
| it('should save token data successfully', async () => { | ||
| await fileTokenStorage.save(mockTokenData); | ||
|
|
||
| expect(mockMkdir).toHaveBeenCalledWith(expect.any(String), { | ||
| recursive: true, | ||
| }); | ||
| expect(mockWriteFile).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| JSON.stringify(mockTokenData, null, 2), | ||
| ); | ||
| }); | ||
|
|
||
| it('should throw OAuth2Error when save fails', async () => { | ||
| mockWriteFile.mockRejectedValue(new Error('Write failed')); | ||
|
|
||
| await expect(fileTokenStorage.save(mockTokenData)).rejects.toThrow( | ||
| OAuth2Error, | ||
| ); | ||
| await expect(fileTokenStorage.save(mockTokenData)).rejects.toThrow( | ||
| 'Failed to save token data to file', | ||
| ); | ||
| }); | ||
|
|
||
| it('should throw OAuth2Error when mkdir fails', async () => { | ||
| mockMkdir.mockRejectedValue(new Error('Mkdir failed')); | ||
|
|
||
| await expect(fileTokenStorage.save(mockTokenData)).rejects.toThrow( | ||
| OAuth2Error, | ||
| ); | ||
| await expect(fileTokenStorage.save(mockTokenData)).rejects.toThrow( | ||
| 'Failed to save token data to file', | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
65 changes: 65 additions & 0 deletions
65
workers/main/src/services/OAuth2/FileTokenStorage.clear.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { promises as fs } from 'fs'; | ||
| import { join } from 'path'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { OAuth2Error } from '../../common/errors'; | ||
| import { FileTokenStorage } from './FileTokenStorage'; | ||
|
|
||
| vi.mock('fs', () => ({ | ||
| promises: { | ||
| mkdir: vi.fn(), | ||
| writeFile: vi.fn(), | ||
| readFile: vi.fn(), | ||
| unlink: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('path', () => ({ | ||
| join: vi.fn(), | ||
| })); | ||
|
|
||
| describe('FileTokenStorage - Clear', () => { | ||
| let fileTokenStorage: FileTokenStorage; | ||
| let mockJoin: ReturnType<typeof vi.fn>; | ||
| let mockUnlink: ReturnType<typeof vi.fn>; | ||
|
|
||
| beforeEach(() => { | ||
| mockJoin = vi.mocked(join); | ||
| mockUnlink = vi.mocked(fs.unlink); | ||
|
|
||
| mockJoin.mockReturnValue('/test/path/token.json'); | ||
| mockUnlink.mockResolvedValue(undefined); | ||
|
|
||
| fileTokenStorage = new FileTokenStorage('test-service'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('clear', () => { | ||
| it('should clear token data successfully', async () => { | ||
| await fileTokenStorage.clear(); | ||
|
|
||
| expect(mockUnlink).toHaveBeenCalledWith('/test/path/token.json'); | ||
| }); | ||
|
|
||
| it('should not throw error when file does not exist', async () => { | ||
| const error = new Error('File not found') as NodeJS.ErrnoException; | ||
|
|
||
| error.code = 'ENOENT'; | ||
| mockUnlink.mockRejectedValue(error); | ||
|
|
||
| await expect(fileTokenStorage.clear()).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should throw OAuth2Error when clear fails with other error', async () => { | ||
| mockUnlink.mockRejectedValue(new Error('Delete failed')); | ||
|
|
||
| await expect(fileTokenStorage.clear()).rejects.toThrow(OAuth2Error); | ||
| await expect(fileTokenStorage.clear()).rejects.toThrow( | ||
| 'Failed to clear token data from file', | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
117 changes: 117 additions & 0 deletions
117
workers/main/src/services/OAuth2/FileTokenStorage.load.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { promises as fs } from 'fs'; | ||
| import { join } from 'path'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { FileTokenStorage } from './FileTokenStorage'; | ||
| import { TokenData } from './types'; | ||
|
|
||
| vi.mock('fs', () => ({ | ||
| promises: { | ||
| mkdir: vi.fn(), | ||
| writeFile: vi.fn(), | ||
| readFile: vi.fn(), | ||
| unlink: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('path', () => ({ | ||
| join: vi.fn(), | ||
| })); | ||
|
|
||
| describe('FileTokenStorage - Load', () => { | ||
| let fileTokenStorage: FileTokenStorage; | ||
| let mockJoin: ReturnType<typeof vi.fn>; | ||
| let mockReadFile: ReturnType<typeof vi.fn>; | ||
|
|
||
| const mockTokenData: TokenData = { | ||
| access_token: 'test-access-token', | ||
| refresh_token: 'test-refresh-token', | ||
| expires_at: Date.now() + 3600000, | ||
| token_type: 'Bearer', | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| mockJoin = vi.mocked(join); | ||
| mockReadFile = vi.mocked(fs.readFile); | ||
|
|
||
| mockJoin.mockReturnValue('/test/path/token.json'); | ||
| mockReadFile.mockResolvedValue(JSON.stringify(mockTokenData)); | ||
|
|
||
| fileTokenStorage = new FileTokenStorage('test-service'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('load', () => { | ||
| it('should load valid token data successfully', async () => { | ||
| const result = await fileTokenStorage.load(); | ||
|
|
||
| expect(mockReadFile).toHaveBeenCalledWith( | ||
| '/test/path/token.json', | ||
| 'utf8', | ||
| ); | ||
| expect(result).toEqual(mockTokenData); | ||
| }); | ||
|
|
||
| it('should return null when file does not exist', async () => { | ||
| const error = new Error('File not found') as NodeJS.ErrnoException; | ||
|
|
||
| error.code = 'ENOENT'; | ||
| mockReadFile.mockRejectedValue(error); | ||
|
|
||
| const result = await fileTokenStorage.load(); | ||
|
|
||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should return null when file read fails with other error', async () => { | ||
| mockReadFile.mockRejectedValue(new Error('Read failed')); | ||
|
|
||
| const result = await fileTokenStorage.load(); | ||
|
|
||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should return null when token data is invalid', async () => { | ||
| const invalidData = { invalid: 'data' }; | ||
|
|
||
| mockReadFile.mockResolvedValue(JSON.stringify(invalidData)); | ||
|
|
||
| const result = await fileTokenStorage.load(); | ||
|
|
||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should return null when token data has missing fields', async () => { | ||
| const invalidData = { | ||
| refresh_token: 'test-refresh-token', | ||
| expires_at: Date.now() + 3600000, | ||
| token_type: 'Bearer', | ||
| }; | ||
|
|
||
| mockReadFile.mockResolvedValue(JSON.stringify(invalidData)); | ||
|
|
||
| const result = await fileTokenStorage.load(); | ||
|
|
||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should return null when token data is null', async () => { | ||
| mockReadFile.mockResolvedValue('null'); | ||
|
|
||
| const result = await fileTokenStorage.load(); | ||
|
|
||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should return null when token data is not an object', async () => { | ||
| mockReadFile.mockResolvedValue('"string"'); | ||
|
|
||
| const result = await fileTokenStorage.load(); | ||
|
|
||
| expect(result).toBeNull(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { promises as fs } from 'fs'; | ||
| import { join } from 'path'; | ||
|
|
||
| import { OAuth2Error } from '../../common/errors'; | ||
| import { TokenStorageProvider } from './types'; | ||
| import { TokenData } from './types'; | ||
|
|
||
| export class FileTokenStorage implements TokenStorageProvider { | ||
| private readonly tokenFilePath: string; | ||
| private readonly serviceName: string; | ||
|
|
||
| constructor(serviceName: string = 'qbo', tokenFilePath?: string) { | ||
| this.serviceName = serviceName; | ||
| this.tokenFilePath = | ||
| tokenFilePath || | ||
| join(process.cwd(), 'data', 'oauth2_tokens', `${serviceName}.json`); | ||
| } | ||
|
|
||
| async save(tokenData: TokenData): Promise<void> { | ||
| try { | ||
| const dir = join(this.tokenFilePath, '..'); | ||
|
|
||
| await fs.mkdir(dir, { recursive: true }); | ||
| await fs.writeFile( | ||
| this.tokenFilePath, | ||
| JSON.stringify(tokenData, null, 2), | ||
| ); | ||
| } catch { | ||
| throw new OAuth2Error('Failed to save token data to file'); | ||
| } | ||
| } | ||
|
|
||
| async load(): Promise<TokenData | null> { | ||
| try { | ||
| const data = await fs.readFile(this.tokenFilePath, 'utf8'); | ||
| const tokenData = JSON.parse(data) as TokenData; | ||
|
|
||
| if (!this.isValidTokenData(tokenData)) { | ||
| return null; | ||
| } | ||
|
|
||
| return tokenData; | ||
| } catch (error) { | ||
| if ((error as NodeJS.ErrnoException).code === 'ENOENT') { | ||
| return null; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
|
|
||
| async clear(): Promise<void> { | ||
| try { | ||
| await fs.unlink(this.tokenFilePath); | ||
| } catch (error) { | ||
| if ((error as NodeJS.ErrnoException).code !== 'ENOENT') { | ||
| throw new OAuth2Error('Failed to clear token data from file'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private isValidTokenData(data: unknown): data is TokenData { | ||
| return ( | ||
| typeof data === 'object' && | ||
| data !== null && | ||
| typeof (data as TokenData).access_token === 'string' && | ||
| typeof (data as TokenData).refresh_token === 'string' && | ||
| typeof (data as TokenData).expires_at === 'number' && | ||
| typeof (data as TokenData).token_type === 'string' | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.