-
Notifications
You must be signed in to change notification settings - Fork 731
Add GitHub OAuth authentication support #983
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ce82a79
feat: support for OAuth with GitHub
skwowet 614bcc3
Merge branch 'main' into oauth
skwowet 828d6a9
Merge branch 'main' into oauth
skwowet e32c08d
Merge branch 'main' into oauth
skwowet 51de08b
Merge branch 'main' into oauth
skwowet a0bb75e
fix: add githubStrategy for the oauth
skwowet cac1140
Merge branch 'oauth' of github.com:yeganathan18/crowd.dev into oauth
skwowet 3e0545b
Merge branch 'main' into oauth
skwowet 3b356c7
Merge branch 'main' into oauth
skwowet 9a50055
add githubStratetgy to the passport middleware
skwowet 1776e8e
Merge branch 'main' into oauth
skwowet 2e958eb
Merge branch 'oauth' of github.com:yeganathan18/crowd.dev into oauth
skwowet 43c2202
fix import and prettify the code
skwowet cbded40
revert: user scopes and emailVerified
skwowet 60f5f92
fix eslint erros and run prettier
skwowet 45c8a2d
Merge branch 'main' into oauth
skwowet b3ca528
Merge branch 'main' into oauth
skwowet c7ae75b
add code structure improvements
skwowet 195e3bb
prettify the code and add remix icon for the github logo
skwowet 4ad459b
Merge branch 'oauth' of github.com:yeganathan18/crowd.dev into oauth
skwowet 85dbc7e
Merge branch 'main' into oauth
skwowet 067b266
remove emailVerified field from profile and introduce gh callbackURL …
skwowet d284009
fix eslint issue
skwowet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
50 changes: 50 additions & 0 deletions
50
backend/src/services/auth/passportStrategies/githubStrategy.ts
This file contains hidden or 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,50 @@ | ||
| import { get } from 'lodash' | ||
| import GithubStrategy from 'passport-github2' | ||
| import { getServiceChildLogger } from '@crowd/logging' | ||
| import { GITHUB_CONFIG } from '../../../conf' | ||
| import { databaseInit } from '../../../database/databaseConnection' | ||
| import AuthService from '../authService' | ||
| import { splitFullName } from '../../../utils/splitName' | ||
| import { AuthProvider } from '../../../types/common' | ||
|
|
||
| const log = getServiceChildLogger('AuthSocial') | ||
|
|
||
| export function getGithubStrategy(): GithubStrategy { | ||
| return new GithubStrategy( | ||
| { | ||
| clientID: GITHUB_CONFIG.clientId, | ||
| clientSecret: GITHUB_CONFIG.clientSecret, | ||
| callbackURL: GITHUB_CONFIG.callbackUrl, | ||
| scope: ['user:email'], // Request email scope | ||
| }, | ||
| (accessToken, refreshToken, profile, done) => { | ||
| databaseInit() | ||
| .then((database) => { | ||
| const email = get(profile, 'emails[0].value') | ||
| // GitHub user's profile doesn't include 'verified' field | ||
| // However, GitHub accounts require email verification for activation | ||
| const emailVerified = !!email | ||
| const displayName = get(profile, 'displayName') | ||
| const { firstName, lastName } = splitFullName(displayName) | ||
|
|
||
| return AuthService.signinFromSocial( | ||
| AuthProvider.GITHUB, | ||
| profile.id, | ||
| email, | ||
| emailVerified, | ||
| firstName, | ||
| lastName, | ||
| displayName, | ||
| { database }, | ||
| ) | ||
| }) | ||
| .then((jwtToken) => { | ||
| done(null, jwtToken) | ||
| }) | ||
| .catch((error) => { | ||
| log.error(error, 'Error while handling github auth!') | ||
| done(error, null) | ||
| }) | ||
| }, | ||
| ) | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,15 @@ | ||
| export function splitFullName(fullName) { | ||
| let firstName | ||
| let lastName | ||
|
|
||
| if (fullName && fullName.split(' ').length > 1) { | ||
| const [firstNameArray, ...lastNameArray] = fullName.split(' ') | ||
| firstName = firstNameArray | ||
| lastName = lastNameArray.join(' ') | ||
| } else { | ||
| firstName = fullName || null | ||
| lastName = null | ||
| } | ||
|
|
||
| return { firstName, lastName } | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.