Skip to content
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

feat: forward id_token to jwt and signIn callbacks #1024

Merged
merged 1 commit into from
Jan 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/server/lib/oauth/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ class OAuthCallbackError extends Error {

* @TODO Refactor to use promises and not callbacks
*/
export default async function oAuthCallback (req, provider, csrfToken) {
export default async function oAuthCallback (req, csrfToken) {
// The "user" object is specific to the Apple provider and is provided on first sign in
// e.g. {"name":{"firstName":"Johnny","lastName":"Appleseed"},"email":"[email protected]"}
let { oauth_token, oauth_verifier, code, user, state } = req.query // eslint-disable-line camelcase
const provider = req.options.providers[req.options.provider]
const client = oAuthClient(provider)

if (provider.version?.startsWith('2.')) {
Expand Down Expand Up @@ -86,6 +87,8 @@ export default async function oAuthCallback (req, provider, csrfToken) {
// Support services that use OpenID ID Tokens to encode profile data
const profileData = decodeIdToken(results.id_token)

profileData.idToken = results.id_token

return _getProfile(error, profileData, accessToken, refreshToken, provider, user)
} else {
// Use custom get() method for oAuth2 flows
Expand All @@ -97,6 +100,7 @@ export default async function oAuthCallback (req, provider, csrfToken) {
accessToken,
results,
async (error, profileData) => {
profileData.idToken = results.id_token
result = await _getProfile(error, profileData, accessToken, refreshToken, provider)
}
)
Expand All @@ -122,6 +126,7 @@ export default async function oAuthCallback (req, provider, csrfToken) {
accessToken,
refreshToken,
async (error, profileData) => {
profileData.idToken = results.id_token
result = await _getProfile(error, profileData, accessToken, refreshToken, provider)
}
)
Expand All @@ -135,7 +140,7 @@ export default async function oAuthCallback (req, provider, csrfToken) {
* //6/30/2020 @geraldnolan added userData parameter to attach additional data to the profileData object
* Returns profile, raw profile and auth provider details
*/
async function _getProfile (error, profileData, accessToken, refreshToken, provider, userData) {
async function _getProfile (error, profileData, accessToken, refreshToken, provider, userData, idToken) {
if (error) {
logger.error('OAUTH_GET_PROFILE_ERROR', error)
throw new OAuthCallbackError(error)
Expand All @@ -152,6 +157,8 @@ async function _getProfile (error, profileData, accessToken, refreshToken, provi
profileData.user = userData
}

profileData.idToken = idToken

logger.debug('PROFILE_DATA', profileData)

const profile = await provider.profile(profileData)
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default async function callback (req, res) {

if (type === 'oauth') {
try {
const { profile, account, OAuthProfile } = await oAuthCallback(req, provider, csrfToken)
const { profile, account, OAuthProfile } = await oAuthCallback(req, csrfToken)
try {
// Make it easier to debug when adding a new provider
logger.debug('OAUTH_CALLBACK_RESPONSE', { profile, account, OAuthProfile })
Expand Down