Преглед изворни кода

:construction: Set up first attempt at reissuing access token

tags/0.0.3^2
tomit4 пре 2 година
родитељ
комит
e3e93694f2

+ 1
- 1
backend/lib/routes/user/getaccess.js Прегледај датотеку

29
             const token = await userService.createToken({
29
             const token = await userService.createToken({
30
                 ...res,
30
                 ...res,
31
                 // NOTE: Set Expiration Time for Access Token Here
31
                 // NOTE: Set Expiration Time for Access Token Here
32
-                expires: 60 * 2,
32
+                expires: Math.floor(Date.now() / 1000) + 60 * 2,
33
             })
33
             })
34
             try {
34
             try {
35
                 const response = h.response({
35
                 const response = h.response({

+ 1
- 1
backend/lib/routes/user/getsession.js Прегледај датотеку

29
             const token = await userService.createToken({
29
             const token = await userService.createToken({
30
                 ...res,
30
                 ...res,
31
                 // NOTE: Set Expiration Time for Session Token Here
31
                 // NOTE: Set Expiration Time for Session Token Here
32
-                expires: 60 * 10,
32
+                expires: Math.floor(Date.now() / 1000) + 60 * 10,
33
             })
33
             })
34
             try {
34
             try {
35
                 const response = h.response({
35
                 const response = h.response({

+ 1
- 0
frontend/src/utils/db.js Прегледај датотеку

43
             const res = await fetch(`${remote}${endpoint}`, header)
43
             const res = await fetch(`${remote}${endpoint}`, header)
44
             const jsonRes = await res.json()
44
             const jsonRes = await res.json()
45
             if (!res.ok) {
45
             if (!res.ok) {
46
+                // NOTE: Somewhat hacky workaround here to get auth working
46
                 if (res.status === 401) {
47
                 if (res.status === 401) {
47
                     return { ...jsonRes.data, status: res.status }
48
                     return { ...jsonRes.data, status: res.status }
48
                 } else {
49
                 } else {

+ 53
- 10
frontend/src/views/OnboardingView.vue Прегледај датотеку

69
         // since it has to be done on created() and every step after 6...
69
         // since it has to be done on created() and every step after 6...
70
         sessionToken = this.grabStoredCookie('siimee_session')
70
         sessionToken = this.grabStoredCookie('siimee_session')
71
         accessToken = this.grabStoredCookie('siimee_access')
71
         accessToken = this.grabStoredCookie('siimee_access')
72
+        // TODO: More graceful way of throwing exceptions if sessionData is not defined??
72
         try {
73
         try {
73
-            await this.verifySessionToken(sessionToken)
74
-            const sessionData = await this.verifyAccessToken(accessToken)
74
+            const sessionData = await this.verifyBothTokens(
75
+                sessionToken,
76
+                accessToken,
77
+            )
75
             await this.isEmailInRegistry(sessionData.payload.email)
78
             await this.isEmailInRegistry(sessionData.payload.email)
76
             // TODO: Validate All routes hit by these methods using tokens in headers
79
             // TODO: Validate All routes hit by these methods using tokens in headers
77
             const userId = await this.grabUserIdByEmail(
80
             const userId = await this.grabUserIdByEmail(
107
                 cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
110
                 cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
108
             return cookieVal
111
             return cookieVal
109
         },
112
         },
113
+        async verifyBothTokens(sessionToken, accessToken) {
114
+            const sessionTokenIsValid = await this.verifySessionToken(
115
+                sessionToken,
116
+            )
117
+            const accessTokenIsValid = await this.verifyAccessToken(accessToken)
118
+            if (
119
+                accessTokenIsValid.status === 401 &&
120
+                sessionTokenIsValid.status !== 401
121
+            ) {
122
+                console.warn(
123
+                    'WARNING :=> Access Token Expired, but Session Token Is Still Valid, reissuing Access Token...',
124
+                )
125
+                // NOTE: Whether to implement newSessionToken is unclear in notes,
126
+                // but without this, user session will expire in 10 minutes no matter what...
127
+                const newSessionToken =
128
+                    await this.authenticator.getSessionToken(
129
+                        sessionTokenIsValid.payload,
130
+                    )
131
+                const newAccessToken = await this.authenticator.getAccessToken(
132
+                    sessionTokenIsValid.payload,
133
+                )
134
+                sessionToken = newSessionToken
135
+                accessToken = newAccessToken
136
+                document.cookie = `siimee_access=${newSessionToken}; max-age=600; path=/; secure`
137
+                document.cookie = `siimee_access=${newAccessToken}; max-age=600; path=/; secure`
138
+                const newAccessTokenIsValid = await this.verifyAccessToken(
139
+                    newAccessToken,
140
+                )
141
+                return newAccessTokenIsValid
142
+            } else if (
143
+                accessTokenIsValid.status === 401 &&
144
+                sessionTokenIsValid.status === 401
145
+            ) {
146
+                sessionToken = null
147
+                accessToken = null
148
+                currentProfileId = null
149
+                throw new Error('Both Session and Access Token Are Expired!!')
150
+            } else return accessTokenIsValid
151
+        },
110
         async verifySessionToken(sessionToken) {
152
         async verifySessionToken(sessionToken) {
111
             if (!sessionToken) {
153
             if (!sessionToken) {
112
-                console.warn('WARNING :=> sessionToken is not defined')
154
+                return console.warn('WARNING :=> sessionToken is not defined')
113
             } else return await this.validateToken(sessionToken)
155
             } else return await this.validateToken(sessionToken)
114
         },
156
         },
115
         async verifyAccessToken(accessToken) {
157
         async verifyAccessToken(accessToken) {
116
             if (!accessToken) {
158
             if (!accessToken) {
117
-                console.warn('WARNING :=> accessToken is not defined')
159
+                return console.warn('WARNING :=> accessToken is not defined')
118
             } else return await this.validateToken(accessToken)
160
             } else return await this.validateToken(accessToken)
119
         },
161
         },
120
         async validateToken(token) {
162
         async validateToken(token) {
121
             const validatedToken = await this.authenticator.validateSession(
163
             const validatedToken = await this.authenticator.validateSession(
122
                 token,
164
                 token,
123
             )
165
             )
124
-            // TODO: consolidate both verifyToken into verifyBothTokens() method
125
-            // and utilize presence of status to determine auth status
126
-            if (validatedToken.status) {
127
-                console.log('validatedToken.status :=>', validatedToken.status)
128
-            }
129
             if (validatedToken.error) {
166
             if (validatedToken.error) {
130
                 throw new Error(validatedToken.error)
167
                 throw new Error(validatedToken.error)
131
             } else {
168
             } else {
137
                 await this.authenticator.checkIfEmailIsRegistered(email)
174
                 await this.authenticator.checkIfEmailIsRegistered(email)
138
             if (!emailIsInRegistry) {
175
             if (!emailIsInRegistry) {
139
                 throw new Error('Email Is NOT in Registry!')
176
                 throw new Error('Email Is NOT in Registry!')
140
-            } else return emailIsInRegistry // true
177
+            } else return emailIsInRegistry
141
         },
178
         },
142
         async grabUserIdByEmail(email) {
179
         async grabUserIdByEmail(email) {
143
             const user = await fetchUserByEmail(email)
180
             const user = await fetchUserByEmail(email)
207
                         this.responses[this.responses.length - 1],
244
                         this.responses[this.responses.length - 1],
208
                         currentProfileId,
245
                         currentProfileId,
209
                     )
246
                     )
247
+                    try {
248
+                        this.verifyBothTokens(sessionToken, accessToken)
249
+                    } catch (err) {
250
+                        console.error('ERROR :=>', err)
251
+                        this.goToStep(0)
252
+                    }
210
                 }
253
                 }
211
                 if (k === 'aspects') return
254
                 if (k === 'aspects') return
212
             }
255
             }

Loading…
Откажи
Сачувај