Skip to content

Instantly share code, notes, and snippets.

@jasonnutter
Created February 11, 2020 20:02
Show Gist options
  • Save jasonnutter/fc82747242c6b7398defa522aebae404 to your computer and use it in GitHub Desktop.
Save jasonnutter/fc82747242c6b7398defa522aebae404 to your computer and use it in GitHub Desktop.
Pseudo code for @azure/msal-node public API
// Top-level exports
import { PublicClientApplication, ConfidentialClientApplication } from '@azure/msal-node';
const app = new PublicClientApplication(config)
// Device code: Option 1 (return promise for token operation and accept callback with user code and verificationUrl)
const {
token,
refreshToken
} = await app.acquireTokenByDeviceCode(request, (userCode, verificationUrl) => {
console.log(userCode, verificationUrl);
});
// Device code: Option 2 (array or object containing user code, verificationUrl, & promise for token)
const [
userCode,
verificationUrl,
resultPromise
] = await app.acquireTokenByDeviceCode(request);
console.log(userCode, verificationUrl);
const {
token,
refreshToken
} = await resultPromise;
// Device code: Option 3 (two seperate promise operations)
const { userCode, deviceCode, verificationUrl } = await app.createDevLoginUrl();
// usercode = "ABC123"
// devicecode = "DEF456"
// verificationUrl = "https://microsoft.com/devicelogin"
console.log(userCode, verificationUrl);
const {
token,
refreshToken
} = await app.pollForToken(deviceCode);
console.log(token, refreshToken);
// Device code: Option 4 (user code callback is provided in constuctor)
// Prior art: https://azuresdkdocs.blob.core.windows.net/$web/javascript/azure-identity/1.0.0/classes/devicecodecredential.html
const app = new PublicClientApplication({
userPromptCallback: (message, userCode, verificationUrl) => {
console.log(message);
console.log(userCode, verificationUrl);
}
});
const {
token,
refreshToken
} = await app.acquireTokenByDeviceCode(request);
////////////////////////
// Auth code (application responsible for redirecting and handling redirect response)
const app = new PublicClientApplication(config);
const { loginUrl } = app.getAuthCodeUrl(request);
<a href={loginUrl}>Login</a>
// myapp.com/auth-redirect?code="code"&state="state"
app.get('/auth-redirect', (req, res) => {
const code = req.query.code;
const { token, refreshToken } = await app.acquireTokenByCode(request, code);
});
// Auth code (silent)
const { token, refreshToken } = await app.acquireTokenSilent(request);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment