-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add skeleton for SSO, add google as testing provider
- Loading branch information
1 parent
27e0c7b
commit b0280fb
Showing
25 changed files
with
338 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { SSOAuthHelper } from '@/helpers/auth/sso.service'; | ||
import { | ||
Controller, | ||
ForbiddenException, | ||
Get, | ||
Param, | ||
Query, | ||
} from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { SSOUrlAuthObj } from 'vitnode-shared/auth/sso.dto'; | ||
|
||
@ApiTags('Core') | ||
@Controller('core/auth/sso') | ||
export class SSOAuthController { | ||
constructor( | ||
private readonly configService: ConfigService, | ||
private readonly ssoHelper: SSOAuthHelper, | ||
) {} | ||
|
||
@Get(':provider/callback') | ||
async callbackSSO( | ||
@Param('provider') provider: string, | ||
@Query() query: Record<string, string>, | ||
) { | ||
const frontendUrl: string = this.configService.getOrThrow('frontend_url'); | ||
|
||
const body = { | ||
client_id: '', | ||
client_secret: '', | ||
code: query.code, | ||
grant_type: 'authorization_code', | ||
redirect_uri: `${frontendUrl}/login/sso/google/callback`, | ||
}; | ||
|
||
const res = await fetch('https://oauth2.googleapis.com/token', { | ||
method: 'POST', | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
const tokenData: { | ||
access_token?: string; | ||
} = await res.json(); | ||
if (!tokenData.access_token) { | ||
throw new ForbiddenException('Invalid token'); | ||
} | ||
|
||
const userInfoResponse = await fetch( | ||
'https://www.googleapis.com/oauth2/v2/userinfo', | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${tokenData.access_token}`, | ||
}, | ||
}, | ||
); | ||
|
||
const userInfo = await userInfoResponse.json(); | ||
|
||
return 'callback'; | ||
} | ||
|
||
@Get(':provider') | ||
getUrlSSO(@Param('provider') provider: string): SSOUrlAuthObj { | ||
return this.ssoHelper.getSSO(provider).getUrl(); | ||
} | ||
} |
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,7 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { SSOAuthController } from './sso.controller'; | ||
|
||
@Module({ | ||
controllers: [SSOAuthController], | ||
}) | ||
export class SSOAuthModule {} |
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,51 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { SSOUrlAuthObj } from 'vitnode-shared/auth/sso.dto'; | ||
|
||
export interface SSOAuthItem { | ||
name: string; | ||
enabled: boolean; | ||
getUrl: () => SSOUrlAuthObj; | ||
code: string; | ||
} | ||
|
||
@Injectable() | ||
export class SSOAuthHelper { | ||
constructor(private readonly configService: ConfigService) {} | ||
|
||
getSSO(code: string): SSOAuthItem { | ||
const item = this.getSSOs().find(sso => sso.code === code); | ||
if (!item) { | ||
throw new NotFoundException(`SSO provider with ${code} code not found`); | ||
} | ||
|
||
return item; | ||
} | ||
|
||
getSSOs(): SSOAuthItem[] { | ||
const frontendUrl: string = this.configService.getOrThrow('frontend_url'); | ||
const redirectUri = (code: string) => | ||
`${frontendUrl}/login/sso/${code}/callback`; | ||
|
||
return [ | ||
{ | ||
name: 'Google', | ||
code: 'google', | ||
enabled: true, | ||
getUrl: () => { | ||
const params = new URLSearchParams({ | ||
client_id: | ||
'1067408430287-igio7a4koou4i26n8vvmqo4eqtcp9gka.apps.googleusercontent.com', | ||
redirect_uri: redirectUri('google'), | ||
response_type: 'code', | ||
scope: 'openid profile email', | ||
}); | ||
|
||
return { | ||
url: `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`, | ||
}; | ||
}, | ||
}, | ||
]; | ||
} | ||
} |
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
Oops, something went wrong.