-
-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
109 additions
and
82 deletions.
There are no files selected for viewing
This file contains 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,50 +1,62 @@ | ||
import { GoogleAuth } from 'google-auth-library'; | ||
import { prisma } from '../../prisma'; | ||
import { EmailQueue } from '../types/email'; | ||
import { GoogleAuth } from "google-auth-library"; | ||
import { prisma } from "../../prisma"; | ||
import { EmailQueue } from "../types/email"; | ||
|
||
export class AuthService { | ||
public static generateXOAuth2Token(username: string, accessToken: string): string { | ||
public static generateXOAuth2Token( | ||
username: string, | ||
accessToken: string | ||
): string { | ||
const authString = [ | ||
`user=${username}`, | ||
`auth=Bearer ${accessToken}`, | ||
'', | ||
'' | ||
].join('\x01'); | ||
return Buffer.from(authString).toString('base64'); | ||
"", | ||
"", | ||
].join("\x01"); | ||
return Buffer.from(authString).toString("base64"); | ||
} | ||
|
||
static async getValidAccessToken(queue: EmailQueue): Promise<string> { | ||
const { clientId, clientSecret, refreshToken, accessToken, expiresIn } = queue; | ||
const { clientId, clientSecret, refreshToken, accessToken, expiresIn } = | ||
queue; | ||
|
||
// Check if token is still valid | ||
const now = Math.floor(Date.now() / 1000); | ||
if (accessToken && expiresIn && now < expiresIn) { | ||
return accessToken; | ||
} | ||
|
||
// Initialize GoogleAuth client | ||
const auth = new GoogleAuth({ | ||
clientOptions: { clientId, clientSecret } | ||
clientOptions: { | ||
clientId: clientId, | ||
clientSecret: clientSecret, | ||
}, | ||
}); | ||
|
||
const oauth2Client = auth.fromJSON({ | ||
client_id: clientId, | ||
client_secret: clientSecret, | ||
refresh_token: refreshToken | ||
refresh_token: refreshToken, | ||
}); | ||
|
||
// Refresh the token if expired | ||
const tokenInfo = await oauth2Client.getAccessToken(); | ||
if (!tokenInfo.token) { | ||
throw new Error('Unable to refresh access token.'); | ||
} | ||
|
||
const expiryDate = (expiresIn || 0) + 3600; | ||
await prisma.emailQueue.update({ | ||
where: { id: queue.id }, | ||
data: { | ||
accessToken: tokenInfo.token, | ||
expiresIn: expiryDate | ||
} | ||
}); | ||
const expiryDate = expiresIn! + 3600; | ||
|
||
return tokenInfo.token; | ||
if (tokenInfo.token) { | ||
await prisma.emailQueue.update({ | ||
where: { id: queue.id }, | ||
data: { | ||
accessToken: tokenInfo.token, | ||
expiresIn: expiryDate, | ||
}, | ||
}); | ||
|
||
return tokenInfo.token; | ||
} else { | ||
throw new Error("Unable to refresh access token."); | ||
} | ||
} | ||
} | ||
} |
This file contains 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