-
-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: integrate gitHub login * feat: dynamic social auth module * feat: add github auth env validation
- Loading branch information
Showing
27 changed files
with
504 additions
and
77 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
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,5 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class GithubGuard extends AuthGuard('github') {} |
39 changes: 39 additions & 0 deletions
39
apps/nestjs-backend/src/features/auth/oauth/oauth.store.ts
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,39 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { getRandomString } from '@teable/core'; | ||
import type { Request } from 'express'; | ||
import { CacheService } from '../../../cache/cache.service'; | ||
import type { IOauth2State } from '../../../cache/types'; | ||
import { second } from '../../../utils/second'; | ||
|
||
@Injectable() | ||
export class OauthStoreService { | ||
key: string = 'oauth2:'; | ||
|
||
constructor(private readonly cacheService: CacheService) {} | ||
|
||
async store(req: Request, callback: (err: unknown, stateId: string) => void) { | ||
const random = getRandomString(16); | ||
await this.cacheService.set( | ||
`oauth2:${random}`, | ||
{ | ||
redirectUri: req.query.redirect_uri as string, | ||
}, | ||
second('12h') | ||
); | ||
callback(null, random); | ||
} | ||
|
||
async verify( | ||
_req: unknown, | ||
stateId: string, | ||
callback: (err: unknown, ok: boolean, state: IOauth2State | string) => void | ||
) { | ||
const state = await this.cacheService.get(`oauth2:${stateId}`); | ||
if (state) { | ||
await this.cacheService.del(`oauth2:${stateId}`); | ||
callback(null, true, state); | ||
} else { | ||
callback(null, false, 'Invalid authorization request state'); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
apps/nestjs-backend/src/features/auth/social/github/github.controller.ts
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,27 @@ | ||
import { Controller, Get, Req, Res, UseGuards } from '@nestjs/common'; | ||
import { Response } from 'express'; | ||
import type { IOauth2State } from '../../../../cache/types'; | ||
import { Public } from '../../decorators/public.decorator'; | ||
import { GithubGuard } from '../../guard/github.guard'; | ||
|
||
@Controller('api/auth') | ||
export class GithubController { | ||
@Get('/github') | ||
@Public() | ||
@UseGuards(GithubGuard) | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
async githubAuthenticate() {} | ||
|
||
@Get('/github/callback') | ||
@Public() | ||
@UseGuards(GithubGuard) | ||
async githubCallback(@Req() req: Express.Request, @Res({ passthrough: true }) res: Response) { | ||
const user = req.user!; | ||
// set cookie, passport login | ||
await new Promise<void>((resolve, reject) => { | ||
req.login(user, (err) => (err ? reject(err) : resolve())); | ||
}); | ||
const redirectUri = (req.authInfo as { state: IOauth2State })?.state?.redirectUri; | ||
return res.redirect(redirectUri || '/'); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
apps/nestjs-backend/src/features/auth/social/github/github.module.ts
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,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { UserModule } from '../../../user/user.module'; | ||
import { OauthStoreService } from '../../oauth/oauth.store'; | ||
import { GithubStrategy } from '../../strategies/github.strategy'; | ||
import { GithubController } from './github.controller'; | ||
|
||
@Module({ | ||
imports: [UserModule], | ||
providers: [GithubStrategy, OauthStoreService], | ||
exports: [], | ||
controllers: [GithubController], | ||
}) | ||
export class GithubModule {} |
12 changes: 12 additions & 0 deletions
12
apps/nestjs-backend/src/features/auth/social/social.module.ts
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,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ConditionalModule } from '@nestjs/config'; | ||
import { GithubModule } from './github/github.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConditionalModule.registerWhen(GithubModule, (env) => { | ||
return Boolean(env.SOCIAL_AUTH_PROVIDERS?.split(',')?.includes('github')); | ||
}), | ||
], | ||
}) | ||
export class SocialModule {} |
Oops, something went wrong.