supaapps-auth 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,153 +0,0 @@
1
- import axios from 'axios';
2
- import MockAdapter from 'axios-mock-adapter';
3
- import { AuthManager } from '../src/AuthManager';
4
- import { AuthEventType } from '../src/types';
5
- import { basename } from 'path';
6
-
7
- const mock = new MockAdapter(axios);
8
-
9
-
10
- const tokenThatWontExpire1 = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiZmlyc3RfbmFtZSI6IkpvaG4gRG9lIiwibGFzdF9uYW1lIjoiRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJzY29wZXMiOiIvcm9vdC8qIiwiZXhwIjo5OTk5OTk5OTk5LCJpZCI6MiwiaXNzIjoxMjMsImF1ZCI6InRlc3RpbmcifQ.843X4Zq2WgNSu8fjRKx-kd_FbDqY_eVkgu2wZZbhhwE';
11
- const tokenThatWontExpire2 = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiZmlyc3RfbmFtZSI6IkpvaG4gRG9lIiwibGFzdF9uYW1lIjoiRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJzY29wZXMiOiIvcm9vdC8qIiwiZXhwIjo5OTk5OTk5OTk5LCJpZCI6MiwiaXNzIjoxMjMsImF1ZCI6InRlc3RpbmcifQ.843X4Zq2WgNSu8fjRKx-kd_FbDqY_eVkgu2wZZbhhwE';
12
- const tokenThatExpired = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiZmlyc3RfbmFtZSI6IkpvaG4gRG9lIiwibGFzdF9uYW1lIjoiRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJzY29wZXMiOiIvcm9vdC8qIiwiZXhwIjo1MDAsImlkIjoyLCJpc3MiOjEyMywiYXVkIjoidGVzdGluZyJ9.ungpbhHfCM5ZP5oiZ1RnMkJ-NKJI8s3_IPJptjyKHR4';
13
-
14
-
15
-
16
-
17
- describe('AuthManager Tests', () => {
18
- beforeAll(() => {
19
- jest.spyOn(localStorage, 'getItem');
20
- });
21
-
22
- beforeEach(() => {
23
- localStorage.clear(); // Clear localStorage before each test
24
- AuthManager.resetInstance(); // Reset singleton instance
25
- });
26
-
27
-
28
-
29
- it('singleton: should throw when getting instance without initialization', () => {
30
- expect(() => AuthManager.getInstance()).toThrow('AuthManager not initialized');
31
- });
32
-
33
- it('singleton: should create an instance', () => {
34
- const loginCallback = jest.fn();
35
- const manager = AuthManager.initialize('http://auth-server.com/', 'example-realm', 'http://myapp.com/callback', loginCallback);
36
- expect(AuthManager.getInstance()).toBeInstanceOf(AuthManager);
37
- });
38
-
39
- it('PKCE Generation: generates a PKCE pair and stores in local storage', () => {
40
- const loginCallback = jest.fn();
41
- const manager = AuthManager.initialize('http://auth-server.com/', 'example-realm', 'http://myapp.com/callback', loginCallback);
42
- // Accessing the private method by casting to any
43
- const pkce = (manager as any).generatePKCEPair();
44
-
45
- expect(pkce).toHaveProperty('verifier');
46
- expect(pkce).toHaveProperty('challenge');
47
- expect(pkce.verifier).toMatch(/[\w-_]+/);
48
- expect(pkce.challenge).toMatch(/[\w-_]+/);
49
-
50
- expect(pkce.verifier).toMatch(/[\w-_=]+/);
51
- expect(pkce.challenge).toMatch(/[\w-_=]+/);
52
- expect(localStorage.setItem).toHaveBeenCalledWith('codeVerifier', expect.anything());
53
- expect(localStorage.setItem).toHaveBeenCalledWith('codeChallenge', expect.anything());
54
- });
55
-
56
-
57
- it('refreshes access token when expired', async () => {
58
- mock.onPost('http://auth-server.com/auth/refresh').reply(200, {
59
- access_token: tokenThatWontExpire2,
60
- refresh_token: 'newRefreshToken'
61
- });
62
-
63
- const loginCallback = jest.fn();
64
- // check that we set localstorage correct
65
- localStorage.setItem('access_token', tokenThatExpired);
66
- localStorage.setItem('refresh_token', 'mockRefreshToken');
67
-
68
- const refresh = localStorage.getItem('refresh_token');
69
- expect(refresh).toEqual('mockRefreshToken');
70
-
71
- const manager = AuthManager.initialize('http://auth-server.com/', 'example-realm', 'http://myapp.com/callback', loginCallback);
72
- const token = await manager.refreshAccessToken();
73
-
74
- expect(token).toEqual(tokenThatWontExpire2);
75
- expect(localStorage.setItem).toHaveBeenCalledWith('access_token', tokenThatWontExpire2);
76
- expect(localStorage.setItem).toHaveBeenCalledWith('refresh_token', 'newRefreshToken');
77
- });
78
-
79
-
80
- describe('AuthManager Tests isolated ', () => {
81
- it('doesn\'t refresh access token when its not expired', async () => {
82
- const stateChange = jest.fn();
83
-
84
-
85
- // check that we set localstorage correct
86
- localStorage.setItem('access_token', tokenThatWontExpire1);
87
- localStorage.setItem('refresh_token', 'mockRefreshToken');
88
-
89
- const manager = AuthManager.initialize('http://auth-server.com/', 'example-realm', 'http://myapp.com/callback', stateChange);
90
-
91
- const currentCallCount = (localStorage.getItem as jest.Mock).mock.calls.length;
92
-
93
- const token = await manager.getAccessToken();
94
-
95
- expect(localStorage.getItem).toHaveBeenCalledTimes(currentCallCount + 1);
96
-
97
- });
98
- });
99
-
100
- it('throws an error when no refresh token is found', async () => {
101
- localStorage.removeItem('refresh_token');
102
-
103
- const loginCallback = jest.fn();
104
- const manager = AuthManager.initialize('http://auth-server.com/', 'example-realm', 'http://myapp.com/callback', loginCallback);
105
-
106
- await expect(manager.refreshAccessToken()).rejects.toThrow('No refresh token found');
107
- await expect(loginCallback).toHaveBeenCalledWith({
108
- type: AuthEventType.REFRESH_FAILED,
109
- });
110
- });
111
-
112
- it('logs in using PKCE and updates local storage', async () => {
113
- localStorage.setItem('codeVerifier', 'mockCodeVerifier');
114
- /*
115
- {
116
- "sub": "1234567890",
117
- "name": "John Doe",
118
- "iat": 1516239022
119
- }
120
- */
121
- const accessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
122
-
123
-
124
- mock.onPost('http://auth-server.com/auth/pkce_exchange').reply(200, {
125
- access_token: accessToken,
126
- refresh_token: 'validRefreshToken'
127
- });
128
-
129
- const loginCallback = jest.fn();
130
- const manager = AuthManager.initialize('http://auth-server.com/', 'example-realm', 'http://myapp.com/callback', loginCallback);
131
- await manager.loginUsingPkce('mockCode');
132
-
133
- expect(localStorage.setItem).toHaveBeenCalledWith('access_token', accessToken);
134
- expect(localStorage.setItem).toHaveBeenCalledWith('refresh_token', 'validRefreshToken');
135
- const userSub = JSON.parse(localStorage.getItem('user') ?? '').sub;
136
- expect(userSub).toEqual('1234567890');
137
- });
138
-
139
- it('logs out and clears local storage', async () => {
140
- mock.onPost('http://auth-server.com/auth/logout').reply(200);
141
-
142
- const loginCallback = jest.fn();
143
- const manager = AuthManager.initialize('http://auth-server.com/', 'example-realm', 'http://myapp.com/callback', loginCallback);
144
- localStorage.setItem('access_token', tokenThatWontExpire1);
145
- await manager.logout();
146
-
147
- expect(localStorage.removeItem).toHaveBeenCalledWith('access_token');
148
- expect(localStorage.removeItem).toHaveBeenCalledWith('refresh_token');
149
- });
150
-
151
-
152
-
153
- });
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "outDir": "./dist",
4
- "declaration": true,
5
- "module": "commonjs",
6
- "target": "ES6"
7
- },
8
- "include": ["src/**/*.ts"]
9
- }