-
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.
- Loading branch information
Showing
13 changed files
with
215 additions
and
29 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,48 @@ | ||
import { custom, Issuer } from "openid-client"; | ||
import passport from "passport"; | ||
|
||
import { googleStrategy } from "./strategy/google"; | ||
import { | ||
getMicrosoftOidcStrategy, | ||
getMicrosoftClientConfig, | ||
MICROSOFT_OPENID_CONFIG_URL, | ||
} from "./strategy/microsoft-oidc"; | ||
|
||
const setupPassport = () => { | ||
// TODO: remove below config (timeout extended for local testing with poor connection) | ||
custom.setHttpOptionsDefaults({ | ||
timeout: 10000, | ||
}); | ||
|
||
// explicitly instantiate new passport for clarity | ||
const passportWithStrategies = new passport.Passport(); | ||
|
||
// build Microsoft OIDC client, and use it to build the related strategy | ||
let microsoftOidcClient; | ||
// TODO: need to block on fetch of issuer, but can't use top level await... | ||
Issuer.discover(MICROSOFT_OPENID_CONFIG_URL).then((microsoftIssuer) => { | ||
console.debug("Discovered issuer %s", microsoftIssuer.issuer); | ||
const microsoftClientConfig = getMicrosoftClientConfig(); | ||
microsoftOidcClient = new microsoftIssuer.Client(microsoftClientConfig); | ||
console.debug("Built Microsoft client: %O", microsoftOidcClient); | ||
passportWithStrategies.use( | ||
"microsoft-oidc", | ||
getMicrosoftOidcStrategy(microsoftOidcClient), | ||
); | ||
}); | ||
|
||
// do any other aspects of passport setup which can be handled here | ||
passportWithStrategies.use("google", googleStrategy); | ||
passportWithStrategies.serializeUser((user: any, done) => { | ||
done(null, user); | ||
}); | ||
passportWithStrategies.deserializeUser((obj: any, done) => { | ||
done(null, obj); | ||
}); | ||
|
||
return { passportWithStrategies, microsoftOidcClient }; | ||
}; | ||
|
||
// instantiate and export the new passport class and Microsoft client as early as possible | ||
const { passportWithStrategies, microsoftOidcClient } = setupPassport(); | ||
export { passportWithStrategies, microsoftOidcClient }; |
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,69 @@ | ||
import { Strategy, TokenSet, Client, ClientMetadata } from "openid-client"; | ||
import { buildJWT } from "../service"; | ||
|
||
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, | ||
}, | ||
async (req: any, tokenSet: TokenSet, done: any): Promise<void> => { | ||
// TODO: use state to pass the redirectTo query param through the auth flow | ||
const state = tokenSet.state; | ||
|
||
const claims = tokenSet.claims(); | ||
const email = claims.email; | ||
const returned_nonce = claims.nonce; | ||
// we grab login_hint to provide to the logout endpoint later (as per OpenID spec) | ||
const login_hint = claims.login_hint; | ||
|
||
if (returned_nonce != req.session.nonce) { | ||
return done( | ||
new Error("Returned nonce does not match session nonce"), | ||
null, | ||
); | ||
} | ||
|
||
if (!email) { | ||
return done(new Error("Unable to authenticate without email"), null); | ||
} | ||
|
||
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?`, | ||
} as any, | ||
null, | ||
); | ||
} | ||
|
||
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.
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.