forked from danny-avila/LibreChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🍎 feat: Apple auth (danny-avila#5473)
* implemented Apple Auth login. Closes: danny-avila#3438 TODO: - write config Doc * removed some comments * removed comment * Add unit tests for Apple login strategy Introduce comprehensive tests for the Apple login strategy, covering new user creation, existing user updates, and error handling scenarios during the authentication flow. Mocks implemented for external dependencies to ensure isolated testing. * Remove unnecessary blank line in socialLogins.js
- Loading branch information
1 parent
e81fd75
commit 4cb69d1
Showing
21 changed files
with
545 additions
and
13 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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const socialLogin = require('./socialLogin'); | ||
const { Strategy: AppleStrategy } = require('passport-apple'); | ||
const { logger } = require('~/config'); | ||
const jwt = require('jsonwebtoken'); | ||
|
||
/** | ||
* Extract profile details from the decoded idToken | ||
* @param {Object} params - Parameters from the verify callback | ||
* @param {string} params.idToken - The ID token received from Apple | ||
* @param {Object} params.profile - The profile object (may contain partial info) | ||
* @returns {Object} - The extracted user profile details | ||
*/ | ||
const getProfileDetails = ({ idToken, profile }) => { | ||
if (!idToken) { | ||
logger.error('idToken is missing'); | ||
throw new Error('idToken is missing'); | ||
} | ||
|
||
const decoded = jwt.decode(idToken); | ||
|
||
logger.debug( | ||
`Decoded Apple JWT: ${JSON.stringify(decoded, null, 2)}`, | ||
); | ||
|
||
return { | ||
email: decoded.email, | ||
id: decoded.sub, | ||
avatarUrl: null, // Apple does not provide an avatar URL | ||
username: decoded.email | ||
? decoded.email.split('@')[0].toLowerCase() | ||
: `user_${decoded.sub}`, | ||
name: decoded.name | ||
? `${decoded.name.firstName} ${decoded.name.lastName}` | ||
: profile.displayName || null, | ||
emailVerified: true, // Apple verifies the email | ||
}; | ||
}; | ||
|
||
// Initialize the social login handler for Apple | ||
const appleLogin = socialLogin('apple', getProfileDetails); | ||
|
||
module.exports = () => | ||
new AppleStrategy( | ||
{ | ||
clientID: process.env.APPLE_CLIENT_ID, | ||
teamID: process.env.APPLE_TEAM_ID, | ||
callbackURL: `${process.env.DOMAIN_SERVER}${process.env.APPLE_CALLBACK_URL}`, | ||
keyID: process.env.APPLE_KEY_ID, | ||
privateKeyLocation: process.env.APPLE_PRIVATE_KEY_PATH, | ||
passReqToCallback: false, // Set to true if you need to access the request in the callback | ||
}, | ||
appleLogin, | ||
); |
Oops, something went wrong.