-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api] add Microsoft strategy to auth module (single sign-on) (#3453)
- Loading branch information
Showing
14 changed files
with
250 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Issuer } from "openid-client"; | ||
import passport, { type Authenticator } from "passport"; | ||
|
||
import { googleStrategy } from "./strategy/google.js"; | ||
import { | ||
getMicrosoftOidcStrategy, | ||
getMicrosoftClientConfig, | ||
MICROSOFT_OPENID_CONFIG_URL, | ||
} from "./strategy/microsoft-oidc.js"; | ||
|
||
export default async (): Promise<Authenticator> => { | ||
// explicitly instantiate new passport class for clarity | ||
const customPassport = new passport.Passport(); | ||
|
||
// instantiate Microsoft OIDC client, and use it to build the related strategy | ||
const microsoftIssuer = await Issuer.discover(MICROSOFT_OPENID_CONFIG_URL); | ||
console.debug("Discovered issuer %s", microsoftIssuer.issuer); | ||
const microsoftOidcClient = new microsoftIssuer.Client( | ||
getMicrosoftClientConfig(), | ||
); | ||
console.debug("Built Microsoft client: %O", microsoftOidcClient); | ||
customPassport.use( | ||
"microsoft-oidc", | ||
getMicrosoftOidcStrategy(microsoftOidcClient), | ||
); | ||
|
||
// note that we don't serialize the user in any meaningful way - we just store the entire jwt in session | ||
// i.e. req.session.passport.user == { jwt: "..." } | ||
customPassport.use("google", googleStrategy); | ||
customPassport.serializeUser((user: Express.User, done) => { | ||
done(null, user); | ||
}); | ||
customPassport.deserializeUser((user: Express.User, done) => { | ||
done(null, user); | ||
}); | ||
|
||
// tsc dislikes the use of 'this' in the passportjs codebase, so we cast explicitly | ||
return customPassport as Authenticator; | ||
}; |
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,16 +1,26 @@ | ||
import { Router } from "express"; | ||
import type { Authenticator } from "passport"; | ||
import * as Middleware from "./middleware.js"; | ||
import * as Controller from "./controller.js"; | ||
|
||
const router = Router(); | ||
export default (passport: Authenticator): Router => { | ||
const router = Router(); | ||
|
||
router.get("/logout", Controller.logout); | ||
router.get("/auth/login/failed", Controller.failedLogin); | ||
router.get("/auth/google", Middleware.useGoogleAuth); | ||
router.get( | ||
"/auth/google/callback", | ||
Middleware.useGoogleCallbackAuth, | ||
Controller.handleSuccess, | ||
); | ||
router.get("/logout", Controller.logout); | ||
// router.get("/auth/frontchannel-logout", Controller.frontChannelLogout) | ||
router.get("/auth/login/failed", Controller.failedLogin); | ||
router.get("/auth/google", Middleware.getGoogleAuthHandler(passport)); | ||
router.get( | ||
"/auth/google/callback", | ||
Middleware.getGoogleCallbackAuthHandler(passport), | ||
Controller.handleSuccess, | ||
); | ||
router.get("/auth/microsoft", Middleware.getMicrosoftAuthHandler(passport)); | ||
router.post( | ||
"/auth/microsoft/callback", | ||
Middleware.getMicrosoftCallbackAuthHandler(passport), | ||
Controller.handleSuccess, | ||
); | ||
|
||
export default router; | ||
return router; | ||
}; |
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,69 @@ | ||
import type { | ||
Client, | ||
ClientMetadata, | ||
IdTokenClaims, | ||
StrategyVerifyCallbackReq, | ||
} from "openid-client"; | ||
import { Strategy } from "openid-client"; | ||
import { buildJWT } from "../service.js"; | ||
|
||
export const MICROSOFT_OPENID_CONFIG_URL = | ||
"https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration"; | ||
|
||
export const getMicrosoftClientConfig = (): ClientMetadata => { | ||
const client_id = process.env.MICROSOFT_CLIENT_ID!; | ||
if (typeof client_id !== "string") { | ||
throw new Error("No MICROSOFT_CLIENT_ID in the environment"); | ||
} | ||
return { | ||
client_id, | ||
client_secret: process.env.MICROSOFT_CLIENT_SECRET!, | ||
redirect_uris: [`${process.env.API_URL_EXT}/auth/microsoft/callback`], | ||
post_logout_redirect_uris: [process.env.EDITOR_URL_EXT!], | ||
response_types: ["id_token"], | ||
}; | ||
}; | ||
|
||
// oidc = OpenID Connect, an auth standard built on top of OAuth 2.0 | ||
export const getMicrosoftOidcStrategy = (client: Client): Strategy<Client> => { | ||
return new Strategy( | ||
{ | ||
client: client, | ||
params: { | ||
scope: "openid email profile", | ||
response_mode: "form_post", | ||
}, | ||
// need the request in the verify callback to validate the returned nonce | ||
passReqToCallback: true, | ||
}, | ||
verifyCallback, | ||
); | ||
}; | ||
|
||
const verifyCallback: StrategyVerifyCallbackReq<Express.User> = async ( | ||
req: Http.IncomingMessageWithSession, | ||
tokenSet, | ||
done, | ||
): Promise<void> => { | ||
// TODO: use tokenSet.state to pass the redirectTo query param through the auth flow | ||
const claims: IdTokenClaims = tokenSet.claims(); | ||
const email = claims.email; | ||
const returned_nonce = claims.nonce; | ||
|
||
if (returned_nonce != req.session?.nonce) { | ||
return done(new Error("Returned nonce does not match session nonce")); | ||
} | ||
if (!email) { | ||
return done(new Error("Unable to authenticate without email")); | ||
} | ||
|
||
const jwt = await buildJWT(email); | ||
if (!jwt) { | ||
return done({ | ||
status: 404, | ||
message: `User (${email}) not found. Do you need to log in to a different Microsoft Account?`, | ||
}); | ||
} | ||
|
||
return done(null, { jwt }); | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.