-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement OpenID Connect Authentication Strategy (works with Keycloak…
…, Authentik etc.)
- Loading branch information
1 parent
0f8f2fe
commit eb7813a
Showing
20 changed files
with
320 additions
and
18 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
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 OpenIDGuard extends AuthGuard("openid") {} |
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,74 @@ | ||
import { BadRequestException, Injectable } from "@nestjs/common"; | ||
import { PassportStrategy } from "@nestjs/passport"; | ||
import { User } from "@prisma/client"; | ||
import { ErrorMessage, processUsername } from "@reactive-resume/utils"; | ||
import { Profile, Strategy, StrategyOptions } from "passport-openidconnect"; | ||
|
||
import { UserService } from "@/server/user/user.service"; | ||
|
||
@Injectable() | ||
export class OpenIDStrategy extends PassportStrategy(Strategy, "openid") { | ||
constructor( | ||
readonly authorizationURL: string, | ||
readonly callbackURL: string, | ||
readonly clientID: string, | ||
readonly clientSecret: string, | ||
readonly issuer: string, | ||
readonly tokenURL: string, | ||
readonly userInfoURL: string, | ||
private readonly userService: UserService, | ||
) { | ||
super({ | ||
authorizationURL, | ||
callbackURL, | ||
clientID, | ||
clientSecret, | ||
issuer, | ||
tokenURL, | ||
userInfoURL, | ||
scope: "openid email profile", | ||
} as StrategyOptions); | ||
} | ||
|
||
async validate( | ||
issuer: unknown, | ||
profile: Profile, | ||
done: (err?: string | Error | null, user?: Express.User, info?: unknown) => void, | ||
) { | ||
const { displayName, emails, photos, username } = profile; | ||
|
||
const email = emails?.[0].value ?? `${username}@github.com`; | ||
const picture = photos?.[0].value; | ||
|
||
let user: User | null = null; | ||
|
||
if (!email) throw new BadRequestException(ErrorMessage.InvalidCredentials); | ||
|
||
try { | ||
const user = | ||
(await this.userService.findOneByIdentifier(email)) ?? | ||
(username && (await this.userService.findOneByIdentifier(username))); | ||
|
||
if (!user) throw new Error(ErrorMessage.InvalidCredentials); | ||
|
||
done(null, user); | ||
} catch { | ||
try { | ||
user = await this.userService.create({ | ||
email, | ||
picture, | ||
locale: "en-US", | ||
name: displayName, | ||
provider: "openid", | ||
emailVerified: true, // auto-verify emails | ||
username: processUsername(username ?? email.split("@")[0]), | ||
secrets: { create: {} }, | ||
}); | ||
|
||
done(null, user); | ||
} catch { | ||
throw new BadRequestException(ErrorMessage.UserAlreadyExists); | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { createZodDto } from "nestjs-zod/dto"; | ||
import { z } from "zod"; | ||
|
||
const authProvidersSchema = z.array(z.enum(["email", "github", "google"])); | ||
const authProvidersSchema = z.array(z.enum(["email", "github", "google", "openid"])); | ||
|
||
export class AuthProvidersDto extends createZodDto(authProvidersSchema) {} |
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.