Skip to content

Commit

Permalink
Merge pull request #286 from sasjs/issue-279
Browse files Browse the repository at this point in the history
fix: return same tokens if not expired
  • Loading branch information
allanbowe authored Sep 21, 2022
2 parents a810f6c + 6f5566d commit 5c40d8a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
10 changes: 10 additions & 0 deletions api/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { InfoJWT } from '../types'
import {
generateAccessToken,
generateRefreshToken,
getTokensFromDB,
removeTokensInDB,
saveTokensInDB
} from '../utils'
Expand Down Expand Up @@ -73,6 +74,15 @@ const token = async (data: any): Promise<TokenResponse> => {

AuthController.deleteCode(userInfo.userId, clientId)

// get tokens from DB
const existingTokens = await getTokensFromDB(userInfo.userId, clientId)
if (existingTokens) {
return {
accessToken: existingTokens.accessToken,
refreshToken: existingTokens.refreshToken
}
}

const accessToken = generateAccessToken(userInfo)
const refreshToken = generateRefreshToken(userInfo)

Expand Down
2 changes: 1 addition & 1 deletion api/src/controllers/stp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const execute = async (
)

if (result instanceof Buffer) {
; (req as any).sasHeaders = httpHeaders
;(req as any).sasHeaders = httpHeaders
}

return result
Expand Down
2 changes: 1 addition & 1 deletion api/src/routes/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
authenticateRefreshToken
} from '../../middlewares'

import { authorizeValidation, tokenValidation } from '../../utils'
import { tokenValidation } from '../../utils'
import { InfoJWT } from '../../types'

const authRouter = express.Router()
Expand Down
34 changes: 34 additions & 0 deletions api/src/utils/getTokensFromDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import jwt from 'jsonwebtoken'
import User from '../model/User'

export const getTokensFromDB = async (userId: number, clientId: string) => {
const user = await User.findOne({ id: userId })
if (!user) return

const currentTokenObj = user.tokens.find(
(tokenObj: any) => tokenObj.clientId === clientId
)

if (currentTokenObj) {
const accessToken = currentTokenObj.accessToken
const refreshToken = currentTokenObj.refreshToken

const verifiedAccessToken: any = jwt.verify(
accessToken,
process.secrets.ACCESS_TOKEN_SECRET
)

const verifiedRefreshToken: any = jwt.verify(
refreshToken,
process.secrets.REFRESH_TOKEN_SECRET
)

if (
verifiedAccessToken?.userId === userId &&
verifiedAccessToken?.clientId === clientId &&
verifiedRefreshToken?.userId === userId &&
verifiedRefreshToken?.clientId === clientId
)
return { accessToken, refreshToken }
}
}
1 change: 1 addition & 0 deletions api/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from './getDesktopFields'
export * from './getPreProgramVariables'
export * from './getRunTimeAndFilePath'
export * from './getServerUrl'
export * from './getTokensFromDB'
export * from './instantiateLogger'
export * from './isDebugOn'
export * from './isPublicRoute'
Expand Down

0 comments on commit 5c40d8a

Please sign in to comment.